Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ sourceSets {
}
}

test {
useJUnitPlatform()
}

dependencies {
integrationTestImplementation(sourceSets.test.output)
integrationTestAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: versions.lombok
Expand Down
2 changes: 1 addition & 1 deletion charts/prl-dgs/values.preview.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ java:
MANAGEMENT_ENDPOINT_HEALTH_CACHE_TIMETOLIVE: "30000"
DOCMOSIS_SERVICE_DEV_MODE_FLAG: "true"
PDF_TEST_ENABLED: "true"
CCD_CASE_DOCS_AM_API: http://prl-ccd-definitions-pr-2424-cdam
CCD_CASE_DOCS_AM_API: http://prl-ccd-definitions-pr-2170-cdam
AUTH_PROVIDER_SERVICE_CLIENT_BASEURL: "http://rpe-service-auth-provider-{{ .Values.global.environment }}.service.core-compute-{{ .Values.global.environment }}.internal"
IDAM_S2S_URL: "http://rpe-service-auth-provider-{{ .Values.global.environment }}.service.core-compute-{{ .Values.global.environment }}.internal"
DOCMOSIS_SERVICE_BASE_URL: "https://docmosis.aat.platform.hmcts.net"
Expand Down
1 change: 1 addition & 0 deletions src/contractTest/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ docmosis:
uri: ${DOCMOSIS_SERVICE_BASE_URL:http://localhost:5501}${docmosis.service.pdf-service.renderEndpoint}
convert: ${DOCMOSIS_SERVICE_BASE_URL:http://localhost:5501}${docmosis.service.pdf-service.convertEndpoint}
health: ${DOCMOSIS_SERVICE_BASE_URL:http://localhost:5501}/health
convert: ${DOCMOSIS_SERVICE_BASE_URL:http://localhost:5501}${docmosis.service.pdf-service.convertEndpoint}
devMode: ${DOCMOSIS_SERVICE_DEV_MODE_FLAG:false}

case_document_am:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import uk.gov.hmcts.reform.prl.documentgenerator.domain.response.GeneratedDocumentInfo;
import uk.gov.hmcts.reform.prl.documentgenerator.service.DocumentManagementService;

import java.io.IOException;
import javax.validation.Valid;

@RestController
Expand All @@ -27,24 +28,26 @@
@Slf4j
public class DocumentGeneratorController {

private final DocumentManagementService documentManagementService;

@Autowired
private DocumentManagementService documentManagementService;
public DocumentGeneratorController(DocumentManagementService documentManagementService) {
this.documentManagementService = documentManagementService;
}

@Operation(description = "Generate PDF document based on the supplied template name and placeholder "
+ "texts and saves it in the evidence management.", tags = {"Document Generation"})
@ApiResponses({
@ApiResponse(responseCode = "200", description = "PDF was generated successfully and stored in the "
+ "evidence management. Returns the url to the stored document.", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class))),
@ApiResponse(responseCode = "400", description = "Returned when input parameters are invalid "
+ "or template not found", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class))),
@ApiResponse(responseCode = "503", description = "Returned when the PDF Service or Evidence "
+ "Management Client Api cannot be reached", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class))),
@ApiResponse(responseCode = "500", description = "Returned when there is an unknown server error",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
})
+ "texts and saves it in the evidence management.", tags = {"Document Generation"})
@ApiResponse(responseCode = "200", description = "PDF was generated successfully and stored in the "
+ "evidence management. Returns the url to the stored document.", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
@ApiResponse(responseCode = "400", description = "Returned when input parameters are invalid "
+ "or template not found", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
@ApiResponse(responseCode = "503", description = "Returned when the PDF Service or Evidence "
+ "Management Client Api cannot be reached", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
@ApiResponse(responseCode = "500", description = "Returned when there is an unknown server error",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
@PostMapping("/generatePDF")
public GeneratedDocumentInfo generateAndUploadPdf(
@RequestHeader(value = "Authorization", required = false)
Expand All @@ -63,19 +66,19 @@ public GeneratedDocumentInfo generateAndUploadPdf(

@Operation(description = "Generate draft PDF document based on the supplied template name and placeholder texts "
+ "and saves it in the evidence management.", tags = {"Document Generation"})
@ApiResponses({
@ApiResponse(responseCode = "200", description = "PDF was generated successfully and stored in the "
+ "evidence management. Returns the url to the stored document.",content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class))),
@ApiResponse(responseCode = "400", description = "Returned when input parameters are invalid or "
+ "template not found", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class))),
@ApiResponse(responseCode = "503", description = "Returned when the PDF Service or Evidence Management "
+ "Client Api cannot be reached", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class))),
@ApiResponse(responseCode = "500", description = "Returned when there is an unknown server error",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
})

@ApiResponse(responseCode = "200", description = "PDF was generated successfully and stored in the "
+ "evidence management. Returns the url to the stored document.",content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
@ApiResponse(responseCode = "400", description = "Returned when input parameters are invalid or "
+ "template not found", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
@ApiResponse(responseCode = "503", description = "Returned when the PDF Service or Evidence Management "
+ "Client Api cannot be reached", content =
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
@ApiResponse(responseCode = "500", description = "Returned when there is an unknown server error",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))

@PostMapping("/generateDraftPDF")
public GeneratedDocumentInfo generateAndUploadDraftPdf(
@RequestHeader(value = "Authorization", required = false)
Expand Down Expand Up @@ -103,7 +106,7 @@ public GeneratedDocumentInfo convertDocumentToPdf(
+ "templateName and the placeholder text map", required = true)
@RequestBody
@Valid
GenerateDocumentRequest templateData) {
GenerateDocumentRequest templateData) throws IOException {
//This service is internal to Divorce system. No need to do service authentication here
log.info("convertDocumentToPdf is getting called");
return documentManagementService.converToPdf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public ResponseEntity<Object> handleTemplateLoadingException(ErrorLoadingTemplat
public ResponseEntity<Object> handleDocumentStorageAndPDFGenerationException(Exception exception) {
log.error(exception.getMessage(), exception);

if (exception.getCause() instanceof HttpClientErrorException) {
HttpStatus httpClientErrorException = ((HttpClientErrorException) exception.getCause()).getStatusCode();
if (exception.getCause() instanceof HttpClientErrorException httpClientErrorException) {
HttpStatus httpClientStatus = httpClientErrorException.getStatusCode();

if (httpClientErrorException == HttpStatus.BAD_REQUEST) {
if (httpClientStatus == HttpStatus.BAD_REQUEST) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(exception.getMessage());
} else {
return ResponseEntity.status(httpClientErrorException).body(exception.getMessage());
return ResponseEntity.status(httpClientStatus).body(exception.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
@Component
public class TemplateDataMapper {


private final DocmosisBasePdfConfig docmosisBasePdfConfig;

@Autowired
private DocmosisBasePdfConfig docmosisBasePdfConfig;
public TemplateDataMapper(DocmosisBasePdfConfig docmosisBasePdfConfig) {
this.docmosisBasePdfConfig = docmosisBasePdfConfig;
}

@SuppressWarnings("unchecked")
public Map<String, Object> map(Map<String, Object> placeholders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import uk.gov.hmcts.reform.prl.documentgenerator.domain.response.GeneratedDocumentInfo;

import java.io.IOException;
import java.util.Map;

public interface DocumentManagementService {
Expand All @@ -15,5 +16,5 @@ GeneratedDocumentInfo generateAndStoreDraftDocument(String templateName, Map<Str

byte[] generateDocument(String templateName, Map<String, Object> placeholders);

GeneratedDocumentInfo converToPdf(Map<String, Object> placeholders, String authorizationToken, String filename);
GeneratedDocumentInfo converToPdf(Map<String, Object> placeholders, String authorizationToken, String filename) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package uk.gov.hmcts.reform.prl.documentgenerator.service;

import java.io.IOException;
import java.util.Map;

public interface PDFGenerationService {

byte[] generate(String templateName, Map<String, Object> placeholders);

byte[] converToPdf(Map<String, Object> placeholders, String fileName);
byte[] converToPdf(Map<String, Object> placeholders, String fileName) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
@Slf4j
public class DocmosisPDFGenerationServiceImpl implements PDFGenerationService {

@Autowired
private RestTemplate restTemplate;
private final RestTemplate restTemplate;

private final TemplateDataMapper templateDataMapper;

@Autowired
private TemplateDataMapper templateDataMapper;
public DocmosisPDFGenerationServiceImpl(RestTemplate restTemplate,TemplateDataMapper templateDataMapper) {
this.restTemplate = restTemplate;
this.templateDataMapper = templateDataMapper;
}

@Value("${docmosis.service.pdf-service.uri}")
private String docmosisPdfServiceEndpoint;
Expand Down Expand Up @@ -88,26 +92,19 @@ private PdfDocumentRequest request(String templateName, Map<String, Object> plac
}

@Override
public byte[] converToPdf(Map<String, Object> placeholders, String fileName) {

try {
String filename = FilenameUtils.getBaseName(fileName) + ".pdf";
ObjectMapper objectMapper = new ObjectMapper();
byte[] docInBytes = objectMapper.convertValue(placeholders.get("fileName"), byte[].class);
File file = new File(fileName);
Files.write(docInBytes, file);

return restTemplate
.postForObject(
docmosisPdfConvertEndpoint,
createRequest(file, filename),
byte[].class
);

} catch (IOException e) {
throw new RuntimeException(e);
}

public byte[] converToPdf(Map<String, Object> placeholders, String fileName) throws IOException {
String filename = FilenameUtils.getBaseName(fileName) + ".pdf";
ObjectMapper objectMapper = new ObjectMapper();
byte[] docInBytes = objectMapper.convertValue(placeholders.get("fileName"), byte[].class);
File file = new File(fileName);
Files.write(docInBytes, file);

return restTemplate
.postForObject(
docmosisPdfConvertEndpoint,
createRequest(file, filename),
byte[].class
);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import uk.gov.hmcts.reform.prl.documentgenerator.service.DocumentManagementService;
import uk.gov.hmcts.reform.prl.documentgenerator.service.PDFGenerationService;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.util.Arrays;
Expand Down Expand Up @@ -141,7 +142,7 @@ private String getCaseId(Map<String, Object> placeholders) {
}

@Override
public GeneratedDocumentInfo converToPdf(Map<String, Object> placeholders, String authorizationToken, String fileName) {
public GeneratedDocumentInfo converToPdf(Map<String, Object> placeholders, String authorizationToken, String fileName) throws IOException {
log.debug(
"Generate document requested with templateName [{}], placeholders of size[{}]",
placeholders.size()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import uk.gov.hmcts.reform.prl.documentgenerator.domain.response.GeneratedDocumentInfo;
import uk.gov.hmcts.reform.prl.documentgenerator.service.DocumentManagementService;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void whenGeneratePDF_thenReturnGeneratedDraftPDFDocumentInfo() {
}

@Test
public void whenConvertPDF_thenReturnConvertedPDFDocumentInfo() {
public void whenConvertPDF_thenReturnConvertedPDFDocumentInfo() throws IOException {
final String templateName = "templateName";
Map<String, Object> placeholder = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package uk.gov.hmcts.reform.prl.documentgenerator.controller.helper;

import org.junit.Test;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
Expand All @@ -11,13 +12,13 @@

import static org.junit.Assert.assertEquals;

@RunWith(BlockJUnit4ClassRunner.class)
public class GlobalExceptionHandlerUTest {
@RunWith(MockitoJUnitRunner.class)
class GlobalExceptionHandlerUTest {

private final GlobalExceptionHandler classUnderTest = new GlobalExceptionHandler();

@Test
public void whenHandleBadRequestException_thenReturnBadRequest() {
void whenHandleBadRequestException_thenReturnBadRequest() {
final Exception exception = new Exception();

ResponseEntity<Object> response = classUnderTest.handleBadRequestException(exception);
Expand All @@ -26,7 +27,7 @@ public void whenHandleBadRequestException_thenReturnBadRequest() {
}

@Test
public void whenHandleTemplateLoadingException_thenReturnBadRequest() {
void whenHandleTemplateLoadingException_thenReturnBadRequest() {
final String message = "some message";
final Exception exception = new Exception();
final ErrorLoadingTemplateException errorLoadingTemplateException =
Expand All @@ -39,7 +40,7 @@ public void whenHandleTemplateLoadingException_thenReturnBadRequest() {
}

@Test
public void givenHttpClientErrorExceptionWrappedIn_whenHandleDocumentStorageAndPDFGenerationException_thenReturnStatusCodeOfHttpClientErrorException() {
void givenHttpClientErrorExceptionWrappedIn_whenHandleDocumentStorageAndPDFGenerationException_thenReturnStatusCodeOfHttpClientErrorException() {
final HttpStatus httpStatus = HttpStatus.MOVED_PERMANENTLY;

final HttpClientErrorException httpClientErrorException = new HttpClientErrorException(httpStatus);
Expand All @@ -56,7 +57,7 @@ public void givenHttpClientErrorExceptionWrappedIn_whenHandleDocumentStorageAndP
}

@Test
public void givenHttpClientErrorResponseCode200_whenHandleDocumentStorageAndPDFGenerationException_thenReturnStatus503() {
void givenHttpClientErrorResponseCode200_whenHandleDocumentStorageAndPDFGenerationException_thenReturnStatus503() {
final HttpStatus httpStatus = HttpStatus.BAD_REQUEST;

final HttpClientErrorException httpClientErrorException = new HttpClientErrorException(httpStatus);
Expand All @@ -73,7 +74,7 @@ public void givenHttpClientErrorResponseCode200_whenHandleDocumentStorageAndPDFG
}

@Test
public void givenWrappedInExceptionIsNull_whenHandleDocumentStorageAndPDFGenerationException_thenReturnInternalServerError() {
void givenWrappedInExceptionIsNull_whenHandleDocumentStorageAndPDFGenerationException_thenReturnInternalServerError() {
final String message = "some message";

PDFGenerationException pdfGenerationException = new PDFGenerationException(message, null);
Expand All @@ -86,7 +87,7 @@ public void givenWrappedInExceptionIsNull_whenHandleDocumentStorageAndPDFGenerat
}

@Test
public void givenWrappedInIsNotHttpClientErrorException_whenHandleDocumentStorageAndPDFGenerationException_thenReturnInternalServerError() {
void givenWrappedInIsNotHttpClientErrorException_whenHandleDocumentStorageAndPDFGenerationException_thenReturnInternalServerError() {
final String message = "some message";
final Exception exception = new Exception();

Expand Down
Loading