Skip to content
Snippets Groups Projects
Commit d2070dfb authored by OZGCloud's avatar OZGCloud
Browse files

prj-42 fix/extend test

parent 4955da1e
Branches
Tags
No related merge requests found
...@@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils; ...@@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import de.itvsh.kop.common.errorhandling.TechnicalException; import de.itvsh.kop.common.errorhandling.TechnicalException;
...@@ -53,13 +54,17 @@ class BescheidEventListener { ...@@ -53,13 +54,17 @@ class BescheidEventListener {
@EventListener(condition = IS_CREATE_BESCHEID) @EventListener(condition = IS_CREATE_BESCHEID)
public void onCreateBescheidCommand(CommandCreatedEvent event) { public void onCreateBescheidCommand(CommandCreatedEvent event) {
Command command = event.getSource(); Command command = event.getSource();
SecurityContext prevContext = null;
try { try {
var prevContext = userService.startSecurityContext(command); prevContext = userService.startSecurityContext(command);
doCreateBescheidBiz(command); doCreateBescheidBiz(command);
userService.resetSecurityContext(prevContext);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error on executing Create Bescheid Command. Command failed.", e); LOG.error("Error on executing Create Bescheid Command. Command failed.", e);
eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(e))); eventPublisher.publishEvent(new CommandFailedEvent(command.getId(), buildErrorMessage(e)));
} finally {
if (prevContext != null) {
userService.resetSecurityContext(prevContext);
}
} }
} }
......
...@@ -17,12 +17,14 @@ import org.mockito.InjectMocks; ...@@ -17,12 +17,14 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.context.ApplicationEventPublisher; 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.Command;
import de.itvsh.ozg.pluto.command.CommandCreatedEventTestFactory; import de.itvsh.ozg.pluto.command.CommandCreatedEventTestFactory;
import de.itvsh.ozg.pluto.command.CommandFailedEvent; import de.itvsh.ozg.pluto.command.CommandFailedEvent;
import de.itvsh.ozg.pluto.command.CommandTestFactory; import de.itvsh.ozg.pluto.command.CommandTestFactory;
import de.ozgcloud.bescheid.binaryfile.BinaryFileService; import de.ozgcloud.bescheid.binaryfile.BinaryFileService;
import de.ozgcloud.bescheid.common.callcontext.CurrentUserService;
import de.ozgcloud.bescheid.nachricht.NachrichtService; import de.ozgcloud.bescheid.nachricht.NachrichtService;
class BescheidEventListenerTest { class BescheidEventListenerTest {
...@@ -40,6 +42,8 @@ class BescheidEventListenerTest { ...@@ -40,6 +42,8 @@ class BescheidEventListenerTest {
@Mock @Mock
private ApplicationEventPublisher eventPublisher; private ApplicationEventPublisher eventPublisher;
@Mock
private CurrentUserService userService;
@Nested @Nested
class TestOnCreateBescheidCommand { class TestOnCreateBescheidCommand {
...@@ -51,6 +55,9 @@ class BescheidEventListenerTest { ...@@ -51,6 +55,9 @@ class BescheidEventListenerTest {
GENEHMIGT_BODYKEY, GENEHMIGT)) GENEHMIGT_BODYKEY, GENEHMIGT))
.build(); .build();
@Mock
private SecurityContext secContext;
@Test @Test
void shouldCreateBescheid() { void shouldCreateBescheid() {
listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command)); listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command));
...@@ -67,6 +74,38 @@ class BescheidEventListenerTest { ...@@ -67,6 +74,38 @@ class BescheidEventListenerTest {
verify(eventPublisher).publishEvent(any(CommandFailedEvent.class)); 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 @Nested
class CreateBescheid { class CreateBescheid {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment