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 ce03da91e83577e2b9be664a9d8dbf9eb291ca48..11ef0a2385acfb734221903f6b1c51770ba7d0cf 100644 --- a/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java +++ b/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java @@ -46,6 +46,7 @@ import de.ozgcloud.bescheid.nachricht.NachrichtService; import de.ozgcloud.bescheid.vorgang.VorgangId; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEvent; +import de.ozgcloud.command.CommandExecutedEvent; import de.ozgcloud.command.CommandFailedEvent; import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.document.BescheidDocumentCreatedEvent; @@ -165,7 +166,7 @@ class BescheidEventListener { void doSendBescheid(Command command) { var bescheidItem = attachedItemService.getItem(command.getRelationId()); if (SendBy.MANUAL.hasValue(bescheidItem.getItem().get(Bescheid.FIELD_SEND_BY))) { - service.sendBescheidManually(bescheidItem, command.getRelationVersion()); + service.sendBescheidManually(bescheidItem, command); } else if (SendBy.NACHRICHT.hasValue(bescheidItem.getItem().get(Bescheid.FIELD_SEND_BY))) { service.sendBescheidPostfachMail(bescheidItem, command); } else { @@ -190,6 +191,15 @@ class BescheidEventListener { return builder.build(); } + @EventListener(condition = IS_SEND_BESCHEID) + public void onBescheidSent(CommandExecutedEvent event) { + runWithSecurityContext(event.getCommand(), this::setAntragBewilligung); + } + + void setAntragBewilligung(Command command) { + service.setAntragBewilligung(attachedItemService.getItem(command.getRelationId())); + } + void runWithSecurityContext(Command command, Consumer<Command> commandExecutor) { SecurityContext prevContext = null; try { diff --git a/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerITCase.java b/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerITCase.java index 96c5ef5dafcba70a2f79b18ae9d7aa8b24bf8670..6347a9570ea12e841e2c6231ed798394daf809d2 100644 --- a/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerITCase.java +++ b/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerITCase.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.context.ApplicationEventPublisher; import de.ozgcloud.apilib.common.command.OzgCloudCommandService; @@ -19,14 +20,19 @@ import de.ozgcloud.bescheid.common.user.UserProfile; import de.ozgcloud.bescheid.common.user.UserProfileService; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEventTestFactory; +import de.ozgcloud.command.CommandExecutedEvent; import de.ozgcloud.command.CommandTestFactory; import de.ozgcloud.common.test.ITCase; @ITCase class BescheidEventListenerITCase { + private static final Command COMMAND_WITH_OTHER_ORDER = CommandTestFactory.createBuilder().order("OTHER").build(); + @Autowired private ApplicationEventPublisher publisher; + @SpyBean + private BescheidEventListener listener; @Autowired private BescheidFeatureProperties bescheidFeatureProperties; @@ -70,7 +76,7 @@ class BescheidEventListenerITCase { @Test void shouldNotReactOnOtherOrder() { - publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(CommandTestFactory.createBuilder().order("OTHER").build())); + publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(COMMAND_WITH_OTHER_ORDER)); verifyNoInteractions(service); } @@ -90,7 +96,7 @@ class BescheidEventListenerITCase { @Test void shouldNotReactOnOtherOrder() { - publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(CommandTestFactory.createBuilder().order("OTHER").build())); + publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(COMMAND_WITH_OTHER_ORDER)); verifyNoInteractions(attachedItemService); } @@ -110,9 +116,29 @@ class BescheidEventListenerITCase { @Test void shouldNotReactOnOtherOrder() { - publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(CommandTestFactory.createBuilder().order("OTHER").build())); + publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(COMMAND_WITH_OTHER_ORDER)); verifyNoInteractions(attachedItemService); } } + + @Nested + class TestBescheidSent { + + private static final Command COMMAND = CommandTestFactory.createBuilder().order(BescheidEventListener.SEND_BESCHEID_ORDER).build(); + + @Test + void shouldCallMethod() { + publisher.publishEvent(new CommandExecutedEvent(COMMAND)); + + verify(listener).setAntragBewilligung(COMMAND); + } + + @Test + void shouldNotReactOnOtherOrder() { + publisher.publishEvent(new CommandExecutedEvent(COMMAND_WITH_OTHER_ORDER)); + + verifyNoInteractions(listener); + } + } } 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 823d76cd45304b38173621fa521a6e3cbec6820e..1940fb2bf4294d0d48bfab82be1b5550d7986c00 100644 --- a/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java +++ b/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java @@ -35,6 +35,7 @@ import de.ozgcloud.bescheid.common.user.UserProfileTestFactory; import de.ozgcloud.bescheid.nachricht.NachrichtService; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandCreatedEventTestFactory; +import de.ozgcloud.command.CommandExecutedEvent; import de.ozgcloud.command.CommandFailedEvent; import de.ozgcloud.command.CommandTestFactory; import de.ozgcloud.common.errorhandling.TechnicalException; @@ -551,8 +552,8 @@ class BescheidEventListenerTest { void shouldCallSendBescheid() { listener.doSendBescheid(COMMAND); - verify(service).sendBescheidManually(bescheidItem, AttachedItemTestFactory.VERSION); - verify(service, never()).sendBescheidPostfachMail(any(), anyLong()); + verify(service).sendBescheidManually(bescheidItem, COMMAND); + verify(service, never()).sendBescheidPostfachMail(any(), any()); } @Test @@ -585,8 +586,8 @@ class BescheidEventListenerTest { void shouldCallBescheidService() { listener.doSendBescheid(COMMAND); - verify(service).sendBescheidPostfachMail(bescheidItem, AttachedItemTestFactory.VERSION); - verify(service, never()).sendBescheidManually(any(), anyLong()); + verify(service).sendBescheidPostfachMail(bescheidItem, COMMAND); + verify(service, never()).sendBescheidManually(any(), any()); } @Test @@ -599,6 +600,49 @@ class BescheidEventListenerTest { } } + @Nested + class TestOnBescheidSent { + + private static final Command COMMAND = CommandTestFactory.create(); + + @Test + void shouldCallRunWithSecurityContext() { + listener.onBescheidSent(new CommandExecutedEvent(COMMAND)); + + verify(listener).runWithSecurityContext(eq(COMMAND), any()); + } + + @Test + void shouldExecuteSetAntragBewilligung() { + listener.onBescheidSent(new CommandExecutedEvent(COMMAND)); + + verify(listener).setAntragBewilligung(COMMAND); + } + } + + @Nested + class TestSetAntragBewilligung { + + private static final Command COMMAND = CommandTestFactory.create(); + + @Test + void shouldCallGetItem() { + listener.setAntragBewilligung(COMMAND); + + verify(attachedItemService).getItem(CommandTestFactory.RELATION_ID); + } + + @Test + void shouldCallSetAntragBewilligung() { + var item = AttachedItemTestFactory.createBescheid(); + when(attachedItemService.getItem(any())).thenReturn(item); + + listener.setAntragBewilligung(COMMAND); + + verify(service).setAntragBewilligung(item); + } + } + @Nested class TestRunWithSecurityContext {