From 7804e1bd59a7ba7d3e15ecd6754f973d1628fe11 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 15 May 2017 12:00:11 +0100 Subject: [PATCH] Add Spring Cloud Contract for hystrix stream --- spring-cloud-netflix-hystrix-stream/pom.xml | 60 ++++++++++++++-- .../hystrix/stream/HystrixStreamTests.java | 34 +++++++-- .../hystrix/stream/StreamSourceTestBase.java | 69 +++++++++++++++++++ .../shouldProduceValidMetricsData.groovy | 24 +++++++ 4 files changed, 176 insertions(+), 11 deletions(-) create mode 100644 spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/StreamSourceTestBase.java create mode 100644 spring-cloud-netflix-hystrix-stream/src/test/resources/contracts/shouldProduceValidMetricsData.groovy diff --git a/spring-cloud-netflix-hystrix-stream/pom.xml b/spring-cloud-netflix-hystrix-stream/pom.xml index 558c2e52..bd156b67 100644 --- a/spring-cloud-netflix-hystrix-stream/pom.xml +++ b/spring-cloud-netflix-hystrix-stream/pom.xml @@ -14,6 +14,7 @@ Spring Cloud Netflix Hystrix Stream ${basedir}/.. + 1.1.0.RELEASE @@ -55,11 +56,6 @@ hystrix-javanica test - - com.netflix.eureka - eureka-client - test - org.springframework.boot spring-boot-starter-test @@ -80,5 +76,59 @@ spring-cloud-stream-test-support test + + org.springframework.cloud + spring-cloud-contract-verifier + ${spring-cloud-contract.version} + test + + + + + org.springframework.cloud + spring-cloud-contract-maven-plugin + ${spring-cloud-contract.version} + true + + + + .* + org.springframework.cloud.netflix.hystrix.stream.StreamSourceTestBase + + + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.springframework.cloud + spring-cloud-contract-maven-plugin + [1.1.0.RELEASE,) + + convert + generateTests + + + + + + + + + + + + + diff --git a/spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/HystrixStreamTests.java b/spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/HystrixStreamTests.java index 63903614..5d25202c 100644 --- a/spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/HystrixStreamTests.java +++ b/spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/HystrixStreamTests.java @@ -16,23 +16,30 @@ package org.springframework.cloud.netflix.hystrix.stream; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; + import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.stream.test.binder.MessageCollector; import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; - import static org.assertj.core.api.Assertions.assertThat; /** @@ -49,10 +56,20 @@ public class HystrixStreamTests { @Autowired private Application application; - + @Autowired private DiscoveryClient discoveryClient; + @Autowired + private ObjectMapper mapper; + + @Autowired + private MessageCollector collector; + + @Autowired + @Qualifier(HystrixStreamClient.OUTPUT) + private MessageChannel output; + @EnableAutoConfiguration @EnableCircuitBreaker @RestController @@ -67,14 +84,19 @@ public class HystrixStreamTests { } @Test - public void contextLoads() { + public void contextLoads() throws Exception { this.application.hello(); - //It is important that local service instance resolves for metrics - //origin details to be populated + // It is important that local service instance resolves for metrics + // origin details to be populated ServiceInstance localServiceInstance = discoveryClient.getLocalServiceInstance(); assertThat(localServiceInstance).isNotNull(); assertThat(localServiceInstance.getServiceId()).isEqualTo("mytestapp"); this.task.gatherMetrics(); + Message message = this.collector.forChannel(output).take(); + assertThat(message.getPayload()).isInstanceOf(String.class); + JsonNode tree = mapper.readTree((String) message.getPayload()); + assertThat(tree.hasNonNull("origin")); + assertThat(tree.hasNonNull("data")); } } diff --git a/spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/StreamSourceTestBase.java b/spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/StreamSourceTestBase.java new file mode 100644 index 00000000..345d5273 --- /dev/null +++ b/spring-cloud-netflix-hystrix-stream/src/test/java/org/springframework/cloud/netflix/hystrix/stream/StreamSourceTestBase.java @@ -0,0 +1,69 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.netflix.hystrix.stream; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; + +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureMessageVerifier; +import org.springframework.cloud.netflix.hystrix.stream.StreamSourceTestBase.Application; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Base class for sensor autogenerated tests (used by Spring Cloud Contract). + * + * This bootstraps the Spring Boot application code. + * + * @author Marius Bogoevici + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class, properties = "spring.cloud.stream.bindings.output.destination=sensor-data") +@AutoConfigureMessageVerifier +public abstract class StreamSourceTestBase { + + @Autowired + Application application; + + public void createMetricsData() { + application.hello(); + } + + @EnableAutoConfiguration + @EnableCircuitBreaker + @RestController + public static class Application { + + @HystrixCommand + @RequestMapping("/") + public String hello() { + return "Hello World"; + } + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + } +} diff --git a/spring-cloud-netflix-hystrix-stream/src/test/resources/contracts/shouldProduceValidMetricsData.groovy b/spring-cloud-netflix-hystrix-stream/src/test/resources/contracts/shouldProduceValidMetricsData.groovy new file mode 100644 index 00000000..0f8f057f --- /dev/null +++ b/spring-cloud-netflix-hystrix-stream/src/test/resources/contracts/shouldProduceValidMetricsData.groovy @@ -0,0 +1,24 @@ +package contracts + +org.springframework.cloud.contract.spec.Contract.make { + // Human readable description + description 'Should produce valid metrics data' + // Label by means of which the output message can be triggered + label 'metrics' + // input to the contract + input { + // the contract will be triggered by a method + triggeredBy('createMetricsData()') + } + // output message of the contract + outputMessage { + // destination to which the output message will be sent + sentTo 'hystrixStreamOutput' + headers { + header('contentType': 'application/json') + } + // the body of the output message + body ([ + ]) + } +}