From 6f8fb381f06471e37775cc120e068115ad82e50a Mon Sep 17 00:00:00 2001 From: pj-cegeka Date: Tue, 13 Feb 2024 10:13:12 +0100 Subject: [PATCH 1/5] feat: add defaultRelation --- .../relations/RelationsAttributer.java | 12 ++++++++++++ .../GeospatialFragmentationStrategy.java | 9 ++++++++- .../GeospatialRelationsAttributer.java | 4 +++- .../TileFragmentRelationsAttributer.java | 5 ++++- .../TileFragmentRelationsAttributerTest.java | 15 +++++++++++++++ .../ReferenceFragmentCreator.java | 7 ++++++- .../ReferenceFragmentRelationsAttributer.java | 18 +++++++++++++----- .../TimeBasedRelationsAttributer.java | 19 ++++++++++--------- 8 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 ldes-fragmentisers/ldes-fragmentisers-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/relations/RelationsAttributer.java diff --git a/ldes-fragmentisers/ldes-fragmentisers-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/relations/RelationsAttributer.java b/ldes-fragmentisers/ldes-fragmentisers-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/relations/RelationsAttributer.java new file mode 100644 index 0000000000..35478d3132 --- /dev/null +++ b/ldes-fragmentisers/ldes-fragmentisers-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/relations/RelationsAttributer.java @@ -0,0 +1,12 @@ +package be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations; + +import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation; +import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment; + +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION; + +public interface RelationsAttributer { + default TreeRelation getDefaultRelation(Fragment childFragment) { + return new TreeRelation("", childFragment.getFragmentId(), "", "", GENERIC_TREE_RELATION); + } +} diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategy.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategy.java index 8a987470b3..ffc4864e29 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategy.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategy.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Set; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE_ROOT; public class GeospatialFragmentationStrategy extends FragmentationStrategyDecorator { @@ -45,7 +46,13 @@ public void addMemberToFragment(Fragment parentFragment, String memberId, Model List fragments = tiles .stream() - .map(tile -> fragmentCreator.getOrCreateTileFragment(parentFragment, tile, rootTileFragment)).toList(); + .map(tile -> { + if (tile.equals(DEFAULT_BUCKET_STRING)) { + return fragmentCreator.getOrCreateTileFragment(parentFragment, tile, parentFragment); + } else { + return fragmentCreator.getOrCreateTileFragment(parentFragment, tile, rootTileFragment); + } + }).toList(); fragments .parallelStream() diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/GeospatialRelationsAttributer.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/GeospatialRelationsAttributer.java index 9fd5ce993e..bb39b8dfa7 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/GeospatialRelationsAttributer.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/GeospatialRelationsAttributer.java @@ -3,6 +3,7 @@ import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.exceptions.MissingFragmentValueException; +import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations.RelationsAttributer; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.connected.BoundingBox; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.converter.BoundingBoxConverter; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.converter.TileConverter; @@ -10,7 +11,7 @@ import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.*; -public class GeospatialRelationsAttributer { +public class GeospatialRelationsAttributer implements RelationsAttributer { public TreeRelation getRelationToParentFragment(Fragment childFragment) { String targetWKT = getWKT(childFragment); @@ -27,4 +28,5 @@ private String getWKT(Fragment currentFragment) { BoundingBox currentBoundingBox = new BoundingBox(currentTile); return BoundingBoxConverter.toWKT(currentBoundingBox); } + } diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java index 7c6f769f33..adff106978 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java @@ -4,6 +4,9 @@ import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.repository.FragmentRepository; +import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING; + public class TileFragmentRelationsAttributer { private final GeospatialRelationsAttributer relationsAttributer = new GeospatialRelationsAttributer(); @@ -14,7 +17,7 @@ public TileFragmentRelationsAttributer(FragmentRepository fragmentRepository) { } public void addRelationsFromRootToBottom(Fragment rootFragment, Fragment tileFragments) { - TreeRelation relationToParentFragment = relationsAttributer.getRelationToParentFragment(tileFragments); + TreeRelation relationToParentFragment = tileFragments.getValueOfKey(FRAGMENT_KEY_TILE).orElse("").equals(DEFAULT_BUCKET_STRING) ? relationsAttributer.getDefaultRelation(tileFragments) : relationsAttributer.getRelationToParentFragment(tileFragments); if (!rootFragment.containsRelation(relationToParentFragment)) { rootFragment.addRelation(relationToParentFragment); fragmentRepository.saveFragment(rootFragment); diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributerTest.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributerTest.java index edc61b136b..95ac6ce371 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributerTest.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributerTest.java @@ -11,6 +11,8 @@ import java.util.List; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; @@ -48,6 +50,19 @@ void when_TileFragmentsAreCreated_RelationsBetweenRootAndCreatedFragmentsAreAdde times(1)).saveFragment(rootFragment); } + @Test + void when_DefaultFragmentIsCreated_RelationsBetweenRootAndCreatedFragmentIsAdded() { + Fragment rootFragment = createTileFragment("0/0/0"); + Fragment tileFragment = createTileFragment(DEFAULT_BUCKET_STRING); + TreeRelation expectedRelation = new TreeRelation("", tileFragment.getFragmentId(), "", "", GENERIC_TREE_RELATION); + + tileFragmentRelationsAttributer.addRelationsFromRootToBottom( + rootFragment, tileFragment); + + assertTrue(rootFragment.containsRelation(expectedRelation)); + verify(fragmentRepository, + times(1)).saveFragment(rootFragment); + } private Fragment createTileFragment(String tile) { return PARENT_FRAGMENT.createChild(new FragmentPair(FRAGMENT_KEY_TILE, tile)); diff --git a/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreator.java b/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreator.java index 03ac3f788e..0d14c7c525 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreator.java +++ b/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreator.java @@ -8,6 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.FragmentationService.LDES_SERVER_CREATE_FRAGMENTS_COUNT; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.metrics.MetricsConstants.FRAGMENTATION_STRATEGY; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.metrics.MetricsConstants.VIEW; @@ -35,7 +36,11 @@ public Fragment getOrCreateFragment(Fragment parentFragment, String reference, F .retrieveFragment(child.getFragmentId()) .orElseGet(() -> { fragmentRepository.saveFragment(child); - relationsAttributer.addRelationsFromRootToBottom(rootFragment, child); + if (reference.equals(DEFAULT_BUCKET_STRING)) { + relationsAttributer.addDefaultRelation(parentFragment, child); + } else { + relationsAttributer.addRelationsFromRootToBottom(rootFragment, child); + } logFragmentation(parentFragment, child); return child; }); diff --git a/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/relations/ReferenceFragmentRelationsAttributer.java b/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/relations/ReferenceFragmentRelationsAttributer.java index b7958a3cb5..1c98256f2f 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/relations/ReferenceFragmentRelationsAttributer.java +++ b/ldes-fragmentisers/ldes-fragmentisers-reference/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/relations/ReferenceFragmentRelationsAttributer.java @@ -3,12 +3,13 @@ import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.exceptions.MissingFragmentValueException; +import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations.RelationsAttributer; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.repository.FragmentRepository; import org.apache.jena.datatypes.xsd.XSDDatatype; import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.TREE; -public class ReferenceFragmentRelationsAttributer { +public class ReferenceFragmentRelationsAttributer implements RelationsAttributer { public static final String TREE_REFERENCE_EQUALS_RELATION = TREE + "EqualToRelation"; @@ -25,10 +26,17 @@ public ReferenceFragmentRelationsAttributer(FragmentRepository fragmentRepositor } public void addRelationsFromRootToBottom(Fragment rootFragment, Fragment referenceFragments) { - TreeRelation relationToParentFragment = getRelationToParentFragment(referenceFragments); - if (!rootFragment.containsRelation(relationToParentFragment)) { - rootFragment.addRelation(relationToParentFragment); - fragmentRepository.saveFragment(rootFragment); + saveRelation(rootFragment, getRelationToParentFragment(referenceFragments)); + } + + public void addDefaultRelation(Fragment rootFragment, Fragment fragment) { + saveRelation(rootFragment, getDefaultRelation(fragment)); + } + + private void saveRelation(Fragment fragment, TreeRelation relation) { + if (!fragment.containsRelation(relation)) { + fragment.addRelation(relation); + fragmentRepository.saveFragment(fragment); } } diff --git a/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java b/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java index 80507cf65f..b55d6621b9 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java +++ b/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java @@ -2,6 +2,7 @@ import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment; +import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations.RelationsAttributer; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.repository.FragmentRepository; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.timebasedhierarchical.config.TimeBasedConfig; import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.timebasedhierarchical.constants.Granularity; @@ -15,7 +16,7 @@ import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.timebasedhierarchical.constants.TimeBasedConstants.TREE_INBETWEEN_RELATION; -public class TimeBasedRelationsAttributer { +public class TimeBasedRelationsAttributer implements RelationsAttributer { private final FragmentRepository fragmentRepository; @@ -33,17 +34,17 @@ public void addInBetweenRelation(Fragment parentFragment, Fragment childFragment childFragment.getFragmentId(), timestamp.asString(), timestamp.getType(), TREE_INBETWEEN_RELATION); - if (!parentFragment.containsRelation(parentChildRelation)) { - parentFragment.addRelation(parentChildRelation); - fragmentRepository.saveFragment(parentFragment); - } + saveRelation(parentFragment, parentChildRelation); } public void addDefaultRelation(Fragment parentFragment, Fragment childFragment) { - TreeRelation parentChildRelation = new TreeRelation("", childFragment.getFragmentId(), "", "", GENERIC_TREE_RELATION); - if (!parentFragment.containsRelation(parentChildRelation)) { - parentFragment.addRelation(parentChildRelation); - fragmentRepository.saveFragment(parentFragment); + saveRelation(parentFragment, getDefaultRelation(childFragment)); + } + + private void saveRelation(Fragment fragment, TreeRelation relation) { + if (!fragment.containsRelation(relation)) { + fragment.addRelation(relation); + fragmentRepository.saveFragment(fragment); } } From 86ed59f97ebc90768fff59f93bccb3db2e78c04c Mon Sep 17 00:00:00 2001 From: pj-cegeka Date: Tue, 13 Feb 2024 11:05:49 +0100 Subject: [PATCH 2/5] feat: add tests --- .../GeospatialFragmentCreatorTest.java | 46 ++++++++++++++++++- .../ReferenceFragmentCreatorTest.java | 38 ++++++++++++++- .../TimeBasedRelationsAttributer.java | 1 - 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java index e68ea22059..4dbab900ef 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Optional; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE_ROOT; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -23,6 +24,7 @@ class GeospatialFragmentCreatorTest { private static final FragmentPair timebasedPair = new FragmentPair("year", "2023"); private static final FragmentPair geoRootPair = new FragmentPair(FRAGMENT_KEY_TILE, FRAGMENT_KEY_TILE_ROOT); private static final FragmentPair geoPair = new FragmentPair(FRAGMENT_KEY_TILE, "15/101/202"); + private static final FragmentPair defaultPair = new FragmentPair(FRAGMENT_KEY_TILE, DEFAULT_BUCKET_STRING); private FragmentRepository fragmentRepository; private GeospatialFragmentCreator geospatialFragmentCreator; @@ -103,8 +105,10 @@ void when_RootFragmentDoesNotExist_RetrievedRootFragmentIsReturned() { VIEW_NAME, List.of(timebasedPair))); Fragment rootFragment = fragment .createChild(geoRootPair); - when(fragmentRepository.retrieveFragment(rootFragment.getFragmentId())) - .thenReturn(Optional.of(rootFragment)); + Fragment defaultFragment = fragment + .createChild(defaultPair); + when(fragmentRepository.retrieveFragment(defaultFragment.getFragmentId())) + .thenReturn(Optional.of(defaultFragment)); Fragment returnedFragment = geospatialFragmentCreator.getOrCreateTileFragment(fragment, FRAGMENT_KEY_TILE_ROOT, rootFragment); @@ -114,4 +118,42 @@ void when_RootFragmentDoesNotExist_RetrievedRootFragmentIsReturned() { verify(fragmentRepository, times(1)).retrieveFragment(rootFragment.getFragmentId()); verifyNoMoreInteractions(fragmentRepository); } + @Test + void when_DefaultFragmentDoesNotExist_NewDefaultFragmentIsCreatedAndSaved() { + Fragment fragment = new Fragment(new LdesFragmentIdentifier( + VIEW_NAME, List.of(timebasedPair))); + Fragment rootFragment = fragment + .createChild(geoRootPair); + Fragment defaultFragment = fragment + .createChild(defaultPair); + when(fragmentRepository.retrieveFragment(defaultFragment.getFragmentId())).thenReturn(Optional.empty()); + + Fragment returnedFragment = geospatialFragmentCreator.getOrCreateRootFragment(fragment, + DEFAULT_BUCKET_STRING); + + assertEquals(new LdesFragmentIdentifier(VIEW_NAME, List.of(timebasedPair, defaultPair)), + returnedFragment.getFragmentId()); + verify(fragmentRepository, times(1)).retrieveFragment(defaultFragment.getFragmentId()); + verify(fragmentRepository, times(1)).saveFragment(returnedFragment); + } + + @Test + void when_DefaultFragmentDoesNotExist_RetrievedDefaultFragmentIsReturned() { + Fragment fragment = new Fragment(new LdesFragmentIdentifier( + VIEW_NAME, List.of(timebasedPair))); + Fragment rootFragment = fragment + .createChild(geoRootPair); + Fragment defaultFragment = fragment + .createChild(defaultPair); + when(fragmentRepository.retrieveFragment(defaultFragment.getFragmentId())) + .thenReturn(Optional.of(defaultFragment)); + + Fragment returnedFragment = geospatialFragmentCreator.getOrCreateTileFragment(fragment, + DEFAULT_BUCKET_STRING, rootFragment); + + assertEquals(new LdesFragmentIdentifier(VIEW_NAME, List.of(timebasedPair, defaultPair)), + returnedFragment.getFragmentId()); + verify(fragmentRepository, times(1)).retrieveFragment(defaultFragment.getFragmentId()); + verifyNoMoreInteractions(fragmentRepository); + } } diff --git a/ldes-fragmentisers/ldes-fragmentisers-reference/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreatorTest.java b/ldes-fragmentisers/ldes-fragmentisers-reference/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreatorTest.java index b748d97b58..b6f013cbbf 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-reference/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreatorTest.java +++ b/ldes-fragmentisers/ldes-fragmentisers-reference/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/reference/fragmentation/ReferenceFragmentCreatorTest.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Optional; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.reference.ReferenceFragmentationStrategyWrapper.DEFAULT_FRAGMENTATION_KEY; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.reference.fragmentation.ReferenceFragmentCreator.FRAGMENT_KEY_REFERENCE_ROOT; import static org.assertj.core.api.Assertions.assertThat; @@ -29,6 +30,7 @@ class ReferenceFragmentCreatorTest { private static final FragmentPair timebasedPair = new FragmentPair("year", "2023"); private static final FragmentPair referenceRootPair = new FragmentPair(DEFAULT_FRAGMENTATION_KEY, FRAGMENT_KEY_REFERENCE_ROOT); private static final FragmentPair referencePair = new FragmentPair(DEFAULT_FRAGMENTATION_KEY, RDF.type.getURI()); + private static final FragmentPair defaultPair = new FragmentPair(DEFAULT_FRAGMENTATION_KEY, DEFAULT_BUCKET_STRING); private ReferenceFragmentCreator referenceFragmentCreator; @@ -63,7 +65,7 @@ void when_ReferenceFragmentDoesNotExist_NewReferenceFragmentIsCreatedAndSaved() } @Test - void when_ReferenceFragmentDoesNotExist_RetrievedReferenceFragmentIsReturned() { + void when_ReferenceFragmentDoesExist_RetrievedReferenceFragmentIsReturned() { Fragment fragment = new Fragment(new LdesFragmentIdentifier(viewName, List.of(timebasedPair))); Fragment rootFragment = fragment.createChild(referenceRootPair); Fragment tileFragment = fragment.createChild(referencePair); @@ -111,5 +113,39 @@ void when_RootFragmentDoesNotExist_RetrievedRootFragmentIsReturned() { verify(fragmentRepository, times(1)).retrieveFragment(rootFragment.getFragmentId()); verifyNoMoreInteractions(fragmentRepository); } + @Test + void when_DefaultFragmentDoesNotExist_DefaultFragmentIsCreatedAndSaved() { + Fragment fragment = new Fragment(new LdesFragmentIdentifier(viewName, List.of(timebasedPair))); + Fragment rootFragment = fragment.createChild(referenceRootPair); + LdesFragmentIdentifier tileFragmentId = fragment.createChild(defaultPair).getFragmentId(); + + when(fragmentRepository.retrieveFragment(tileFragmentId)).thenReturn(Optional.empty()); + + Fragment childFragment = referenceFragmentCreator.getOrCreateFragment(fragment, DEFAULT_BUCKET_STRING, rootFragment); + + assertThat(new LdesFragmentIdentifier(viewName, List.of(timebasedPair, defaultPair))) + .isEqualTo(childFragment.getFragmentId()); + verify(fragmentRepository, + times(1)).retrieveFragment(tileFragmentId); + verify(fragmentRepository, + times(1)).saveFragment(childFragment); + } + + @Test + void when_DefaultFragmentDoesExist_RetrievedDefaultFragmentIsReturned() { + Fragment fragment = new Fragment(new LdesFragmentIdentifier(viewName, List.of(timebasedPair))); + Fragment rootFragment = fragment.createChild(referenceRootPair); + Fragment tileFragment = fragment.createChild(defaultPair); + + when(fragmentRepository.retrieveFragment(tileFragment.getFragmentId())).thenReturn(Optional.of(tileFragment)); + + Fragment childFragment = referenceFragmentCreator.getOrCreateFragment(fragment, DEFAULT_BUCKET_STRING, rootFragment); + + assertThat(new LdesFragmentIdentifier(viewName, List.of(timebasedPair, defaultPair))) + .isEqualTo(childFragment.getFragmentId()); + verify(fragmentRepository, + times(1)).retrieveFragment(tileFragment.getFragmentId()); + verifyNoMoreInteractions(fragmentRepository); + } } \ No newline at end of file diff --git a/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java b/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java index b55d6621b9..4e01d9d23c 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java +++ b/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedRelationsAttributer.java @@ -13,7 +13,6 @@ import java.util.HashMap; import java.util.Map; -import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.timebasedhierarchical.constants.TimeBasedConstants.TREE_INBETWEEN_RELATION; public class TimeBasedRelationsAttributer implements RelationsAttributer { From a7c09ab0e73de1b6bdd0631ad07b13d48381a693 Mon Sep 17 00:00:00 2001 From: pj-cegeka Date: Tue, 13 Feb 2024 11:17:16 +0100 Subject: [PATCH 3/5] feat: add tests --- .../fragments/GeospatialFragmentCreatorTest.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java index 4dbab900ef..70726c8945 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/fragments/GeospatialFragmentCreatorTest.java @@ -105,13 +105,11 @@ void when_RootFragmentDoesNotExist_RetrievedRootFragmentIsReturned() { VIEW_NAME, List.of(timebasedPair))); Fragment rootFragment = fragment .createChild(geoRootPair); - Fragment defaultFragment = fragment - .createChild(defaultPair); - when(fragmentRepository.retrieveFragment(defaultFragment.getFragmentId())) - .thenReturn(Optional.of(defaultFragment)); + when(fragmentRepository.retrieveFragment(rootFragment.getFragmentId())) + .thenReturn(Optional.of(rootFragment)); - Fragment returnedFragment = geospatialFragmentCreator.getOrCreateTileFragment(fragment, - FRAGMENT_KEY_TILE_ROOT, rootFragment); + Fragment returnedFragment = geospatialFragmentCreator.getOrCreateRootFragment(fragment, + FRAGMENT_KEY_TILE_ROOT); assertEquals(new LdesFragmentIdentifier(VIEW_NAME, List.of(timebasedPair, geoRootPair)), returnedFragment.getFragmentId()); @@ -128,8 +126,8 @@ void when_DefaultFragmentDoesNotExist_NewDefaultFragmentIsCreatedAndSaved() { .createChild(defaultPair); when(fragmentRepository.retrieveFragment(defaultFragment.getFragmentId())).thenReturn(Optional.empty()); - Fragment returnedFragment = geospatialFragmentCreator.getOrCreateRootFragment(fragment, - DEFAULT_BUCKET_STRING); + Fragment returnedFragment = geospatialFragmentCreator.getOrCreateTileFragment(fragment, + DEFAULT_BUCKET_STRING, rootFragment); assertEquals(new LdesFragmentIdentifier(VIEW_NAME, List.of(timebasedPair, defaultPair)), returnedFragment.getFragmentId()); From 34eef276c5d07a72e4ff742f91cce9847097603c Mon Sep 17 00:00:00 2001 From: pj-cegeka Date: Wed, 14 Feb 2024 09:22:58 +0100 Subject: [PATCH 4/5] feat: add tests --- .../GeospatialFragmentationStrategyTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategyTest.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategyTest.java index 7fb488fa85..60ca15d58a 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategyTest.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/GeospatialFragmentationStrategyTest.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.Set; +import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE; import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE_ROOT; import static org.mockito.Mockito.*; @@ -72,6 +73,24 @@ void when_MemberIsAddedToFragment_GeospatialFragmentationIsApplied() { any(), any(), any(Observation.class)); verifyNoMoreInteractions(decoratedFragmentationStrategy); } + @Test + void when_MemberIsAddedToDefaultFragment_GeospatialFragmentationIsApplied() { + Member member = mock(Member.class); + + when(geospatialBucketiser.bucketise(member.id(), member.model())).thenReturn(Set.of(DEFAULT_BUCKET_STRING)); + Fragment defaultTileFragment = PARENT_FRAGMENT.createChild(new FragmentPair(FRAGMENT_KEY_TILE, DEFAULT_BUCKET_STRING)); + when(fragmentCreator.getOrCreateTileFragment(PARENT_FRAGMENT, DEFAULT_BUCKET_STRING, + PARENT_FRAGMENT)) + .thenReturn(defaultTileFragment); + + geospatialFragmentationStrategy.addMemberToFragment(PARENT_FRAGMENT, member.id(), + member.model(), mock(Observation.class)); + + verify(decoratedFragmentationStrategy, + times(1)).addMemberToFragment(eq(defaultTileFragment), + any(), any(), any(Observation.class)); + verifyNoMoreInteractions(decoratedFragmentationStrategy); + } private Fragment mockCreationGeospatialFragment(String tile) { Fragment tileFragment = PARENT_FRAGMENT.createChild(new FragmentPair(FRAGMENT_KEY_TILE, tile)); From 1f4ca1c31da5cf3b1b13eb24d52a3c5328781121 Mon Sep 17 00:00:00 2001 From: pj-cegeka Date: Wed, 14 Feb 2024 13:36:50 +0100 Subject: [PATCH 5/5] feat: PR remark, make default path clearer --- .../connected/relations/TileFragmentRelationsAttributer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java index adff106978..d0ff45fea9 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java +++ b/ldes-fragmentisers/ldes-fragmentisers-geospatial/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/geospatial/connected/relations/TileFragmentRelationsAttributer.java @@ -17,7 +17,8 @@ public TileFragmentRelationsAttributer(FragmentRepository fragmentRepository) { } public void addRelationsFromRootToBottom(Fragment rootFragment, Fragment tileFragments) { - TreeRelation relationToParentFragment = tileFragments.getValueOfKey(FRAGMENT_KEY_TILE).orElse("").equals(DEFAULT_BUCKET_STRING) ? relationsAttributer.getDefaultRelation(tileFragments) : relationsAttributer.getRelationToParentFragment(tileFragments); + boolean isDefaultFragment = tileFragments.getValueOfKey(FRAGMENT_KEY_TILE).orElse("").equals(DEFAULT_BUCKET_STRING); + TreeRelation relationToParentFragment = isDefaultFragment ? relationsAttributer.getDefaultRelation(tileFragments) : relationsAttributer.getRelationToParentFragment(tileFragments); if (!rootFragment.containsRelation(relationToParentFragment)) { rootFragment.addRelation(relationToParentFragment); fragmentRepository.saveFragment(rootFragment);