From d2070dfbbdf9930f7443a43b920697828a33b819 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Mon, 26 Jun 2023 12:40:50 +0200 Subject: [PATCH] prj-42 fix/extend test --- .../bescheid/BescheidEventListener.java | 9 ++++- .../bescheid/BescheidEventListenerTest.java | 39 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) 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 9d59d16c1..4e6d574a0 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 a1278a661..fb958be97 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 { -- GitLab