RED-1186: Return mocked placeholders in unittest

This commit is contained in:
Dominique Eifländer 2021-04-26 09:41:57 +02:00
parent 2d5b3fe4fa
commit 951a901702
5 changed files with 47 additions and 2 deletions

View File

@ -0,0 +1,13 @@
package com.iqser.red.service.redaction.report.v1.server.client;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Placeholder {
private String placeholder;
private String value;
}

View File

@ -0,0 +1,11 @@
package com.iqser.red.service.redaction.report.v1.server.client;
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "PlaceholderResource", url = "${configuration-service.url}")
public interface PlaceholderClient {
List<Placeholder> getPlaceholders();
}

View File

@ -16,6 +16,8 @@ import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.iqser.red.service.redaction.report.v1.api.model.ReportType; import com.iqser.red.service.redaction.report.v1.api.model.ReportType;
import com.iqser.red.service.redaction.report.v1.server.client.Placeholder;
import com.iqser.red.service.redaction.report.v1.server.client.PlaceholderClient;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry; import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.report.v1.server.utils.ResourceLoader; import com.iqser.red.service.redaction.report.v1.server.utils.ResourceLoader;
@ -27,6 +29,7 @@ public class WordReportGenerationService {
private byte[] appendixA1Template; private byte[] appendixA1Template;
private byte[] appendixA2Template; private byte[] appendixA2Template;
private PlaceholderClient placeholderClient;
@PostConstruct @PostConstruct
@ -39,6 +42,10 @@ public class WordReportGenerationService {
public byte[] generateReport(ReportType reportType, List<ReportRedactionEntry> reportEntries, String filename) { public byte[] generateReport(ReportType reportType, List<ReportRedactionEntry> reportEntries, String filename) {
List<Placeholder> placeholder = placeholderClient.getPlaceholders();
byte[] template; byte[] template;
if (reportType.equals(ReportType.WORD_SINGLE_FILE_APPENDIX_A1_TEMPLATE)) { if (reportType.equals(ReportType.WORD_SINGLE_FILE_APPENDIX_A1_TEMPLATE)) {
@ -52,6 +59,8 @@ public class WordReportGenerationService {
try (ByteArrayInputStream is = new ByteArrayInputStream(template)) { try (ByteArrayInputStream is = new ByteArrayInputStream(template)) {
XWPFDocument doc = new XWPFDocument(is); XWPFDocument doc = new XWPFDocument(is);
// Replace placeholders.
addTableRows(doc, reportEntries, filename); addTableRows(doc, reportEntries, filename);
return toByteArray(doc); return toByteArray(doc);
} catch (IOException e) { } catch (IOException e) {

View File

@ -1,5 +1,6 @@
package com.iqser.red.service.redaction.report.v1.server; package com.iqser.red.service.redaction.report.v1.server;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -8,6 +9,8 @@ import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
@ -20,6 +23,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.configuration.v1.api.model.LegalBasisMapping; import com.iqser.red.service.configuration.v1.api.model.LegalBasisMapping;
import com.iqser.red.service.redaction.report.v1.api.model.ReportType; import com.iqser.red.service.redaction.report.v1.api.model.ReportType;
import com.iqser.red.service.redaction.report.v1.server.client.LegalBasisMappingClient; import com.iqser.red.service.redaction.report.v1.server.client.LegalBasisMappingClient;
import com.iqser.red.service.redaction.report.v1.server.client.Placeholder;
import com.iqser.red.service.redaction.report.v1.server.client.PlaceholderClient;
import com.iqser.red.service.redaction.report.v1.server.configuration.MessagingConfiguration; import com.iqser.red.service.redaction.report.v1.server.configuration.MessagingConfiguration;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry; import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.report.v1.server.service.ExcelReportGenerationService; import com.iqser.red.service.redaction.report.v1.server.service.ExcelReportGenerationService;
@ -44,7 +49,7 @@ public class RedactionReportIntegrationTest {
@MockBean @MockBean
private AmazonS3 s3Client; private AmazonS3 s3Client;
@Autowired @InjectMocks
private WordReportGenerationService wordReportGenerationService; private WordReportGenerationService wordReportGenerationService;
@Autowired @Autowired
@ -56,10 +61,15 @@ public class RedactionReportIntegrationTest {
@Autowired @Autowired
private RedactionLogConverterService redactionLogConverterService; private RedactionLogConverterService redactionLogConverterService;
@Mock
private PlaceholderClient placeholderClient;
@Test @Test
public void testWordReportGeneration() throws IOException { public void testWordReportGeneration() throws IOException {
when(placeholderClient.getPlaceholders()).thenReturn(List.of(Placeholder.builder().placeholder("{{dossier.name}}").value("Dossier 1").build()));
ClassPathResource redactionLogResource = new ClassPathResource("files/RedactedLog.txt"); ClassPathResource redactionLogResource = new ClassPathResource("files/RedactedLog.txt");
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class); RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class);
@ -73,12 +83,14 @@ public class RedactionReportIntegrationTest {
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping); List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
byte[] report = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE_APPENDIX_A2_TEMPLATE, reportEntries, redactionLog wordReportGenerationService.init();
byte[] report = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE_APPENDIX_A1_TEMPLATE, reportEntries, redactionLog
.getFilename()); .getFilename());
try (FileOutputStream fileOutputStream = new FileOutputStream("/tmp/efsa_template.docx")) { try (FileOutputStream fileOutputStream = new FileOutputStream("/tmp/efsa_template.docx")) {
fileOutputStream.write(report); fileOutputStream.write(report);
} }
} }