Skip to content

Commit

Permalink
[PRDP-291] removed duplicated code and fixed cart container name env
Browse files Browse the repository at this point in the history
  • Loading branch information
giomella committed Jan 10, 2024
1 parent 0ed89e4 commit dc9db0c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.FeedResponse;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;

public interface CartReceiptsCosmosClient {

CartForReceipt getCartItem(String eventId) throws CartNotFoundException;
/**
* Retrieve the cart with the provided id from Cosmos
*
* @param cartId the cart id
* @return the cart
* @throws CartNotFoundException if no cart was found in the container
*/
CartForReceipt getCartItem(String cartId) throws CartNotFoundException;

CosmosItemResponse<CartForReceipt> saveCart(CartForReceipt receipt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.FeedResponse;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.IoMessageNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;

Expand Down Expand Up @@ -58,13 +56,4 @@ public interface ReceiptCosmosClient {
Iterable<FeedResponse<Receipt>> getIOErrorToNotifyReceiptDocuments(String continuationToken, Integer pageSize);

IOMessage getIoMessage(String messageId) throws IoMessageNotFoundException;

/**
* Retrieve the cart with the provided id from Cosmos
*
* @param cartId the cart id
* @return the cart
* @throws CartNotFoundException if no cart was found in the container
*/
CartForReceipt getCartDocument(String cartId) throws CartNotFoundException;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package it.gov.pagopa.receipt.pdf.helpdesk.client.impl;

import com.azure.cosmos.*;
import com.azure.cosmos.ConsistencyLevel;
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.FeedResponse;
Expand All @@ -10,7 +14,6 @@
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
Expand All @@ -19,13 +22,12 @@ public class CartReceiptsCosmosClientImpl implements CartReceiptsCosmosClient {

private static CartReceiptsCosmosClientImpl instance;
private final String databaseId = System.getenv("COSMOS_RECEIPT_DB_NAME");
private final String cartForReceiptContainerName = System.getenv("CART_FOR_RECEIPT_CONTAINER_NAME");
private final String cartForReceiptContainerName = System.getenv("COSMOS_RECEIPT_CART_CONTAINER_NAME");

private final String millisDiff = System.getenv("MAX_DATE_DIFF_CART_MILLIS");

private final String numDaysCartNotSent = System.getenv().getOrDefault("RECOVER_CART_MASSIVE_MAX_DAYS", "0");


private final CosmosClient cosmosClient;

private CartReceiptsCosmosClientImpl() {
Expand All @@ -52,31 +54,24 @@ public static CartReceiptsCosmosClientImpl getInstance() {
}

/**
* Retrieve receipt document from CosmosDB database
*
* @param eventId Biz-event id
* @return receipt document
* @throws ReceiptNotFoundException in case no receipt has been found with the given idEvent
* {@inheritDoc}
*/
@Override
public CartForReceipt getCartItem(String eventId) throws CartNotFoundException {
public CartForReceipt getCartItem(String cartId) throws CartNotFoundException {
CosmosDatabase cosmosDatabase = this.cosmosClient.getDatabase(databaseId);

CosmosContainer cosmosContainer = cosmosDatabase.getContainer(cartForReceiptContainerName);

//Build query
String query = "SELECT * FROM c WHERE c.id = " + "'" + eventId + "'";
String query = String.format("SELECT * FROM c WHERE c.id = '%s'", cartId);

//Query the container
CosmosPagedIterable<CartForReceipt> queryResponse = cosmosContainer
.queryItems(query, new CosmosQueryRequestOptions(), CartForReceipt.class);

if (queryResponse.iterator().hasNext()) {
return queryResponse.iterator().next();
} else {
throw new CartNotFoundException("Document not found in the defined container");
}

throw new CartNotFoundException("Document not found in the defined container");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import com.azure.cosmos.models.FeedResponse;
import com.azure.cosmos.util.CosmosPagedIterable;
import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptErrorStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.IoMessageNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;

Expand All @@ -31,7 +29,6 @@ public class ReceiptCosmosClientImpl implements ReceiptCosmosClient {

private final String databaseId = System.getenv().getOrDefault("COSMOS_RECEIPT_DB_NAME", "db");
private final String containerId = System.getenv().getOrDefault("COSMOS_RECEIPT_CONTAINER_NAME", "receipt");
private final String containerCartId = System.getenv().getOrDefault("COSMOS_RECEIPT_CART_CONTAINER_NAME", "cart-for-receipts");
private final String containerMessageId = System.getenv().getOrDefault("COSMOS_RECEIPT_MESSAGE_CONTAINER_NAME", "receipts-io-messages");
private final String containerReceiptErrorId = System.getenv().getOrDefault("COSMOS_RECEIPT_ERROR_CONTAINER_NAME", "receipts-message-errors");

Expand Down Expand Up @@ -264,21 +261,4 @@ public IOMessage getIoMessage(String messageId) throws IoMessageNotFoundExceptio
throw new IoMessageNotFoundException(DOCUMENT_NOT_FOUND_ERR_MSG);
}

@Override
public CartForReceipt getCartDocument(String cartId) throws CartNotFoundException {
CosmosDatabase cosmosDatabase = this.cosmosClient.getDatabase(databaseId);
CosmosContainer cosmosContainer = cosmosDatabase.getContainer(containerCartId);

//Build query
String query = String.format("SELECT * FROM c WHERE c.id = '%s'", cartId);

//Query the container
CosmosPagedIterable<CartForReceipt> queryResponse = cosmosContainer
.queryItems(query, new CosmosQueryRequestOptions(), CartForReceipt.class);

if (queryResponse.iterator().hasNext()) {
return queryResponse.iterator().next();
}
throw new CartNotFoundException(DOCUMENT_NOT_FOUND_ERR_MSG);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package it.gov.pagopa.receipt.pdf.helpdesk.service.impl;

import com.azure.cosmos.models.FeedResponse;
import it.gov.pagopa.receipt.pdf.helpdesk.client.CartReceiptsCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.CartReceiptsCosmosClientImpl;
import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.ReceiptCosmosClientImpl;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage;
Expand All @@ -15,13 +17,16 @@
public class ReceiptCosmosServiceImpl implements ReceiptCosmosService {

private final ReceiptCosmosClient receiptCosmosClient;
private final CartReceiptsCosmosClient cartReceiptsCosmosClient;

public ReceiptCosmosServiceImpl() {
this.receiptCosmosClient = ReceiptCosmosClientImpl.getInstance();
this.cartReceiptsCosmosClient = CartReceiptsCosmosClientImpl.getInstance();
}

ReceiptCosmosServiceImpl(ReceiptCosmosClient receiptCosmosClient) {
ReceiptCosmosServiceImpl(ReceiptCosmosClient receiptCosmosClient, CartReceiptsCosmosClient cartReceiptsCosmosClient) {
this.receiptCosmosClient = receiptCosmosClient;
this.cartReceiptsCosmosClient = cartReceiptsCosmosClient;
}

/**
Expand Down Expand Up @@ -109,7 +114,7 @@ public IOMessage getReceiptMessage(String messageId) throws IoMessageNotFoundExc
public CartForReceipt getCart(String cartId) throws CartNotFoundException {
CartForReceipt cartForReceipt;
try {
cartForReceipt = this.receiptCosmosClient.getCartDocument(cartId);
cartForReceipt = this.cartReceiptsCosmosClient.getCartItem(cartId);
} catch (CartNotFoundException e) {
String errorMsg = String.format("Receipt not found with the biz-event id %s", cartId);
throw new CartNotFoundException(errorMsg, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.util.CosmosPagedIterable;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.IoMessageNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,7 +26,6 @@
class ReceiptCosmosClientImplTest {

private static final String RECEIPT_ID = "a valid receipt id";
private static final String CART_ID = "a valid cart id";

private CosmosClient mockClient;

Expand Down Expand Up @@ -245,52 +242,4 @@ void getIoMessageReceiptDocumentFail() {

assertThrows(IoMessageNotFoundException.class, () -> client.getIoMessage("an invalid receipt id"));
}


@Test
void getCartDocumentSuccess() {
CosmosDatabase mockDatabase = mock(CosmosDatabase.class);
CosmosContainer mockContainer = mock(CosmosContainer.class);

CosmosPagedIterable mockIterable = mock(CosmosPagedIterable.class);

Iterator<CartForReceipt> mockIterator = mock(Iterator.class);
CartForReceipt cart = new CartForReceipt();
cart.setId(CART_ID);

when(mockIterator.hasNext()).thenReturn(true);
when(mockIterator.next()).thenReturn(cart);

when(mockIterable.iterator()).thenReturn(mockIterator);

when(mockContainer.queryItems(anyString(), any(), eq(CartForReceipt.class)))
.thenReturn(mockIterable);
when(mockDatabase.getContainer(any())).thenReturn(mockContainer);
when(mockClient.getDatabase(any())).thenReturn(mockDatabase);

CartForReceipt cartForReceipt = assertDoesNotThrow(() -> client.getCartDocument(CART_ID));

assertEquals(CART_ID, cartForReceipt.getId());
}

@Test
void getCartDocumentFail() {
CosmosDatabase mockDatabase = mock(CosmosDatabase.class);
CosmosContainer mockContainer = mock(CosmosContainer.class);

CosmosPagedIterable mockIterable = mock(CosmosPagedIterable.class);

Iterator<Receipt> mockIterator = mock(Iterator.class);

when(mockIterator.hasNext()).thenReturn(false);

when(mockIterable.iterator()).thenReturn(mockIterator);

when(mockContainer.queryItems(anyString(), any(), eq(CartForReceipt.class)))
.thenReturn(mockIterable);
when(mockDatabase.getContainer(any())).thenReturn(mockContainer);
when(mockClient.getDatabase(any())).thenReturn(mockDatabase);

assertThrows(CartNotFoundException.class, () -> client.getCartDocument("an invalid receipt id"));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.gov.pagopa.receipt.pdf.helpdesk.service.impl;

import com.azure.cosmos.models.FeedResponse;
import it.gov.pagopa.receipt.pdf.helpdesk.client.CartReceiptsCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage;
Expand Down Expand Up @@ -29,14 +30,16 @@
class ReceiptCosmosServiceImplTest {

private ReceiptCosmosClient receiptCosmosClientMock;
private CartReceiptsCosmosClient cartReceiptsCosmosClientMock;

private ReceiptCosmosService sut;

@BeforeEach
void setUp() {
receiptCosmosClientMock = mock(ReceiptCosmosClient.class);
cartReceiptsCosmosClientMock = mock(CartReceiptsCosmosClient.class);

sut = spy(new ReceiptCosmosServiceImpl(receiptCosmosClientMock));
sut = spy(new ReceiptCosmosServiceImpl(receiptCosmosClientMock, cartReceiptsCosmosClientMock));
}

@Test
Expand Down Expand Up @@ -204,7 +207,7 @@ void getReceiptMessageFailClientReturnNull() throws IoMessageNotFoundException {

@Test
void getCartSuccess() throws CartNotFoundException {
when(receiptCosmosClientMock.getCartDocument(anyString())).thenReturn(new CartForReceipt());
when(cartReceiptsCosmosClientMock.getCartItem(anyString())).thenReturn(new CartForReceipt());

CartForReceipt cart = assertDoesNotThrow(() -> sut.getCart(anyString()));

Expand All @@ -213,14 +216,14 @@ void getCartSuccess() throws CartNotFoundException {

@Test
void getCartFailClientThrowsCartNotFoundException() throws CartNotFoundException {
when(receiptCosmosClientMock.getCartDocument(anyString())).thenThrow(CartNotFoundException.class);
when(cartReceiptsCosmosClientMock.getCartItem(anyString())).thenThrow(CartNotFoundException.class);

assertThrows(CartNotFoundException.class, () -> sut.getCart(anyString()));
}

@Test
void getCartFailClientReturnNull() throws CartNotFoundException {
when(receiptCosmosClientMock.getCartDocument(anyString())).thenReturn(null);
when(cartReceiptsCosmosClientMock.getCartItem(anyString())).thenReturn(null);

assertThrows(CartNotFoundException.class, () -> sut.getCart(anyString()));
}
Expand Down

0 comments on commit dc9db0c

Please sign in to comment.