Skip to content

Commit

Permalink
[PRDP-254] fixed massive not notified query param
Browse files Browse the repository at this point in the history
  • Loading branch information
giomella committed Nov 30, 2023
1 parent 678e656 commit 6f1d0a5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.OutputBinding;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.BindingName;
import com.microsoft.azure.functions.annotation.CosmosDBOutput;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
Expand Down Expand Up @@ -56,11 +55,10 @@ public RecoverNotNotifiedReceiptMassive() {
@FunctionName("RecoverNotNotifiedReceiptMassive")
public HttpResponseMessage run(
@HttpTrigger(name = "RecoverNotNotifiedMassiveTrigger",
methods = {HttpMethod.PUT},
route = "/receipts/recover-not-notified?status{status-type}",
methods = {HttpMethod.POST},
route = "/receipts/recover-not-notified",
authLevel = AuthorizationLevel.FUNCTION)
HttpRequestMessage<Optional<String>> request,
@BindingName("status-type") ReceiptStatusType statusType,
@CosmosDBOutput(
name = "ReceiptDatastore",
databaseName = "db",
Expand All @@ -70,7 +68,9 @@ public HttpResponseMessage run(
final ExecutionContext context) {
logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now());

if (statusType == null) {
// Get named parameter
String status = request.getQueryParameters().get("status");
if (status == null) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ProblemJson.builder()
Expand All @@ -81,6 +81,20 @@ public HttpResponseMessage run(
.build();
}

ReceiptStatusType statusType;
try {
statusType = ReceiptStatusType.valueOf(status);
} catch (IllegalArgumentException e) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid status to recover")
.status(HttpStatus.BAD_REQUEST.value())
.build())
.build();
}

if (!statusType.equals(ReceiptStatusType.IO_ERROR_TO_NOTIFY) && !statusType.equals(ReceiptStatusType.GENERATED)) {
String responseMsg = String.format("The requested status to recover %s is not one of the expected status",
statusType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public void releaseMocks() throws Exception {

@Test
void recoverNotNotifiedReceiptMassiveForIOErrorToNotifySuccess() {
when(requestMock.getQueryParameters())
.thenReturn(Collections.singletonMap("status", ReceiptStatusType.IO_ERROR_TO_NOTIFY.name()));

FeedResponse feedResponseMock = mock(FeedResponse.class);
List<Receipt> receiptList = getReceiptList(ReceiptStatusType.IO_ERROR_TO_NOTIFY);
when(feedResponseMock.getResults()).thenReturn(receiptList);
Expand All @@ -88,7 +91,7 @@ void recoverNotNotifiedReceiptMassiveForIOErrorToNotifySuccess() {
}).when(requestMock).createResponseBuilder(any(HttpStatus.class));

// test execution
HttpResponseMessage response = sut.run(requestMock, ReceiptStatusType.IO_ERROR_TO_NOTIFY, documentReceipts, executionContextMock);
HttpResponseMessage response = sut.run(requestMock, documentReceipts, executionContextMock);

// test assertion
assertNotNull(response);
Expand All @@ -114,6 +117,9 @@ void recoverNotNotifiedReceiptMassiveForIOErrorToNotifySuccess() {

@Test
void recoverNotNotifiedReceiptMassiveForGeneratedSuccess() {
when(requestMock.getQueryParameters())
.thenReturn(Collections.singletonMap("status", ReceiptStatusType.GENERATED.name()));

FeedResponse feedResponseMock = mock(FeedResponse.class);
List<Receipt> receiptList = getReceiptList(ReceiptStatusType.GENERATED);
when(feedResponseMock.getResults()).thenReturn(receiptList);
Expand All @@ -126,7 +132,7 @@ void recoverNotNotifiedReceiptMassiveForGeneratedSuccess() {
}).when(requestMock).createResponseBuilder(any(HttpStatus.class));

// test execution
HttpResponseMessage response = sut.run(requestMock, ReceiptStatusType.GENERATED, documentReceipts, executionContextMock);
HttpResponseMessage response = sut.run(requestMock, documentReceipts, executionContextMock);

// test assertion
assertNotNull(response);
Expand All @@ -152,6 +158,9 @@ void recoverNotNotifiedReceiptMassiveForGeneratedSuccess() {

@Test
void recoverNotNotifiedReceiptMassiveSuccessWithNoReceiptUpdated() {
when(requestMock.getQueryParameters())
.thenReturn(Collections.singletonMap("status", ReceiptStatusType.IO_ERROR_TO_NOTIFY.name()));

FeedResponse feedResponseMock = mock(FeedResponse.class);
when(feedResponseMock.getResults()).thenReturn(Collections.emptyList());
when(receiptCosmosClientMock.getNotNotifiedReceiptDocuments(any(), any(), any()))
Expand All @@ -163,7 +172,7 @@ void recoverNotNotifiedReceiptMassiveSuccessWithNoReceiptUpdated() {
}).when(requestMock).createResponseBuilder(any(HttpStatus.class));

// test execution
HttpResponseMessage response = sut.run(requestMock, ReceiptStatusType.IO_ERROR_TO_NOTIFY, documentReceipts, executionContextMock);
HttpResponseMessage response = sut.run(requestMock, documentReceipts, executionContextMock);

// test assertion
assertNotNull(response);
Expand All @@ -175,13 +184,41 @@ void recoverNotNotifiedReceiptMassiveSuccessWithNoReceiptUpdated() {

@Test
void recoverReceiptFailMissingQueryParam() {
when(requestMock.getQueryParameters()).thenReturn(Collections.emptyMap());

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

// test execution
HttpResponseMessage response = sut.run(requestMock, null, documentReceipts, executionContextMock);
HttpResponseMessage response = sut.run(requestMock, documentReceipts, executionContextMock);

// test assertion
assertNotNull(response);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatus());

ProblemJson problemJson = (ProblemJson) response.getBody();
assertNotNull(problemJson);
assertEquals(HttpStatus.BAD_REQUEST.value(), problemJson.getStatus());
assertEquals(HttpStatus.BAD_REQUEST.name(), problemJson.getTitle());
assertNotNull(problemJson.getDetail());

verify(documentReceipts, never()).setValue(receiptCaptor.capture());
}

@Test
void recoverReceiptFailInvalidStatusType() {
when(requestMock.getQueryParameters())
.thenReturn(Collections.singletonMap("status", "INVALID_STATUS"));

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

// test execution
HttpResponseMessage response = sut.run(requestMock, documentReceipts, executionContextMock);

// test assertion
assertNotNull(response);
Expand All @@ -198,13 +235,16 @@ void recoverReceiptFailMissingQueryParam() {

@Test
void recoverReceiptFailInvalidRestoreStatusRequested() {
when(requestMock.getQueryParameters())
.thenReturn(Collections.singletonMap("status", ReceiptStatusType.FAILED.name()));

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

// test execution
HttpResponseMessage response = sut.run(requestMock, ReceiptStatusType.FAILED, documentReceipts, executionContextMock);
HttpResponseMessage response = sut.run(requestMock, documentReceipts, executionContextMock);

// test assertion
assertNotNull(response);
Expand Down

0 comments on commit 6f1d0a5

Please sign in to comment.