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

OZG-4606 add user data to bescheid request

parent 4c553c71
No related branches found
No related tags found
No related merge requests found
Showing
with 161 additions and 32 deletions
......@@ -16,9 +16,11 @@
<properties>
<vorgang-manager.version>2.0.0-SNAPSHOT</vorgang-manager.version>
<api-lib.version>0.4.0-SNAPSHOT</api-lib.version>
</properties>
<dependencies>
<!-- ozg-cloud -->
<dependency>
<groupId>de.ozgcloud.vorgang</groupId>
<artifactId>vorgang-manager-interface</artifactId>
......@@ -39,36 +41,47 @@
<groupId>de.itvsh.kop.common</groupId>
<artifactId>kop-common-lib</artifactId>
</dependency>
<dependency>
<groupId>de.ozgcloud.api-lib</groupId>
<artifactId>api-lib-core</artifactId>
<version>${api-lib.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--dev tools-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--TEST-->
<dependency>
......
......@@ -6,7 +6,6 @@ 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;
......@@ -21,10 +20,12 @@ import de.ozgcloud.command.Command;
import de.ozgcloud.command.CommandCreatedEvent;
import de.ozgcloud.command.CommandFailedEvent;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Component
@RequiredArgsConstructor
class BescheidEventListener {
public static final String ORDER = "CREATE_BESCHEID";
......@@ -39,17 +40,12 @@ class BescheidEventListener {
private static final String ERROR_MESSAGE = "Error on executing Create Bescheid Command.";
@Autowired
private BescheidService service;
@Autowired
private BinaryFileService fileService;
@Autowired
private NachrichtService nachrichtService;
private final BescheidService service;
private final BinaryFileService fileService;
private final NachrichtService nachrichtService;
@Autowired
private ApplicationEventPublisher eventPublisher;
@Autowired
private CurrentUserService userService;
private final ApplicationEventPublisher eventPublisher;
private final CurrentUserService userService;
@EventListener(condition = IS_CREATE_BESCHEID)
public void onCreateBescheidCommand(CommandCreatedEvent event) {
......@@ -79,7 +75,7 @@ class BescheidEventListener {
nachrichtService.createNachrichtDraft(bescheid);
}
private BescheidRequest createRequest(Command command) {
BescheidRequest createRequest(Command command) {
var eventBody = command.getBodyObject();
var builder = BescheidRequest.builder();
......@@ -89,7 +85,7 @@ class BescheidEventListener {
Optional.ofNullable(eventBody.get(GENEHMIGT_BODYKEY))
.map(Object::toString).map(Boolean::valueOf)
.ifPresentOrElse(builder::genehmigt, () -> builder.genehmigt(true));
builder.createFor(UserId.from(command.getCreatedBy()));
builder.createFor(userService.getUserProfile());
return builder.build();
}
......
......@@ -2,6 +2,7 @@ package de.ozgcloud.bescheid;
import java.time.LocalDate;
import de.ozgcloud.bescheid.common.callcontext.UserProfile;
import de.ozgcloud.bescheid.vorgang.VorgangId;
import lombok.Builder;
import lombok.Getter;
......@@ -15,5 +16,5 @@ public class BescheidRequest {
private LocalDate bescheidVom;
private boolean genehmigt;
private UserId createFor;
private UserProfile createFor;
}
......@@ -28,7 +28,6 @@ import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
......@@ -36,18 +35,30 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import de.itvsh.kop.common.errorhandling.TechnicalException;
import de.ozgcloud.apilib.user.OzgCloudUserId;
import de.ozgcloud.apilib.user.OzgCloudUserProfileService;
import de.ozgcloud.command.Command;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Service("bescheid_currentUserService")
public class CurrentUserService {
@Autowired
private AuthenticationTrustResolver trustResolver;
private final AuthenticationTrustResolver trustResolver;
private final OzgCloudUserProfileService userProfileService;
private final UserProfileMapper mapper;
public CallContextUser getUser() {
return findUser().orElseThrow(() -> new IllegalStateException("No authenticated User found"));
}
public UserProfile getUserProfile() {
return getUser().getUserId().map(OzgCloudUserId::from)
.map(userProfileService::getById)
.map(mapper::mapProfile).orElseThrow(() -> new TechnicalException("Unkown UserId or cannot load user profile."));
}
public Optional<CallContextUser> findUser() {
return findTrustedAuthentication()
.map(this::mapToCallContextUser)
......
package de.ozgcloud.bescheid.common.callcontext;
import de.ozgcloud.bescheid.UserId;
import lombok.Builder;
import lombok.Getter;
@Builder
@Getter
public class UserProfile {
private UserId id;
private String firstName;
private String lastName;
private String email;
}
package de.ozgcloud.bescheid.common.callcontext;
import org.mapstruct.Mapper;
import de.ozgcloud.apilib.user.OzgCloudUserId;
import de.ozgcloud.apilib.user.OzgCloudUserProfile;
import de.ozgcloud.bescheid.UserId;
@Mapper
public interface UserProfileMapper {
UserProfile mapProfile(OzgCloudUserProfile userProfile);
default UserId mapUserId(OzgCloudUserId id) {
return UserId.from(id.toString());
}
}
package de.ozgcloud.bescheid.common.callcontext;
import java.util.UUID;
import com.thedeanda.lorem.LoremIpsum;
import de.ozgcloud.bescheid.UserId;
public class UserProfileTestFactory {
private static final LoremIpsum LOREM_IPSUM = LoremIpsum.getInstance();
public static final UserId ID = UserId.from(UUID.randomUUID().toString());
public static final String FIRST_NAME = LOREM_IPSUM.getFirstName();
public static final String LAST_NAME = LOREM_IPSUM.getLastName();
public static final String EMAIL = "test-email@local";
public static UserProfile create() {
return createBuilder().build();
}
public static UserProfile.UserProfileBuilder createBuilder() {
return UserProfile.builder()
.id(ID)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.email(EMAIL);
}
}
......@@ -18,8 +18,10 @@ import de.itvsh.kop.common.binaryfile.FileDataDeserializer;
import de.ozgcloud.bescheid.Bescheid;
import de.ozgcloud.bescheid.BescheidRemoteService;
import de.ozgcloud.bescheid.BescheidRequest;
import de.ozgcloud.bescheid.common.callcontext.UserProfile;
import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsRequest.CustomerData;
import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsRequest.CustomerData.BescheidData;
import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsRequest.CustomerData.UserData;
import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsRequest.SmartDocument.Selection;
import de.ozgcloud.bescheid.vorgang.Vorgang;
import lombok.AllArgsConstructor;
......@@ -62,7 +64,7 @@ class SmartDocumentsBescheidRemoteService implements BescheidRemoteService {
.size(smartDocumentsFile.getDocument().getData().length())
.contentType(MediaType.APPLICATION_PDF_VALUE)
.genehmigt(request.isGenehmigt())
.createdBy(request.getCreateFor())
.createdBy(request.getCreateFor().getId())
.vorgangId(request.getVorgangId())
.build();
}
......@@ -73,18 +75,27 @@ class SmartDocumentsBescheidRemoteService implements BescheidRemoteService {
.findAny().orElseThrow(() -> new IllegalStateException("No PDF File in SmartDocuments Response found."));
}
private SmartDocumentsRequest createRequest(BescheidRequest request, Vorgang vorgang) {
SmartDocumentsRequest createRequest(BescheidRequest request, Vorgang vorgang) {
return logRequest(SmartDocumentsRequest.builder()
.smartDocument(buildSDSection(vorgang))
.customerData(
CustomerData.builder()
.vorgang(vorgang)
.userData(buildUserData(request.getCreateFor()))
.bescheid(BescheidData.builder().bescheidVom(request.getBescheidVom()).genehmigt(request.isGenehmigt()).build())
.build())
.build());
}
private UserData buildUserData(UserProfile userProfile) {
return UserData.builder()
.firstName(userProfile.getFirstName())
.lastName(userProfile.getLastName())
.email(userProfile.getEmail())
.build();
}
private SmartDocumentsRequest.SmartDocument buildSDSection(Vorgang vorgang) {
return SmartDocumentsRequest.SmartDocument.builder().selection(Selection.builder()
.templateGroup(properties.getTemplateGroup())
......
......@@ -41,6 +41,7 @@ class SmartDocumentsRequest {
static class UserData {
private String firstName;
private String lastName;
private String email;
}
}
......
......@@ -21,6 +21,8 @@ import org.springframework.security.core.context.SecurityContext;
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.vorgang.VorgangId;
import de.ozgcloud.command.Command;
......@@ -107,6 +109,19 @@ class BescheidEventListenerTest {
}
}
@Nested
class CreateBescheidRequest {
@Test
void shouldContainUserProfile() {
UserProfile user = UserProfileTestFactory.create();
when(userService.getUserProfile()).thenReturn(user);
var request = listener.createRequest(command);
assertThat(request.getCreateFor()).isSameAs(user);
}
}
@Nested
class CreateBescheid {
......@@ -120,6 +135,7 @@ class BescheidEventListenerTest {
void init() {
when(service.createBescheid(any())).thenReturn(bescheid);
when(fileService.uploadBescheidFile(any())).thenReturn(bescheidWithFileId);
when(userService.getUserProfile()).thenReturn(UserProfileTestFactory.create());
}
@Test
......
......@@ -2,9 +2,9 @@ package de.ozgcloud.bescheid;
import java.time.LocalDate;
import de.ozgcloud.bescheid.common.callcontext.UserProfileTestFactory;
import de.ozgcloud.bescheid.vorgang.VorgangId;
import de.ozgcloud.bescheid.vorgang.VorgangTestFactory;
import de.ozgcloud.command.CommandTestFactory;
public class BescheidRequestTestFactory {
......@@ -22,7 +22,7 @@ public class BescheidRequestTestFactory {
.vorgangId(VORGANG_ID)
.bescheidVom(BESCHEID_VOM)
.genehmigt(GENEHMIGT)
.createFor(UserId.from(CommandTestFactory.CREATED_BY));
.createFor(UserProfileTestFactory.create());
}
}
......@@ -5,13 +5,13 @@ import java.io.File;
import org.springframework.http.MediaType;
import de.itvsh.kop.common.binaryfile.TempFileUtils;
import de.ozgcloud.bescheid.common.callcontext.UserProfileTestFactory;
import de.ozgcloud.bescheid.vorgang.ServiceKontoTestFactory;
import de.ozgcloud.bescheid.vorgang.VorgangTestFactory;
import de.ozgcloud.command.CommandTestFactory;
public class BescheidTestFactory {
public static final UserId CREATED_BY = UserId.from(CommandTestFactory.CREATED_BY);
public static final UserId CREATED_BY = UserProfileTestFactory.ID;
public static final String FILE_NAME = "bescheid.pdf";
public static final byte[] TEST_BESCHEID = "testbescheid".getBytes();
......
......@@ -5,15 +5,22 @@ import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import de.ozgcloud.bescheid.BescheidRequestTestFactory;
import de.ozgcloud.bescheid.BescheidTestFactory;
import de.ozgcloud.bescheid.common.callcontext.UserProfileTestFactory;
import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsRequest.CustomerData.UserData;
import de.ozgcloud.bescheid.vorgang.VorgangTestFactory;
class SmartDocumentsBescheidRemoteServiceTest {
@InjectMocks
private SmartDocumentsBescheidRemoteService service;
@Mock
private SmartDocumentsProperties properties;
@Nested
class TestBuildBescheid {
......@@ -25,4 +32,15 @@ class SmartDocumentsBescheidRemoteServiceTest {
}
}
@Nested
class TestCreateRequest {
@Test
void shouldFillUserData() {
var request = service.createRequest(BescheidRequestTestFactory.create(), VorgangTestFactory.create());
assertThat(request.getCustomerData().getUserData()).isNotNull()
.extracting(UserData::getEmail).isEqualTo(UserProfileTestFactory.EMAIL);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment