Commit 63d432c4 authored by Hando Lukats's avatar Hando Lukats
Browse files

Merge branch 'TEIS-2380' into 'develop'

Resolve TEIS-2380

Closes TEIS-2380

See merge request teis/riskassessments-service!142
parents 0ed0aa67 aa20723c
......@@ -2,13 +2,13 @@ package ee.sm.ti.teis.riskassessments.riskassessment;
import ee.sm.ti.teis.ErrorDTO;
import ee.sm.ti.teis.RiskAssessmentsAppTestBase;
import ee.sm.ti.teis.cache.person.PersonService;
import ee.sm.ti.teis.commongateway.riskassessments.riskassessment.dto.RiskAssessmentPublicDto;
import ee.sm.ti.teis.domain.person.Person;
import ee.sm.ti.teis.officegateway.riskassessments.riskassessment.dto.CreateRiskAssessmentOfficeDto;
import ee.sm.ti.teis.officegateway.riskassessments.riskassessment.request.CreateRiskAssessmentOfficeRequest;
import ee.sm.ti.teis.officegateway.riskassessments.riskassessment.response.CreateRiskAssessmentOfficeResponse;
import ee.sm.ti.teis.riskassessments.riskassessment.listener.RiskAssessmentOfficeGwListener;
import ee.sm.ti.teis.riskassessments.serviceclient.PersonServiceClient;
import ee.sm.ti.teis.servicecommon.test.ListenerCreateRequestTestBase;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import org.junit.jupiter.api.BeforeEach;
......@@ -66,7 +66,7 @@ public class CreateRiskAssessmentOfficeGwListenerTest extends RiskAssessmentsApp
@Test
@Transactional
public void create_minimumData_success() {
PersonServiceClient.PersonResponseMessage personResponse = new PersonServiceClient.PersonResponseMessage();
PersonService.PersonResponse personResponse = new PersonService.PersonResponse();
Person person = Person.builder().id(employerId).build();
personResponse.setPayload(person, new RequestMetaDTO());
doReturn(personResponse)
......@@ -152,7 +152,7 @@ public class CreateRiskAssessmentOfficeGwListenerTest extends RiskAssessmentsApp
@Test
public void createRiskAssessment_employerIdNotExists_notFound() {
PersonServiceClient.PersonResponseMessage personResponse = new PersonServiceClient.PersonResponseMessage();
PersonService.PersonResponse personResponse = new PersonService.PersonResponse();
ErrorDTO errorDTO = new ErrorDTO();
errorDTO.setCode(RESOURCE_NOT_FOUND.getCode());
personResponse.setError(errorDTO);
......
package ee.sm.ti.teis.riskassessments.riskassessment;
import ee.sm.ti.teis.RiskAssessmentsAppTestBase;
import ee.sm.ti.teis.cache.person.PersonService;
import ee.sm.ti.teis.commongateway.docgen.dto.GeneratedDocumentDto;
import ee.sm.ti.teis.commongateway.riskassessments.riskassessment.dto.RiskAssessmentPdfDto;
import ee.sm.ti.teis.commongateway.riskassessments.riskassessment.request.RiskAssessmentPDFRequest;
import ee.sm.ti.teis.commongateway.riskassessments.riskassessment.response.GetRiskAssessmentPDFResponse;
import ee.sm.ti.teis.commongateway.riskassessments.riskassessment.response.GetRiskAssessmentPDFTestResponse;
import ee.sm.ti.teis.domain.docgen.GeneratedDocument;
import ee.sm.ti.teis.domain.person.LegalPerson;
import ee.sm.ti.teis.domain.person.Person;
import ee.sm.ti.teis.domain.signing.SignFileStream;
import ee.sm.ti.teis.riskassessments.riskassessment.listener.RiskAssessmentPublicGwListener;
import ee.sm.ti.teis.riskassessments.serviceclient.DocGenServiceClient;
import ee.sm.ti.teis.riskassessments.serviceclient.SigningServiceClient;
import ee.sm.ti.teis.servicecommon.test.ListenerGetObjectRequestTestBase;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
import static ee.sm.ti.teis.servicerequest.UserType.PERSON;
import static ee.sm.ti.teis.types.enums.RolePrivilegeCode.EX_MANAGE_ACCIDENT;
import static ee.sm.ti.teis.types.enums.RolePrivilegeCode.EX_MANAGE_RISK_ASSESSMENTS;
import static ee.sm.ti.teis.utils.TestUtils.*;
import static java.util.UUID.randomUUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
public class GetRiskAssessmentPdfPublicGwListenerTest extends RiskAssessmentsAppTestBase implements ListenerGetObjectRequestTestBase {
private static final UUID RISK_ASSESSMENT_ID = UUID.fromString("faf5868f-00ae-4d41-a3c1-d8738d5ae624");
private static final UUID EMPLOYER_ID = UUID.fromString("69a31369-f74d-404d-a221-ba7d30bf3455");
private static final String DOMAIN_GET_PERSON = "domain.Person.request";
private static final String ASICE_CONTENT_TYPE = "application/vnd.etsi.asic-e+zip";
@Autowired
RiskAssessmentPublicGwListener listener;
@Autowired
RabbitTemplate msRabbitTemplate;
RiskAssessmentPDFRequest pdfRequest;
RequestMetaDTO requestMetaDTO;
@BeforeEach
void setUp() {
pdfRequest = new RiskAssessmentPDFRequest();
requestMetaDTO = createRequestMeta(randomUUID().toString(), PERSON, EMPLOYER_ID.toString(),
List.of(EX_MANAGE_RISK_ASSESSMENTS.name()));
pdfRequest.setRequestMetaDTO(requestMetaDTO);
PersonService.PersonResponse personResponse = new PersonService.PersonResponse();
LegalPerson legalPerson = LegalPerson.builder().name("Subaru Auto AS").regCode("12197746").build();
Person person = Person.builder().legalPerson(legalPerson).id(EMPLOYER_ID).build();
personResponse.setPayload(person, new RequestMetaDTO());
doReturn(personResponse)
.when(msRabbitTemplate).convertSendAndReceiveAsType(eq(DOMAIN_GET_PERSON), any(Object.class), any());
}
@Test
public void getObjectData_success() {
pdfRequest.setPayload(RISK_ASSESSMENT_ID, requestMetaDTO);
GetRiskAssessmentPDFTestResponse response = listener.getPDFTest(pdfRequest);
RiskAssessmentPdfDto payload = response.getPayload();
assertThat(payload.getTitle()).isEqualTo("Subaru risk assessment (in_progress)");
assertThat(payload.getEmployerName()).isEqualTo("Subaru Auto AS");
assertThat(payload.getRegCode()).isEqualTo("12197746");
assertThat(payload.getStatus()).isEqualTo("Pooleli");
assertThat(payload.getArchivedDate()).isNull();
assertThat(payload.getPublishedDate()).isNull();
assertThat(payload.getPrimaryBusinessType()).isEqualTo("TEST shop");
assertThat(payload.getSecondaryBusinessTypes()).isEqualTo("TEST y-demolition");
assertThat(payload.getCategories()).isNotEmpty();
assertThat(payload.getCategories().get(0).getRiskFactors()).isNotEmpty();
assertThat(payload.getCategories().get(0).getName()).isEqualTo("Täiendavad ohud");
assertThat(payload.getCategories().get(0).getRiskFactors().get(1).getName()).isEqualTo("api tests: risk factor assessment (in_progress)");
}
@Test
@Override
public void getObject_success() {
GeneratedDocument generatedDocument = GeneratedDocument.builder()
.docName("doc.pdf")
.templateId("RISK_ASSESSMENT_PDF")
.contentType("application/pdf")
.content("content".getBytes(StandardCharsets.UTF_8))
.build();
DocGenServiceClient.PreviewDocumentMsResponse documentResponse = new DocGenServiceClient.PreviewDocumentMsResponse();
documentResponse.setPayload(generatedDocument, requestMetaDTO);
doReturn(documentResponse)
.when(msRabbitTemplate)
.convertSendAndReceiveAsType(eq("domain.GenerateDocument.generatedPreview"), any(Object.class), any());
SignFileStream signedDocument = SignFileStream.builder()
.fileName("doc.asice")
.contentType(ASICE_CONTENT_TYPE)
.content("content".getBytes(StandardCharsets.UTF_8))
.build();
SigningServiceClient.FileStreamSignedMsResponse fileSignedResponse = new SigningServiceClient.FileStreamSignedMsResponse();
fileSignedResponse.setPayload(signedDocument, requestMetaDTO);
doReturn(fileSignedResponse)
.when(msRabbitTemplate)
.convertSendAndReceiveAsType(eq("domain.SignFileStream.create"), any(Object.class), any());
pdfRequest.setPayload(RISK_ASSESSMENT_ID, requestMetaDTO);
GetRiskAssessmentPDFResponse response = listener.getPDF(pdfRequest);
GeneratedDocumentDto payload = response.getPayload();
assertThat(payload.getTemplateId()).isEqualTo("RISK_ASSESSMENT_PDF");
assertThat(payload.getDocName()).isEqualTo("doc.asice");
assertThat(payload.getContent()).isNotEmpty();
}
@Override
@Test
public void getObject_idNotExists_notFound() {
pdfRequest.setPayload(UUID.randomUUID(), requestMetaDTO);
GetRiskAssessmentPDFTestResponse response = listener.getPDFTest(pdfRequest);
assertResponseNotFound(response.getError());
}
@Override
public void getObject_objectStatusDeleted_success() {
// not applicable
}
@Override
@Test
public void missingPrivilege_forbidden() {
requestMetaDTO.setPrivileges(List.of(EX_MANAGE_ACCIDENT.name()));
pdfRequest.setPayload(RISK_ASSESSMENT_ID, requestMetaDTO);
GetRiskAssessmentPDFResponse response = listener.getPDF(pdfRequest);
assertResponseForbidden(response.getError());
}
@Override
public void wrongUserType_forbidden() {
// not applicable
}
}
......@@ -8,6 +8,8 @@ import ee.sm.ti.teis.cache.classifier.ClassifierItemCacheUpdateService.Classifie
import ee.sm.ti.teis.cache.classifier.ClassifierItemCacheUpdateService.ClassifierItemCreatedDTO;
import ee.sm.ti.teis.cache.classifier.ClassifierItemCacheUpdateService.ClassifierItemReinitializeDTO;
import ee.sm.ti.teis.cache.classifier.ClassifierItemCacheUpdateService.ClassifierItemUpdatedDTO;
import ee.sm.ti.teis.cache.person.IndividualPersonCacheUpdateService;
import ee.sm.ti.teis.cache.person.LegalPersonCacheUpdateService;
import ee.sm.ti.teis.configuration.DomainCacheList;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
......@@ -23,11 +25,15 @@ public class RiskAssessmentDomainCacheConfig implements DomainCacheList {
private final ClassifierItemCacheUpdateService classifierItemCacheUpdateService;
private final ClassifierDefinitionCacheUpdateService classifierDefinitionCacheUpdateService;
private final LegalPersonCacheUpdateService legalPersonCacheUpdateService;
private final IndividualPersonCacheUpdateService individualPersonCacheUpdateService;
@Override
public Set<BaseCacheUpdateService> cacheUpdateServices() {
return Set.of(classifierItemCacheUpdateService,
classifierDefinitionCacheUpdateService);
classifierDefinitionCacheUpdateService,
legalPersonCacheUpdateService,
individualPersonCacheUpdateService);
}
@RabbitListener(queues = "#{@classifierItemCacheUpdateService.cacheResponseQueueName}")
......@@ -64,4 +70,57 @@ public class RiskAssessmentDomainCacheConfig implements DomainCacheList {
public void classifierItemDeletedEvent(ClassifierItemCacheUpdateService.ClassifierItemDeletedDTO updatedDTO) {
classifierItemCacheUpdateService.deleted(updatedDTO.getPayload().getId(), updatedDTO.getPayload());
}
@RabbitListener(queues = "#{@legalPersonCacheUpdateService.createdEventsQueueName}")
public void legalPersonCreatedEvent(LegalPersonCacheUpdateService.LegalPersonCreatedDTO createdDTO) {
legalPersonCacheUpdateService.created(createdDTO.getPayload());
}
@RabbitListener(queues = "#{@legalPersonCacheUpdateService.updatedEventsQueueName}")
public void legalPersonUpdatedEvent(LegalPersonCacheUpdateService.LegalPersonUpdatedDTO updatedDTO) {
legalPersonCacheUpdateService.updated(updatedDTO.getPayload().getId(), updatedDTO.getPayload());
}
@RabbitListener(queues = "#{@legalPersonCacheUpdateService.deletedEventsQueueName}")
public void legalPersonDeletedEvent(LegalPersonCacheUpdateService.LegalPersonDeletedDTO deletedDTO) {
legalPersonCacheUpdateService.deleted(deletedDTO.getPayload().getId(), deletedDTO.getPayload());
}
@RabbitListener(queues = "#{@individualPersonCacheUpdateService.createdEventsQueueName}")
public void individualPersonCreatedEvent(IndividualPersonCacheUpdateService.IndividualPersonCreatedDTO createdDTO) {
individualPersonCacheUpdateService.created(createdDTO.getPayload());
}
@RabbitListener(queues = "#{@individualPersonCacheUpdateService.updatedEventsQueueName}")
public void individualPersonUpdatedEvent(IndividualPersonCacheUpdateService.IndividualPersonUpdatedDTO updatedDTO) {
individualPersonCacheUpdateService.updated(updatedDTO.getPayload().getId(), updatedDTO.getPayload());
}
@RabbitListener(queues = "#{@individualPersonCacheUpdateService.deletedEventsQueueName}")
public void individualPersonDeletedEvent(IndividualPersonCacheUpdateService.IndividualPersonDeletedDTO deletedDTO) {
individualPersonCacheUpdateService.deleted(deletedDTO.getPayload().getId(), deletedDTO.getPayload());
}
@RabbitListener(queues = "#{@legalPersonCacheUpdateService.cacheResponseQueueName}")
public void legalPersonCacheResponseEvent(
LegalPersonCacheUpdateService.LegalPersonCacheResponseDTO cacheResponseDTO) {
legalPersonCacheUpdateService.processSnapshot(cacheResponseDTO.getPayload(), cacheResponseDTO.getRequestMetaDTO());
}
@RabbitListener(queues = "#{@legalPersonCacheUpdateService.cacheReinitializeQueueName}")
public void legalPersonReinitializeEvent(LegalPersonCacheUpdateService.LegalPersonReinitializeDTO reinitializeDTO) {
legalPersonCacheUpdateService.reinitialize();
}
@RabbitListener(queues = "#{@individualPersonCacheUpdateService.cacheResponseQueueName}")
public void individualPersonCacheResponseEvent(
IndividualPersonCacheUpdateService.IndividualPersonCacheResponseDTO cacheResponseDTO) {
individualPersonCacheUpdateService.processSnapshot(cacheResponseDTO.getPayload(), cacheResponseDTO.getRequestMetaDTO());
}
@RabbitListener(queues = "#{@individualPersonCacheUpdateService.cacheReinitializeQueueName}")
public void individualPersonReinitializeEvent(
IndividualPersonCacheUpdateService.IndividualPersonReinitializeDTO reinitializeDTO) {
individualPersonCacheUpdateService.reinitialize();
}
}
package ee.sm.ti.teis.riskassessments.riskassessment.service;
import ee.sm.ti.teis.cache.person.PersonService;
import ee.sm.ti.teis.exceptions.TeisBusinessException;
import ee.sm.ti.teis.officegateway.riskassessments.riskassessment.dto.CountRiskAssessmentDto;
import ee.sm.ti.teis.officegateway.riskassessments.riskassessment.dto.CreateRiskAssessmentOfficeDto;
import ee.sm.ti.teis.riskassessments.serviceclient.PersonServiceClient;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
......@@ -14,7 +14,7 @@ import static ee.sm.ti.teis.errors.CommonErrorCode.BAD_REQUEST;
@RequiredArgsConstructor
public class RiskAssessmentOfficeValidationService {
private final PersonServiceClient personServiceClient;
private final PersonService personService;
public void validateCount(CountRiskAssessmentDto dto) {
int fieldsWithValue = countFieldsWithValue(dto);
......@@ -47,6 +47,6 @@ public class RiskAssessmentOfficeValidationService {
}
public void validateRiskAssessment(CreateRiskAssessmentOfficeDto dto, RequestMetaDTO requestMetaDTO) {
personServiceClient.getPerson(dto.getEmployerId(), requestMetaDTO);
personService.getPerson(dto.getEmployerId(), requestMetaDTO);
}
}
package ee.sm.ti.teis.riskassessments.riskassessment.service;
import ee.sm.ti.teis.commongateway.file.FileReferenceDto;
import ee.sm.ti.teis.commongateway.riskassessments.riskassessment.dto.*;
import ee.sm.ti.teis.file.filereference.FileReferenceBusinessService;
import ee.sm.ti.teis.riskassessments.riskindicator.entity.RiskIndicatorEntity;
import ee.sm.ti.teis.riskassessments.riskindicator.service.RiskIndicatorDataService;
import ee.sm.ti.teis.riskassessments.riskindicatorvalue.RiskIndicatorValueEntity;
import ee.sm.ti.teis.riskassessments.riskindicatorvalue.service.RiskIndicatorValueDataService;
import ee.sm.ti.teis.riskassessments.riskstatementassessment.RiskStatementAssessmentEntity;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static ee.sm.ti.teis.types.enums.classifier.ObjectType.RISK_STATEMENT_ASSESSMENT;
@Service
@RequiredArgsConstructor
@Validated
public class RiskAssessmentPDFComposeService {
//TODO text values should be in properties file or similar
private static final String ACTION_TAKEN = "Abinõu on kasutusel";
private static final String NO_ACTION_OR_DECISION = "Abinõu ei ole kasutusel, tegevuskavasse kandmine on otsustamata";
private static final String ACTION_NOT_EVALUATED = "Abinõu kasutamine on täpsustamata";
private static final String NO_ACTION_NEEDED = "Abinõud ei ole vaja rakendada";
private static final String ADDED_TO_ACTION_PLAN = "Abinõu on lisatud tegevuskavasse";
private final RiskIndicatorValueDataService riskIndicatorValueDataService;
private final RiskIndicatorDataService riskIndicatorDataService;
private final FileReferenceBusinessService fileReferenceBusinessService;
public RiskFactorPdfCategoryDto composeRiskFactorCategoryDto(String name, Long seqNo) {
return RiskFactorPdfCategoryDto.builder()
.name(name)
.seqNo(seqNo)
.build();
}
public RiskFactorPdfDto composeRiskFactorPdfDto(String name, String impact, String explanation) {
return RiskFactorPdfDto.builder()
.name(name)
.description(impact)
.explanation(explanation)
.build();
}
public RiskIndicatorPdfDto composeRiskIndicator(RiskStatementAssessmentPdfDto riskStatementAssessment,
RequestMetaDTO requestMetaDTO) {
List<RiskIndicatorValuesPdfDto> riskIndicatorList = new ArrayList<>();
List<RiskIndicatorValueEntity> riskIndicatorValues = riskIndicatorValueDataService.getCurrentByRiskStatementAssessmentId(riskStatementAssessment.getId());
if (!riskIndicatorValues.isEmpty()) {
RiskIndicatorEntity riskIndicator = riskIndicatorDataService.getCurrentById(riskIndicatorValues.get(0).getRiskIndicatorId());
riskIndicatorValues.forEach(item -> {
RiskIndicatorValuesPdfDto riskIndicatorValue = RiskIndicatorValuesPdfDto.builder()
.value(item.getValue() + riskIndicator.getUnit())
.comment(item.getComment())
.build();
riskIndicatorList.add(riskIndicatorValue);
});
riskStatementAssessment.setRiskIndicatorName(riskIndicator.getName());
}
RiskIndicatorPdfDto riskIndicatorPdfDto = RiskIndicatorPdfDto.builder()
.riskIndicatorValues(riskIndicatorList)
.build();
List<FileReferenceDto> fileReferences = fileReferenceBusinessService.getFilesMetadataByObjectIdAndObjectType(riskStatementAssessment.getId(),
RISK_STATEMENT_ASSESSMENT.getId(), requestMetaDTO);
riskIndicatorPdfDto.setHasFiles(!fileReferences.isEmpty());
return riskIndicatorPdfDto;
}
public void addToRiskFactorCategory(List<RiskFactorPdfCategoryDto> categoryList,
RiskFactorPdfCategoryDto categoryDto,
Optional<RiskFactorPdfCategoryDto> existingCategory, RiskFactorPdfDto riskFactorData) {
existingCategory.ifPresentOrElse(
category -> category.getRiskFactors().add(riskFactorData),
() -> {
List<RiskFactorPdfDto> riskFactorList = new ArrayList<>();
riskFactorList.add(riskFactorData);
categoryDto.setRiskFactors(riskFactorList);
categoryList.add(categoryDto);
}
);
}
public String composeRiskStatementAssessmentStatus(
RiskStatementAssessmentEntity riskStatementAssessment) {
if (riskStatementAssessment.getAssessment() == null) {
return ACTION_NOT_EVALUATED;
} else if (BooleanUtils.isTrue(riskStatementAssessment.getAssessment())) {
return ACTION_TAKEN;
} else {
if (riskStatementAssessment.getAction() != null && riskStatementAssessment.getAction()) {
return ADDED_TO_ACTION_PLAN;
} else if (riskStatementAssessment.getAction() != null && !riskStatementAssessment.getAction()) {
return NO_ACTION_NEEDED;
} else {
return NO_ACTION_OR_DECISION;
}
}
}
}
package ee.sm.ti.teis.riskassessments.serviceclient;
import ee.sm.ti.teis.ErrorDTO;
import ee.sm.ti.teis.domain.person.Person;
import ee.sm.ti.teis.domainrequest.DomainResponseDTO;
import ee.sm.ti.teis.domainrequest.rabbit.TeisRabbitClient;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class PersonServiceClient extends TeisRabbitClient<Person> {
public PersonServiceClient(RabbitTemplate msRabbitTemplate) {
super(msRabbitTemplate);
}
public Person getPerson(UUID employerId, RequestMetaDTO requestMetaDTO) {
Person requestedPerson = Person.builder().id(employerId).build();
return getItem(requestedPerson, PersonResponseMessage.class, requestMetaDTO);
}
public static class PersonResponseMessage extends DomainResponseDTO<Person, ErrorDTO> {
}
}
......@@ -55,7 +55,7 @@ teis:
data:
resetmagic: ${TEST_DATA_RESET_MAGIC:}
cache:
queues: classifierItems, classifierDefinitions
queues: classifierItems, classifierDefinitions, individualPersons, legalPersons
rabbit:
gw-exchange-name: gw_exchange
gw-exchange-reply-timeout: ${RABBIT_TEMPLATE_GW_EXCHANGE_REPLY_TIMEOUT:6000}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment