Commit 901ed29d authored by Rostislav Vatolin [SLB]'s avatar Rostislav Vatolin [SLB]
Browse files

Merge branch 'h_check_upd' into 'master'

Update health check api

See merge request !68
parents b5286921 f4c5c3a9
Pipeline #36853 passed with stages
in 15 minutes and 51 seconds
package org.opengroup.osdu.entitlements.v2.api;
import org.opengroup.osdu.entitlements.v2.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -9,6 +11,9 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/_ah")
public class HealthChecksApi {
@Autowired
private HealthService healthService;
@GetMapping("/liveness_check")
public ResponseEntity<Void> livenessCheck() {
return ResponseEntity.ok().build();
......@@ -16,6 +21,7 @@ public class HealthChecksApi {
@GetMapping("/readiness_check")
public ResponseEntity<Void> readinessCheck() {
healthService.performHealthCheck();
return ResponseEntity.ok().build();
}
}
package org.opengroup.osdu.entitlements.v2.service;
public interface HealthService {
void performHealthCheck();
}
package org.opengroup.osdu.entitlements.v2.api;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.entitlements.v2.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(HealthChecksApi.class)
@ComponentScan("org.opengroup.osdu.entitlements.v2")
public class HealthChecksApiTest {
private HealthChecksApi sut;
private static final String DATA_PARTITION_ID = "common";
@Before
public void setup() {
this.sut = new HealthChecksApi();
@Autowired
private MockMvc mockMvc;
@MockBean
private ITenantFactory tenantFactory;
@MockBean
private HealthService healthService;
@Test
public void shouldReturnHttp200WhenCheckingLiveness() throws Exception {
performHealthCheckRequest("/_ah/liveness_check").andExpect(status().isOk());
}
@Test
public void should_returnHttp200_when_checkLiveness() {
assertEquals(200, this.sut.livenessCheck().getStatusCodeValue());
public void shouldReturn200WhenCheckingReadiness() throws Exception {
performHealthCheckRequest("/_ah/readiness_check").andExpect(status().isOk());
Mockito.verify(healthService).performHealthCheck();
}
@Test
public void should_returnHttp200_when_checkReadiness() {
assertEquals(200, this.sut.readinessCheck().getStatusCodeValue());
public void shouldReturnInternalServerErrorOnErrorFromHealthService() throws Exception {
Mockito.doThrow(new RuntimeException("some error")).when(healthService).performHealthCheck();
performHealthCheckRequest("/_ah/readiness_check").andExpect(status().is5xxServerError());
Mockito.verify(healthService).performHealthCheck();
}
private ResultActions performHealthCheckRequest(String path) throws Exception {
return mockMvc.perform(get(path)
.contentType(MediaType.APPLICATION_JSON)
.header(DpsHeaders.AUTHORIZATION, "Bearer token")
.header(DpsHeaders.USER_ID, "a@b.com")
.header(DpsHeaders.DATA_PARTITION_ID, DATA_PARTITION_ID));
}
}
package org.opengroup.osdu.entitlements.v2.azure.service;
import lombok.AllArgsConstructor;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.entitlements.v2.model.ParentReferences;
import org.opengroup.osdu.entitlements.v2.service.HealthService;
import org.springframework.stereotype.Service;
@Service
@AllArgsConstructor
public class HealthServiceAzure implements HealthService {
private final ICache<String, ParentReferences> redisGroupCache;
/**
* Checks if connection to redis is in healthy state
*/
@Override
public void performHealthCheck() {
redisGroupCache.get("some-key");
}
}
package org.opengroup.osdu.entitlements.v2.azure.configuration;
import org.opengroup.osdu.core.common.cache.RedisCache;
import org.opengroup.osdu.entitlements.v2.model.ParentReferences;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
@TestConfiguration
public class CacheTestConfiguration {
@Primary
@Bean
public RedisCache<String, ParentReferences> groupCache() {
return new RedisCache<>("localhost", 7000, 0, String.class, ParentReferences.class);
}
}
package org.opengroup.osdu.entitlements.v2.azure.service;
import com.lambdaworks.redis.RedisCommandTimeoutException;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.entitlements.v2.model.ParentReferences;
import org.opengroup.osdu.entitlements.v2.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit4.SpringRunner;
import redis.embedded.RedisServer;
@SpringBootTest
@RunWith(SpringRunner.class)
public class HealthServiceAzureTest {
@Autowired
private HealthService healthService;
@SpyBean
private ICache<String, ParentReferences> redisGroupCache;
private static RedisServer redisServer;
@BeforeClass
public static void setupClass() {
redisServer = RedisServer.builder().port(7000).build();
redisServer.start();
}
@AfterClass
public static void end() {
redisServer.stop();
}
@Test
public void shouldSucceedInHealthCheck() {
healthService.performHealthCheck();
Mockito.verify(redisGroupCache).get("some-key");
}
@Test
public void shouldFailInHealthCheck() {
redisServer.stop();
try {
healthService.performHealthCheck();
Assert.fail("Expecting exception here");
} catch (RedisCommandTimeoutException e) {
Assert.assertEquals("Command timed out after 30 SECONDS", e.getMessage());
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment