generated from bcgov/quickstart-openshift
-
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.
fix: forest client api integration (#367)
- Loading branch information
Ricardo Campos
authored
Sep 12, 2024
1 parent
30acd17
commit 8807620
Showing
14 changed files
with
309 additions
and
105 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/ca/bc/gov/restapi/results/common/config/ProvidersConfig.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,17 @@ | ||
package ca.bc.gov.restapi.results.common.config; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** This class contains configurations for all external APIs like address and keys. */ | ||
@Getter | ||
@Configuration | ||
public class ProvidersConfig { | ||
|
||
@Value("${forest-client-api.address}") | ||
private String forestClientBaseUri; | ||
|
||
@Value("${forest-client-api.key}") | ||
private String forestClientApiKey; | ||
} |
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
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/ca/bc/gov/restapi/results/common/enums/ForestClientStatusEnum.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
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/ca/bc/gov/restapi/results/common/enums/ForestClientTypeEnum.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
87 changes: 87 additions & 0 deletions
87
backend/src/main/java/ca/bc/gov/restapi/results/common/provider/ForestClientApiProvider.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,87 @@ | ||
package ca.bc.gov.restapi.results.common.provider; | ||
|
||
import ca.bc.gov.restapi.results.common.config.ProvidersConfig; | ||
import ca.bc.gov.restapi.results.common.dto.ForestClientDto; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** This class contains methods to integrate SILVA REST API with ForestClient API. */ | ||
@Slf4j | ||
@Component | ||
public class ForestClientApiProvider { | ||
|
||
private final ProvidersConfig providersConfig; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
private final String rootUri; | ||
|
||
private static final String PROVIDER = "ForestClient API"; | ||
|
||
ForestClientApiProvider(ProvidersConfig providersConfig, RestTemplateBuilder templateBuilder) { | ||
this.providersConfig = providersConfig; | ||
this.restTemplate = templateBuilder.build(); | ||
this.rootUri = providersConfig.getForestClientBaseUri(); | ||
} | ||
|
||
/** | ||
* Fetch a ForestClient by its number. | ||
* | ||
* @param number the client number to search for | ||
* @return the ForestClient with client number, if one exists | ||
*/ | ||
public Optional<ForestClientDto> fetchClientByNumber(String number) { | ||
String apiUrl = String.format("%s/clients/findByClientNumber/{number}", rootUri); | ||
log.info("Starting {} request to {}", PROVIDER, apiUrl); | ||
|
||
try { | ||
ResponseEntity<ForestClientDto> response = | ||
restTemplate.exchange( | ||
apiUrl, | ||
HttpMethod.GET, | ||
new HttpEntity<>(addHttpHeaders()), | ||
ForestClientDto.class, | ||
createParamsMap("number", number)); | ||
|
||
log.info("Finished {} request for function {} - 200 OK!", PROVIDER, "fetchClientByNumber"); | ||
return Optional.of(response.getBody()); | ||
} catch (HttpClientErrorException httpExc) { | ||
log.error("Finished {} request - Response code error: {}", PROVIDER, httpExc.getStatusCode()); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
private String[] addAuthorizationHeader() { | ||
String apiKey = this.providersConfig.getForestClientApiKey(); | ||
return new String[] {"X-API-KEY", apiKey}; | ||
} | ||
|
||
private HttpHeaders addHttpHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE); | ||
String[] authorization = addAuthorizationHeader(); | ||
headers.set(authorization[0], authorization[1]); | ||
|
||
return headers; | ||
} | ||
|
||
private Map<String, String> createParamsMap(String... values) { | ||
Map<String, String> uriVars = new HashMap<>(); | ||
for (int i = 0; i < values.length; i += 2) { | ||
uriVars.put(values[i], values[i + 1]); | ||
} | ||
return uriVars; | ||
} | ||
} |
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
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
83 changes: 83 additions & 0 deletions
83
.../src/test/java/ca/bc/gov/restapi/results/common/provider/ForestClientApiProviderTest.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,83 @@ | ||
package ca.bc.gov.restapi.results.common.provider; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; | ||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; | ||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; | ||
|
||
import ca.bc.gov.restapi.results.common.config.ProvidersConfig; | ||
import ca.bc.gov.restapi.results.common.dto.ForestClientDto; | ||
import ca.bc.gov.restapi.results.common.enums.ForestClientStatusEnum; | ||
import ca.bc.gov.restapi.results.common.enums.ForestClientTypeEnum; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.client.MockRestServiceServer; | ||
|
||
@RestClientTest(ForestClientApiProvider.class) | ||
class ForestClientApiProviderTest { | ||
|
||
@Autowired private ForestClientApiProvider forestClientApiProvider; | ||
|
||
@Autowired private MockRestServiceServer mockServer; | ||
|
||
@MockBean private ProvidersConfig providersConfig; | ||
|
||
@Test | ||
@DisplayName("Fetch client by number happy path should succeed") | ||
public void fetchClientByNumber_happyPath_shouldSucceed() { | ||
String clientNumber = "00012797"; | ||
String url = "/null/clients/findByClientNumber/" + clientNumber; | ||
String json = | ||
""" | ||
{ | ||
"clientNumber": "00012797", | ||
"clientName": "MINISTRY OF FORESTS", | ||
"legalFirstName": null, | ||
"legalMiddleName": null, | ||
"clientStatusCode": "ACT", | ||
"clientTypeCode": "F", | ||
"acronym": "MOF" | ||
} | ||
"""; | ||
|
||
when(providersConfig.getForestClientApiKey()).thenReturn("1z2x2a4s5d5"); | ||
|
||
mockServer.expect(requestTo(url)).andRespond(withSuccess(json, MediaType.APPLICATION_JSON)); | ||
|
||
Optional<ForestClientDto> clientDto = forestClientApiProvider.fetchClientByNumber(clientNumber); | ||
|
||
Assertions.assertTrue(clientDto.isPresent()); | ||
|
||
ForestClientDto forestClient = clientDto.get(); | ||
Assertions.assertEquals("00012797", forestClient.clientNumber()); | ||
Assertions.assertEquals("MINISTRY OF FORESTS", forestClient.clientName()); | ||
Assertions.assertNull(forestClient.legalFirstName()); | ||
Assertions.assertNull(forestClient.legalMiddleName()); | ||
Assertions.assertEquals(ForestClientStatusEnum.ACTIVE, forestClient.clientStatusCode()); | ||
Assertions.assertEquals( | ||
ForestClientTypeEnum.MINISTRY_OF_FORESTS_AND_RANGE, forestClient.clientTypeCode()); | ||
Assertions.assertEquals("MOF", forestClient.acronym()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Fetch client by number client not found should succeed") | ||
void fetchClientByNumber_clientNotFound_shouldSucceed() { | ||
String clientNumber = "00012797"; | ||
String url = "/null/clients/findByClientNumber/" + clientNumber; | ||
|
||
when(providersConfig.getForestClientApiKey()).thenReturn("1z2x2a4s5d5"); | ||
|
||
mockServer.expect(requestTo(url)).andRespond(withStatus(HttpStatus.NOT_FOUND)); | ||
|
||
Optional<ForestClientDto> clientDto = forestClientApiProvider.fetchClientByNumber(clientNumber); | ||
|
||
Assertions.assertTrue(clientDto.isEmpty()); | ||
} | ||
} |
Oops, something went wrong.