From c841e3d8f13b28ad60f8fd2bc1272d092740bca2 Mon Sep 17 00:00:00 2001 From: Vishal Lal Date: Thu, 20 Oct 2016 15:59:41 -0700 Subject: [PATCH 1/2] Initial checkin for healthcheck. Add 10s timeout limit for Mongo --- .../configuration/MongoConfiguration.java | 21 +++++++ .../cart/controllers/CartsController.java | 1 + .../controllers/HealthCheckController.java | 46 +++++++++++++++ .../socks/cart/entities/HealthCheck.java | 59 +++++++++++++++++++ src/main/resources/application.properties | 1 + 5 files changed, 128 insertions(+) create mode 100644 src/main/java/works/weave/socks/cart/configuration/MongoConfiguration.java create mode 100644 src/main/java/works/weave/socks/cart/controllers/HealthCheckController.java create mode 100644 src/main/java/works/weave/socks/cart/entities/HealthCheck.java diff --git a/src/main/java/works/weave/socks/cart/configuration/MongoConfiguration.java b/src/main/java/works/weave/socks/cart/configuration/MongoConfiguration.java new file mode 100644 index 000000000..89688859d --- /dev/null +++ b/src/main/java/works/weave/socks/cart/configuration/MongoConfiguration.java @@ -0,0 +1,21 @@ +package works.weave.socks.cart.configuration; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.mongodb.MongoClientOptions; + +@Configuration +@AutoConfigureBefore(MongoAutoConfiguration.class) +public class MongoConfiguration { + + @Bean + public MongoClientOptions optionsProvider() { + MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); + optionsBuilder.serverSelectionTimeout(10000); + MongoClientOptions options = optionsBuilder.build(); + return options; + } +} diff --git a/src/main/java/works/weave/socks/cart/controllers/CartsController.java b/src/main/java/works/weave/socks/cart/controllers/CartsController.java index d1326ef5b..71ef78d4c 100644 --- a/src/main/java/works/weave/socks/cart/controllers/CartsController.java +++ b/src/main/java/works/weave/socks/cart/controllers/CartsController.java @@ -10,6 +10,7 @@ import works.weave.socks.cart.cart.CartResource; import works.weave.socks.cart.entities.Cart; + @RestController @RequestMapping(path = "/carts") public class CartsController { diff --git a/src/main/java/works/weave/socks/cart/controllers/HealthCheckController.java b/src/main/java/works/weave/socks/cart/controllers/HealthCheckController.java new file mode 100644 index 000000000..1a42c8848 --- /dev/null +++ b/src/main/java/works/weave/socks/cart/controllers/HealthCheckController.java @@ -0,0 +1,46 @@ +package works.weave.socks.cart.controllers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import works.weave.socks.cart.entities.HealthCheck; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +public class HealthCheckController { + + @Autowired + private MongoTemplate mongoTemplate; + + @ResponseStatus(HttpStatus.OK) + @RequestMapping(method = RequestMethod.GET, path = "/health") + public + @ResponseBody + Map> getHealth() { + Map> map = new HashMap>(); + List healthChecks = new ArrayList(); + Date dateNow = Calendar.getInstance().getTime(); + + HealthCheck app = new HealthCheck("cart", "OK", dateNow); + HealthCheck database = new HealthCheck("cart-db", "OK", dateNow); + + try { + mongoTemplate.executeCommand("{ buildInfo: 1 }"); + } catch (Exception e) { + database.setStatus("err"); + } + + healthChecks.add(app); + healthChecks.add(database); + + map.put("health", healthChecks); + return map; + } +} diff --git a/src/main/java/works/weave/socks/cart/entities/HealthCheck.java b/src/main/java/works/weave/socks/cart/entities/HealthCheck.java new file mode 100644 index 000000000..07f26dd99 --- /dev/null +++ b/src/main/java/works/weave/socks/cart/entities/HealthCheck.java @@ -0,0 +1,59 @@ +package works.weave.socks.cart.entities; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonFormat; + +import java.util.Calendar; +import java.util.Date; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class HealthCheck { + private String service; + private String status; + + @JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX") + private Date date = Calendar.getInstance().getTime(); + + public HealthCheck() { + + } + + public HealthCheck(String service, String status, Date date) { + this.service = service; + this.status = status; + this.date = date; + } + + @Override + public String toString() { + return "HealthCheck{" + + "service='" + service + '\'' + + ", status='" + status + '\'' + + ", date='" + date + + '}'; + } + + public String getService() { + return service; + } + + public void setService(String service) { + this.service = service; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6001fe6ec..7b7638f99 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,3 @@ server.port=${port:8081} spring.data.mongodb.uri=mongodb://${db:cart-db}:27017/data +endpoints.health.enabled=false From 5daa3fc3b69b2a95c67c1a008743acb5a9800d92 Mon Sep 17 00:00:00 2001 From: Vishal Lal Date: Mon, 31 Oct 2016 21:24:10 -0700 Subject: [PATCH 2/2] Add test --- .../UnitHealthCheckController.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/works/weave/socks/cart/controllers/UnitHealthCheckController.java diff --git a/src/test/java/works/weave/socks/cart/controllers/UnitHealthCheckController.java b/src/test/java/works/weave/socks/cart/controllers/UnitHealthCheckController.java new file mode 100644 index 000000000..739113b8d --- /dev/null +++ b/src/test/java/works/weave/socks/cart/controllers/UnitHealthCheckController.java @@ -0,0 +1,39 @@ +package works.weave.socks.cart.controllers; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.hamcrest.collection.IsEmptyCollection.empty; +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; +import static org.junit.Assert.assertThat; + +import works.weave.socks.cart.entities.HealthCheck; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class UnitHealthCheckController { + + @Autowired + private HealthCheckController healthCheckController; + + @Test + public void shouldGetHealth() { + Map> results = healthCheckController.getHealth(); + assertThat(results.get("health").size(), is(equalTo(2))); + } +}