diff --git a/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java b/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java index a2398a8514299622407c46e6320f6926f0a5be0f..248e80f2360bb8372526fcac7f563604c6bd984f 100644 --- a/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java +++ b/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java @@ -25,6 +25,7 @@ package de.ozgcloud.bescheid; import java.time.LocalDate; import java.util.Optional; +import java.util.function.Consumer; import java.util.function.Predicate; import org.apache.commons.lang3.StringUtils; @@ -56,25 +57,27 @@ class BescheidEventListener { public static final String CREATE_BESCHEID_ORDER = "CREATE_BESCHEID"; public static final String DELETE_BESCHEID_ORDER = "DELETE_BESCHEID"; public static final String UPDATE_BESCHEID_ORDER = "UPDATE_BESCHEID"; + public static final String CREATE_BESCHEID_DOCUMENT_ORDER = "CREATE_BESCHEID_DOCUMENT"; - public static final Predicate<Command> IS_CREATE_BESCHEID_COMMAND = command -> command.getOrder().equals(CREATE_BESCHEID_ORDER); + public static final Predicate<Command> IS_CREATE_BESCHEID_COMMAND = command -> CREATE_BESCHEID_ORDER.equals(command.getOrder()); private static final String IS_CREATE_BESCHEID = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_CREATE_BESCHEID_COMMAND.test(event.getSource())}"; - public static final Predicate<Command> IS_DELETE_BESCHEID_COMMAND = command -> command.getOrder().equals(DELETE_BESCHEID_ORDER); + public static final Predicate<Command> IS_DELETE_BESCHEID_COMMAND = command -> DELETE_BESCHEID_ORDER.equals(command.getOrder()); private static final String IS_DELETE_BESCHEID = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_DELETE_BESCHEID_COMMAND.test(event.getSource())}"; - public static final Predicate<Command> IS_UPDATE_BESCHEID_COMMAND = command -> command.getOrder().equals(UPDATE_BESCHEID_ORDER); + public static final Predicate<Command> IS_UPDATE_BESCHEID_COMMAND = command -> UPDATE_BESCHEID_ORDER.equals(command.getOrder()); private static final String IS_UPDATE_BESCHEID = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_UPDATE_BESCHEID_COMMAND.test(event.getSource())}"; + public static final Predicate<Command> IS_CREATE_BESCHEID_DOCUMENT_COMMAND = command -> CREATE_BESCHEID_DOCUMENT_ORDER.equals(command.getOrder()); + private static final String IS_CREATE_BESCHEID_DOCUMENT = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_CREATE_BESCHEID_DOCUMENT_COMMAND.test(event.getSource())}"; + private static final String TEMPLATE_GROUP_KIEL = "Kiel"; static final String VORGANG_ID_BODYKEY = "vorgangId"; static final String BESCHEID_VOM_BODYKEY = "bescheidVom"; static final String GENEHMIGT_BODYKEY = "genehmigt"; private static final String LOG_MESSAGE_TEMPLATE = "{}. Command failed."; - private static final String CREATE_BESCHEID_ERROR_MESSAGE = "Error on executing Create Bescheid Command."; - private static final String DELETE_BESCHEID_ERROR_MESSAGE = "Error on executing Delete Bescheid Command."; - private static final String UPDATE_BESCHEID_ERROR_MESSAGE = "Error on executing Update Bescheid Command."; + private static final String ERROR_MESSAGE_TEMPLATE = "Error on executing %s Command."; private final BescheidService service; private final BinaryFileService fileService; @@ -90,29 +93,13 @@ class BescheidEventListener { @EventListener(condition = IS_CREATE_BESCHEID) public void onCreateBescheidCommand(CommandCreatedEvent event) { - Command command = event.getSource(); - - doCreateBescheid(command); - } - - private void doCreateBescheid(Command command) { - SecurityContext prevContext = null; - try { - prevContext = userService.startSecurityContext(command); - execute(command); - } catch (Exception e) { - LOG.error(LOG_MESSAGE_TEMPLATE, CREATE_BESCHEID_ERROR_MESSAGE, e); - eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(CREATE_BESCHEID_ERROR_MESSAGE, e))); - } finally { - userService.resetSecurityContext(prevContext); - } + runWithSecurityContext(event.getSource(), this::doCreateBescheid); } - void execute(Command command) { + void doCreateBescheid(Command command) { if (isKielEnvironment()) { - doCreateBescheidBiz(command).ifPresentOrElse( - documentId -> eventPublisher.publishEvent(new BescheidCreatedEvent(command, documentId)), - () -> eventPublisher.publishEvent(new BescheidCreatedEvent(command))); + doCreateBescheidBiz(command); + eventPublisher.publishEvent(new BescheidCreatedEvent(command)); return; } var createdItemId = attachedItemService.createBescheidDraft(command); @@ -124,15 +111,10 @@ class BescheidEventListener { return smartDocumentsProperties.filter(configuredForKiel).isPresent(); } - public Optional<String> doCreateBescheidBiz(@NonNull Command command) { + void doCreateBescheidBiz(@NonNull Command command) { var bescheid = service.createBescheid(createRequest(command)); bescheid = fileService.uploadBescheidFile(bescheid); - - if (featureProperties.isStoreAsDocument()) { - return Optional.of(documentService.createBescheidDocument(command, bescheid)); - } nachrichtService.createNachrichtDraft(bescheid); - return Optional.empty(); } BescheidRequest createRequest(Command command) { @@ -152,34 +134,46 @@ class BescheidEventListener { @EventListener(condition = IS_DELETE_BESCHEID) public void onDeleteBescheid(CommandCreatedEvent event) { - SecurityContext prevContext = null; - Command command = event.getSource(); - try { - prevContext = userService.startSecurityContext(command); - attachedItemService.deleteBescheidDraft(command); - eventPublisher.publishEvent(new BescheidDeletedEvent(command)); - } catch (Exception e) { - LOG.error(LOG_MESSAGE_TEMPLATE, DELETE_BESCHEID_ERROR_MESSAGE, e); - eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(DELETE_BESCHEID_ERROR_MESSAGE, e))); - } finally { - userService.resetSecurityContext(prevContext); - } + runWithSecurityContext(event.getSource(), this::doDeleteBescheid); } + + void doDeleteBescheid(Command command) { + attachedItemService.deleteBescheidDraft(command); + eventPublisher.publishEvent(new BescheidDeletedEvent(command)); + } + @EventListener(condition = IS_UPDATE_BESCHEID) public void onUpdateBescheidCommand(CommandCreatedEvent event) { - Command command = event.getSource(); + runWithSecurityContext(event.getSource(), this::doUpdateBescheid); + } + + void doUpdateBescheid(Command command) { + attachedItemService.updateBescheidDraft(command); + eventPublisher.publishEvent(new BescheidUpdatedEvent(command)); + } + + @EventListener(condition = IS_CREATE_BESCHEID_DOCUMENT) + public void onCreatedBescheidDocument(CommandCreatedEvent event) { + runWithSecurityContext(event.getSource(), this::doCreateBescheidDocument); + } + + void doCreateBescheidDocument(Command command) { + } + + void runWithSecurityContext(Command command, Consumer<Command> commandExecutor) { SecurityContext prevContext = null; try { prevContext = userService.startSecurityContext(command); - attachedItemService.updateBescheidDraft(command); - eventPublisher.publishEvent(new BescheidUpdatedEvent(command)); + commandExecutor.accept(command); } catch (Exception e) { - LOG.error(LOG_MESSAGE_TEMPLATE, UPDATE_BESCHEID_ERROR_MESSAGE, e); - eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(UPDATE_BESCHEID_ERROR_MESSAGE, e))); + var errorMessage = ERROR_MESSAGE_TEMPLATE.formatted(command.getOrder()); + LOG.error(LOG_MESSAGE_TEMPLATE, errorMessage, e); + eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(errorMessage, e))); } finally { userService.resetSecurityContext(prevContext); } } + private String buildErrorMessage(String message, Exception cause) { try { StringBuilder sb = new StringBuilder(message); @@ -191,7 +185,7 @@ class BescheidEventListener { return sb.toString(); } catch (Exception e2) { LOG.error("Error in building Error Message (sick).", e2); - return CREATE_BESCHEID_ERROR_MESSAGE; + return message; } } diff --git a/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java b/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java index c8f17b35e29a5acad61f731f0e91d5c254a11fe1..ec195d21cfcf7d40bb87ada893038ff50b7a6e60 100644 --- a/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java +++ b/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java @@ -8,8 +8,10 @@ import static org.mockito.Mockito.*; import java.util.Map; import java.util.Optional; +import java.util.function.Consumer; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -28,7 +30,6 @@ import de.ozgcloud.bescheid.common.callcontext.UserProfile; import de.ozgcloud.bescheid.common.callcontext.UserProfileTestFactory; import de.ozgcloud.bescheid.nachricht.NachrichtService; import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsProperties; -import de.ozgcloud.bescheid.vorgang.VorgangId; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEventTestFactory; import de.ozgcloud.command.CommandFailedEvent; @@ -62,91 +63,36 @@ class BescheidEventListenerTest { @Nested class TestOnCreateBescheidCommand { - private Command command = CommandTestFactory.createBuilder() - .bodyObject( - Map.of(VORGANG_ID_BODYKEY, VORGANG_ID.toString(), - BESCHEID_VOM_BODYKEY, BESCHEID_VOM_STRING, - GENEHMIGT_BODYKEY, GENEHMIGT)) - .build(); - - @Mock - private SecurityContext secContext; + private Command command = CommandTestFactory.create(); @Test - void shouldCreateBescheid() { - doReturn(true).when(listener).isKielEnvironment(); - + void shouldCallRunWithSecurityContext() { listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); - verify(listener).execute(command); + verify(listener).runWithSecurityContext(eq(command), any()); } @Test - void shouldPublishErrorEventOnException() { - doReturn(true).when(listener).isKielEnvironment(); - doThrow(new RuntimeException("ups")).when(listener).doCreateBescheidBiz(any()); - + void shouldExecuteCreateBescheid() { listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); - verify(eventPublisher).publishEvent(any(CommandFailedEvent.class)); - } - - @Nested - class HandleSecurityContext { - - @BeforeEach - void init() { - when(userService.startSecurityContext(any())).thenReturn(secContext); - } - - @Test - void shouldStartSecurityContext() { - listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); - - verify(userService).startSecurityContext(command); - } - - @Test - void shouldResetSecurityContext() { - listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); - - verify(userService).resetSecurityContext(secContext); - } - - @Test - void shouldResetSecurityContextAfterException() { - doReturn(true).when(listener).isKielEnvironment(); - doThrow(new RuntimeException("ups")).when(listener).doCreateBescheidBiz(any()); - - listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); - - verify(userService).resetSecurityContext(secContext); - } - } - - @Nested - class CreateBescheidRequest { - @Test - void shouldContainUserProfile() { - UserProfile user = UserProfileTestFactory.create(); - when(userService.getUserProfile()).thenReturn(user); - - var request = listener.createRequest(command); - - assertThat(request.getCreateFor()).isSameAs(user); - } + verify(listener).doCreateBescheid(command); } - } @Nested - class TestExecute { + class TestDoCreateBescheid { + + private static final Command COMMAND = CommandTestFactory.createBuilder() + .bodyObject( + Map.of(VORGANG_ID_BODYKEY, VORGANG_ID.toString(), + BESCHEID_VOM_BODYKEY, BESCHEID_VOM_STRING, + GENEHMIGT_BODYKEY, GENEHMIGT)) + .build(); @Captor private ArgumentCaptor<BescheidCreatedEvent> eventCaptor; - private Command command = CommandTestFactory.create(); - @Nested class TestKielConfigured { @@ -157,35 +103,18 @@ class BescheidEventListenerTest { @Test void shouldCallDoCreateBescheidBiz() { - listener.execute(command); - - verify(listener).doCreateBescheidBiz(command); - } - - @Test - void shouldNotCallVorgangAttachedItemService() { - listener.execute(command); + listener.doCreateBescheid(COMMAND); + verify(listener).doCreateBescheidBiz(COMMAND); verify(attachedItemService, never()).createBescheidDraft(any()); } @Test - void shouldCallPublishEvent() { - listener.execute(command); - - verify(eventPublisher).publishEvent(eventCaptor.capture()); - assertThat(eventCaptor.getValue().getCreatedResource()).isNull(); - } - - @Test - void shouldPublishEventWithDocumentId() { - var documentId = "documentId"; - doReturn(Optional.of(documentId)).when(listener).doCreateBescheidBiz(any()); - - listener.execute(command); + void shouldPublishBescheidCreatedEvent() { + listener.doCreateBescheid(COMMAND); verify(eventPublisher).publishEvent(eventCaptor.capture()); - assertThat(eventCaptor.getValue().getCreatedResource()).isEqualTo(documentId); + assertThat(eventCaptor.getValue().getSource()).isEqualTo(CommandTestFactory.ID); } } @@ -198,305 +127,352 @@ class BescheidEventListenerTest { } @Test - void shouldNotCallDoCreateBescheidBiz() { - listener.execute(command); + void shouldCallCreateBescheidDraft() { + listener.doCreateBescheid(COMMAND); + verify(attachedItemService).createBescheidDraft(COMMAND); verify(listener, never()).doCreateBescheidBiz(any()); } @Test - void shouldCallVorgangAttachedItemService() { - listener.execute(command); + @DisplayName("should publish BescheidCreatedEvent after creating BescheidDraft") + void shouldPublishBescheidCreatedEventWithCommand() { + var createdResource = "item-id"; + when(attachedItemService.createBescheidDraft(any())).thenReturn(createdResource); - verify(attachedItemService).createBescheidDraft(command); + listener.doCreateBescheid(COMMAND); + + verify(eventPublisher).publishEvent(eventCaptor.capture()); + assertThat(eventCaptor.getValue().getCommand()).isSameAs(COMMAND); } @Test - void shouldCallPublishEvent() { - var expectedItemId = "createdItemId"; - when(attachedItemService.createBescheidDraft(any())).thenReturn(expectedItemId); + @DisplayName("should publish BescheidCreatedEvent with created resource") + void shouldPublishBescheidCreatedEventWithCreatedResource() { + var createdResource = "item-id"; + when(attachedItemService.createBescheidDraft(any())).thenReturn(createdResource); - listener.execute(command); + listener.doCreateBescheid(COMMAND); verify(eventPublisher).publishEvent(eventCaptor.capture()); - assertThat(eventCaptor.getValue().getCreatedResource()).isEqualTo(expectedItemId); + assertThat(eventCaptor.getValue().getCreatedResource()).isEqualTo(createdResource); } } } @Nested - class CreateBescheid { + class TestIsKielEnvironment { - private Command command = CommandTestFactory.createBuilder() - .bodyObject( - Map.of(VORGANG_ID_BODYKEY, VORGANG_ID.toString(), - BESCHEID_VOM_BODYKEY, BESCHEID_VOM_STRING, - GENEHMIGT_BODYKEY, GENEHMIGT)) - .build(); + @Mock + private SmartDocumentsProperties smartDocumentsProperties; - @Captor - private ArgumentCaptor<BescheidRequest> requestCaptor; + @Test + void shouldReturnTrueIfKiel() { + when(smartDocumentsProperties.getTemplateGroup()).thenReturn("Kiel"); + ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.of(smartDocumentsProperties)); - private Bescheid bescheid = BescheidTestFactory.create(); - private Bescheid bescheidWithFileId = BescheidTestFactory.create(); + var result = listener.isKielEnvironment(); - @BeforeEach - void init() { - when(service.createBescheid(any())).thenReturn(bescheid); - when(fileService.uploadBescheidFile(any())).thenReturn(bescheidWithFileId); - when(userService.getUserProfile()).thenReturn(UserProfileTestFactory.create()); + assertThat(result).isTrue(); } @Test - void shouldCreateBescheid() { - listener.doCreateBescheidBiz(command); + void shouldReturnFalseIfNotConfigured() { + ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.empty()); + + var result = listener.isKielEnvironment(); - verify(service).createBescheid(requestCaptor.capture()); - assertThat(requestCaptor.getValue()).usingRecursiveComparison() - .isEqualTo(BescheidRequestTestFactory.createBuilder() - .vorgangId(VorgangId.from(CommandTestFactory.VORGANG_ID)) - .build()); + assertThat(result).isFalse(); } @Test - void shouldSaveFile() { - listener.doCreateBescheidBiz(command); + void shouldReturnFalseIfNotKiel() { + when(smartDocumentsProperties.getTemplateGroup()).thenReturn("NotKiel"); + ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.of(smartDocumentsProperties)); - verify(fileService).uploadBescheidFile(bescheid); - } + var result = listener.isKielEnvironment(); - @Nested - class TestStoreAsDocumentDisabled { + assertThat(result).isFalse(); + } + } - @Test - void shouldSaveNachricht() { - listener.doCreateBescheidBiz(command); + @Nested + class TestCreateBescheidBiz { - verify(nachrichtService).createNachrichtDraft(bescheidWithFileId); - } + private Command command = CommandTestFactory.create(); - @Test - void shouldNotCallDocumentService() { - listener.doCreateBescheidBiz(command); + @Test + void shouldCallCreateRequest() { + listener.doCreateBescheidBiz(command); - verifyNoInteractions(documentService); - } + verify(listener).createRequest(command); } - @Nested - class TestStoreAsDocumentEnabled { + @Test + void shouldCallBescheidService() { + var bescheidRequest = BescheidRequestTestFactory.create(); + doReturn(bescheidRequest).when(listener).createRequest(any()); - private static final String DOCUMENT_ID = "documentId"; + listener.doCreateBescheidBiz(command); - @BeforeEach - void init() { - when(featureProperties.isStoreAsDocument()).thenReturn(true); - when(documentService.createBescheidDocument(any(), any(Bescheid.class))).thenReturn(DOCUMENT_ID); - } + verify(service).createBescheid(bescheidRequest); + } - @Test - void shouldNotCallNachrichtService() { - listener.doCreateBescheidBiz(command); + @Test + void shouldCallFileService() { + var bescheid = BescheidTestFactory.create(); + when(service.createBescheid(any())).thenReturn(bescheid); - verifyNoInteractions(nachrichtService); - } + listener.doCreateBescheidBiz(command); - @Test - void shouldCallDocumentService() { - listener.doCreateBescheidBiz(command); + verify(fileService).uploadBescheidFile(bescheid); + } - verify(documentService).createBescheidDocument(command, bescheidWithFileId); - } + @Test + void shouldCallNachrichtService() { + var bescheid = BescheidTestFactory.create(); + when(fileService.uploadBescheidFile(any())).thenReturn(bescheid); - @Test - void shouldReturnDocumentId() { - var result = listener.doCreateBescheidBiz(command); + listener.doCreateBescheidBiz(command); - assertThat(result).contains(DOCUMENT_ID); - } + verify(nachrichtService).createNachrichtDraft(bescheid); } } @Nested - class TestIsKielEnvironment { + class TestCreateBescheidRequest { - @Mock - private SmartDocumentsProperties smartDocumentsProperties; + private static final Command COMMAND = CommandTestFactory.createBuilder() + .vorgangId(VORGANG_ID.toString()) + .bodyObject( + Map.of(BESCHEID_VOM_BODYKEY, BESCHEID_VOM_STRING, + GENEHMIGT_BODYKEY, false)) + .build(); @Test - void shouldReturnTrueIfKiel() { - when(smartDocumentsProperties.getTemplateGroup()).thenReturn("Kiel"); - ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.of(smartDocumentsProperties)); + void shouldSetVorgangId() { + var request = createBescheidRequest(); - var result = listener.isKielEnvironment(); + assertThat(request.getVorgangId()).isEqualTo(VORGANG_ID); + } - assertThat(result).isTrue(); + @Test + void shouldSetBescheidVom() { + var request = createBescheidRequest(); + + assertThat(request.getBescheidVom()).isEqualTo(BESCHEID_VOM); } @Test - void shouldReturnFalseIfNotConfigured() { - ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.empty()); + void shouldSetGenehmigt() { + var request = createBescheidRequest(); - var result = listener.isKielEnvironment(); + assertThat(request.isGenehmigt()).isFalse(); + } - assertThat(result).isFalse(); + @Test + void shouldSetDefaultGenehmigt() { + Command command = CommandTestFactory.createBuilder() + .vorgangId(VORGANG_ID.toString()) + .bodyObject( + Map.of(BESCHEID_VOM_BODYKEY, BESCHEID_VOM_STRING)) + .build(); + + var request = listener.createRequest(command); + + assertThat(request.isGenehmigt()).isTrue(); } @Test - void shouldReturnFalseIfNotKiel() { - when(smartDocumentsProperties.getTemplateGroup()).thenReturn("NotKiel"); - ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.of(smartDocumentsProperties)); + void shouldContainUserProfile() { + UserProfile user = UserProfileTestFactory.create(); + when(userService.getUserProfile()).thenReturn(user); - var result = listener.isKielEnvironment(); + var request = listener.createRequest(COMMAND); - assertThat(result).isFalse(); + assertThat(request.getCreateFor()).isSameAs(user); + } + + private BescheidRequest createBescheidRequest() { + return listener.createRequest(COMMAND); } } @Nested class TestOnDeleteBescheid { - @Captor - private ArgumentCaptor<BescheidDeletedEvent> bescheidDeletedEventCaptor; - @Captor - private ArgumentCaptor<CommandFailedEvent> commandFailedEventCaptor; - private Command command = CommandTestFactory.create(); @Test - void shouldCallAttachedItemService() { + void shouldCallRunWithSecurityContext() { listener.onDeleteBescheid(CommandCreatedEventTestFactory.withCommand(command)); - verify(attachedItemService).deleteBescheidDraft(command); + verify(listener).runWithSecurityContext(eq(command), any()); } @Test - void shouldPublishCommandExecutedEvent() { + void shouldExecuteCreateBescheid() { listener.onDeleteBescheid(CommandCreatedEventTestFactory.withCommand(command)); - verify(eventPublisher).publishEvent(bescheidDeletedEventCaptor.capture()); - assertThat(bescheidDeletedEventCaptor.getValue().getCommand()).isSameAs(command); + verify(listener).doDeleteBescheid(command); } + } - @Test - void shouldPublishErrorEventOnException() { - doThrow(new RuntimeException("ups")).when(attachedItemService).deleteBescheidDraft(any()); + @Nested + class TestDoDeleteBescheid { - listener.onDeleteBescheid(CommandCreatedEventTestFactory.withCommand(command)); + @Captor + private ArgumentCaptor<BescheidDeletedEvent> bescheidDeletedEventCaptor; - verify(eventPublisher).publishEvent(commandFailedEventCaptor.capture()); - assertThat(commandFailedEventCaptor.getValue().getSource()).isEqualTo(command.getId()); - assertThat(commandFailedEventCaptor.getValue().getErrorMessage()).isNotEmpty(); - } + private Command command = CommandTestFactory.create(); - @Nested - class HandleSecurityContext { + @Test + void shouldCallAttachedItemService() { + listener.doDeleteBescheid(command); - @Mock - private SecurityContext secContext; + verify(attachedItemService).deleteBescheidDraft(command); + } - @BeforeEach - void init() { - when(userService.startSecurityContext(any())).thenReturn(secContext); - } + @Test + void shouldPublishCommandExecutedEvent() { + listener.doDeleteBescheid(command); - @Test - void shouldStartSecurityContext() { - listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + verify(eventPublisher).publishEvent(bescheidDeletedEventCaptor.capture()); + assertThat(bescheidDeletedEventCaptor.getValue().getCommand()).isSameAs(command); + } - verify(userService).startSecurityContext(command); - } + } - @Test - void shouldResetSecurityContext() { - listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + @Nested + class TestOnUpdateBescheid { - verify(userService).resetSecurityContext(secContext); - } + private Command command = CommandTestFactory.create(); - @Test - void shouldResetSecurityContextAfterException() { - doReturn(true).when(listener).isKielEnvironment(); - doThrow(new RuntimeException("ups")).when(listener).doCreateBescheidBiz(any()); + @Test + void shouldCallRunWithSecurityContext() { + listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); - listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + verify(listener).runWithSecurityContext(eq(command), any()); + } - verify(userService).resetSecurityContext(secContext); - } + @Test + void shouldExecuteCreateBescheid() { + listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + + verify(listener).doUpdateBescheid(command); } + } @Nested - class TestOnUpdateBescheid { + class TestDoUpdateBescheid { @Captor private ArgumentCaptor<BescheidUpdatedEvent> bescheidUpdatedEventCaptor; - @Captor - private ArgumentCaptor<CommandFailedEvent> updateFailedEventCaptor; private Command command = CommandTestFactory.createBuilder().build(); @Test void shouldCallUpdateBescheidDraft() { - listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + listener.doUpdateBescheid(command); verify(attachedItemService).updateBescheidDraft(command); } @Test void shouldPublishBescheidUpdatedEvent() { - listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + listener.doUpdateBescheid(command); verify(eventPublisher).publishEvent(bescheidUpdatedEventCaptor.capture()); assertThat(bescheidUpdatedEventCaptor.getValue().getCommand()).isSameAs(command); } + } + + @Nested + class TestOnCreateBescheidDocument { + + private Command command = CommandTestFactory.create(); + @Test - void shouldPublishCommandFailedEvent() { - doThrow(new RuntimeException("ups")).when(attachedItemService).updateBescheidDraft(any()); + void shouldCallRunWithSecurityContext() { + listener.onCreatedBescheidDocument(CommandCreatedEventTestFactory.withCommand(command)); - listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + verify(listener).runWithSecurityContext(eq(command), any()); + } + + @Test + void shouldExecuteCreateBescheidDocument() { + listener.onCreatedBescheidDocument(CommandCreatedEventTestFactory.withCommand(command)); - verify(eventPublisher).publishEvent(updateFailedEventCaptor.capture()); - assertThat(updateFailedEventCaptor.getValue().getSource()).isEqualTo(command.getId()); + verify(listener).doCreateBescheidDocument(command); } + } - @Nested - class HandleSecurityContext { + @Nested + class TestDoCreateBescheidDocument { - @Mock - private SecurityContext secContext; + private Command command = CommandTestFactory.create(); - @BeforeEach - void init() { - when(userService.startSecurityContext(any())).thenReturn(secContext); - } + } - @Test - void shouldStartSecurityContext() { - listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + @Nested + class TestRunWithSecurityContext { - verify(userService).startSecurityContext(command); - } + private Command command = CommandTestFactory.createBuilder().build(); - @Test - void shouldResetSecurityContext() { - listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + @Mock + private Consumer<Command> commandExecutor; + @Mock + private SecurityContext secContext; + @Captor + private ArgumentCaptor<CommandFailedEvent> commandFailedEventCaptor; - verify(userService).resetSecurityContext(secContext); - } + @BeforeEach + void init() { + when(userService.startSecurityContext(any())).thenReturn(secContext); + } - @Test - void shouldResetSecurityContextAfterException() { - doThrow(new RuntimeException("ups")).when(attachedItemService).updateBescheidDraft(any()); + @Test + void shouldStartSecurityContext() { + listener.runWithSecurityContext(command, commandExecutor); - listener.onUpdateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + verify(userService).startSecurityContext(command); + } - verify(userService).resetSecurityContext(secContext); - } + @Test + void shouldExecuteCommand() { + listener.runWithSecurityContext(command, commandExecutor); + + verify(commandExecutor).accept(command); } + @Test + void shouldResetSecurityContext() { + listener.runWithSecurityContext(command, commandExecutor); + + verify(userService).resetSecurityContext(secContext); + } + + @Test + void shouldResetSecurityContextAfterException() { + doThrow(new RuntimeException("ups")).when(commandExecutor).accept(any()); + + listener.runWithSecurityContext(command, commandExecutor); + + verify(userService).resetSecurityContext(secContext); + } + @Test + void shouldPublishCommandFailedEvent() { + doThrow(new RuntimeException("ups")).when(commandExecutor).accept(any()); + + listener.runWithSecurityContext(command, commandExecutor); + + verify(eventPublisher).publishEvent(commandFailedEventCaptor.capture()); + assertThat(commandFailedEventCaptor.getValue().getSource()).isEqualTo(command.getId()); + assertThat(commandFailedEventCaptor.getValue().getErrorMessage()).isNotEmpty(); + } } }