Skip to content

Commit

Permalink
Merge pull request #8 from pagopa/fix-to-reviewed-status-limit
Browse files Browse the repository at this point in the history
[fix] Improved toReviewed API adding receiptError status limit
  • Loading branch information
pasqualespica authored Nov 30, 2023
2 parents 84acc79 + e91c77e commit ad52825
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public HttpResponseMessage run (
ReceiptToReviewedRequest receiptSupportRequest = request.getBody().isPresent() ? request.getBody().get() : new ReceiptToReviewedRequest();

if (receiptSupportRequest.getEventId() != null) {

ReceiptError receiptError = receiptCosmosClient.getReceiptError(
receiptSupportRequest.getEventId());

if(!receiptError.getStatus().equals(ReceiptErrorStatusType.TO_REVIEW)){
String msg = String.format("Found receiptError with invalid status %s", receiptError.getStatus());
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(msg).build();
}
receiptError.setStatus(ReceiptErrorStatusType.REVIEWED);
receiptList.add(receiptError);
} else {
Expand All @@ -85,16 +89,18 @@ public HttpResponseMessage run (
} while (continuationToken != null);
}

if(!receiptList.isEmpty()){
documentdb.setValue(receiptList);
if (receiptList.isEmpty()) {
return request.createResponseBuilder(HttpStatus.OK).body("No documents restored").build();
}

return request.createResponseBuilder(HttpStatus.OK)
.body("OK")
.build();
documentdb.setValue(receiptList);

String msg = String.format("Changed status of %s documents with success", receiptList.size());
return request.createResponseBuilder(HttpStatus.OK).body(msg).build();

} catch (NoSuchElementException | ReceiptNotFoundException e) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
return request.createResponseBuilder(HttpStatus.NOT_FOUND)
.body("No receiptError has been found")
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand All @@ -34,46 +35,46 @@ class ReceiptToReviewedTest {

private ReceiptToReviewed function;

@Mock
private ReceiptCosmosClient receiptCosmosClient;
@Mock
private ReceiptCosmosClient receiptCosmosClient;

@Captor
private ArgumentCaptor<List<ReceiptError>> receiptErrorCaptor;

@Test
void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFoundException {
ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest();
receiptToReviewedRequest.setEventId(BIZ_EVENT_ID);
HttpRequestMessage<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest));
@Test
void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFoundException {
ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest();
receiptToReviewedRequest.setEventId(BIZ_EVENT_ID);
HttpRequestMessage<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest));

doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}).when(request).createResponseBuilder(any(HttpStatus.class));
doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}).when(request).createResponseBuilder(any(HttpStatus.class));

ReceiptError receiptError = ReceiptError.builder()
.bizEventId(BIZ_EVENT_ID)
.status(ReceiptErrorStatusType.TO_REVIEW)
.build();
when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenReturn(receiptError);
ReceiptError receiptError = ReceiptError.builder()
.bizEventId(BIZ_EVENT_ID)
.status(ReceiptErrorStatusType.TO_REVIEW)
.build();
when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenReturn(receiptError);


function = new ReceiptToReviewed(receiptCosmosClient);
function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);
@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);

// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb)));
assertEquals(HttpStatus.OK , responseMessage.get().getStatus());
// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb)));
assertEquals(HttpStatus.OK, responseMessage.get().getStatus());

verify(documentdb).setValue(receiptErrorCaptor.capture());
ReceiptError captured = receiptErrorCaptor.getValue().get(0);
assertEquals(BIZ_EVENT_ID, captured.getBizEventId());
assertEquals(ReceiptErrorStatusType.REVIEWED, captured.getStatus());
}
verify(documentdb).setValue(receiptErrorCaptor.capture());
ReceiptError captured = receiptErrorCaptor.getValue().get(0);
assertEquals(BIZ_EVENT_ID, captured.getBizEventId());
assertEquals(ReceiptErrorStatusType.REVIEWED, captured.getStatus());
}

@Test
void requestWithoutBizEventIdSaveMultipleReceiptErrorInReviewed() {
Expand All @@ -98,7 +99,7 @@ void requestWithoutBizEventIdSaveMultipleReceiptErrorInReviewed() {
.bizEventId("2")
.status(ReceiptErrorStatusType.TO_REVIEW)
.build();
List<ReceiptError> listReceipt = List.of(receiptError1,receiptError2);
List<ReceiptError> listReceipt = List.of(receiptError1, receiptError2);
when(feedResponse.getResults()).thenReturn(listReceipt);
Iterable<FeedResponse<ReceiptError>> feedResponseIterator = List.of(feedResponse);

Expand All @@ -112,7 +113,7 @@ void requestWithoutBizEventIdSaveMultipleReceiptErrorInReviewed() {
// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb)));
assertEquals(HttpStatus.OK , responseMessage.get().getStatus());
assertEquals(HttpStatus.OK, responseMessage.get().getStatus());

verify(documentdb).setValue(receiptErrorCaptor.capture());

Expand Down Expand Up @@ -146,7 +147,7 @@ void requestWithoutRequestBodySaveMultipleReceiptErrorInReviewed() {
.bizEventId("2")
.status(ReceiptErrorStatusType.TO_REVIEW)
.build();
List<ReceiptError> listReceipt = List.of(receiptError1,receiptError2);
List<ReceiptError> listReceipt = List.of(receiptError1, receiptError2);
when(feedResponse.getResults()).thenReturn(listReceipt);
Iterable<FeedResponse<ReceiptError>> feedResponseIterator = List.of(feedResponse);

Expand All @@ -160,7 +161,7 @@ void requestWithoutRequestBodySaveMultipleReceiptErrorInReviewed() {
// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb)));
assertEquals(HttpStatus.OK , responseMessage.get().getStatus());
assertEquals(HttpStatus.OK, responseMessage.get().getStatus());

verify(documentdb).setValue(receiptErrorCaptor.capture());

Expand Down Expand Up @@ -195,7 +196,37 @@ void requestWithValidBizEventIdButReceiptNotFound() throws ReceiptNotFoundExcept
// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb)));
assertEquals(HttpStatus.BAD_REQUEST , responseMessage.get().getStatus());
assertEquals(HttpStatus.NOT_FOUND, responseMessage.get().getStatus());

verifyNoInteractions(documentdb);
}

@Test
void requestWithValidBizEventIdButReceiptWrongStatus() throws ReceiptNotFoundException {
ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest();
receiptToReviewedRequest.setEventId(BIZ_EVENT_ID);
HttpRequestMessage<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest));

doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}).when(request).createResponseBuilder(any(HttpStatus.class));

when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenReturn(ReceiptError.builder()
.bizEventId(BIZ_EVENT_ID)
.status(ReceiptErrorStatusType.REQUEUED)
.build());

function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);

// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb)));
assertEquals(HttpStatus.BAD_REQUEST, responseMessage.get().getStatus());

verifyNoInteractions(documentdb);
}
Expand Down Expand Up @@ -227,7 +258,7 @@ void requestWithoutRequestBodyDoesNotFindAnyReceiptError() {
// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb)));
assertEquals(HttpStatus.OK , responseMessage.get().getStatus());
assertEquals(HttpStatus.OK, responseMessage.get().getStatus());

verifyNoInteractions(documentdb);
}
Expand Down

0 comments on commit ad52825

Please sign in to comment.