diff --git a/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationEventListener.java b/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationEventListener.java index 2dcbab418aad29b55cb2f4c4cd6bdd5fa3795670..cb8abfd8503f06fb6ce2409e826e4c333fd04ddf 100644 --- a/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationEventListener.java +++ b/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationEventListener.java @@ -34,6 +34,7 @@ import org.springframework.security.core.context.SecurityContext; import org.springframework.stereotype.Component; import de.ozgcloud.collaboration.common.callcontext.CurrentUserService; +import de.ozgcloud.collaboration.fachstelle.FachstelleService; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEvent; import de.ozgcloud.command.CommandFailedEvent; @@ -54,6 +55,7 @@ class CollaborationEventListener { private static final String ERROR_MESSAGE_TEMPLATE = "Error on executing %s Command (id: %s)."; private final CollaborationService collaborationService; + private final FachstelleService fachstelleService; @Qualifier(CollaborationManagerConfiguration.CURRENT_USER_SERVICE_NAME) // NOSONAR private final CurrentUserService currentUserService; private final ApplicationEventPublisher eventPublisher; @@ -68,15 +70,27 @@ class CollaborationEventListener { void createCollaborationRequest(Command command) { var collaborationRequest = collaborationRequestMapper.toCollaborationRequest(command); switch (collaborationRequest.getCollaborationLevel()) { - case 1 -> collaborationService.createCollaborationRequest(collaborationRequest); - case 4 -> collaborationService.createFachstellenBeteiligungRequest(collaborationRequest); - default -> { - LOG.error("Unknown Collaboration Level: {}", collaborationRequest.getCollaborationLevel()); - eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), "Unknown Collaboration Level.")); - } + case 1 -> createLevel1CollaborationRequest(collaborationRequest, command); + case 4 -> createLevel4CollaborationRequest(collaborationRequest, command); + default -> { + LOG.error("Unknown Collaboration Level: {}", collaborationRequest.getCollaborationLevel()); + eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), "Unknown Collaboration Level.")); + } } } + void createLevel1CollaborationRequest(CollaborationRequest collaborationRequest, Command command) { + var fachstelle = fachstelleService + .getOrganisationsEinheit((String) command.getBodyObject().get(CollaborationRequest.PROPERTY_ZUSTAENDIGE_STELLE)); + collaborationService.createCollaborationRequest(collaborationRequest.toBuilder().zustaendigeStelle(fachstelle).build()); + } + + void createLevel4CollaborationRequest(CollaborationRequest collaborationRequest, Command command) { + var fachstelle = fachstelleService + .getExterneFachstelle((String) command.getBodyObject().get(CollaborationRequest.PROPERTY_ZUSTAENDIGE_STELLE)); + collaborationService.createFachstellenBeteiligungRequest(collaborationRequest.toBuilder().zustaendigeStelle(fachstelle).build()); + } + void runWithSecurityContext(Command command, Consumer<Command> commandExecutor) { SecurityContext prevContext = null; try { diff --git a/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationRequestMapper.java b/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationRequestMapper.java index 8fff61717ae74b22472b217dcc21e3ee4a3ac9d8..19e0a4cc590ebac0d4bd4d2bfe8f8686ff859764 100644 --- a/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationRequestMapper.java +++ b/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/CollaborationRequestMapper.java @@ -42,7 +42,6 @@ import de.ozgcloud.collaboration.common.callcontext.CollaborationManagerCallCont import de.ozgcloud.collaboration.common.vorgang.attached_item.VorgangAttachedItem; import de.ozgcloud.collaboration.fachstelle.Fachstelle; import de.ozgcloud.collaboration.fachstelle.FachstelleMapper; -import de.ozgcloud.collaboration.fachstelle.FachstelleService; import de.ozgcloud.collaboration.request.CollaborationRequestId; import de.ozgcloud.collaboration.request.GrpcCollaborationRequest; import de.ozgcloud.command.Command; @@ -64,26 +63,18 @@ public abstract class CollaborationRequestMapper { static final String FIELD_ITEM_NAME = "itemName"; static final String FIELD_ITEM_BODY = "item"; - @Autowired - private FachstelleService fachstelleService; @Autowired private FachstelleMapper fachstelleMapper; @Mapping(target = "collaborationVorgangId", ignore = true) @Mapping(target = "createdBy", ignore = true) + @Mapping(target = "zustaendigeStelle", ignore = true) @Mapping(target = "commandId", source = "id") @Mapping(target = "titel", expression = "java(getStringProperty(CollaborationRequest.PROPERTY_TITEL, command.getBodyObject()))") - @Mapping(target = "zustaendigeStelle", expression = "java(getZustaendigeStelleFromCommandBody(command.getBodyObject()))") @Mapping(target = "beschreibung", expression = "java(getStringProperty(CollaborationRequest.PROPERTY_BESCHREIBUNG, command.getBodyObject()))") @Mapping(target = "collaborationLevel", expression = "java(getIntProperty(CollaborationRequest.PROPERTY_COLLABORATION_LEVEL, command.getBodyObject()))") public abstract CollaborationRequest toCollaborationRequest(Command command); - Fachstelle getZustaendigeStelleFromCommandBody(Map<String, Object> propertyMap) { - var technicalId = getStringProperty(CollaborationRequest.PROPERTY_ZUSTAENDIGE_STELLE, propertyMap); - var collaborationLevel = getIntProperty(CollaborationRequest.PROPERTY_COLLABORATION_LEVEL, propertyMap); - return fachstelleService.getFachstelle(technicalId, collaborationLevel); - } - @Mapping(target = "id", ignore = true) @Mapping(target = "status", ignore = true) @Mapping(target = "relationVersion", ignore = true) diff --git a/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/fachstelle/FachstelleService.java b/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/fachstelle/FachstelleService.java index dd5ffd6bb72685362ba19d7e91ef721778139b86..1bc8039327c4e29831ccb6e12f2fff821afe4fe2 100644 --- a/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/fachstelle/FachstelleService.java +++ b/collaboration-manager-server/src/main/java/de/ozgcloud/collaboration/fachstelle/FachstelleService.java @@ -10,14 +10,11 @@ public class FachstelleService { private final FachstelleRemoteService remoteService; - public Fachstelle getFachstelle(String technicalId, int collaborationLevel) { - if (collaborationLevel == 1) { - return remoteService.getOrganisationsEinheit(technicalId); - } - if (collaborationLevel == 4) { - return remoteService.getExterneFachstelle(technicalId); - } - throw new IllegalArgumentException( - "Unknown collaboration level! Collaboration level has to be either 1 or 4, but was %s.".formatted(collaborationLevel)); + public Fachstelle getOrganisationsEinheit(String technicalId) { + return remoteService.getOrganisationsEinheit(technicalId); + } + + public Fachstelle getExterneFachstelle(String technicalId) { + return remoteService.getExterneFachstelle(technicalId); } } diff --git a/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationEventListenerTest.java b/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationEventListenerTest.java index 86f085a55b7744711243001ca66abaf71dbba8ef..ccd5e1497fb0227f84b9de0cefb82e1a85575c64 100644 --- a/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationEventListenerTest.java +++ b/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationEventListenerTest.java @@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; +import java.util.Map; import java.util.function.Consumer; import org.junit.jupiter.api.BeforeEach; @@ -44,6 +45,8 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.security.core.context.SecurityContext; import de.ozgcloud.collaboration.common.callcontext.CurrentUserService; +import de.ozgcloud.collaboration.fachstelle.FachstelleService; +import de.ozgcloud.collaboration.fachstelle.FachstelleTestFactory; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEventTestFactory; import de.ozgcloud.command.CommandFailedEvent; @@ -62,6 +65,8 @@ class CollaborationEventListenerTest { @Mock private CollaborationService collaborationService; @Mock + private FachstelleService fachstelleService; + @Mock private CollaborationRequestMapper commandMapper; @Nested @@ -84,7 +89,7 @@ class CollaborationEventListenerTest { } @Nested - class TestCollaborationRequest { + class TestCreateCollaborationRequest { private final Command command = CommandTestFactory.createBuilder().build(); @@ -101,23 +106,23 @@ class CollaborationEventListenerTest { } @Test - void shouldCallCreateCollaborationRequest() { + void shouldCallCreateLevel1CollaborationRequest() { var collaborationRequest = CollaborationRequestTestFactory.create(); when(commandMapper.toCollaborationRequest(any())).thenReturn(collaborationRequest); eventListener.createCollaborationRequest(command); - verify(collaborationService).createCollaborationRequest(collaborationRequest); + verify(eventListener).createLevel1CollaborationRequest(collaborationRequest, command); } @Test - void shouldCallCreateFachstellenBeteiligungRequest() { + void shouldCallCreateLevel4CollaborationRequest() { var collaborationRequest = CollaborationRequestTestFactory.createBuilder().collaborationLevel(4).build(); when(commandMapper.toCollaborationRequest(any())).thenReturn(collaborationRequest); eventListener.createCollaborationRequest(command); - verify(collaborationService).createFachstellenBeteiligungRequest(collaborationRequest); + verify(eventListener).createLevel4CollaborationRequest(collaborationRequest, command); } @DisplayName("should decline request") @@ -134,6 +139,86 @@ class CollaborationEventListenerTest { } } + @Nested + class TestCreateLevel1CollaborationRequest { + + private final Map<String, Object> bodyObject = Map.of( + CollaborationRequest.PROPERTY_TITEL, CollaborationRequestTestFactory.TITEL, + CollaborationRequest.PROPERTY_BESCHREIBUNG, CollaborationRequestTestFactory.BESCHREIBUNG, + CollaborationRequest.PROPERTY_ZUSTAENDIGE_STELLE, FachstelleTestFactory.TECHNICAL_ID, + CollaborationRequest.PROPERTY_COLLABORATION_LEVEL, CollaborationRequestTestFactory.COLLABORATION_LEVEL); + private final Command command = CommandTestFactory.createBuilder() + .bodyObject(bodyObject) + .build(); + private final CollaborationRequest collaborationRequest = CollaborationRequestTestFactory.createBuilder() + .zustaendigeStelle(null) + .build(); + + @Captor + private ArgumentCaptor<CollaborationRequest> collaborationRequestCaptor; + + @Test + void shouldCallFachstelleService() { + createLevel1CollaborationRequest(); + + verify(fachstelleService).getOrganisationsEinheit(FachstelleTestFactory.TECHNICAL_ID); + } + + @Test + void shouldCallCreateCollaborationRequest() { + when(fachstelleService.getOrganisationsEinheit(any())).thenReturn(CollaborationRequestTestFactory.ZUSTAENDIGE_STELLE); + + createLevel1CollaborationRequest(); + + verify(collaborationService).createCollaborationRequest(collaborationRequestCaptor.capture()); + assertThat(collaborationRequestCaptor.getValue()).usingRecursiveComparison().isEqualTo(CollaborationRequestTestFactory.create()); + } + + private void createLevel1CollaborationRequest() { + eventListener.createLevel1CollaborationRequest(collaborationRequest, command); + } + } + + @Nested + class TestCreateLevel4CollaborationRequest { + + private final Map<String, Object> bodyObject = Map.of( + CollaborationRequest.PROPERTY_TITEL, CollaborationRequestTestFactory.TITEL, + CollaborationRequest.PROPERTY_BESCHREIBUNG, CollaborationRequestTestFactory.BESCHREIBUNG, + CollaborationRequest.PROPERTY_ZUSTAENDIGE_STELLE, FachstelleTestFactory.TECHNICAL_ID, + CollaborationRequest.PROPERTY_COLLABORATION_LEVEL, CollaborationRequestTestFactory.COLLABORATION_LEVEL); + private final Command command = CommandTestFactory.createBuilder() + .bodyObject(bodyObject) + .build(); + private final CollaborationRequest collaborationRequest = CollaborationRequestTestFactory.createBuilder() + .zustaendigeStelle(null) + .build(); + + @Captor + private ArgumentCaptor<CollaborationRequest> collaborationRequestCaptor; + + @Test + void shouldCallFachstelleService() { + createLevel4CollaborationRequest(); + + verify(fachstelleService).getExterneFachstelle(FachstelleTestFactory.TECHNICAL_ID); + } + + @Test + void shouldCallCreateFachstellenBeteiligungRequest() { + when(fachstelleService.getExterneFachstelle(any())).thenReturn(CollaborationRequestTestFactory.ZUSTAENDIGE_STELLE); + + createLevel4CollaborationRequest(); + + verify(collaborationService).createFachstellenBeteiligungRequest(collaborationRequestCaptor.capture()); + assertThat(collaborationRequestCaptor.getValue()).usingRecursiveComparison().isEqualTo(CollaborationRequestTestFactory.create()); + } + + private void createLevel4CollaborationRequest() { + eventListener.createLevel4CollaborationRequest(collaborationRequest, command); + } + } + @Nested class TestRunWithSecurityContext { diff --git a/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationRequestMapperTest.java b/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationRequestMapperTest.java index 87aec78c83b80e76b24f865f2542134586bee3be..0566f090440a4865532281f2d1605c01b2a7f9be 100644 --- a/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationRequestMapperTest.java +++ b/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/CollaborationRequestMapperTest.java @@ -82,21 +82,12 @@ class CollaborationRequestMapperTest { .vorgangId(VorgangTestFactory.ID_STR) .build(); - @Test - void shouldCallGetZustaendigeStelle() { - mapper.toCollaborationRequest(COMMAND); - - verify(mapper).getZustaendigeStelleFromCommandBody(BODY_OBJECT); - } - @Test void shouldMapCommand() { - doReturn(CollaborationRequestTestFactory.ZUSTAENDIGE_STELLE).when(mapper).getZustaendigeStelleFromCommandBody(any()); - var result = mapper.toCollaborationRequest(COMMAND); assertThat(result).usingRecursiveComparison() - .ignoringFields("id", "createdBy", "collaborationVorgangId") + .ignoringFields("id", "createdBy", "collaborationVorgangId", "zustaendigeStelle") .isEqualTo(CollaborationRequestTestFactory.create()); } } diff --git a/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/fachstelle/FachstelleServiceTest.java b/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/fachstelle/FachstelleServiceTest.java index 8c364282ce37fefbc0c8ca04d79efa7ee1b321e9..943a384472dd4ebd62869a87051d00436faa8a8e 100644 --- a/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/fachstelle/FachstelleServiceTest.java +++ b/collaboration-manager-server/src/test/java/de/ozgcloud/collaboration/fachstelle/FachstelleServiceTest.java @@ -1,7 +1,6 @@ package de.ozgcloud.collaboration.fachstelle; import static org.assertj.core.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -19,57 +18,44 @@ class FachstelleServiceTest { private FachstelleRemoteService remoteService; @Nested - class TestGetFachstelle { + class TestGetOrganisationsEinheit { - @Nested - class TestOnCollaborationLevel1 { + @Test + void shouldGetOrganisationsEinheit() { + service.getOrganisationsEinheit(FachstelleTestFactory.TECHNICAL_ID); - @Test - void shouldGetOrganisationsEinheit() { - service.getFachstelle(FachstelleTestFactory.TECHNICAL_ID, 1); - - verify(remoteService).getOrganisationsEinheit(FachstelleTestFactory.TECHNICAL_ID); - } - - @Test - void shouldReturnFachstelle() { - var expectedFachstelle = FachstelleTestFactory.create(); - when(remoteService.getOrganisationsEinheit(any())).thenReturn(expectedFachstelle); - - var actualFachstelle = service.getFachstelle(FachstelleTestFactory.TECHNICAL_ID, 1); - - assertThat(actualFachstelle).isSameAs(expectedFachstelle); - } + verify(remoteService).getOrganisationsEinheit(FachstelleTestFactory.TECHNICAL_ID); } - @Nested - class TestOnCollaborationLevel4 { + @Test + void shouldReturnFachstelle() { + var expectedFachstelle = FachstelleTestFactory.create(); + when(remoteService.getOrganisationsEinheit(any())).thenReturn(expectedFachstelle); - @Test - void shouldGetExterneFachstelle() { - service.getFachstelle(FachstelleTestFactory.TECHNICAL_ID, 4); + var actualFachstelle = service.getOrganisationsEinheit(FachstelleTestFactory.TECHNICAL_ID); - verify(remoteService).getExterneFachstelle(FachstelleTestFactory.TECHNICAL_ID); - } + assertThat(actualFachstelle).isSameAs(expectedFachstelle); + } + } - @Test - void shouldReturnFachstelle() { - var expectedFachstelle = FachstelleTestFactory.create(); - when(remoteService.getExterneFachstelle(any())).thenReturn(expectedFachstelle); + @Nested + class TestGetExterneFachstelle { - var actualFachstelle = service.getFachstelle(FachstelleTestFactory.TECHNICAL_ID, 4); + @Test + void shouldGetExterneFachstelle() { + service.getExterneFachstelle(FachstelleTestFactory.TECHNICAL_ID); - assertThat(actualFachstelle).isSameAs(expectedFachstelle); - } + verify(remoteService).getExterneFachstelle(FachstelleTestFactory.TECHNICAL_ID); } - @Nested - class TestOnUnknownCollaborationLevel { + @Test + void shouldReturnFachstelle() { + var expectedFachstelle = FachstelleTestFactory.create(); + when(remoteService.getExterneFachstelle(any())).thenReturn(expectedFachstelle); + + var actualFachstelle = service.getExterneFachstelle(FachstelleTestFactory.TECHNICAL_ID); - @Test - void shouldThrowIlleagalArgumentException() { - assertThrows(IllegalArgumentException.class, () -> service.getFachstelle(FachstelleTestFactory.TECHNICAL_ID, 5)); - } + assertThat(actualFachstelle).isSameAs(expectedFachstelle); } } }