Added Feign tests and fixed the implementation

This commit is contained in:
Marcin Grzejszczak
2015-08-14 23:49:08 +02:00
parent 3364aca001
commit 6d0543d5b2
7 changed files with 94 additions and 11 deletions

11
pom.xml
View File

@@ -132,6 +132,11 @@
<artifactId>spring-cloud-netflix-core</artifactId>
<version>${spring-cloud-netflix.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>${spring-cloud-netflix.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
@@ -227,6 +232,12 @@
<version>2.4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<version>8.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

View File

@@ -145,6 +145,16 @@
<scope>test</scope>
<version>1.53</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -21,6 +21,7 @@ import com.netflix.loadbalancer.AbstractServerList;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceInstance;
import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
@@ -48,7 +49,12 @@ public class ZookeeperServerList extends AbstractServerList<ZookeeperServer> {
}
public void initFromDependencies(IClientConfig clientConfig, ZookeeperDependencies zookeeperDependencies) {
this.serviceId = zookeeperDependencies.getPathForAlias(clientConfig.getClientName());
this.serviceId = getServiceIdFromDepsOrClientName(clientConfig, zookeeperDependencies);
}
private String getServiceIdFromDepsOrClientName(IClientConfig clientConfig, ZookeeperDependencies zookeeperDependencies) {
String serviceIdFromDeps = zookeeperDependencies.getPathForAlias(clientConfig.getClientName());
return StringUtils.hasText(serviceIdFromDeps) ? serviceIdFromDeps : clientConfig.getClientName();
}
@Override

View File

@@ -52,8 +52,6 @@ public class ZookeeperDependencies {
@NoArgsConstructor
public static class ZookeeperDependency {
private String id;
private String path;
private LoadBalancerType loadBalancerType = LoadBalancerType.ROUND_ROBIN;

View File

@@ -25,26 +25,34 @@ import org.springframework.cloud.client.ServiceInstance
import org.springframework.cloud.client.discovery.DiscoveryClient
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.cloud.netflix.feign.EnableFeignClients
import org.springframework.cloud.netflix.feign.FeignClient
import org.springframework.cloud.zookeeper.common.CommonTestConfig
import org.springframework.cloud.zookeeper.common.TestRibbonClient
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.Profile
import org.springframework.stereotype.Controller
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.client.RestTemplate
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@ContextConfiguration(classes = Config, loader = SpringApplicationContextLoader)
@ActiveProfiles('ribbon')
@WebIntegrationTest(randomPort = true)
class ZookeeperDiscoveryISpec extends Specification {
class ZookeeperDiscoveryISpec extends Specification implements PollingUtils {
@Autowired TestRibbonClient testRibbonClient
@Autowired DiscoveryClient discoveryClient
@Autowired ZookeeperServiceDiscovery serviceDiscovery
@Value('${spring.application.name}') String springAppName
@Autowired IdUsingFeignClient idUsingFeignClient
PollingConditions conditions = new PollingConditions()
def 'should find the app by its name via Ribbon'() {
@@ -60,6 +68,13 @@ class ZookeeperDiscoveryISpec extends Specification {
'UP' == registeredServiceStatus(instance)
}
def 'should find an instance using feign via service id'() {
expect:
conditions.eventually willPass {
assert idUsingFeignClient.beans
}
}
private String registeredServiceStatusViaServiceName() {
return new JsonSlurper().parseText(testRibbonClient.thisHealthCheck()).status
}
@@ -73,10 +88,17 @@ class ZookeeperDiscoveryISpec extends Specification {
serviceDiscovery.serviceInstance.address == discoveryClient.localServiceInstance.host
}
@FeignClient("ribbonApp")
public static interface IdUsingFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/beans")
String getBeans();
}
@Configuration
@EnableAutoConfiguration
@Import(CommonTestConfig)
@EnableDiscoveryClient
@EnableFeignClients
@Profile('ribbon')
static class Config {
@@ -86,4 +108,13 @@ class ZookeeperDiscoveryISpec extends Specification {
return new TestRibbonClient(restTemplate, springAppName)
}
}
@Controller
@Profile('ribbon')
class PingController {
@RequestMapping('/ping') String ping() {
return 'pong'
}
}
}

View File

@@ -22,7 +22,6 @@ import spock.lang.Unroll
class ZookeeperDependenciesSpec extends Specification {
private static final ZookeeperDependencies.ZookeeperDependency EXPECTED_DEPENDENCY = new ZookeeperDependencies.ZookeeperDependency(
'id',
'path',
LoadBalancerType.RANDOM,
'contentTypeTemplate',

View File

@@ -14,7 +14,6 @@
* limitations under the License.
*/
package org.springframework.cloud.zookeeper.discovery.dependency
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.SpringApplicationContextLoader
@@ -23,6 +22,8 @@ import org.springframework.cloud.client.ServiceInstance
import org.springframework.cloud.client.discovery.DiscoveryClient
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.cloud.netflix.feign.EnableFeignClients
import org.springframework.cloud.netflix.feign.FeignClient
import org.springframework.cloud.zookeeper.common.CommonTestConfig
import org.springframework.cloud.zookeeper.common.TestRibbonClient
import org.springframework.cloud.zookeeper.discovery.PollingUtils
@@ -34,6 +35,7 @@ import org.springframework.stereotype.Controller
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.client.RestTemplate
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@@ -45,11 +47,9 @@ class ZookeeperDiscoveryWithDependenciesISpec extends Specification implements P
@Autowired TestRibbonClient testRibbonClient
@Autowired DiscoveryClient discoveryClient
PollingConditions conditions
def setup() {
conditions = new PollingConditions()
}
@Autowired AliasUsingFeignClient aliasUsingFeignClient
@Autowired IdUsingFeignClient idUsingFeignClient
PollingConditions conditions = new PollingConditions()
def 'should find an instance via path when alias is not found'() {
expect:
@@ -58,6 +58,13 @@ class ZookeeperDiscoveryWithDependenciesISpec extends Specification implements P
}
}
def 'should find an instance using feign via serviceID when alias is not found'() {
expect:
conditions.eventually willPass {
assert idUsingFeignClient.beans
}
}
def 'should find a collaborator via Ribbon by using its alias from dependencies'() {
expect:
conditions.eventually willPass {
@@ -65,6 +72,13 @@ class ZookeeperDiscoveryWithDependenciesISpec extends Specification implements P
}
}
def 'should find a collaborator using feign by using its alias from dependencies'() {
expect:
conditions.eventually willPass {
assert aliasUsingFeignClient.beans
}
}
def 'should find a collaborator via discovery client'() {
given:
List<ServiceInstance> instances = discoveryClient.getInstances('someAlias')
@@ -87,6 +101,7 @@ class ZookeeperDiscoveryWithDependenciesISpec extends Specification implements P
@EnableAutoConfiguration
@Import(CommonTestConfig)
@EnableDiscoveryClient
@EnableFeignClients
@Profile('dependencies')
static class Config {
@@ -97,6 +112,18 @@ class ZookeeperDiscoveryWithDependenciesISpec extends Specification implements P
}
@FeignClient("someAlias")
public static interface AliasUsingFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/beans")
String getBeans();
}
@FeignClient("nameWithoutAlias")
public static interface IdUsingFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/beans")
String getBeans();
}
@Controller
@Profile('dependencies')
class PingController {
@@ -105,4 +132,5 @@ class ZookeeperDiscoveryWithDependenciesISpec extends Specification implements P
return 'pong'
}
}
}