Skip to content

Commit

Permalink
[SELC-3493] feat: Reject Endpoint for Enhanced Onboarding Process App…
Browse files Browse the repository at this point in the history
…roval (#96)

* rename method service

* unit test

* some comments

* unit test
  • Loading branch information
manuraf authored Jan 19, 2024
1 parent 14f0f4e commit e6cf5de
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ public Uni<OnboardingGetResponse> getOnboardingWithFilter(@QueryParam(value = "p
return onboardingService.onboardingGet(productId, taxCode, status, from, to, page, size);
}

@Operation(summary = "Perform reject operation of an onboarding request receiving onboarding id." +
"Function change status to REJECT for an onboarding request that is not COMPLETED. " )
@PUT
@Path("/{onboardingId}/delete")
@Path("/{onboardingId}/reject")
public Uni<Response> delete(@PathParam(value = "onboardingId") String onboardingId) {
return onboardingService.deleteOnboarding(onboardingId)
return onboardingService.rejectOnboarding(onboardingId)
.map(ignore -> Response
.status(HttpStatus.SC_NO_CONTENT)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface OnboardingService {

Uni<OnboardingGetResponse> onboardingGet(String productId, String taxCode, String status, String from, String to, Integer page, Integer size);

Uni<Long> deleteOnboarding(String onboardingId);
Uni<Long> rejectOnboarding(String onboardingId);

Uni<OnboardingGet> onboardingPending(String onboardingId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,14 @@ private List<OnboardingGet> convertOnboardingListToResponse(List<Onboarding> ite
}

@Override
public Uni<Long> deleteOnboarding(String onboardingId) {
public Uni<Long> rejectOnboarding(String onboardingId) {
return checkOnboardingIdFormat(onboardingId)
.onItem()
.transformToUni(id -> updateStatus(onboardingId, OnboardingStatus.DELETED));
.onItem().transformToUni(ignore -> Onboarding.findById(new ObjectId(onboardingId))
.onItem().transform(onboarding -> (Onboarding) onboarding))
.onItem().transformToUni(onboardingGet -> OnboardingStatus.COMPLETED.equals(onboardingGet.getStatus())
? Uni.createFrom().failure(new InvalidRequestException(String.format("Onboarding with id %s is COMPLETED!", onboardingId)))
: Uni.createFrom().item(onboardingGet))
.onItem().transformToUni(id -> updateStatus(onboardingId, OnboardingStatus.REJECTED));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,19 @@ void complete() throws IOException {
void deleteOK(){
String onboardingId = "actual-onboarding-id";

when(onboardingService.deleteOnboarding(onboardingId))
when(onboardingService.rejectOnboarding(onboardingId))
.thenReturn(Uni.createFrom().item(1L));

given()
.when()
.pathParam("onboardingId", onboardingId)
.put("/{onboardingId}/delete")
.put("/{onboardingId}/reject")
.then()
.statusCode(204);

ArgumentCaptor<String> expectedId = ArgumentCaptor.forClass(String.class);
verify(onboardingService, times(1))
.deleteOnboarding(expectedId.capture());
.rejectOnboarding(expectedId.capture());
assertEquals(expectedId.getValue(), onboardingId);
}

Expand All @@ -253,19 +253,19 @@ void deleteOK(){
void deleteInvalidOnboardingIdOrOnboardingNotFound(){
String onboardingId = "actual-onboarding-id";

when(onboardingService.deleteOnboarding(onboardingId))
when(onboardingService.rejectOnboarding(onboardingId))
.thenThrow(InvalidRequestException.class);

given()
.when()
.pathParam("onboardingId", onboardingId)
.put("/{onboardingId}/delete")
.put("/{onboardingId}/reject")
.then()
.statusCode(400);

ArgumentCaptor<String> expectedId = ArgumentCaptor.forClass(String.class);
verify(onboardingService, times(1))
.deleteOnboarding(expectedId.capture());
.rejectOnboarding(expectedId.capture());
assertEquals(expectedId.getValue(), onboardingId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,15 @@ private Onboarding createDummyOnboarding() {

@Test
void testOnboardingUpdateStatusOK() {
String onboardingId = "655df045dc52ea5f37c80955";
mockUpdateOnboarding(onboardingId, 1L);

Onboarding onboarding = createDummyOnboarding();
PanacheMock.mock(Onboarding.class);
when(Onboarding.findById(onboarding.getId()))
.thenReturn(Uni.createFrom().item(onboarding));

mockUpdateOnboarding(onboarding.getId().toHexString(), 1L);
UniAssertSubscriber<Long> subscriber = onboardingService
.deleteOnboarding(onboardingId)
.rejectOnboarding(onboarding.getId().toHexString())
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

Expand All @@ -1016,29 +1021,51 @@ void testOnboardingUpdateStatusOK() {
void testOnboardingUpdateStatusInvalidOnboardingId() {
String onboardingId = "123456";
UniAssertSubscriber<Long> subscriber = onboardingService
.deleteOnboarding(onboardingId)
.rejectOnboarding(onboardingId)
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

subscriber.assertFailedWith(InvalidRequestException.class, "Given onboardingId [123456] has wrong format");
}

@Test
void rejectOnboarding_statusIsCOMPLETED() {
Onboarding onboarding = createDummyOnboarding();
onboarding.setStatus(OnboardingStatus.COMPLETED);
PanacheMock.mock(Onboarding.class);
when(Onboarding.findById(onboarding.getId()))
.thenReturn(Uni.createFrom().item(onboarding));

mockUpdateOnboarding(onboarding.getId().toHexString(), 1L);
UniAssertSubscriber<Long> subscriber = onboardingService
.rejectOnboarding(onboarding.getId().toHexString())
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

subscriber.assertFailedWith(InvalidRequestException.class);
}

@Test
void testOnboardingDeleteOnboardingNotFoundOrAlreadyDeleted() {
String onboardingId = "655df045dc52ea5f37c80955";
mockUpdateOnboarding(onboardingId, 0L);

Onboarding onboarding = createDummyOnboarding();
PanacheMock.mock(Onboarding.class);
when(Onboarding.findById(onboarding.getId()))
.thenReturn(Uni.createFrom().item(onboarding));
mockUpdateOnboarding(onboarding.getId().toHexString(), 0L);

UniAssertSubscriber<Long> subscriber = onboardingService
.deleteOnboarding(onboardingId)
.rejectOnboarding(onboarding.getId().toHexString())
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

subscriber.assertFailedWith(InvalidRequestException.class, "Onboarding with id 655df045dc52ea5f37c80955 not found or already deleted");
subscriber.assertFailedWith(InvalidRequestException.class);
}

private void mockUpdateOnboarding(String onboardingId, Long updatedItemCount) {
ReactivePanacheUpdate query = mock(ReactivePanacheUpdate.class);
PanacheMock.mock(Onboarding.class);
when(Onboarding.update(Onboarding.Fields.status.name(), OnboardingStatus.DELETED)).thenReturn(query);
when(Onboarding.update(Onboarding.Fields.status.name(), OnboardingStatus.REJECTED)).thenReturn(query);
when(query.where("_id", onboardingId)).thenReturn(Uni.createFrom().item(updatedItemCount));
}

Expand Down

0 comments on commit e6cf5de

Please sign in to comment.