Skip to content

Commit

Permalink
feat(BILL-CPC-934) Implement Negative Testing in BillServiceImplTest (#…
Browse files Browse the repository at this point in the history
…580)

JIRA:
https://champlainsaintlambert.atlassian.net/jira/software/c/projects/CPC/boards/15/backlog?selectedIssue=CPC-934
## Context:
This ticket involves adding negative tests to the BillServiceImplTest to
ensure that the service behaves correctly when encountering various
error scenarios.
## Changes

Created 6 negative tests,

-	test_getBillByNonExistentBillId
-	test_updateNonExistentBillId
-	test_deleteNonExistentBillId
-	test_updateBillWithInvalidRequest
-	test_GetBillByNonExistentCustomerId
-	test_CreateBillWithInvalidData


## Before and After UI (Required for UI-impacting PRs)

## Dev notes (Optional)

## Linked pull requests (Optional)
  • Loading branch information
MinseokBUZZ authored Oct 23, 2023
1 parent bda738f commit c1701df
Showing 1 changed file with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,111 @@ public void test_UpdateBill() {
.verifyComplete();
}

@Test
public void test_getBillByNonExistentBillId() {
String nonExistentBillId = "nonExistentId";

when(repo.findByBillId(nonExistentBillId)).thenReturn(Mono.empty());

Mono<BillResponseDTO> billDTOMono = billService.getBillByBillId(nonExistentBillId);

StepVerifier.create(billDTOMono)
.expectNextCount(0)
.verifyComplete();
}

@Test
public void test_updateNonExistentBillId() {
String nonExistentBillId = "nonExistentId";
double updatedAmount = 20.0;
BillRequestDTO updatedBillRequestDTO = buildBillRequestDTO();
updatedBillRequestDTO.setAmount(updatedAmount);
Mono<BillRequestDTO> updatedBillRequestMono = Mono.just(updatedBillRequestDTO);

when(repo.findByBillId(nonExistentBillId)).thenReturn(Mono.empty());

Mono<BillResponseDTO> updatedBillMono = billService.updateBill(nonExistentBillId, updatedBillRequestMono);

StepVerifier.create(updatedBillMono)
.expectNextCount(0)
.verifyComplete();
}


@Test
public void test_deleteNonExistentBillId() {
String nonExistentBillId = "nonExistentId";

when(repo.deleteBillByBillId(nonExistentBillId)).thenReturn(Mono.empty());

Mono<Void> deletedObj = billService.DeleteBill(nonExistentBillId);

StepVerifier.create(deletedObj)
.expectNextCount(0)
.verifyComplete();
}

@Test
public void test_updateBillWithInvalidRequest() {
String billId = "validBillId";
double updatedAmount = -5.0; // Negative amount, which is invalid
BillRequestDTO updatedBillRequestDTO = buildBillRequestDTO();
updatedBillRequestDTO.setAmount(updatedAmount);
Mono<BillRequestDTO> updatedBillRequestMono = Mono.just(updatedBillRequestDTO);

when(repo.findByBillId(billId)).thenReturn(Mono.just(buildBill()));

Mono<BillResponseDTO> updatedBillMono = billService.updateBill(billId, updatedBillRequestMono);

StepVerifier.create(updatedBillMono)
.expectError()
.verify();
}


@Test
public void test_GetBillByNonExistentCustomerId() {
String nonExistentCustomerId = "nonExistentId";


when(repo.findByCustomerId(nonExistentCustomerId)).thenReturn(Flux.empty());

Flux<BillResponseDTO> billDTOMono = billService.GetBillsByCustomerId(nonExistentCustomerId);

StepVerifier.create(billDTOMono)
.expectNextCount(0)
.verifyComplete();
}

@Test
public void test_CreateBillWithInvalidData() {
BillRequestDTO billDTO = buildInvalidBillRequestDTO(); // Create a BillRequestDTO with invalid data

Mono<BillRequestDTO> billRequestMono = Mono.just(billDTO);

when(repo.insert(any(Bill.class))).thenReturn(Mono.error(new RuntimeException("Invalid data")));

Mono<BillResponseDTO> returnedBill = billService.CreateBill(billRequestMono);

StepVerifier.create(returnedBill)
.expectError()
.verify();
}

private BillRequestDTO buildInvalidBillRequestDTO() {
LocalDate date = LocalDate.now();

return BillRequestDTO.builder()
.customerId("1")
.vetId("2")
.visitType("") // Empty visitType, which is considered invalid
.date(date)
.amount(100.0)
.billStatus(BillStatus.PAID)
.dueDate(date)
.build();
}

private Bill buildBill(){

Calendar calendar = Calendar.getInstance();
Expand Down

0 comments on commit c1701df

Please sign in to comment.