diff --git a/ldes-fragmentisers/ldes-fragmentisers-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/FragmentDeletionSteps.java b/ldes-fragmentisers/ldes-fragmentisers-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/FragmentDeletionSteps.java index d76193a8d4..dd1a5ac990 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/FragmentDeletionSteps.java +++ b/ldes-fragmentisers/ldes-fragmentisers-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/FragmentDeletionSteps.java @@ -13,7 +13,6 @@ import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import java.util.stream.Stream; import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION; @@ -36,7 +35,7 @@ public Fragment FragmentEntryTransformer(Map row) { row.get("relation").isEmpty() ? new ArrayList<>() : Arrays.stream(row.get("relation").split(",")).map(treeNode -> new TreeRelation("", LdesFragmentIdentifier.fromFragmentId(treeNode), "", "", GENERIC_TREE_RELATION)) - .collect(Collectors.toList()), + .toList(), getDeleteTime(row.get("daysUntilDeletion"))); } diff --git a/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedFragmentFinderTest.java b/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedFragmentFinderTest.java index ddb342a553..7a0adc43e1 100644 --- a/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedFragmentFinderTest.java +++ b/ldes-fragmentisers/ldes-fragmentisers-timebased-hierarchical/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentisers/timebasedhierarchical/services/TimeBasedFragmentFinderTest.java @@ -25,13 +25,12 @@ class TimeBasedFragmentFinderTest { private static final Fragment PARENT = new Fragment(new LdesFragmentIdentifier(VIEW_NAME, List.of())); private static final FragmentationTimestamp TIME = new FragmentationTimestamp(LocalDateTime.of(2023, 1, 1, 0, 0, 0), Granularity.DAY); - private TimeBasedConfig config; - private TimeBasedFragmentCreator fragmentCreator; + private TimeBasedFragmentCreator fragmentCreator; private TimeBasedFragmentFinder fragmentFinder; @BeforeEach void setUp() { - config = new TimeBasedConfig(".*", "", Granularity.DAY, false); + TimeBasedConfig config = new TimeBasedConfig(".*", "", Granularity.DAY, false); fragmentCreator = mock(TimeBasedFragmentCreator.class); fragmentFinder = new TimeBasedFragmentFinder(fragmentCreator, config); @@ -54,8 +53,8 @@ void when_GetLowestIsCalled_Then_ReturnExpectedFragment() { @Test void when_GetDefaultIsCalled_Then_ReturnExpectedFragment() { - Fragment actual = fragmentFinder.getDefaultFragment(PARENT); + fragmentFinder.getDefaultFragment(PARENT); - verify(fragmentCreator, times(1)).getOrCreateFragment(PARENT, DEFAULT_BUCKET_STRING, Granularity.YEAR); + verify(fragmentCreator).getOrCreateFragment(PARENT, DEFAULT_BUCKET_STRING, Granularity.YEAR); } } diff --git a/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/domain/validation/ShaclShapeValidatorTest.java b/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/domain/validation/ShaclShapeValidatorTest.java index ea75a6e474..2c7c72f4be 100644 --- a/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/domain/validation/ShaclShapeValidatorTest.java +++ b/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/domain/validation/ShaclShapeValidatorTest.java @@ -13,7 +13,6 @@ import java.io.File; import java.io.IOException; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import static org.assertj.core.api.Assertions.*; diff --git a/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/ResourceRemover.java b/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/ResourceRemover.java index 42251d5ad2..fbc75a9cf6 100644 --- a/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/ResourceRemover.java +++ b/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/ResourceRemover.java @@ -7,15 +7,14 @@ import org.springframework.stereotype.Component; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; @Component public class ResourceRemover { private final ApplicationEventPublisher applicationEventPublisher; - private List usedStreams = new ArrayList<>(); + private final List usedStreams = new ArrayList<>(); - private ResourceRemover(ApplicationEventPublisher applicationEventPublisher) { + protected ResourceRemover(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } diff --git a/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/SpringIntegrationTest.java b/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/SpringIntegrationTest.java index b9631bb08b..37bdef7be0 100644 --- a/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/SpringIntegrationTest.java +++ b/ldes-server-admin/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/config/SpringIntegrationTest.java @@ -11,8 +11,6 @@ import be.vlaanderen.informatievlaanderen.ldes.server.admin.rest.controllers.*; import be.vlaanderen.informatievlaanderen.ldes.server.domain.converter.PrefixAdderImpl; import be.vlaanderen.informatievlaanderen.ldes.server.domain.converter.RdfModelConverter; -import be.vlaanderen.informatievlaanderen.ldes.server.domain.events.admin.EventStreamCreatedEvent; -import be.vlaanderen.informatievlaanderen.ldes.server.domain.events.admin.EventStreamDeletedEvent; import be.vlaanderen.informatievlaanderen.ldes.server.domain.rest.PrefixConstructor; import io.cucumber.spring.CucumberContextConfiguration; import org.springframework.beans.factory.annotation.Autowired; @@ -22,15 +20,10 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.event.EventListener; -import org.springframework.stereotype.Component; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; -import java.util.ArrayList; -import java.util.List; - @SpringBootTest @AutoConfigureMockMvc @CucumberContextConfiguration diff --git a/ldes-server-compaction/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/compaction/CompactionServiceSteps.java b/ldes-server-compaction/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/compaction/CompactionServiceSteps.java index d1a47a1286..4de65ba854 100644 --- a/ldes-server-compaction/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/compaction/CompactionServiceSteps.java +++ b/ldes-server-compaction/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/compaction/CompactionServiceSteps.java @@ -28,134 +28,133 @@ @SuppressWarnings("java:S3415") public class CompactionServiceSteps extends CompactionIntegrationTest { - @DataTableType - public ViewSpecification ViewSpecificationEntryTransformer(Map row) { - return new ViewSpecification( - ViewName.fromString(row.get("viewName")), - List.of(), List.of(), Integer.parseInt(row.get("pageSize"))); - } - - @DataTableType - public FragmentAllocations FragmentAllocationsListEntryTransformer(Map row) { - List memberAllocations = new ArrayList<>(); - String fragmentIdentifier = row.get("fragmentIdentifier"); - for (String memberId : row.get("members").split(",")) { - memberAllocations.add(new MemberAllocation(fragmentIdentifier + "/" + memberId, "mobility-hindrances", - "by-page", fragmentIdentifier, memberId)); - } - return new FragmentAllocations(fragmentIdentifier, memberAllocations); - } - - @DataTableType - public MemberFragmentations MemberFragmentationsEntryTransformer(Map row) { - return new MemberFragmentations(row.get("fragmentId"), Arrays.stream(row.get("memberIds").split(",")).toList()); - } - - @DataTableType(replaceWithEmptyString = "[blank]") - public Fragment FragmentEntryTransformer(Map row) { - return new Fragment( - LdesFragmentIdentifier.fromFragmentId(row.get("fragmentIdentifier")), - Boolean.parseBoolean(row.get("immutable")), Integer.parseInt(row.get("nrOfMembersAdded")), - row.get("relation").isEmpty() ? new ArrayList<>() - : Arrays.stream(row.get("relation").split(",")).map(treeNode -> new TreeRelation("", - LdesFragmentIdentifier.fromFragmentId(treeNode), "", "", GENERIC_TREE_RELATION)) - .collect(Collectors.toList()), - null); - } - - @Given("a view with the following properties") - public void aViewWithTheFollowingProperties(ViewSpecification viewSpecification) { - applicationEventPublisher.publishEvent(new ViewAddedEvent(viewSpecification)); - } - - @And("the following Fragments are available") - public void theFollowingFragmentsAreAvailable(List fragments) { - fragments.forEach(fragment -> { - when(fragmentRepository.retrieveFragment(fragment.getFragmentId())).thenReturn(Optional.of(fragment)); - when(fragmentRepository.retrieveFragment(fragment.getFragmentId())).thenReturn(Optional.of(fragment)); - if (fragment.getFragmentPairs().isEmpty()) { - when(fragmentRepository.retrieveRootFragment(fragment.getViewName().asString())) - .thenReturn(Optional.of(fragment)); - } - fragment.getRelations() - .forEach(treeRelation -> when( - fragmentRepository.retrieveFragmentsByOutgoingRelation(treeRelation.treeNode())) - .thenReturn(List.of(fragment))); - }); - } - - @And("the following allocations are present") - public void theFollowingAllocationsArePresent(List fragmentAllocations) { - when(allocationRepository.getPossibleCompactionCandidates(any(ViewName.class), anyInt())) - .thenAnswer(i -> - getAllocationAggregates(fragmentAllocations, - i.getArgument(0, ViewName.class), - i.getArgument(1, Integer.class)) - ); - - when(allocationRepository.getMemberAllocationIdsByFragmentIds(ArgumentMatchers.any())) - .thenAnswer(x -> { - Set requested = x.getArgument(0); - return fragmentAllocations.stream() - .filter(fragmentAllocation -> requested.contains(fragmentAllocation.fragmentId)) - .flatMap(fragmentAllocations1 -> fragmentAllocations1.memberAllocations.stream() - .map(MemberAllocation::memberId)).toList(); - }); - } - - private Stream getAllocationAggregates(List fragmentAllocations, ViewName viewName, Integer viewCapacity) { - return fragmentAllocations.stream() - .filter(fragmentAllocation -> { - var fragmentId = LdesFragmentIdentifier.fromFragmentId(fragmentAllocation.fragmentId); - return fragmentId.getViewName().equals(viewName); - }) - .map(fragmentAllocation -> new CompactionCandidate(fragmentAllocation.fragmentId, fragmentAllocation.memberAllocations.size())); - } - - @And("verify creation of the following fragments") - public void verifyCreationOfTheFollowingFragments(List createdFragments) { - createdFragments.forEach(createdFragment -> verify(fragmentRepository) - .saveFragment(new Fragment(LdesFragmentIdentifier.fromFragmentId(createdFragment)))); - } - - @And("verify update of predecessor relations") - public void verifyUpdateOfPredecessorRelations(List predecessorFragments) { - predecessorFragments.forEach(predecessorFragment -> { - verify(fragmentRepository) - .saveFragment(new Fragment(LdesFragmentIdentifier.fromFragmentId(predecessorFragment))); - - List treeRelations = fragmentRepository - .retrieveFragment(LdesFragmentIdentifier.fromFragmentId(predecessorFragment)) - .orElseThrow() - .getRelations(); - assertThat(treeRelations) - .map(TreeRelation::treeNode) - .map(LdesFragmentIdentifier::asDecodedFragmentId) - .filteredOn(identifier -> !identifier.contains("dummy")) - .hasSize(1); - }); - } - - @And("verify fragmentation of members") - public void verifyFragmentationOfMembers(List memberFragmentations) { - verify(eventConsumer, times(memberFragmentations.size())).consumeEvent(any(BulkMemberAllocatedEvent.class)); - memberFragmentations.forEach(memberFragmentation -> { - verify(fragmentRepository).incrementNrOfMembersAdded(LdesFragmentIdentifier.fromFragmentId(memberFragmentation.fragmentId), memberFragmentation.memberIds.size()); - }); - } - - @Then("wait for {int} seconds until compaction has executed at least once") - public void waitForSecondsUntilCompactionHasExecutedAtLeastOnce(int secondsToWait) { - await() - .timeout(secondsToWait + 1, SECONDS) - .pollDelay(secondsToWait, SECONDS) - .untilAsserted(() -> assertThat(true).isTrue()); - } - - public record FragmentAllocations(String fragmentId, List memberAllocations) { - } - - public record MemberFragmentations(String fragmentId, List memberIds) { - } + @DataTableType + public ViewSpecification ViewSpecificationEntryTransformer(Map row) { + return new ViewSpecification( + ViewName.fromString(row.get("viewName")), + List.of(), List.of(), Integer.parseInt(row.get("pageSize"))); + } + + @DataTableType + public FragmentAllocations FragmentAllocationsListEntryTransformer(Map row) { + List memberAllocations = new ArrayList<>(); + String fragmentIdentifier = row.get("fragmentIdentifier"); + for (String memberId : row.get("members").split(",")) { + memberAllocations.add(new MemberAllocation(fragmentIdentifier + "/" + memberId, "mobility-hindrances", + "by-page", fragmentIdentifier, memberId)); + } + return new FragmentAllocations(fragmentIdentifier, memberAllocations); + } + + @DataTableType + public MemberFragmentations MemberFragmentationsEntryTransformer(Map row) { + return new MemberFragmentations(row.get("fragmentId"), Arrays.stream(row.get("memberIds").split(",")).toList()); + } + + @DataTableType(replaceWithEmptyString = "[blank]") + public Fragment FragmentEntryTransformer(Map row) { + final ArrayList relations = row.get("relation").isEmpty() ? new ArrayList<>() + : Arrays.stream(row.get("relation").split(",")).map(treeNode -> new TreeRelation("", + LdesFragmentIdentifier.fromFragmentId(treeNode), "", "", GENERIC_TREE_RELATION)) + .collect(Collectors.toCollection(ArrayList::new)); + return new Fragment( + LdesFragmentIdentifier.fromFragmentId(row.get("fragmentIdentifier")), + Boolean.parseBoolean(row.get("immutable")), Integer.parseInt(row.get("nrOfMembersAdded")), + relations, + null); + } + + @Given("a view with the following properties") + public void aViewWithTheFollowingProperties(ViewSpecification viewSpecification) { + applicationEventPublisher.publishEvent(new ViewAddedEvent(viewSpecification)); + } + + @And("the following Fragments are available") + public void theFollowingFragmentsAreAvailable(List fragments) { + fragments.forEach(fragment -> { + when(fragmentRepository.retrieveFragment(fragment.getFragmentId())).thenReturn(Optional.of(fragment)); + when(fragmentRepository.retrieveFragment(fragment.getFragmentId())).thenReturn(Optional.of(fragment)); + if (fragment.getFragmentPairs().isEmpty()) { + when(fragmentRepository.retrieveRootFragment(fragment.getViewName().asString())) + .thenReturn(Optional.of(fragment)); + } + fragment.getRelations() + .forEach(treeRelation -> when( + fragmentRepository.retrieveFragmentsByOutgoingRelation(treeRelation.treeNode())) + .thenReturn(List.of(fragment))); + }); + } + + @And("the following allocations are present") + public void theFollowingAllocationsArePresent(List fragmentAllocations) { + when(allocationRepository.getPossibleCompactionCandidates(any(ViewName.class), anyInt())) + .thenAnswer(i -> + getAllocationAggregates(fragmentAllocations, i.getArgument(0, ViewName.class)) + ); + + when(allocationRepository.getMemberAllocationIdsByFragmentIds(ArgumentMatchers.any())) + .thenAnswer(x -> { + Set requested = x.getArgument(0); + return fragmentAllocations.stream() + .filter(fragmentAllocation -> requested.contains(fragmentAllocation.fragmentId)) + .flatMap(fragmentAllocations1 -> fragmentAllocations1.memberAllocations.stream() + .map(MemberAllocation::memberId)).toList(); + }); + } + + private Stream getAllocationAggregates(List fragmentAllocations, ViewName viewName) { + return fragmentAllocations.stream() + .filter(fragmentAllocation -> { + var fragmentId = LdesFragmentIdentifier.fromFragmentId(fragmentAllocation.fragmentId); + return fragmentId.getViewName().equals(viewName); + }) + .map(fragmentAllocation -> new CompactionCandidate(fragmentAllocation.fragmentId, fragmentAllocation.memberAllocations.size())); + } + + @And("verify creation of the following fragments") + public void verifyCreationOfTheFollowingFragments(List createdFragments) { + createdFragments.forEach(createdFragment -> verify(fragmentRepository) + .saveFragment(new Fragment(LdesFragmentIdentifier.fromFragmentId(createdFragment)))); + } + + @And("verify update of predecessor relations") + public void verifyUpdateOfPredecessorRelations(List predecessorFragments) { + predecessorFragments.forEach(predecessorFragment -> { + verify(fragmentRepository) + .saveFragment(new Fragment(LdesFragmentIdentifier.fromFragmentId(predecessorFragment))); + + List treeRelations = fragmentRepository + .retrieveFragment(LdesFragmentIdentifier.fromFragmentId(predecessorFragment)) + .orElseThrow() + .getRelations(); + assertThat(treeRelations) + .map(TreeRelation::treeNode) + .map(LdesFragmentIdentifier::asDecodedFragmentId) + .filteredOn(identifier -> !identifier.contains("dummy")) + .hasSize(1); + }); + } + + @And("verify fragmentation of members") + public void verifyFragmentationOfMembers(List memberFragmentations) { + verify(eventConsumer, times(memberFragmentations.size())).consumeEvent(any(BulkMemberAllocatedEvent.class)); + memberFragmentations.forEach(memberFragmentation -> { + verify(fragmentRepository).incrementNrOfMembersAdded(LdesFragmentIdentifier.fromFragmentId(memberFragmentation.fragmentId), memberFragmentation.memberIds.size()); + }); + } + + @Then("wait for {int} seconds until compaction has executed at least once") + public void waitForSecondsUntilCompactionHasExecutedAtLeastOnce(int secondsToWait) { + await() + .timeout(secondsToWait + 1, SECONDS) + .pollDelay(secondsToWait, SECONDS) + .untilAsserted(() -> assertThat(true).isTrue()); + } + + public record FragmentAllocations(String fragmentId, List memberAllocations) { + } + + public record MemberFragmentations(String fragmentId, List memberIds) { + } } diff --git a/ldes-server-domain/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/rest/PrefixConstructorTest.java b/ldes-server-domain/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/rest/PrefixConstructorTest.java index 918a151778..eb14db0381 100644 --- a/ldes-server-domain/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/rest/PrefixConstructorTest.java +++ b/ldes-server-domain/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/rest/PrefixConstructorTest.java @@ -20,7 +20,7 @@ class PrefixConstructorTest { private PrefixConstructor prefixConstructor; @Test - void when_NotUsingRelativeUrls_Then_GetHostname() throws Exception { + void when_NotUsingRelativeUrls_Then_GetHostname() { prefixConstructor = new PrefixConstructor(hostname, false); MockHttpServletRequest request = new MockHttpServletRequest(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); @@ -32,7 +32,7 @@ void when_NotUsingRelativeUrls_Then_GetHostname() throws Exception { } @ParameterizedTest(name = "Request with URI {0} returns {1}") @ArgumentsSource(RequestUriArgumentsProvider.class) - void when_UsingRelativeUrls_Then_GetCorrectPrefix(String requestUri, String expected) throws Exception { + void when_UsingRelativeUrls_Then_GetCorrectPrefix(String requestUri, String expected) { prefixConstructor = new PrefixConstructor(hostname, true); MockHttpServletRequest request = new MockHttpServletRequest(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); @@ -53,5 +53,5 @@ public Stream provideArguments(ExtensionContext extensionCo Arguments.of("", ""), Arguments.of("test/testing/testing/testing", "../../../..")); } - }; + } } \ No newline at end of file diff --git a/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/MemberPostgresRepository.java b/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/MemberPostgresRepository.java index f4ec0e2bb0..2a9e1b02fa 100644 --- a/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/MemberPostgresRepository.java +++ b/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/MemberPostgresRepository.java @@ -3,7 +3,6 @@ import be.vlaanderen.informatievlaanderen.ldes.server.ingest.entities.IngestedMember; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.mapper.MemberEntityMapper; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.repository.MemberEntityRepository; -import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.service.MemberEntityListener; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.repositories.MemberRepository; import io.micrometer.core.instrument.Metrics; import jakarta.persistence.EntityManager; @@ -32,7 +31,6 @@ public MemberPostgresRepository(MemberEntityRepository repository, this.repository = repository; this.mapper = mapper; this.entityManager = entityManager; - MemberEntityListener.repository = repository; } public boolean memberExists(String memberId) { @@ -98,8 +96,8 @@ public void deleteMembers(List memberIds) { @Override @Transactional public void removeFromEventSource(List ids) { - Query query = entityManager.createQuery("UPDATE MemberEntity m SET m.isInEventSource = false " + - "WHERE m.id IN :memberIds"); + final String sql = "UPDATE MemberEntity m SET m.isInEventSource = false WHERE m.id IN :memberIds"; + Query query = entityManager.createQuery(sql); query.setParameter("memberIds", ids); query.executeUpdate(); } diff --git a/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/service/MemberEntityListener.java b/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/service/MemberEntityListener.java index 0f6d5a1c10..cc240e54c2 100644 --- a/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/service/MemberEntityListener.java +++ b/ldes-server-infra-postgres/postgres-ingest-repository/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/postgres/service/MemberEntityListener.java @@ -4,17 +4,23 @@ import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.entity.MemberEntity; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.repository.MemberEntityRepository; import jakarta.persistence.PrePersist; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.stereotype.Component; @Component @SuppressWarnings({"java:S14444", "java:S1104"}) public class MemberEntityListener { - public static MemberEntityRepository repository; + private final ConfigurableApplicationContext applicationContext; - @PrePersist + public MemberEntityListener(ConfigurableApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + @PrePersist public void onPrePersist(MemberEntity memberEntity) { if (memberEntity.getSequenceNr() == null) { + final MemberEntityRepository repository = applicationContext.getBean(MemberEntityRepository.class); Long maxSequenceNr = repository.getNextSequenceNr(memberEntity.getCollectionName()); memberEntity.setSequenceNr(maxSequenceNr != null ? maxSequenceNr + 1 : 1); } diff --git a/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java b/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java index 1e788f7d4e..45cb321ce5 100644 --- a/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java +++ b/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java @@ -2,6 +2,7 @@ import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.MemberPostgresRepository; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.repository.MemberEntityRepository; +import be.vlaanderen.informatievlaanderen.ldes.server.ingest.postgres.service.MemberEntityListener; import io.cucumber.spring.CucumberContextConfiguration; import io.zonky.test.db.AutoConfigureEmbeddedDatabase; import org.springframework.beans.factory.annotation.Autowired; @@ -16,7 +17,7 @@ @DataJpaTest @AutoConfigureEmbeddedDatabase @ActiveProfiles("postgres-test") -@ContextConfiguration(classes = { MemberEntityRepository.class }) +@ContextConfiguration(classes = { MemberEntityRepository.class, MemberEntityListener.class }) @ComponentScan(value = { "be.vlaanderen.informatievlaanderen.ldes.server.ingest" }) @SuppressWarnings("java:S2187") public class PostgresIngestIntegrationTest { diff --git a/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/caching/EtagCachingStrategyTest.java b/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/caching/EtagCachingStrategyTest.java index 768ccc31c3..60d23a4743 100644 --- a/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/caching/EtagCachingStrategyTest.java +++ b/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/caching/EtagCachingStrategyTest.java @@ -22,10 +22,6 @@ class EtagCachingStrategyTest { private static final LdesFragmentIdentifier node3 = new LdesFragmentIdentifier( new ViewName("collectionName", "node3"), List.of()); - private static TreeNode createView(String viewName) { - return new TreeNode("/" + viewName, false, true, List.of(), List.of(), "collectionName", null); - } - private static TreeNode createView(String viewName, List relations, List members) { return new TreeNode("/" + viewName, false, true, relations, members, "collectionName", null); } diff --git a/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/services/TreeNodeConverterImplTest.java b/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/services/TreeNodeConverterImplTest.java index d63460fd8e..038c23d61b 100644 --- a/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/services/TreeNodeConverterImplTest.java +++ b/ldes-server-port-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/services/TreeNodeConverterImplTest.java @@ -18,7 +18,6 @@ import org.junit.jupiter.api.Test; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.*; import static org.apache.jena.rdf.model.ResourceFactory.createProperty; @@ -158,12 +157,6 @@ private void verifyMemberStatements(Model model) { .hasToString("[http://localhost:8080/mobility-hindrances, https://w3id.org/tree#member, https://private-api.gipod.beta-vlaanderen.be/api/v1/mobility-hindrances/10228622/165]"); } - private int getNumberOfStatements(Model model) { - AtomicInteger statementCounter = new AtomicInteger(); - model.listStatements().forEach((statement) -> statementCounter.getAndIncrement()); - return statementCounter.get(); - } - private void verifyTreeNodeStatement(Model model) { assertThat(model.listStatements(null, RDF_SYNTAX_TYPE, createResource(TREE_NODE_RESOURCE)).nextStatement()) .hasToString(String.format("[%s, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, https://w3id.org/tree#Node]", @@ -184,14 +177,6 @@ private void verifyIsPartOfStatement(Model model) { HOST_NAME + "/" + COLLECTION_NAME)); } - private void verifyRemainingItemsStatement(Model model) { - assertThat(model.listStatements(null, createProperty(TREE_REMAINING_ITEMS), (Resource) null).nextStatement()).hasToString( - String.format("[%s, %s, \"0\"^^http://www.w3.org/2001/XMLSchema#long]", - HOST_NAME + "/" + COLLECTION_NAME + "/" + VIEW_NAME, - TREE_REMAINING_ITEMS) - ); - } - private void verifyRemainingItemsStatementAbsent(Model model) { assertThat(model.listStatements(null, createProperty(TREE_REMAINING_ITEMS), (Resource) null).hasNext()).isFalse(); } diff --git a/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/RetentionPolicyFactoryImplTest.java b/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/RetentionPolicyFactoryImplTest.java index 26f1d7d678..b390fd9426 100644 --- a/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/RetentionPolicyFactoryImplTest.java +++ b/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/RetentionPolicyFactoryImplTest.java @@ -22,104 +22,104 @@ import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThatThrownBy; class RetentionPolicyFactoryImplTest { - private final RetentionPolicyFactory retentionPolicyFactory = new RetentionPolicyFactoryImpl(); - - @ParameterizedTest - @ArgumentsSource(FileNameRetentionPolicyArgumentsProvider.class) - void when_RetentionPolicyDescriptionIsAValidRetentionPolicy_then_ACorrectRetentionPolicyImplementationIsReturned( - String fileName, Class expectedRetentionPolicyClass) - throws URISyntaxException { - ViewName viewName = ViewName.fromString("col/view"); - var viewSpecification = new ViewSpecification(viewName, List.of(readModelFromFile(fileName)), List.of(), - 100); - - Optional result = retentionPolicyFactory.extractRetentionPolicy(viewSpecification); - - assertThat(result).isNotEmpty().containsInstanceOf(expectedRetentionPolicyClass); - } - - static class FileNameRetentionPolicyArgumentsProvider implements ArgumentsProvider { - - @Override - public Stream provideArguments(ExtensionContext context) { - return Stream.of( - Arguments.of("retentionpolicy/timebased/valid_timebased.ttl", TimeBasedRetentionPolicy.class), - Arguments.of("retentionpolicy/versionbased/valid_versionbased.ttl", - VersionBasedRetentionPolicy.class)); - } - } - - @Test - void when_RetentionPolicyDescriptionDoesNotHaveASyntaxTypeStatement_then_AnIllegalArgumentExceptionIsThrown() - throws URISyntaxException { - ViewName viewName = ViewName.fromString("col/view"); - List retentionPolicies = List.of(readModelFromFile("retentionpolicy/retentionpolicy-without-type.ttl")); - var viewSpecification = new ViewSpecification(viewName, retentionPolicies, List.of(), 100); - - IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, - () -> retentionPolicyFactory.extractRetentionPolicy(viewSpecification)); - assertEquals("Cannot Extract Retention Policy from statements:\n" + - "[ \"PT2M\"^^ ] .\n", - illegalArgumentException.getMessage()); - } - - @Test - void when_RetentionPolicyDescriptionHasAnUnknownType_then_AnIllegalArgumentExceptionIsThrown() - throws URISyntaxException { - ViewName viewName = ViewName.fromString("col/view"); - List retentionPolicies = List - .of(readModelFromFile("retentionpolicy/retentionpolicy-with-unknown-type.ttl")); - var viewSpecification = new ViewSpecification(viewName, retentionPolicies, List.of(), 100); - - IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, - () -> retentionPolicyFactory.extractRetentionPolicy(viewSpecification)); - assertEquals("Cannot Create Retention Policy from type: https://w3id.org/ldes#UnkownPolicy", - illegalArgumentException.getMessage()); - } - - @Test - void when_ViewHasNoRetentionPolicies_then_EmptyIsReturned() { - ViewName viewName = ViewName.fromString("col/view"); - var viewSpecification = new ViewSpecification(viewName, List.of(), List.of(), 100); - - Optional result = retentionPolicyFactory.extractRetentionPolicy(viewSpecification); - - assertThat(result).isEmpty(); - } - - @Test - void when_ViewHasMoreThan2RetentionPolicies_then_ExceptionIsThrown() throws URISyntaxException { - ViewName viewName = ViewName.fromString("col/view"); - Model policyModel = readModelFromFile("retentionpolicy/timebased/valid_timebased.ttl"); - var viewSpecification = new ViewSpecification(viewName, List.of(policyModel, policyModel, policyModel), - List.of(), 100); - - IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, - () -> retentionPolicyFactory.extractRetentionPolicy(viewSpecification)); - assertEquals("A view cannot have more than 2 retention policies!", - illegalArgumentException.getMessage()); - } - - @Test - void when_ViewContainsTimeBasedAndVersionPolicies_then_ATimeAndVersionBasedRetentionPolicyIsReturned() throws URISyntaxException { - ViewName viewName = ViewName.fromString("col/view"); - Model timeBasedPolicy = readModelFromFile("retentionpolicy/timebased/valid_timebased.ttl"); - Model versionBasedPolicy = readModelFromFile("retentionpolicy/versionbased/valid_versionbased.ttl"); - List models = List.of(timeBasedPolicy, versionBasedPolicy); - var viewSpecification = new ViewSpecification(viewName, models, List.of(), 100); - - Optional result = retentionPolicyFactory.extractRetentionPolicy(viewSpecification); - - assertThat(result).isNotEmpty().containsInstanceOf(TimeAndVersionBasedRetentionPolicy.class); - } - - private Model readModelFromFile(String fileName) throws URISyntaxException { - ClassLoader classLoader = getClass().getClassLoader(); - String uri = Objects.requireNonNull(classLoader.getResource(fileName)).toURI().toString(); - return RDFDataMgr.loadModel(uri); - } + private final RetentionPolicyFactory retentionPolicyFactory = new RetentionPolicyFactoryImpl(); + + @ParameterizedTest + @ArgumentsSource(FileNameRetentionPolicyArgumentsProvider.class) + void when_RetentionPolicyDescriptionIsAValidRetentionPolicy_then_ACorrectRetentionPolicyImplementationIsReturned( + String fileName, Class expectedRetentionPolicyClass) + throws URISyntaxException { + ViewName viewName = ViewName.fromString("col/view"); + var viewSpecification = new ViewSpecification(viewName, List.of(readModelFromFile(fileName)), List.of(), + 100); + + Optional result = retentionPolicyFactory.extractRetentionPolicy(viewSpecification); + + assertThat(result).isNotEmpty().containsInstanceOf(expectedRetentionPolicyClass); + } + + static class FileNameRetentionPolicyArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext context) { + return Stream.of( + Arguments.of("retentionpolicy/timebased/valid_timebased.ttl", TimeBasedRetentionPolicy.class), + Arguments.of("retentionpolicy/versionbased/valid_versionbased.ttl", + VersionBasedRetentionPolicy.class)); + } + } + + @Test + void when_RetentionPolicyDescriptionDoesNotHaveASyntaxTypeStatement_then_AnIllegalArgumentExceptionIsThrown() + throws URISyntaxException { + final String expectedMessage = """ + Cannot Extract Retention Policy from statements: + [ "PT2M"^^ ] . + """; + ViewName viewName = ViewName.fromString("col/view"); + List retentionPolicies = List.of(readModelFromFile("retentionpolicy/retentionpolicy-without-type.ttl")); + var viewSpecification = new ViewSpecification(viewName, retentionPolicies, List.of(), 100); + + assertThatThrownBy(() -> retentionPolicyFactory.extractRetentionPolicy(viewSpecification)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(expectedMessage); + } + + @Test + void when_RetentionPolicyDescriptionHasAnUnknownType_then_AnIllegalArgumentExceptionIsThrown() + throws URISyntaxException { + ViewName viewName = ViewName.fromString("col/view"); + List retentionPolicies = List + .of(readModelFromFile("retentionpolicy/retentionpolicy-with-unknown-type.ttl")); + var viewSpecification = new ViewSpecification(viewName, retentionPolicies, List.of(), 100); + + assertThatThrownBy(() -> retentionPolicyFactory.extractRetentionPolicy(viewSpecification)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot Create Retention Policy from type: https://w3id.org/ldes#UnkownPolicy"); + } + + @Test + void when_ViewHasNoRetentionPolicies_then_EmptyIsReturned() { + ViewName viewName = ViewName.fromString("col/view"); + var viewSpecification = new ViewSpecification(viewName, List.of(), List.of(), 100); + + Optional result = retentionPolicyFactory.extractRetentionPolicy(viewSpecification); + + assertThat(result).isEmpty(); + } + + @Test + void when_ViewHasMoreThan2RetentionPolicies_then_ExceptionIsThrown() throws URISyntaxException { + ViewName viewName = ViewName.fromString("col/view"); + Model policyModel = readModelFromFile("retentionpolicy/timebased/valid_timebased.ttl"); + var viewSpecification = new ViewSpecification(viewName, List.of(policyModel, policyModel, policyModel), + List.of(), 100); + + assertThatThrownBy(() -> retentionPolicyFactory.extractRetentionPolicy(viewSpecification)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("A view cannot have more than 2 retention policies!"); + } + + @Test + void when_ViewContainsTimeBasedAndVersionPolicies_then_ATimeAndVersionBasedRetentionPolicyIsReturned() throws URISyntaxException { + ViewName viewName = ViewName.fromString("col/view"); + Model timeBasedPolicy = readModelFromFile("retentionpolicy/timebased/valid_timebased.ttl"); + Model versionBasedPolicy = readModelFromFile("retentionpolicy/versionbased/valid_versionbased.ttl"); + List models = List.of(timeBasedPolicy, versionBasedPolicy); + var viewSpecification = new ViewSpecification(viewName, models, List.of(), 100); + + Optional result = retentionPolicyFactory.extractRetentionPolicy(viewSpecification); + + assertThat(result).isNotEmpty().containsInstanceOf(TimeAndVersionBasedRetentionPolicy.class); + } + + private Model readModelFromFile(String fileName) throws URISyntaxException { + ClassLoader classLoader = getClass().getClassLoader(); + String uri = Objects.requireNonNull(classLoader.getResource(fileName)).toURI().toString(); + return RDFDataMgr.loadModel(uri); + } } diff --git a/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/timebased/TimeBasedRetentionPolicyCreatorTest.java b/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/timebased/TimeBasedRetentionPolicyCreatorTest.java index 42ca8fb6b8..3ee1103586 100644 --- a/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/timebased/TimeBasedRetentionPolicyCreatorTest.java +++ b/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/timebased/TimeBasedRetentionPolicyCreatorTest.java @@ -9,43 +9,46 @@ import java.net.URISyntaxException; import java.util.Objects; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; class TimeBasedRetentionPolicyCreatorTest { - private final TimeBasedRetentionPolicyCreator timeBasedRetentionPolicyCreator = new TimeBasedRetentionPolicyCreator(); - - @Test - void when_ModelDescribesAValidTimeBasedRetentionPolicy_then_ATimeBasedRetentionPolicyIsReturned() - throws URISyntaxException { - Model retentionModel = readModelFromFile("retentionpolicy/timebased/valid_timebased.ttl"); - - RetentionPolicy retentionPolicy = timeBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel); - - assertInstanceOf(TimeBasedRetentionPolicy.class, retentionPolicy); - } - - @Test - void when_ModelDoesNotExactlyHaveOneTreeValueStatement_then_AnIllegalArgumentExceptionIsThrown() - throws URISyntaxException { - Model retentionModel = readModelFromFile("retentionpolicy/timebased/invalid_timebased.ttl"); - - IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, - () -> timeBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel)); - assertEquals( - "Cannot Create Time Based Retention Policy in which there is not exactly 1 https://w3id.org/tree#value statement.\n" - + - " Found 2 statements in :\n" + - "[ a ;\n" + - " \"PT3M\"^^ , \"PT2M\"^^\n" - + - "] .\n", - illegalArgumentException.getMessage()); - } - - private Model readModelFromFile(String fileName) throws URISyntaxException { - ClassLoader classLoader = getClass().getClassLoader(); - String uri = Objects.requireNonNull(classLoader.getResource(fileName)).toURI().toString(); - return RDFDataMgr.loadModel(uri); - } + private final TimeBasedRetentionPolicyCreator timeBasedRetentionPolicyCreator = new TimeBasedRetentionPolicyCreator(); + + @Test + void when_ModelDescribesAValidTimeBasedRetentionPolicy_then_ATimeBasedRetentionPolicyIsReturned() + throws URISyntaxException { + Model retentionModel = readModelFromFile("retentionpolicy/timebased/valid_timebased.ttl"); + + RetentionPolicy retentionPolicy = timeBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel); + + assertThat(retentionPolicy).isInstanceOf(TimeBasedRetentionPolicy.class); + } + + @Test + void when_ModelDoesNotExactlyHaveOneTreeValueStatement_then_AnIllegalArgumentExceptionIsThrown() + throws URISyntaxException { + Model retentionModel = readModelFromFile("retentionpolicy/timebased/invalid_timebased.ttl"); + + assertThatThrownBy(() -> timeBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(getExpectedMessage()); + } + + private Model readModelFromFile(String fileName) throws URISyntaxException { + ClassLoader classLoader = getClass().getClassLoader(); + String uri = Objects.requireNonNull(classLoader.getResource(fileName)).toURI().toString(); + return RDFDataMgr.loadModel(uri); + } + + private String getExpectedMessage() { + return """ + Cannot Create Time Based Retention Policy in which there is not exactly 1 https://w3id.org/tree#value statement. + Found 2 statements in : + [ a ; + "PT3M"^^ , "PT2M"^^ + ] . + """; + } } \ No newline at end of file diff --git a/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/versionbased/VersionBasedRetentionPolicyCreatorTest.java b/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/versionbased/VersionBasedRetentionPolicyCreatorTest.java index c7c8358c6c..b61ca1ad62 100644 --- a/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/versionbased/VersionBasedRetentionPolicyCreatorTest.java +++ b/ldes-server-retention/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/retention/services/retentionpolicy/creation/versionbased/VersionBasedRetentionPolicyCreatorTest.java @@ -12,48 +12,49 @@ import java.net.URISyntaxException; import java.util.Objects; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @ExtendWith(MockitoExtension.class) class VersionBasedRetentionPolicyCreatorTest { - private VersionBasedRetentionPolicyCreator versionBasedRetentionPolicyCreator; - - @BeforeEach - void setUp() { - this.versionBasedRetentionPolicyCreator = new VersionBasedRetentionPolicyCreator(); - } - - @Test - void when_ModelDescribesAValidVersionBasedRetentionPolicy_then_AVersionBasedRetentionPolicyIsReturned() - throws URISyntaxException { - Model retentionModel = readModelFromFile("retentionpolicy/versionbased/valid_versionbased.ttl"); - - RetentionPolicy retentionPolicy = versionBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel); - - assertInstanceOf(VersionBasedRetentionPolicy.class, retentionPolicy); - } - - @Test - void when_ModelDoesNotExactlyHaveOneLdesAmountStatement_then_AnIllegalArgumentExceptionIsThrown() - throws URISyntaxException { - Model retentionModel = readModelFromFile("retentionpolicy/versionbased/invalid_versionbased.ttl"); - - IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, - () -> versionBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel)); - assertEquals( - "Cannot Create Version Based Retention Policy in which there is not exactly 1 https://w3id.org/ldes#amount statement.\n" - + - " Found 2 statements in :\n" + - "[ a ;\n" + - " 3 , 2\n" + - "] .\n", - illegalArgumentException.getMessage()); - } - - private Model readModelFromFile(String fileName) throws URISyntaxException { - ClassLoader classLoader = getClass().getClassLoader(); - String uri = Objects.requireNonNull(classLoader.getResource(fileName)).toURI().toString(); - return RDFDataMgr.loadModel(uri); - } + private VersionBasedRetentionPolicyCreator versionBasedRetentionPolicyCreator; + + @BeforeEach + void setUp() { + this.versionBasedRetentionPolicyCreator = new VersionBasedRetentionPolicyCreator(); + } + + @Test + void when_ModelDescribesAValidVersionBasedRetentionPolicy_then_AVersionBasedRetentionPolicyIsReturned() + throws URISyntaxException { + Model retentionModel = readModelFromFile("retentionpolicy/versionbased/valid_versionbased.ttl"); + + RetentionPolicy retentionPolicy = versionBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel); + + assertThat(retentionPolicy).isInstanceOf(VersionBasedRetentionPolicy.class); + } + + @Test + void when_ModelDoesNotExactlyHaveOneLdesAmountStatement_then_AnIllegalArgumentExceptionIsThrown() + throws URISyntaxException { + final String expectedMessage = """ + Cannot Create Version Based Retention Policy in which there is not exactly 1 https://w3id.org/ldes#amount statement. + Found 2 statements in : + [ a ; + 3 , 2 + ] . + """; + Model retentionModel = readModelFromFile("retentionpolicy/versionbased/invalid_versionbased.ttl"); + + assertThatThrownBy(() -> versionBasedRetentionPolicyCreator.createRetentionPolicy(retentionModel)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(expectedMessage); + } + + private Model readModelFromFile(String fileName) throws URISyntaxException { + ClassLoader classLoader = getClass().getClassLoader(); + String uri = Objects.requireNonNull(classLoader.getResource(fileName)).toURI().toString(); + return RDFDataMgr.loadModel(uri); + } } \ No newline at end of file