Skip to content

Commit

Permalink
improving test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
soletsdev committed Feb 21, 2024
1 parent ffd7a91 commit b1df93a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
16 changes: 10 additions & 6 deletions api/src/main/java/ca/bc/gov/educ/penreg/api/rest/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
Expand Down Expand Up @@ -387,11 +387,15 @@ public List<SchoolContact> getStudentRegistrationContactList(final String mincod
" {\"key\":\"schoolId\",\"operation\":\"eq\",\"value\":\"" + school.getSchoolId() + "\",\"valueType\":\"UUID\",\"condition\":\"AND\"}]}," +
" {\"key\":\"email\",\"operation\":\"neq\",\"value\":\"null\",\"valueType\":\"UUID\",\"condition\":\"AND\"}]}";
SchoolContactSearchWrapper schoolContactSearchWrapper = this.webClient.get()
.uri(getSchoolContactURI(criterion))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(SchoolContactSearchWrapper.class)
.blockFirst();
.uri(getSchoolContactURI(criterion))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(SchoolContactSearchWrapper.class)
.blockFirst();

if (schoolContactSearchWrapper == null) {
throw new PenRegAPIRuntimeException("API call to Institute API received null response when getting student registration contacts, contact the Ministry for more info.");
}

return schoolContactSearchWrapper.getContent();
}catch(Exception e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.nats.client.Message;
import java.net.*;
import java.util.*;
import lombok.val;
import org.junit.Before;
Expand Down Expand Up @@ -104,6 +105,15 @@ private GradeCode[] createGradeCodeArray() {
codes[0] = GradeCode.builder().gradeCode("A").label("A").description("hello").build();
return codes;
}

private SchoolContactSearchWrapper createSchoolContactSearchWrapper() {
SchoolContactSearchWrapper schoolSearchWrapper = new SchoolContactSearchWrapper();
schoolSearchWrapper.setContent(Arrays.asList(SchoolContact.builder().email("pen@email.com").firstName("Joe").lastName("Blow").build(),
SchoolContact.builder().email("2@email.com").firstName("2").lastName("2").build()));

return schoolSearchWrapper;
}

@Test
public void testGetStudentByStudentID_givenAPICallSuccess_shouldReturnData() {
final String studentID = UUID.randomUUID().toString();
Expand Down Expand Up @@ -223,6 +233,25 @@ public void testGetGradeCode_shouldReturnData() {
assertThat(result.get(0).getGradeCode()).isEqualTo("A");
}

@Test
public void testGetStudentRegistrationContacts_shouldReturnData() {
when(this.webClient.get()).thenReturn(this.requestHeadersUriMock);
when(this.requestHeadersUriMock.uri(any(URI.class)))
.thenReturn(this.requestHeadersMock);
when(this.requestHeadersMock.header(any(), any())).thenReturn(this.requestHeadersMock);
when(this.requestHeadersUriMock.uri(eq(this.applicationProperties.getInstituteApiUrl()), any(Function.class)))
.thenReturn(this.requestHeadersMock);
when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock);
when(this.responseMock.bodyToFlux(SchoolContactSearchWrapper.class)).thenReturn(Flux.just(createSchoolContactSearchWrapper()));

final var result = this.restUtils.getStudentRegistrationContactList("10200001");
assertThat(result.size()).isEqualTo(2);
assertThat(result.get(0).getEmail()).isEqualTo("pen@email.com");
assertThat(result.get(0).getFirstName()).isEqualTo("Joe");
assertThat(result.get(1).getEmail()).isEqualTo("2@email.com");
assertThat(result.get(1).getFirstName()).isEqualTo("2");
}

private WebClient.RequestBodySpec returnMockBodySpec() {
return this.requestBodyMock;
}
Expand Down

0 comments on commit b1df93a

Please sign in to comment.