Skip to content

Commit

Permalink
test(Health) : Add tests for indicators.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Wojtczak committed May 18, 2017
1 parent e6e3a00 commit 6da4b32
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ after_success:
- ci/travis/codacy_java_coverage.sh
- ci/travis/coveralls_java_coverage.sh
- ci/travis/docker_deploy.sh
- mvn javadoc:javadoc
- mvn site

deploy:
- provider: pages
local_dir: target/site/apidocs
local_dir: target/site
skip_cleanup: true
github_token: $GITHUB_TOKEN
on:
Expand Down
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.github.maxwo</groupId>
<artifactId>healthcheck</artifactId>
<version>0.1.0</version>
<version>0.2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
Expand Down Expand Up @@ -86,6 +86,19 @@
<repoToken>${coveralls.project.token}</repoToken>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>site</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
7 changes: 6 additions & 1 deletion src/main/java/io/github/maxwo/healthcheck/HealthCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ public class HealthCheck {
* @param args
* Arguments.
*/
public static void main(String[] args) {
public static void main(final String[] args) {
SpringApplication.run(HealthCheck.class, args);
}

/**
* Add CORS headers globally.
*
* @return A configuration with CORS enabled.
*/
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ public boolean isHealthy() {
/**
* Set the healthy flag.
*
* @param healthy Flag value to set.
* @param healthState Flag value to set.
* @return Health set.
*/
@ApiOperation(
value = "Sets the healthy state.",
consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Health state is set.")
@ApiResponse(code = 200, message = "Health state is set.", response = Boolean.class)
})
@RequestMapping(method = RequestMethod.PUT, consumes = {MediaType.APPLICATION_JSON_VALUE})
public void setHealty(@RequestBody final boolean healthy) {
healthCheckDao.setState(healthy);
public boolean setHealty(@RequestBody final boolean healthState) {
healthCheckDao.setState(healthState);
return healthState;
}

}
8 changes: 5 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
endpoints.health.enabled=true
endpoints.health.sensitive=false
# Spring configuration
spring.main.banner-mode=off

spring.main.banner-mode=off
# Application informations
info.app.name=healthcheck
info.app.version=${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -45,28 +46,32 @@ public class HealthCheckControllerTest {
private MockMvc mockMvc;

@Test
public void shouldReturn2XXWhenHealthy() throws Exception {
public void shouldSaveHealthyState() throws Exception {
mockMvc.perform(
put("/healthcheck")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(Boolean.TRUE.toString()))
.andExpect(status().isNoContent());
.andExpect(status().isOk())
.andExpect(content().string("true"));

mockMvc.perform(
get("/healthcheck"))
.andExpect(status().isNoContent());
.andExpect(status().isOk())
.andExpect(content().string("true"));
}

@Test
public void shouldReturn500WhenNotHealthy() throws Exception {
public void shouldSaveUnhealthyState() throws Exception {
mockMvc.perform(
put("/healthcheck")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(Boolean.FALSE.toString()))
.andExpect(status().isNoContent());
.andExpect(status().isOk())
.andExpect(content().string("false"));

mockMvc.perform(
get("/healthcheck"))
.andExpect(status().isInternalServerError());
.andExpect(status().isOk())
.andExpect(content().string("false"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2017 Maxime Wojtczak
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIO
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.maxwo.healthcheck.indicator;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;

/**
* Unit test for healthcheck.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HealthStateIndicatorTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private HealthStateIndicator healthStateIndicator;

@Test
public void shouldBeAHealthIndicator() {
Assert.assertTrue(healthStateIndicator instanceof HealthIndicator);
}

@Test
public void shouldBeHealthyWhenHealthStateIsTrue() throws Exception {
mockMvc.perform(
put("/healthcheck")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(Boolean.TRUE.toString()));

Assert.assertEquals(Status.UP, healthStateIndicator.health().getStatus());
}

@Test
public void shouldBeUnhealthyWhenHealthStateIsFalse() throws Exception {
mockMvc.perform(
put("/healthcheck")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(Boolean.FALSE.toString()));

Assert.assertEquals(Status.DOWN, healthStateIndicator.health().getStatus());
}
}

0 comments on commit 6da4b32

Please sign in to comment.