From b1df93adefb53cbd97a63fddb29d95bcd11882a8 Mon Sep 17 00:00:00 2001 From: soletsdev Date: Wed, 21 Feb 2024 07:58:07 -0800 Subject: [PATCH] improving test coverage --- .../gov/educ/penreg/api/rest/RestUtils.java | 16 ++++++---- .../educ/penreg/api/rest/RestUtilsTest.java | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/penreg/api/rest/RestUtils.java b/api/src/main/java/ca/bc/gov/educ/penreg/api/rest/RestUtils.java index 39e216e3..24dee4d5 100644 --- a/api/src/main/java/ca/bc/gov/educ/penreg/api/rest/RestUtils.java +++ b/api/src/main/java/ca/bc/gov/educ/penreg/api/rest/RestUtils.java @@ -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; @@ -387,11 +387,15 @@ public List 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){ diff --git a/api/src/test/java/ca/bc/gov/educ/penreg/api/rest/RestUtilsTest.java b/api/src/test/java/ca/bc/gov/educ/penreg/api/rest/RestUtilsTest.java index 9e0b17bc..701c1f7f 100644 --- a/api/src/test/java/ca/bc/gov/educ/penreg/api/rest/RestUtilsTest.java +++ b/api/src/test/java/ca/bc/gov/educ/penreg/api/rest/RestUtilsTest.java @@ -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; @@ -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(); @@ -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; }