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

OZG-5093 call smart document only in Kiel

parent 1cdd14ab
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ import java.util.Optional;
import java.util.function.Predicate;
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;
......@@ -14,6 +15,7 @@ import org.springframework.stereotype.Component;
import de.ozgcloud.bescheid.binaryfile.BinaryFileService;
import de.ozgcloud.bescheid.common.callcontext.CurrentUserService;
import de.ozgcloud.bescheid.nachricht.NachrichtService;
import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsProperties;
import de.ozgcloud.bescheid.vorgang.VorgangId;
import de.ozgcloud.command.Command;
import de.ozgcloud.command.CommandCreatedEvent;
......@@ -34,6 +36,7 @@ class BescheidEventListener {
public static final Predicate<Command> IS_CREATE_BESCHEID_COMMAND = command -> command.getOrder().equals(ORDER);
private static final String TEMPLATE_GROUP_KIEL = "Kiel";
static final String VORGANG_ID_BODYKEY = "vorgangId";
static final String BESCHEID_VOM_BODYKEY = "bescheidVom";
static final String GENEHMIGT_BODYKEY = "genehmigt";
......@@ -46,6 +49,7 @@ class BescheidEventListener {
private final ApplicationEventPublisher eventPublisher;
private final CurrentUserService userService;
private final Optional<SmartDocumentsProperties> smartDocumentsProperties;
@EventListener(condition = IS_CREATE_BESCHEID)
public void onCreateBescheidCommand(CommandCreatedEvent event) {
......@@ -58,7 +62,11 @@ class BescheidEventListener {
SecurityContext prevContext = null;
try {
prevContext = userService.startSecurityContext(command);
if (isKielEnvironment()) {
doCreateBescheidBiz(command);
} else {
saveDraft(command);
}
eventPublisher.publishEvent(new BescheidCreatedEvent(command));
} catch (Exception e) {
LOG.error("Error on executing Create Bescheid Command. Command failed.", e);
......@@ -68,6 +76,15 @@ class BescheidEventListener {
}
}
private void saveDraft(Command command) {
}
boolean isKielEnvironment() {
Predicate<SmartDocumentsProperties> configuredForKiel = properties -> TEMPLATE_GROUP_KIEL.equals(properties.getTemplateGroup());
return smartDocumentsProperties.filter(configuredForKiel).isPresent();
}
public void doCreateBescheidBiz(@NonNull Command command) {
var bescheid = service.createBescheid(createRequest(command));
bescheid = fileService.uploadBescheidFile(bescheid);
......
......@@ -7,6 +7,7 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
......@@ -18,12 +19,14 @@ import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.test.util.ReflectionTestUtils;
import de.ozgcloud.bescheid.binaryfile.BinaryFileService;
import de.ozgcloud.bescheid.common.callcontext.CurrentUserService;
import de.ozgcloud.bescheid.common.callcontext.UserProfile;
import de.ozgcloud.bescheid.common.callcontext.UserProfileTestFactory;
import de.ozgcloud.bescheid.nachricht.NachrichtService;
import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsProperties;
import de.ozgcloud.bescheid.vorgang.VorgangId;
import de.ozgcloud.command.Command;
import de.ozgcloud.command.CommandCreatedEventTestFactory;
......@@ -63,6 +66,8 @@ class BescheidEventListenerTest {
@Test
void shouldCreateBescheid() {
doReturn(true).when(listener).isKielEnvironment();
listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command));
verify(listener).doCreateBescheidBiz(command);
......@@ -70,6 +75,7 @@ class BescheidEventListenerTest {
@Test
void shouldPublishErrorEventOnException() {
doReturn(true).when(listener).isKielEnvironment();
doThrow(new RuntimeException("ups")).when(listener).doCreateBescheidBiz(any());
listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command));
......@@ -101,6 +107,7 @@ class BescheidEventListenerTest {
@Test
void shouldResetSecurityContextAfterException() {
doReturn(true).when(listener).isKielEnvironment();
doThrow(new RuntimeException("ups")).when(listener).doCreateBescheidBiz(any());
listener.onCreateBescheidCommand(CommandCreatedEventTestFactory.withCommand(command));
......@@ -165,4 +172,39 @@ class BescheidEventListenerTest {
}
}
@Nested
class TestIsKielEnvironment {
@Mock
private SmartDocumentsProperties smartDocumentsProperties;
@Test
void shouldReturnTrueIfKiel() {
when(smartDocumentsProperties.getTemplateGroup()).thenReturn("Kiel");
ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.of(smartDocumentsProperties));
var result = listener.isKielEnvironment();
assertThat(result).isTrue();
}
@Test
void shouldReturnFalseIfNotConfigured() {
ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.empty());
var result = listener.isKielEnvironment();
assertThat(result).isFalse();
}
@Test
void shouldReturnFalseIfNotKiel() {
when(smartDocumentsProperties.getTemplateGroup()).thenReturn("NotKiel");
ReflectionTestUtils.setField(listener, "smartDocumentsProperties", Optional.of(smartDocumentsProperties));
var result = listener.isKielEnvironment();
assertThat(result).isFalse();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment