-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More coverage to provide more test cases & also remove unused codes.
More coverage to provide more test cases & also remove unused codes.
- Loading branch information
Showing
7 changed files
with
331 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
api/src/test/java/ca/bc/gov/educ/api/batchgraduation/util/RESTServiceDeleteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package ca.bc.gov.educ.api.batchgraduation.util; | ||
|
||
import ca.bc.gov.educ.api.batchgraduation.exception.ServiceException; | ||
import ca.bc.gov.educ.api.batchgraduation.rest.RESTService; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; | ||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest | ||
@RunWith(SpringRunner.class) | ||
@ExtendWith(MockitoExtension.class) | ||
@ActiveProfiles("test") | ||
public class RESTServiceDeleteTest { | ||
|
||
@Autowired | ||
@InjectMocks | ||
private RESTService restService; | ||
|
||
@Mock | ||
private WebClient.RequestHeadersSpec requestHeadersMock; | ||
@Mock | ||
private WebClient.RequestHeadersUriSpec requestHeadersUriMock; | ||
@Mock | ||
private WebClient.RequestBodySpec requestBodyMock; | ||
@Mock | ||
private WebClient.RequestBodyUriSpec requestBodyUriMock; | ||
@Mock | ||
private WebClient.ResponseSpec responseMock; | ||
|
||
@MockBean | ||
@Qualifier("webClient") | ||
WebClient webClient; | ||
|
||
@MockBean | ||
@Qualifier("batchClient") | ||
WebClient batchWebClient; | ||
|
||
@Mock | ||
private ClientRegistrationRepository clientRegistrationRepositoryMock; | ||
|
||
@Mock | ||
private OAuth2AuthorizedClientRepository oAuth2AuthorizedClientRepositoryMock; | ||
|
||
private static final String TEST_URL_200 = "https://httpstat.us/200"; | ||
private static final String TEST_URL_403 = "https://httpstat.us/403"; | ||
private static final String TEST_URL_503 = "https://httpstat.us/503"; | ||
private static final String OK_RESPONSE = "200 OK"; | ||
|
||
@Before | ||
public void setUp(){ | ||
when(this.batchWebClient.delete()).thenReturn(this.requestHeadersUriMock); | ||
when(this.requestHeadersUriMock.uri(any(String.class))).thenReturn(this.requestHeadersMock); | ||
when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); | ||
when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); | ||
when(this.responseMock.onStatus(any(), any())).thenReturn(this.responseMock); | ||
} | ||
|
||
@Test | ||
public void testDelete_GivenProperData_Expect200Response(){ | ||
when(this.responseMock.bodyToMono(String.class)).thenReturn(Mono.just(OK_RESPONSE)); | ||
String response = this.restService.delete(TEST_URL_200, String.class); | ||
Assert.assertEquals("200 OK", response); | ||
} | ||
|
||
@Test(expected = ServiceException.class) | ||
public void testDelete_Given5xxErrorFromService_ExpectServiceError(){ | ||
when(this.responseMock.bodyToMono(ServiceException.class)).thenReturn(Mono.just(new ServiceException())); | ||
this.restService.delete(TEST_URL_503, String.class); | ||
} | ||
|
||
@Test(expected = ServiceException.class) | ||
public void testDelete_Given4xxErrorFromService_ExpectServiceError(){ | ||
when(this.responseMock.bodyToMono(ServiceException.class)).thenReturn(Mono.just(new ServiceException())); | ||
this.restService.delete(TEST_URL_403, String.class); | ||
} | ||
|
||
} |
93 changes: 83 additions & 10 deletions
93
api/src/test/java/ca/bc/gov/educ/api/batchgraduation/util/RESTServiceGetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,115 @@ | ||
package ca.bc.gov.educ.api.batchgraduation.util; | ||
|
||
import ca.bc.gov.educ.api.batchgraduation.exception.ServiceException; | ||
import ca.bc.gov.educ.api.batchgraduation.rest.RESTService; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; | ||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@RunWith(SpringRunner.class) | ||
@ExtendWith(MockitoExtension.class) | ||
@ActiveProfiles("test") | ||
public class RESTServiceGetTest { | ||
|
||
@Autowired | ||
private RESTService restService; | ||
|
||
//@Test | ||
@Mock | ||
private WebClient.RequestHeadersSpec requestHeadersMock; | ||
@Mock | ||
private WebClient.RequestHeadersUriSpec requestHeadersUriMock; | ||
@Mock | ||
private WebClient.RequestBodySpec requestBodyMock; | ||
@Mock | ||
private WebClient.RequestBodyUriSpec requestBodyUriMock; | ||
@Mock | ||
private WebClient.ResponseSpec responseMock; | ||
|
||
@MockBean | ||
@Qualifier("webClient") | ||
WebClient webClient; | ||
|
||
@MockBean | ||
@Qualifier("batchClient") | ||
WebClient batchWebClient; | ||
|
||
@Mock | ||
private ClientRegistrationRepository clientRegistrationRepositoryMock; | ||
|
||
@Mock | ||
private OAuth2AuthorizedClientRepository oAuth2AuthorizedClientRepositoryMock; | ||
|
||
private static final String TEST_URL_200 = "https://httpstat.us/200"; | ||
private static final String TEST_URL_403 = "https://httpstat.us/403"; | ||
private static final String TEST_URL_503 = "https://httpstat.us/503"; | ||
private static final String OK_RESPONSE = "200 OK"; | ||
|
||
@Before | ||
public void setUp(){ | ||
when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); | ||
when(this.batchWebClient.get()).thenReturn(this.requestHeadersUriMock); | ||
when(this.requestHeadersUriMock.uri(any(String.class))).thenReturn(this.requestHeadersMock); | ||
when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); | ||
when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); | ||
when(this.responseMock.onStatus(any(), any())).thenReturn(this.responseMock); | ||
} | ||
|
||
@Test | ||
public void testGet_GivenProperData_Expect200Response(){ | ||
String response; | ||
response = this.restService.get("https://httpstat.us/200", String.class, "1234"); | ||
when(this.responseMock.bodyToMono(String.class)).thenReturn(Mono.just(OK_RESPONSE)); | ||
String response = this.restService.get(TEST_URL_200, String.class, "1234"); | ||
Assert.assertEquals("200 OK", response); | ||
} | ||
|
||
//@Test(expected = ServiceException.class) | ||
@Test | ||
public void testGetOverride_GivenProperData_Expect200Response(){ | ||
when(this.responseMock.bodyToMono(String.class)).thenReturn(Mono.just(OK_RESPONSE)); | ||
String response = this.restService.get(TEST_URL_200, String.class); | ||
Assert.assertEquals(OK_RESPONSE, response); | ||
} | ||
|
||
@Test(expected = ServiceException.class) | ||
public void testGet_Given5xxErrorFromService_ExpectServiceError(){ | ||
this.restService.get("https://httpstat.us/503", String.class, "1234"); | ||
when(this.responseMock.bodyToMono(ServiceException.class)).thenReturn(Mono.just(new ServiceException())); | ||
this.restService.get(TEST_URL_503, String.class, "1234"); | ||
} | ||
|
||
@Test(expected = ServiceException.class) | ||
public void testGetOverride_Given5xxErrorFromService_ExpectServiceError(){ | ||
when(this.responseMock.bodyToMono(ServiceException.class)).thenReturn(Mono.just(new ServiceException())); | ||
this.restService.get(TEST_URL_503, String.class); | ||
} | ||
|
||
//@Test(expected = ServiceException.class) | ||
@Test(expected = ServiceException.class) | ||
public void testGet_Given4xxErrorFromService_ExpectServiceError(){ | ||
this.restService.get("https://httpstat.us/403", String.class, "1234"); | ||
when(this.responseMock.bodyToMono(ServiceException.class)).thenReturn(Mono.just(new ServiceException())); | ||
this.restService.get(TEST_URL_403, String.class, "1234"); | ||
} | ||
|
||
@Test | ||
public void testDoNothing() { | ||
Assert.assertTrue(true); | ||
@Test(expected = ServiceException.class) | ||
public void testGetOverride_Given4xxErrorFromService_ExpectServiceError(){ | ||
when(this.responseMock.bodyToMono(ServiceException.class)).thenReturn(Mono.just(new ServiceException())); | ||
this.restService.get(TEST_URL_403, String.class); | ||
} | ||
|
||
} |
Oops, something went wrong.