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

bug(VIST-CPC-1015): add more coverage to api-gateway tests #590

Merged
Show file tree
Hide file tree
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 @@ -143,6 +143,31 @@ void getVisitsForStatus() throws JsonProcessingException{
.verifyComplete();
}

@Test
void getVisitByPractitionerId() throws JsonProcessingException {
VisitResponseDTO visitResponseDTO = VisitResponseDTO.builder()
.visitId("73b5c112-5703-4fb7-b7bc-ac8186811ae1")
.visitDate(LocalDateTime.parse("2024-11-25 13:45", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.description("this is a dummy description")
.petId("2")
.petName("YourPetNameHere")
.petBirthDate(new Date())
.practitionerId("2")
.vetFirstName("VetFirstNameHere")
.vetLastName("VetLastNameHere")
.vetEmail("[email protected]")
.vetPhoneNumber("123-456-7890")
.status(Status.UPCOMING)
.build();
server.enqueue(new MockResponse().setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.setBody(objectMapper.writeValueAsString(visitResponseDTO)).addHeader("Content-Type", "application/json"));

Flux<VisitResponseDTO> visitResponseDTOFlux = visitsServiceClient.getVisitByPractitionerId("2");
StepVerifier.create(visitResponseDTOFlux)
.expectNext(visitResponseDTO)
.verifyComplete();
}

@Test
void createVisitForPet_Valid() throws JsonProcessingException {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.petclinic.bffapigateway.dtos.Pets.PetTypeResponseDTO;
import com.petclinic.bffapigateway.dtos.Vets.*;
import com.petclinic.bffapigateway.dtos.Visits.Status;
import com.petclinic.bffapigateway.dtos.Visits.VisitRequestDTO;
import com.petclinic.bffapigateway.dtos.Visits.VisitResponseDTO;
import com.petclinic.bffapigateway.exceptions.ExistingVetNotFoundException;
import com.petclinic.bffapigateway.exceptions.GenericHttpException;
Expand Down Expand Up @@ -2215,13 +2216,15 @@ void shouldDeleteBillsByVetId() {
//todo fix
/*@Test
void shouldCreateAVisitWithOwnerInfo(){
String ownerId = "1";
String cookie = "aCookie";
OwnerResponseDTO owner = new OwnerResponseDTO();
VisitRequestDTO visit = VisitRequestDTO.builder()
.visitDate(LocalDateTime.parse("2021-12-12T14:00:00"))
.visitDate(LocalDateTime.parse("2021-12-12T14:00"))
.description("Charle's Richard cat has a paw infection.")
.petId("1")
.practitionerId("1")
.status(false)
.status(Status.UPCOMING)
.build();

VisitResponseDTO visitResponseDTO = VisitResponseDTO.builder()
Expand All @@ -2230,7 +2233,7 @@ void shouldCreateAVisitWithOwnerInfo(){
.petId("1")
.description("Charle's Richard cat has a paw infection.")
.practitionerId("1")
.status(false)
.status(Status.UPCOMING)
.build();


Expand All @@ -2239,23 +2242,107 @@ void shouldCreateAVisitWithOwnerInfo(){


client.post()
.uri("/api/gateway/visit/owners/{ownerId}/pets/{petId}/visits", owner.getOwnerId(), visit.getPetId())
.body(Mono.just(visit), VisitDetails.class)
.uri("/api/gateway/visit/owners/" + ownerId + "/pets/" + visitResponseDTO.getPetId() + "/visits", owner.getOwnerId(), visit.getPetId())
.cookie("Bearer",cookie)
.body(Mono.just(visit), VisitRequestDTO.class)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectStatus().isCreated()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.visitId").isEqualTo(visitResponseDTO.getVisitId())
.jsonPath("$.petId").isEqualTo("1")
.jsonPath("$.visitDate").isEqualTo("2021-12-12T14:00:00")
.jsonPath("$.visitDate").isEqualTo("2021-12-12 14:00")
.jsonPath("$.description").isEqualTo("Charle's Richard cat has a paw infection.")
.jsonPath("$.status").isEqualTo(false)
.jsonPath("$.status").isEqualTo("UPCOMING")
.jsonPath("$.practitionerId").isEqualTo(1);
}*/

@Test
public void addVisit_ShouldReturnCreatedStatus() {
String ownerId = "owner1";
String petId = "pet1";
VisitRequestDTO visit = VisitRequestDTO.builder()
.visitDate(LocalDateTime.parse("2021-12-12T14:00"))
.description("Charle's Richard cat has a paw infection.")
.petId("1")
.practitionerId("1")
.status(Status.UPCOMING)
.build();

VisitResponseDTO visitResponseDTO = VisitResponseDTO.builder()
.visitId(VISIT_ID)
.visitDate(LocalDateTime.parse("2021-12-12T14:00:00"))
.petId("1")
.description("Charle's Richard cat has a paw infection.")
.practitionerId("1")
.status(Status.UPCOMING)
.build();

when(visitsServiceClient.createVisitForPet(any(VisitRequestDTO.class)))
.thenReturn(Mono.just(visitResponseDTO));

client.post()
.uri("/api/gateway/visit/owners/{ownerId}/pets/{petId}/visits", ownerId, petId)
.cookie("Bearer", "your-auth-token") // Assuming "Bearer" is the name of the cookie
.body(Mono.just(visit), VisitRequestDTO.class)
.accept(MediaType.APPLICATION_JSON)
.exchange()
// Validate the response
.expectStatus().isCreated()
.expectBody()
.jsonPath("$.visitId").isEqualTo(visitResponseDTO.getVisitId())
.jsonPath("$.petId").isEqualTo("1")
.jsonPath("$.visitDate").isEqualTo("2021-12-12 14:00")
.jsonPath("$.description").isEqualTo("Charle's Richard cat has a paw infection.")
.jsonPath("$.status").isEqualTo("UPCOMING")
.jsonPath("$.practitionerId").isEqualTo(1);
}

}

@Test
void shouldCreateAVisitWithOwnerAndPetInfo(){
String ownerId = "5fe81e29-1f1d-4f9d-b249-8d3e0cc0b7dd";
String petId = "9";
VisitRequestDTO visit = VisitRequestDTO.builder()
.visitDate(LocalDateTime.parse("2021-12-12T14:00:00"))
.description("Charle's Richard cat has a paw infection.")
.petId(petId)
.practitionerId("1")
.status(Status.UPCOMING)
.build();

VisitResponseDTO visitResponseDTO = VisitResponseDTO.builder()
.visitId(VISIT_ID)
.visitDate(LocalDateTime.parse("2021-12-12T14:00:00"))
.petId(petId)
.description("Charle's Richard cat has a paw infection.")
.practitionerId("1")
.status(Status.UPCOMING)
.build();

when(visitsServiceClient.createVisitForPet(visit))
.thenReturn(Mono.just(visitResponseDTO));

client.post()
.uri("/api/gateway/visit/owners/{ownerId}/pets/{petId}/visits", ownerId, petId)
.body(Mono.just(visit), VisitRequestDTO.class)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isCreated()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.visitId").isEqualTo(visitResponseDTO.getVisitId())
.jsonPath("$.petId").isEqualTo(petId)
.jsonPath("$.visitDate").isEqualTo("2021-12-12 14:00")
.jsonPath("$.description").isEqualTo("Charle's Richard cat has a paw infection.")
.jsonPath("$.status").isEqualTo("UPCOMING")
.jsonPath("$.practitionerId").isEqualTo("1");
}



/* @Test
void shouldDeleteAVisit() {
VisitDetails visit = new VisitDetails();
OwnerDetails owner = new OwnerDetails();
Expand Down Expand Up @@ -2551,7 +2638,7 @@ void getSingleVisit_Valid() {
when(visitsServiceClient.getVisitByVisitId(anyString())).thenReturn(Mono.just(visitResponseDTO));

client.get()
.uri("/api/gateway/visits/{visitId}", visitResponseDTO.getVisitId())
.uri("/api/gateway/visits/" + visitResponseDTO.getVisitId())
.exchange()
.expectStatus().isOk()
.expectBody()
Expand All @@ -2561,6 +2648,78 @@ void getSingleVisit_Valid() {
.jsonPath("$.description").isEqualTo(visitResponseDTO.getDescription())
.jsonPath("$.practitionerId").isEqualTo(visitResponseDTO.getPractitionerId());
}
@Test
void getVisitsByStatus_Valid() {
VisitResponseDTO visitResponseDTO = VisitResponseDTO.builder()
.visitId("73b5c112-5703-4fb7-b7bc-ac8186811ae1")
.visitDate(LocalDateTime.parse("2024-11-25 13:45", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.description("this is a dummy description")
.petId("2")
.petName("YourPetNameHere")
.petBirthDate(new Date())
.practitionerId("2")
.vetFirstName("VetFirstNameHere")
.vetLastName("VetLastNameHere")
.vetEmail("[email protected]")
.vetPhoneNumber("123-456-7890")
.status(Status.UPCOMING)
.build();

when(visitsServiceClient.getVisitsForStatus(visitResponseDTO.getStatus().toString())).thenReturn(Flux.just(visitResponseDTO));

client.get()
.uri("/api/gateway/visits/status/{status}", visitResponseDTO.getStatus())
.exchange()
.expectStatus().isOk()
.expectBodyList(VisitResponseDTO.class)
.consumeWith(response -> {
Assertions.assertTrue(response.getResponseBody().size() > 0);
VisitResponseDTO responseBody = response.getResponseBody().get(0);
// Asserting that the values match what's expected
Assertions.assertEquals(visitResponseDTO.getVisitId(), responseBody.getVisitId());
Assertions.assertEquals(visitResponseDTO.getPetId(), responseBody.getPetId());
Assertions.assertEquals(visitResponseDTO.getVisitDate(), responseBody.getVisitDate());
Assertions.assertEquals(visitResponseDTO.getDescription(), responseBody.getDescription());
Assertions.assertEquals(visitResponseDTO.getPractitionerId(), responseBody.getPractitionerId());
});
}

@Test
void getVisitsByPractitionerId_Valid() {
VisitResponseDTO visitResponseDTO = VisitResponseDTO.builder()
.visitId("73b5c112-5703-4fb7-b7bc-ac8186811ae1")
.visitDate(LocalDateTime.parse("2024-11-25 13:45", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.description("this is a dummy description")
.petId("2")
.petName("YourPetNameHere")
.petBirthDate(new Date())
.practitionerId("2")
.vetFirstName("VetFirstNameHere")
.vetLastName("VetLastNameHere")
.vetEmail("[email protected]")
.vetPhoneNumber("123-456-7890")
.status(Status.UPCOMING)
.build();

when(visitsServiceClient.getVisitByPractitionerId(visitResponseDTO.getPractitionerId())).thenReturn(Flux.just(visitResponseDTO));

client.get()
.uri("/api/gateway/visits/vets/{practitionerId}", visitResponseDTO.getPractitionerId())
.exchange()
.expectStatus().isOk()
.expectBodyList(VisitResponseDTO.class)
.consumeWith(response -> {
Assertions.assertTrue(response.getResponseBody().size() > 0);
VisitResponseDTO responseBody = response.getResponseBody().get(0);

Assertions.assertEquals(visitResponseDTO.getVisitId(), responseBody.getVisitId());
Assertions.assertEquals(visitResponseDTO.getPetId(), responseBody.getPetId());
Assertions.assertEquals(visitResponseDTO.getVisitDate(), responseBody.getVisitDate());
Assertions.assertEquals(visitResponseDTO.getDescription(), responseBody.getDescription());
Assertions.assertEquals(visitResponseDTO.getPractitionerId(), responseBody.getPractitionerId());
});
}


// @Test
// void getSingleVisit_Invalid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@
switch (statusString){

case("UPCOMING"):
status = Status.UPCOMING;

Check warning on line 56 in visits-service-new/src/main/java/com/petclinic/visits/visitsservicenew/BusinessLayer/VisitServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused assignment

The value `Status.UPCOMING` assigned to `status` is never used

case("CONFIRMED"):
status = Status.CONFIRMED;

Check warning on line 59 in visits-service-new/src/main/java/com/petclinic/visits/visitsservicenew/BusinessLayer/VisitServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused assignment

The value `Status.CONFIRMED` assigned to `status` is never used

case("CANCELLED"):
status = Status.CANCELLED;

Check warning on line 62 in visits-service-new/src/main/java/com/petclinic/visits/visitsservicenew/BusinessLayer/VisitServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused assignment

The value `Status.CANCELLED` assigned to `status` is never used

default:
status = Status.COMPLETED;

Check warning on line 65 in visits-service-new/src/main/java/com/petclinic/visits/visitsservicenew/BusinessLayer/VisitServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused assignment

The value `Status.COMPLETED` assigned to `status` is never used
}
return repo.findAllByStatus(statusString)
.flatMap(visit -> entityDtoUtil.toVisitResponseDTO(visit));
Expand Down Expand Up @@ -90,9 +90,9 @@
.then(Mono.just(visitRequestDTO))
.doOnNext(s->{

authServiceClient.getUserById(visitRequestDTO.getJwtToken(), visitRequestDTO.getOwnerId()).subscribe(user->mailService.sendMail(generateVisitRequestEmail(user, visitRequestDTO.getPetId(), visitRequestDTO.getVisitDate())));

Check warning on line 93 in visits-service-new/src/main/java/com/petclinic/visits/visitsservicenew/BusinessLayer/VisitServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Calling 'subscribe' in "reactive" methods

Calling 'subscribe' in non-blocking context is not recommended

// Mono<UserDetails> user = getUserById(auth, ownerId);

Check notice on line 95 in visits-service-new/src/main/java/com/petclinic/visits/visitsservicenew/BusinessLayer/VisitServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (4 lines)
// try{
// simpleJavaMailClient.sendMail(emailBuilder("[email protected]"));
// }catch(Exception e){System.out.println("Email failed to send: "+e.getMessage());}
Expand All @@ -100,7 +100,7 @@
)
// .doOnNext(v -> System.out.println("Request Date: " + v.getVisitDate())) // Debugging
.map(visitRequestDTO -> entityDtoUtil.toVisitEntity(visitRequestDTO))
// .doOnNext(x -> x.setVisitId(entityDtoUtil.generateVisitIdString()))
.doOnNext(x -> x.setVisitId(entityDtoUtil.generateVisitIdString()))
// .doOnNext(v -> System.out.println("Entity Date: " + v.getVisitDate())) // Debugging
.flatMap(visit ->
repo.findByVisitDateAndPractitionerId(visit.getVisitDate(), visit.getPractitionerId()) // FindVisits method in repository
Expand Down Expand Up @@ -143,7 +143,7 @@
}


// @Override

Check notice on line 146 in visits-service-new/src/main/java/com/petclinic/visits/visitsservicenew/BusinessLayer/VisitServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (9 lines)
// public Mono<VetDTO> testingGetVetDTO(String vetId) {
// return vetsClient.getVetByVetId(vetId);
// }
Expand Down
Loading