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

prj-42 fix/extend test

parent 4955da1e
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment