Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BILL-CPC-934) Implement Negative Testing in BillServiceImplTest #580

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading