From f9b88dd407fe39d7fcee8a5d6f0cf9bbe945e506 Mon Sep 17 00:00:00 2001 From: Gheorghe Soimu Date: Wed, 16 Oct 2024 14:30:30 +0300 Subject: [PATCH] Added CreatePostcoordinationFromParentCommandHandlerIT --- ...eatePostcoordinationFromParentRequest.java | 6 + ...oordinationFromParentCommandHandlerIT.java | 193 ++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentCommandHandlerIT.java diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentRequest.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentRequest.java index c45dd40..7b8abbf 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentRequest.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentRequest.java @@ -13,6 +13,12 @@ public record CreatePostcoordinationFromParentRequest( public final static String CHANNEL = "webprotege.postcoordination.CreateFromParentEntity"; + public static CreatePostcoordinationFromParentRequest create(IRI newEntityIri, + IRI parentEntityIri, + ProjectId projectId) { + return new CreatePostcoordinationFromParentRequest(newEntityIri, parentEntityIri, projectId); + } + @Override public String getChannel() { return CHANNEL; diff --git a/src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentCommandHandlerIT.java b/src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentCommandHandlerIT.java new file mode 100644 index 0000000..1b5a598 --- /dev/null +++ b/src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/CreatePostcoordinationFromParentCommandHandlerIT.java @@ -0,0 +1,193 @@ +package edu.stanford.protege.webprotege.postcoordinationservice.handlers; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import edu.stanford.protege.webprotege.common.*; +import edu.stanford.protege.webprotege.ipc.ExecutionContext; +import edu.stanford.protege.webprotege.postcoordinationservice.*; +import edu.stanford.protege.webprotege.postcoordinationservice.dto.PostCoordinationSpecification; +import edu.stanford.protege.webprotege.postcoordinationservice.model.*; +import edu.stanford.protege.webprotege.postcoordinationservice.services.*; +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.semanticweb.owlapi.model.IRI; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.*; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.io.*; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@SpringBootTest +@Import({WebprotegePostcoordinationServiceServiceApplication.class}) +@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS) +@ExtendWith({SpringExtension.class, IntegrationTest.class}) +@ActiveProfiles("test") +class CreatePostcoordinationFromParentCommandHandlerIT { + + @Autowired + private CreatePostcoordinationFromParentCommandHandler handler; + + @Autowired + private PostCoordinationEventProcessor eventProcessor; + + @MockBean + private LinearizationService linearizationService; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private MongoTemplate mongoTemplate; + + private ProjectId projectId; + private ExecutionContext executionContext; + private String parentEntityIri; + private String newEntityIri; + + @BeforeEach + void setUp() throws IOException { + projectId = ProjectId.generate(); + executionContext = new ExecutionContext(UserId.valueOf("testUser"), "testToken"); + parentEntityIri = "http://example.org/parentEntity"; + newEntityIri = "http://example.org/newEntity"; + FileInputStream defintions = new FileInputStream("src/test/resources/LinearizationDefinitions.json"); + when(linearizationService.getLinearizationDefinitions()) + .thenReturn(objectMapper.readValue(defintions, new TypeReference<>() { + })); + + mongoTemplate.getDb().drop(); + } + + @Test + void GIVEN_validParentWithCoreLinId_WHEN_handleRequest_THEN_defaultAxesShouldBeInherited() { + CreatePostcoordinationFromParentRequest request = CreatePostcoordinationFromParentRequest.create( + IRI.create(newEntityIri), IRI.create(parentEntityIri), projectId + ); + + WhoficEntityPostCoordinationSpecification parentSpec = new WhoficEntityPostCoordinationSpecification( + parentEntityIri, "ICD", List.of( + new PostCoordinationSpecification("http://id.who.int/icd/release/11/mms", List.of("axis1"), List.of("axis2"), List.of(), List.of()) + )); + eventProcessor.saveNewSpecificationRevision(parentSpec, UserId.getGuest(), projectId); + + handler.handleRequest(request, executionContext).block(); + + WhoficEntityPostCoordinationSpecification savedSpec = eventProcessor.fetchHistory(newEntityIri, projectId); + + assertNotNull(savedSpec, "The new specification should be saved."); + assertEquals(newEntityIri, savedSpec.whoficEntityIri(), "The saved specification should match the new entity IRI."); + + PostCoordinationSpecification inheritedSpec = savedSpec.postcoordinationSpecifications().get(0); + assertEquals(List.of("axis1", "axis2"), inheritedSpec.getNotAllowedAxes(), "Axes inherited from parent should have not allowed values for main linearizations"); + assertTrue(inheritedSpec.getDefaultAxes().isEmpty(), "Axes inherited from parent should have default values for telescopic linearizations"); + } + + + @Test + void GIVEN_validParentWithoutCoreLinId_WHEN_handleRequest_THEN_notAllowedAxesShouldBeInherited() { + CreatePostcoordinationFromParentRequest request = CreatePostcoordinationFromParentRequest.create( + IRI.create(newEntityIri), IRI.create(parentEntityIri), projectId + ); + + WhoficEntityPostCoordinationSpecification parentSpec = WhoficEntityPostCoordinationSpecification.create( + parentEntityIri, "ICD", List.of( + new PostCoordinationSpecification("http://id.who.int/icd/release/11/icd-o", List.of("axis1"), List.of(), List.of(), List.of()) + )); + eventProcessor.saveNewSpecificationRevision(parentSpec, UserId.getGuest(), projectId); + + + handler.handleRequest(request, executionContext).block(); + + WhoficEntityPostCoordinationSpecification savedSpec = eventProcessor.fetchHistory(newEntityIri, projectId); + + assertNotNull(savedSpec, "The new specification should be saved."); + assertEquals(newEntityIri, savedSpec.whoficEntityIri(), "The saved specification should match the new entity IRI."); + + PostCoordinationSpecification inheritedSpec = savedSpec.postcoordinationSpecifications().get(0); + assertTrue(inheritedSpec.getDefaultAxes().isEmpty(), "defaultAxes should be empty for main linearizations"); + assertEquals(List.of("axis1"), inheritedSpec.getNotAllowedAxes(), "notAllowedAxes should inherit from the parent all axes when linearization view is main linearization"); + } + + @Test + void GIVEN_invalidParentEntity_WHEN_handleRequest_THEN_noSpecificationShouldBeSaved() { + CreatePostcoordinationFromParentRequest request = CreatePostcoordinationFromParentRequest.create( + IRI.create(newEntityIri), IRI.create("http://invalid-parent-iri"), projectId + ); + + handler.handleRequest(request, executionContext).block(); + + Query query = new Query(); + query.addCriteria(Criteria.where("whoficEntityIri").is(newEntityIri)); + WhoficEntityPostCoordinationSpecification savedSpec = mongoTemplate.findOne(query, WhoficEntityPostCoordinationSpecification.class); + + assertNull(savedSpec, "No specification should have been saved when the parent entity is invalid."); + } + + @Test + void GIVEN_parentWithMultipleLinearizations_WHEN_handleRequest_THEN_axesShouldBeInheritedForEachView() { + CreatePostcoordinationFromParentRequest request = CreatePostcoordinationFromParentRequest.create( + IRI.create(newEntityIri), IRI.create(parentEntityIri), projectId + ); + + WhoficEntityPostCoordinationSpecification parentSpec = new WhoficEntityPostCoordinationSpecification( + parentEntityIri, "ICD", List.of( + new PostCoordinationSpecification("http://id.who.int/icd/release/11/mms", List.of("axis1"), List.of(), List.of(), List.of()), + new PostCoordinationSpecification("http://id.who.int/icd/release/11/pch", List.of("axis2"), List.of(), List.of(), List.of()) + )); + eventProcessor.saveNewSpecificationRevision(parentSpec, UserId.getGuest(), projectId); + + + handler.handleRequest(request, executionContext).block(); + + WhoficEntityPostCoordinationSpecification savedSpec = eventProcessor.fetchHistory(newEntityIri, projectId); + + assertNotNull(savedSpec, "The new specification should be saved."); + assertEquals(2, savedSpec.postcoordinationSpecifications().size(), "There should be two linearization specifications."); + + for (PostCoordinationSpecification inheritedSpec : savedSpec.postcoordinationSpecifications()) { + if (inheritedSpec.getLinearizationView().equals("http://id.who.int/icd/release/11/mms")) { + assertEquals(List.of("axis1"), inheritedSpec.getNotAllowedAxes(), "Should inherit axes for MMS (main linearization) and have not allowed value."); + } else if (inheritedSpec.getLinearizationView().equals("http://id.who.int/icd/release/11/pch")) { + assertEquals(List.of("axis2"), inheritedSpec.getDefaultAxes(), "Should inherit axes for PCH (telescopic linearization) and have default value"); + } + } + } + + @Test + void GIVEN_validRequest_WHEN_handleRequestTwice_THEN_noDuplicateSpecificationsShouldBeSaved() { + CreatePostcoordinationFromParentRequest request = CreatePostcoordinationFromParentRequest.create( + IRI.create(newEntityIri), IRI.create(parentEntityIri), projectId + ); + + WhoficEntityPostCoordinationSpecification parentSpec = new WhoficEntityPostCoordinationSpecification( + parentEntityIri, "ICD", List.of( + new PostCoordinationSpecification("http://id.who.int/icd/release/11/mms", List.of("axis1"), List.of(), List.of(), List.of()) + )); + eventProcessor.saveNewSpecificationRevision(parentSpec, UserId.getGuest(), projectId); + + + handler.handleRequest(request, executionContext).block(); + handler.handleRequest(request, executionContext).block(); + + Query query = new Query(); + query.addCriteria(Criteria.where("whoficEntityIri").is(newEntityIri)); + EntityPostCoordinationHistory savedHistory = mongoTemplate.findOne(query, EntityPostCoordinationHistory.class); + + assertNotNull(savedHistory); + assertEquals(1, savedHistory.getPostCoordinationRevisions().size(), "There should only be one saved revision."); + } + + +} + +