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 9d59d16c1f7a24cef18840906b5d9e43c736b5d3..4e6d574a0b9e3375f834a62eb155112ceeb5db31 100644 --- a/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java +++ b/bescheid-manager/src/main/java/de/ozgcloud/bescheid/BescheidEventListener.java @@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.EventListener; +import org.springframework.security.core.context.SecurityContext; import org.springframework.stereotype.Component; import de.itvsh.kop.common.errorhandling.TechnicalException; @@ -53,13 +54,17 @@ class BescheidEventListener { @EventListener(condition = IS_CREATE_BESCHEID) public void onCreateBescheidCommand(CommandCreatedEvent event) { Command command = event.getSource(); + SecurityContext prevContext = null; try { - var prevContext = userService.startSecurityContext(command); + prevContext = userService.startSecurityContext(command); doCreateBescheidBiz(command); - userService.resetSecurityContext(prevContext); } catch (Exception e) { LOG.error("Error on executing Create Bescheid Command. Command failed.", e); eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(e))); + } finally { + if (prevContext != null) { + userService.resetSecurityContext(prevContext); + } } } 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 a1278a661c7d18476232fb5fa5018afabc115149..fb958be97aa92627dc24fca69aacdfd6153e0882 100644 --- a/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java +++ b/bescheid-manager/src/test/java/de/ozgcloud/bescheid/BescheidEventListenerTest.java @@ -17,12 +17,14 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.security.core.context.SecurityContext; import de.itvsh.ozg.pluto.command.Command; import de.itvsh.ozg.pluto.command.CommandCreatedEventTestFactory; import de.itvsh.ozg.pluto.command.CommandFailedEvent; import de.itvsh.ozg.pluto.command.CommandTestFactory; import de.ozgcloud.bescheid.binaryfile.BinaryFileService; +import de.ozgcloud.bescheid.common.callcontext.CurrentUserService; import de.ozgcloud.bescheid.nachricht.NachrichtService; class BescheidEventListenerTest { @@ -40,6 +42,8 @@ class BescheidEventListenerTest { @Mock private ApplicationEventPublisher eventPublisher; + @Mock + private CurrentUserService userService; @Nested class TestOnCreateBescheidCommand { @@ -51,6 +55,9 @@ class BescheidEventListenerTest { GENEHMIGT_BODYKEY, GENEHMIGT)) .build(); + @Mock + private SecurityContext secContext; + @Test void shouldCreateBescheid() { listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); @@ -67,6 +74,38 @@ class BescheidEventListenerTest { 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() { + doThrow(new RuntimeException("ups")).when(listener).doCreateBescheidBiz(any()); + + listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); + + verify(userService).resetSecurityContext(secContext); + } + } + @Nested class CreateBescheid {