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

PRJ-42 implement bescheid event listener

parent 1847adbe
No related branches found
No related tags found
No related merge requests found
Showing
with 300 additions and 0 deletions
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.itvsh.kop.common</groupId>
<artifactId>kop-common-parent</artifactId>
<version>1.7.0</version>
<relativePath />
</parent>
<groupId>de.ozgcloud.bescheid</groupId>
<artifactId>bescheid-manager</artifactId>
<name>OZG-Cloud Bescheid Manager</name>
<version>1.0.0-SNAPSHOT</version>
<properties>
<pluto.version>1.12.0-SNAPSHOT</pluto.version>
</properties>
<dependencies>
<dependency>
<groupId>de.itvsh.ozg.pluto</groupId>
<artifactId>pluto-interface</artifactId>
</dependency>
<dependency>
<groupId>de.itvsh.ozg.pluto</groupId>
<artifactId>pluto-command</artifactId>
<version>${pluto.version}</version>
</dependency>
<dependency>
<groupId>de.itvsh.ozg.pluto</groupId>
<artifactId>pluto-command</artifactId>
<version>${pluto.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package de.ozgcloud.bescheid;
import java.time.LocalDate;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import de.itvsh.ozg.pluto.command.Command;
import de.itvsh.ozg.pluto.command.CommandCreatedEvent;
@Component
class BescheidEventListener {
public static final String ORDER = "CREATE_BESCHEID";
private static final String IS_CREATE_BESCHEID = "{T(de.ozgcloud.bescheid.BescheidEventListener).IS_CREATE_BESCHEID_COMMAND.test(event.getSource())}";
public static final Predicate<Command> IS_CREATE_BESCHEID_COMMAND = command -> command.getOrder().equals(ORDER);
static final String BESCHEID_VOM_BODYKEY = "bescheidVom";
static final String GENEHMIGT_BODYKEY = "genehmigt";
@Autowired
private BescheidService service;
@EventListener(condition = IS_CREATE_BESCHEID)
public void createBescheid(CommandCreatedEvent event) {
service.createBescheid(createRequest(event.getSource().getBodyObject()));
}
private BescheidRequest createRequest(Map<String, Object> eventBody) {
var builder = BescheidRequest.builder();
Optional.ofNullable(eventBody.get(BESCHEID_VOM_BODYKEY)).map(LocalDate.class::cast).ifPresent(builder::bescheidVom);
Optional.ofNullable(eventBody.get(GENEHMIGT_BODYKEY)).map(Boolean.class::cast).ifPresent(builder::genehmigt);
return builder.build();
}
}
package de.ozgcloud.bescheid;
import java.time.LocalDate;
import lombok.Builder;
import lombok.Getter;
@Builder
@Getter
public class BescheidRequest {
private LocalDate bescheidVom;
private boolean genehmigt;
}
package de.ozgcloud.bescheid;
import org.springframework.stereotype.Service;
@Service
class BescheidService {
public void createBescheid(BescheidRequest request) {
// TODO Auto-generated method stub
}
}
package de.ozgcloud.bescheid;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationEventPublisher;
import de.itvsh.kop.common.test.ITCase;
import de.itvsh.ozg.pluto.command.Command;
import de.itvsh.ozg.pluto.command.CommandCreatedEventTestFactory;
import de.itvsh.ozg.pluto.command.CommandTestFactory;
@ITCase
class BescheidEventListenerITCase {
@Autowired
private ApplicationEventPublisher publisher;
@MockBean
private BescheidService service;
@Nested
class TestCreateBesched {
private Command command = CommandTestFactory.createBuilder().order(BescheidEventListener.ORDER).build();
@Test
void shouldCallService() {
publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(command));
verify(service).createBescheid(any());
}
@Test
void shouldNotReactOnOtherOrder() {
publisher.publishEvent(CommandCreatedEventTestFactory.withCommand(CommandTestFactory.createBuilder().order("OTHER").build()));
verifyNoInteractions(service);
}
}
}
package de.ozgcloud.bescheid;
import static de.ozgcloud.bescheid.BescheidEventListener.*;
import static de.ozgcloud.bescheid.BescheidRequestTestFactory.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Map;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import de.itvsh.ozg.pluto.command.Command;
import de.itvsh.ozg.pluto.command.CommandCreatedEventTestFactory;
import de.itvsh.ozg.pluto.command.CommandTestFactory;
class BescheidEventListenerTest {
@InjectMocks
private BescheidEventListener listener;
@Mock
private BescheidService service;
@Nested
class TestCreateBescheid {
@Captor
private ArgumentCaptor<BescheidRequest> requestCaptor;
private Command command = CommandTestFactory.createBuilder()
.bodyObject(Map.of(BESCHEID_VOM_BODYKEY, BESCHEID_VOM, GENEHMIGT_BODYKEY, GENEHMIGT))
.build();
@Test
void shouldCallService() {
listener.createBescheid(CommandCreatedEventTestFactory.withCommand(command));
verify(service).createBescheid(requestCaptor.capture());
assertThat(requestCaptor.getValue()).usingRecursiveComparison().isEqualTo(BescheidRequestTestFactory.create());
}
}
}
package de.ozgcloud.bescheid;
import java.time.LocalDate;
public class BescheidRequestTestFactory {
static final String BESCHEID_VOM_STRING = "2023-01-04";
static final LocalDate BESCHEID_VOM = LocalDate.parse(BESCHEID_VOM_STRING);
static final boolean GENEHMIGT = true;
static BescheidRequest create() {
return createBuilder().build();
}
static BescheidRequest.BescheidRequestBuilder createBuilder() {
return BescheidRequest.builder()
.bescheidVom(BESCHEID_VOM)
.genehmigt(GENEHMIGT);
}
}
package de.ozgcloud.bescheid;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({ "de.ozgcloud.*", "de.itvsh.*" })
public class BescheidTestApplication {
}
org.mockito.junit.jupiter.MockitoExtension
\ No newline at end of file
junit.jupiter.extensions.autodetection.enabled = true
\ No newline at end of file
package de.itvsh.ozg.pluto.command;
public class CommandCreatedEventTestFactory {
public static CommandCreatedEvent create() {
return new CommandCreatedEvent(CommandTestFactory.create());
}
public static CommandCreatedEvent withCommand(Command command) {
return new CommandCreatedEvent(command);
}
}
package de.itvsh.ozg.pluto.command;
import java.util.Map;
public class CommandTestFactory {
public static final String ORDER = "DO_TEST";
public static Command create() {
return createBuilder().build();
}
public static TestCommand.TestCommandBuilder createBuilder() {
return TestCommand.builder()
.body(Map.of())
.bodyObject(Map.of());
}
}
package de.itvsh.ozg.pluto.command;
import java.time.ZonedDateTime;
import java.util.Map;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class TestCommand implements Command {
private String id;
private String vorgangId;
private String relationId;
private long relationVersion;
private String order;
private ZonedDateTime createdAt;
private ZonedDateTime finishedAt;
private String createdBy;
private String createdByName;
private CommandStatus status;
private Map<String, Object> bodyObject;
private Map<String, String> body;
private String errorMessage;
}
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
<module>pluto-utils</module> <module>pluto-utils</module>
<module>notification-manager</module> <module>notification-manager</module>
<module>pluto-command</module> <module>pluto-command</module>
<module>bescheid-manager</module>
</modules> </modules>
<build> <build>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment