forked from box/mojito
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for Proxy Health Check
- Loading branch information
1 parent
70730b2
commit 1a6beff
Showing
7 changed files
with
112 additions
and
42 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
39 changes: 39 additions & 0 deletions
39
restclient/src/main/java/com/box/l10n/mojito/rest/resttemplate/ProxyHealthCheckService.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,39 @@ | ||
package com.box.l10n.mojito.rest.resttemplate; | ||
|
||
import org.apache.hc.client5.http.config.RequestConfig; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
public class ProxyHealthCheckService implements InitializingBean { | ||
|
||
private boolean isHealthy; | ||
|
||
@Autowired ResttemplateConfig restTemplateConfig; | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
RestTemplate restTemplate = buildRestTemplate(); | ||
ProxyHealthChecker proxyHealthChecker = new ProxyHealthChecker(); | ||
isHealthy = proxyHealthChecker.isProxyHealthy(restTemplate, restTemplateConfig); | ||
} | ||
|
||
public boolean isProxyHealthy() { | ||
return isHealthy; | ||
} | ||
|
||
/*** | ||
* This is needed because the default RestTemplate does not allow the caller | ||
* to set the Host header | ||
*/ | ||
private RestTemplate buildRestTemplate() { | ||
CloseableHttpClient httpClient = | ||
HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build()).build(); | ||
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient)); | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
restclient/src/test/java/com/box/l10n/mojito/rest/resttemplate/ProxyHealthCheckerTest.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,65 @@ | ||
package com.box.l10n.mojito.rest.resttemplate; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* @author byronantak | ||
*/ | ||
class ProxyHealthCheckerTest { | ||
|
||
@Mock private RestTemplate restTemplate; | ||
|
||
ProxyHealthChecker proxyHealthChecker; | ||
|
||
@BeforeEach | ||
void init() { | ||
MockitoAnnotations.openMocks(this); | ||
proxyHealthChecker = new ProxyHealthChecker(); | ||
} | ||
|
||
@Test | ||
void shouldReturnTrueWhenProxyRequestRespondsWithA200() { | ||
when(restTemplate.exchange( | ||
anyString(), eq(HttpMethod.GET), any(HttpEntity.class), eq(Void.class))) | ||
.thenReturn(new ResponseEntity<Void>(HttpStatus.OK)); | ||
|
||
boolean isHealthy = proxyHealthChecker.isProxyHealthy(restTemplate, new ResttemplateConfig()); | ||
assertTrue(isHealthy); | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenProxyRequestRaisesException() { | ||
when(restTemplate.exchange( | ||
anyString(), eq(HttpMethod.GET), any(HttpEntity.class), eq(Void.class))) | ||
.thenThrow(new RestClientException("An error occurred")); | ||
|
||
boolean isHealthy = proxyHealthChecker.isProxyHealthy(restTemplate, new ResttemplateConfig()); | ||
assertFalse(isHealthy); | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenProxyRequestReturnsBadStatusCode() { | ||
when(restTemplate.exchange( | ||
anyString(), eq(HttpMethod.GET), any(HttpEntity.class), eq(Void.class))) | ||
.thenReturn(new ResponseEntity<Void>(HttpStatus.NOT_FOUND)); | ||
|
||
boolean isHealthy = proxyHealthChecker.isProxyHealthy(restTemplate, new ResttemplateConfig()); | ||
assertFalse(isHealthy); | ||
} | ||
} |
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