Skip to content

Commit

Permalink
[PagoPa-1838] update get pdf receipt (#72)
Browse files Browse the repository at this point in the history
* [PAGOPA-1838] pdf receipt: FIRST impl

* [PAGOPA-1838] pdf receipt: updated junit test

---------

Co-authored-by: aacitelli <aacitelli@PDD-NB-0205.dgsgroup.it>
  • Loading branch information
alessio-acitelli and aacitelli authored Jun 18, 2024
1 parent c303396 commit 0d12a66
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import it.gov.pagopa.bizeventsservice.client.IReceiptGeneratePDFClient;
import it.gov.pagopa.bizeventsservice.client.IReceiptGetPDFClient;
import it.gov.pagopa.bizeventsservice.controller.ITransactionController;
import it.gov.pagopa.bizeventsservice.exception.AppException;
import it.gov.pagopa.bizeventsservice.model.response.AttachmentsDetailsResponse;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionDetailResponse;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListResponse;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListWrapResponse;
Expand All @@ -16,8 +14,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import feign.FeignException;

import javax.validation.constraints.NotBlank;

/**
Expand All @@ -29,16 +25,12 @@ public class TransactionController implements ITransactionController {

private final ITransactionService transactionService;
private final IBizEventsService bizEventsService;
private final IReceiptGetPDFClient receiptClient;
private final IReceiptGeneratePDFClient generateReceiptClient;

@Autowired
public TransactionController(ITransactionService transactionService, IBizEventsService bizEventsService,
IReceiptGetPDFClient receiptClient, IReceiptGeneratePDFClient generateReceiptClient) {
this.transactionService = transactionService;
this.bizEventsService = bizEventsService;
this.receiptClient = receiptClient;
this.generateReceiptClient = generateReceiptClient;
}

@Override
Expand Down Expand Up @@ -77,44 +69,14 @@ public ResponseEntity<Void> disableTransaction(String fiscalCode, String transac

@Override
public ResponseEntity<byte[]> getPDFReceipt(@NotBlank String fiscalCode, @NotBlank String eventId) {
return acquirePDFReceipt(fiscalCode, eventId);
}

private ResponseEntity<byte[]> acquirePDFReceipt(String fiscalCode, String eventId) {
// to check if is an OLD event present only on the PM --> the receipt is not available for events present exclusively on the PM
bizEventsService.getBizEvent(eventId);
String url;
try {
// call the receipt-pdf-service to retrieve the PDF receipt details
AttachmentsDetailsResponse response = receiptClient.getAttachments(fiscalCode, eventId);
url = response.getAttachments().get(0).getUrl();
} catch (FeignException.NotFound e) {
throw new AppException(HttpStatus.NOT_FOUND, "Receipt Not Found", e.getMessage());
}
return this.getAttachment(fiscalCode, eventId, url);
}

private ResponseEntity<byte[]> getAttachment(String fiscalCode, String eventId, String url) {
byte[] receiptFile = {};
try {
// call the receipt-pdf-service to retrieve the PDF receipt attachment
receiptFile = receiptClient.getReceipt(fiscalCode, eventId, url);
return ResponseEntity
.ok()
.contentLength(receiptFile.length)
.contentType(MediaType.APPLICATION_PDF)
.header("content-disposition", "filename=receipt")
.body(receiptFile);
} catch (FeignException.NotFound e) {
// re-generate the PDF receipt and return the generated file by getReceipt call
generateReceiptClient.generateReceipt(eventId, "false", "{}");
receiptFile = receiptClient.getReceipt(fiscalCode, eventId, url);
return ResponseEntity
.ok()
.contentLength(receiptFile.length)
.contentType(MediaType.APPLICATION_PDF)
.header("content-disposition", "filename=receipt")
.body(receiptFile);
}
bizEventsService.getBizEvent(eventId);
byte[] receiptFile = transactionService.getPDFReceipt(fiscalCode, eventId);
return ResponseEntity
.ok()
.contentLength(receiptFile.length)
.contentType(MediaType.APPLICATION_PDF)
.header("content-disposition", "filename=receipt")
.body(receiptFile);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package it.gov.pagopa.bizeventsservice.model.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

@Getter
@Builder
@SuperBuilder
@Jacksonized
public class Attachment {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.gov.pagopa.bizeventsservice.model.response;

import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

import java.util.List;
Expand All @@ -10,7 +10,7 @@
* Model class for the attachment details response
*/
@Getter
@Builder
@SuperBuilder
@Jacksonized
public class AttachmentsDetailsResponse {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package it.gov.pagopa.bizeventsservice.model.response.enumeration;

public enum StatusType {
NA, RETRY, FAILED, DONE
NA, RETRY, FAILED, DONE, INGESTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface ITransactionService {
TransactionListResponse getTransactionList(String fiscalCode, String continuationToken, Integer size);
TransactionListResponse getCachedTransactionList(String fiscalCode, Integer page, Integer size);
TransactionDetailResponse getTransactionDetails(String fiscalCode, String transactionId);
byte[] getPDFReceipt (String fiscalCode, String eventId);

void disableTransaction(String fiscalCode, String transactionId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package it.gov.pagopa.bizeventsservice.service.impl;

import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;

import feign.FeignException;
import it.gov.pagopa.bizeventsservice.client.IReceiptGeneratePDFClient;
import it.gov.pagopa.bizeventsservice.client.IReceiptGetPDFClient;
import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewCart;
import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewGeneral;
import it.gov.pagopa.bizeventsservice.entity.view.BizEventsViewUser;
import it.gov.pagopa.bizeventsservice.exception.AppError;
import it.gov.pagopa.bizeventsservice.exception.AppException;
import it.gov.pagopa.bizeventsservice.mapper.ConvertViewsToTransactionDetailResponse;
import it.gov.pagopa.bizeventsservice.model.PageInfo;
import it.gov.pagopa.bizeventsservice.model.response.AttachmentsDetailsResponse;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionDetailResponse;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListItem;
import it.gov.pagopa.bizeventsservice.model.response.transaction.TransactionListResponse;
Expand Down Expand Up @@ -41,17 +46,26 @@ public class TransactionService implements ITransactionService {
private final BizEventsViewCartRepository bizEventsViewCartRepository;
private final BizEventsViewUserRepository bizEventsViewUserRepository;
private final RedisRepository redisRepository;
private final IReceiptGetPDFClient receiptClient;
private final IReceiptGeneratePDFClient generateReceiptClient;


@Value("${spring.redis.ttl}")
private long redisTTL;

@Autowired
public TransactionService(BizEventsViewGeneralRepository bizEventsViewGeneralRepository, BizEventsViewCartRepository bizEventsViewCartRepository,
BizEventsViewUserRepository bizEventsViewUserRepository, RedisRepository redisRepository) {
public TransactionService(BizEventsViewGeneralRepository bizEventsViewGeneralRepository,
BizEventsViewCartRepository bizEventsViewCartRepository,
BizEventsViewUserRepository bizEventsViewUserRepository,
RedisRepository redisRepository,
IReceiptGetPDFClient receiptClient,
IReceiptGeneratePDFClient generateReceiptClient) {
this.bizEventsViewGeneralRepository = bizEventsViewGeneralRepository;
this.bizEventsViewCartRepository = bizEventsViewCartRepository;
this.bizEventsViewUserRepository = bizEventsViewUserRepository;
this.redisRepository = redisRepository;
this.receiptClient = receiptClient;
this.generateReceiptClient = generateReceiptClient;
}

@Override
Expand Down Expand Up @@ -174,7 +188,33 @@ private List<List<BizEventsViewUser>> retrievePaginatedList (String taxCode, Int
}
return pagedListOfViewUser;
}



@Override
public byte[] getPDFReceipt(String fiscalCode, String eventId) {
return this.acquirePDFReceipt(fiscalCode, eventId);
}

private byte[] acquirePDFReceipt(String fiscalCode, String eventId) {
String url = "";
try {
// call the receipt-pdf-service to retrieve the PDF receipt details
AttachmentsDetailsResponse response = receiptClient.getAttachments(fiscalCode, eventId);
url = response.getAttachments().get(0).getUrl();
} catch (FeignException.NotFound e) {
generateReceiptClient.generateReceipt(eventId, "false", "{}");
url = receiptClient.getAttachments(fiscalCode, eventId).getAttachments().get(0).getUrl();
}
return this.getAttachment(fiscalCode, eventId, url);
}

private byte[] getAttachment(String fiscalCode, String eventId, String url) {
try {
// call the receipt-pdf-service to retrieve the PDF receipt attachment
return receiptClient.getReceipt(fiscalCode, eventId, url);
} catch (FeignException.NotFound e) {
// re-generate the PDF receipt and return the generated file by getReceipt call
generateReceiptClient.generateReceipt(eventId, "false", "{}");
return receiptClient.getReceipt(fiscalCode, eventId, url);
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ spring.redis.pwd=${REDIS_PWD}
# 5 min
spring.redis.ttl=5

# timeout
feign.client.config.default.connect-timeout=30000
feign.client.config.default.read-timeout=30000

springdoc.group-configs[0].group=all
springdoc.group-configs[0].displayName=Biz-Events All
springdoc.group-configs[0].paths-to-match=/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import feign.FeignException;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -86,6 +84,7 @@ void setUp() throws IOException {
when(transactionService.getTransactionList(eq(VALID_FISCAL_CODE), anyString(), anyInt())).thenReturn(transactionListResponse);
when(transactionService.getCachedTransactionList(eq(VALID_FISCAL_CODE), anyInt(), anyInt())).thenReturn(transactionListResponse);
when(transactionService.getTransactionDetails(anyString(), anyString())).thenReturn(transactionDetailResponse);
when(transactionService.getPDFReceipt(anyString(), anyString())).thenReturn(receipt);
Attachment attachmentDetail = mock (Attachment.class);
AttachmentsDetailsResponse attachments = AttachmentsDetailsResponse.builder().attachments(Arrays.asList(attachmentDetail)).build();
when(receiptClient.getAttachments(anyString(), anyString())).thenReturn(attachments);
Expand Down Expand Up @@ -218,7 +217,9 @@ void getPDFReceipt_ShouldReturnOK() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_PDF))
.andReturn();

assertEquals(receipt.length, result.getResponse().getContentAsByteArray().length);
verify(bizEventsService).getBizEvent("event-id");
verify(transactionService).getPDFReceipt(VALID_FISCAL_CODE,"event-id");
assertEquals(receipt.length, result.getResponse().getContentAsByteArray().length);
}

@Test
Expand All @@ -232,54 +233,7 @@ void getPDFReceiptForOldPMEvent_ShouldReturnNOTFOUND() throws Exception {
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
}

@Test
void getPDFReceiptForMissingEventId_ShouldReturnNOTFOUND() throws Exception {

BizEvent bizEvent = mock (BizEvent.class);
when (bizEventsService.getBizEvent(anyString())).thenReturn(bizEvent);
when(receiptClient.getAttachments(anyString(), eq("missing-id"))).thenThrow(FeignException.NotFound.class);

mvc.perform(get("/transactions/missing-id/pdf")
.header(FISCAL_CODE_HEADER_KEY, VALID_FISCAL_CODE)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
verify(bizEventsService).getBizEvent("event-id");
}

@Test
void getPDFReceiptForUnhandledException_ShouldReturnKO() throws Exception {

BizEvent bizEvent = mock (BizEvent.class);
when (bizEventsService.getBizEvent(anyString())).thenReturn(bizEvent);
// Override @BeforeEach condition
when(receiptClient.getAttachments(anyString(), eq("event-id"))).thenThrow(FeignException.BadRequest.class);

mvc.perform(get(TRANSACTION_RECEIPT_PATH)
.header(FISCAL_CODE_HEADER_KEY, INVALID_FISCAL_CODE)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isInternalServerError())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
}

@Test
void getPDFReceiptForMissingPDFFile_ShouldReturnOK() throws Exception {

BizEvent bizEvent = mock (BizEvent.class);
when (bizEventsService.getBizEvent(anyString())).thenReturn(bizEvent);
when(receiptClient.getReceipt(anyString(), eq("missing-pdf-file"), any())).thenThrow(FeignException.NotFound.class).thenReturn(receipt);

MvcResult result = mvc.perform(get("/transactions/missing-pdf-file/pdf")
.header(FISCAL_CODE_HEADER_KEY, VALID_FISCAL_CODE)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_PDF))
.andReturn();

assertEquals(receipt.length, result.getResponse().getContentAsByteArray().length);
}

}
Loading

0 comments on commit 0d12a66

Please sign in to comment.