diff --git a/pom.xml b/pom.xml index 5aee93d9..994a57d7 100644 --- a/pom.xml +++ b/pom.xml @@ -197,6 +197,12 @@ 2.4.0 test + + com.jayway.awaitility + awaitility + 1.7.0 + test + diff --git a/spring-cloud-zookeeper-discovery/pom.xml b/spring-cloud-zookeeper-discovery/pom.xml index e100d595..ae27ad5c 100644 --- a/spring-cloud-zookeeper-discovery/pom.xml +++ b/spring-cloud-zookeeper-discovery/pom.xml @@ -164,6 +164,11 @@ assertj-core test + + com.jayway.awaitility + awaitility + test + diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/CustomZookeeperServiceDiscovery.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/CustomZookeeperServiceDiscovery.groovy deleted file mode 100644 index 0af34b48..00000000 --- a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/CustomZookeeperServiceDiscovery.groovy +++ /dev/null @@ -1,49 +0,0 @@ -package org.springframework.cloud.zookeeper.discovery - -import javax.annotation.PreDestroy - -import org.apache.curator.framework.CuratorFramework -import org.apache.curator.x.discovery.ServiceDiscoveryBuilder -import org.apache.curator.x.discovery.ServiceInstance -import org.apache.curator.x.discovery.UriSpec - -class CustomZookeeperServiceDiscovery extends ZookeeperServiceDiscovery { - - private final String applicationName - private final String basePath - - CustomZookeeperServiceDiscovery(String applicationName, String basePath, CuratorFramework curator) { - super(curator, null, null) - this.applicationName = applicationName - this.basePath = basePath - build() - } - - CustomZookeeperServiceDiscovery(String applicationName, CuratorFramework curator) { - this(applicationName, '/', curator) - } - - @Override - void build() { - setPort(10) - def instance = ServiceInstance.builder().uriSpec(new UriSpec("{scheme}://{address}:{port}/")) - .address('anyUrl') - .port(10) - .name(applicationName) - .build() - getServiceInstanceRef().set(instance) - def discovery = ServiceDiscoveryBuilder - .builder(Void) - .basePath(basePath) - .client(getCurator()) - .thisInstance(instance) - .build() - getServiceDiscoveryRef().set(discovery) - discovery.start() - } - - @PreDestroy - void close() { - getServiceDiscoveryRef().get().close() - } -} \ No newline at end of file diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/CustomZookeeperServiceDiscovery.java b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/CustomZookeeperServiceDiscovery.java new file mode 100644 index 00000000..48db3dea --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/CustomZookeeperServiceDiscovery.java @@ -0,0 +1,61 @@ +package org.springframework.cloud.zookeeper.discovery; + +import javax.annotation.PreDestroy; + +import java.io.IOException; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.x.discovery.ServiceDiscovery; +import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; +import org.apache.curator.x.discovery.ServiceInstance; +import org.apache.curator.x.discovery.UriSpec; + +public class CustomZookeeperServiceDiscovery extends ZookeeperServiceDiscovery { + + private final String applicationName; + private final String basePath; + + public CustomZookeeperServiceDiscovery(String applicationName, String basePath, CuratorFramework curator) { + super(curator, null, null); + this.applicationName = applicationName; + this.basePath = basePath; + build(); + } + + public CustomZookeeperServiceDiscovery(String applicationName, CuratorFramework curator) { + this(applicationName, "/", curator); + } + + @Override + public void build(){ + try { + setPort(10); + ServiceInstance instance = ServiceInstance.builder().uriSpec(new UriSpec("{scheme}://{address}:{port}/")) + .address("anyUrl") + .port(10) + .name(this.applicationName) + .build(); + getServiceInstanceRef().set(instance); + ServiceDiscovery discovery = ServiceDiscoveryBuilder + .builder(Void.class) + .basePath(this.basePath) + .client(getCurator()) + .thisInstance(instance) + .build(); + getServiceDiscoveryRef().set(discovery); + discovery.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @PreDestroy + void close() { + try { + getServiceDiscoveryRef().get().close(); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/TestServiceRegistrar.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/TestServiceRegistrar.groovy deleted file mode 100644 index 7f8d34b5..00000000 --- a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/TestServiceRegistrar.groovy +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2013-2015 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.zookeeper.discovery - -import groovy.transform.CompileStatic -import org.apache.curator.framework.CuratorFramework -import org.apache.curator.x.discovery.ServiceDiscovery -import org.apache.curator.x.discovery.ServiceDiscoveryBuilder -import org.apache.curator.x.discovery.ServiceInstance -import org.apache.curator.x.discovery.UriSpec - -@CompileStatic -class TestServiceRegistrar { - - private final int serverPort - private final CuratorFramework curatorFramework - private final ServiceDiscovery serviceDiscovery - - TestServiceRegistrar(int serverPort, CuratorFramework curatorFramework) { - this.serverPort = serverPort - this.curatorFramework = curatorFramework - this.serviceDiscovery = serviceDiscovery() - } - - void start() { - serviceDiscovery.start() - } - - ServiceInstance serviceInstance() { - return ServiceInstance.builder().uriSpec(new UriSpec("{scheme}://{address}:{port}/")) - .address('localhost') - .port(serverPort) - .name('testInstance') - .build() - } - - ServiceDiscovery serviceDiscovery() { - return ServiceDiscoveryBuilder - .builder(Void) - .basePath('/services') - .client(curatorFramework) - .thisInstance(serviceInstance()) - .build() - } - - - void stop() { - serviceDiscovery.close() - } -} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/TestServiceRegistrar.java b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/TestServiceRegistrar.java new file mode 100644 index 00000000..925f5233 --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/TestServiceRegistrar.java @@ -0,0 +1,76 @@ +/* + * Copyright 2013-2015 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.zookeeper.discovery; + +import java.io.IOException; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.x.discovery.ServiceDiscovery; +import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; +import org.apache.curator.x.discovery.ServiceInstance; +import org.apache.curator.x.discovery.UriSpec; + +public class TestServiceRegistrar { + + private final int serverPort; + private final CuratorFramework curatorFramework; + private final ServiceDiscovery serviceDiscovery; + + public TestServiceRegistrar(int serverPort, CuratorFramework curatorFramework) { + this.serverPort = serverPort; + this.curatorFramework = curatorFramework; + this.serviceDiscovery = serviceDiscovery(); + } + + public void start() { + try { + this.serviceDiscovery.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public ServiceInstance serviceInstance() { + try { + return ServiceInstance.builder().uriSpec(new UriSpec("{scheme}://{address}:{port}/")) + .address("localhost") + .port(this.serverPort) + .name("testInstance") + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public ServiceDiscovery serviceDiscovery() { + return ServiceDiscoveryBuilder + .builder(Void.class) + .basePath("/services") + .client(this.curatorFramework) + .thisInstance(serviceInstance()) + .build(); + } + + + public void stop() { + try { + this.serviceDiscovery.close(); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StickyRuleISpec.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StickyRuleISpec.groovy deleted file mode 100644 index b48001aa..00000000 --- a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StickyRuleISpec.groovy +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 2013-2015 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.zookeeper.discovery.dependency -import com.netflix.loadbalancer.IPing -import com.netflix.loadbalancer.NoOpPing -import org.apache.curator.framework.CuratorFramework -import org.apache.curator.test.TestingServer -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.autoconfigure.EnableAutoConfiguration -import org.springframework.boot.test.SpringApplicationContextLoader -import org.springframework.boot.test.WebIntegrationTest -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.client.loadbalancer.LoadBalancerClient -import org.springframework.cloud.zookeeper.ZookeeperProperties -import org.springframework.cloud.zookeeper.discovery.PollingUtils -import org.springframework.cloud.zookeeper.discovery.TestServiceRegistrar -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.context.annotation.Profile -import org.springframework.test.context.ActiveProfiles -import org.springframework.test.context.ContextConfiguration -import org.springframework.util.SocketUtils -import org.springframework.web.client.RestTemplate -import spock.lang.Specification -import spock.util.concurrent.PollingConditions -import spock.util.environment.RestoreSystemProperties - -@ContextConfiguration(classes = Config, loader = SpringApplicationContextLoader) -@ActiveProfiles('loadbalancerclient') -@WebIntegrationTest(randomPort = true) -class StickyRuleISpec extends Specification implements PollingUtils { - - @Autowired LoadBalancerClient loadBalancerClient - @Autowired DiscoveryClient discoveryClient - PollingConditions conditions - - def setup() { - conditions = new PollingConditions() - } - - @RestoreSystemProperties - def 'should use sticky load balancing strategy taken from Zookeeper dependencies'() { - given: - System.setProperty('spring.cloud.zookeeper.dependency.ribbon.loadbalancer.checkping', 'false') - expect: - thereAreTwoRegisteredServices() - URI uri = getUriForAlias() - conditions.eventually willPass { - 2.times { - assert uri == getUriForAlias() - } - } - } - - private boolean thereAreTwoRegisteredServices() { - return discoveryClient.getInstances('someAlias')?.size() == 2 - } - - private URI getUriForAlias() { - return loadBalancerClient.choose('someAlias')?.uri - } - - - @Configuration - @EnableAutoConfiguration - @EnableDiscoveryClient - @Profile('loadbalancerclient') - static class Config { - - @Bean - @LoadBalanced - RestTemplate loadBalancedRestTemplate() { - return new RestTemplate() - } - - @Bean(destroyMethod = 'close') - TestingServer testingServer() { - return new TestingServer(SocketUtils.findAvailableTcpPort()) - } - - @Bean ZookeeperProperties zookeeperProperties() { - return new ZookeeperProperties(connectString: "localhost:${testingServer().port}") - } - - @Bean(initMethod = "start", destroyMethod = "stop") TestServiceRegistrar serviceOne(CuratorFramework curatorFramework) { - return new TestServiceRegistrar(SocketUtils.findAvailableTcpPort(), curatorFramework) - } - - @Bean(initMethod = "start", destroyMethod = "stop") TestServiceRegistrar serviceTwo(CuratorFramework curatorFramework) { - return new TestServiceRegistrar(SocketUtils.findAvailableTcpPort(), curatorFramework) - } - - @Bean IPing noOpPing() { - return new NoOpPing() - } - } -} \ No newline at end of file diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StickyRuleTests.java b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StickyRuleTests.java new file mode 100644 index 00000000..af1cbdee --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StickyRuleTests.java @@ -0,0 +1,114 @@ +package org.springframework.cloud.zookeeper.discovery.dependency; + +import java.net.URI; +import java.util.List; +import java.util.concurrent.Callable; + +import com.jayway.awaitility.Awaitility; +import com.netflix.loadbalancer.IPing; +import com.netflix.loadbalancer.NoOpPing; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.test.TestingServer; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.WebIntegrationTest; +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.client.loadbalancer.LoadBalancerClient; +import org.springframework.cloud.zookeeper.ZookeeperProperties; +import org.springframework.cloud.zookeeper.discovery.TestServiceRegistrar; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.SocketUtils; +import org.springframework.web.client.RestTemplate; + +/** + * @author Marcin Grzejszczak + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = StickyRuleTests.Config.class) +@ActiveProfiles("loadbalancerclient") +@WebIntegrationTest(randomPort = true) +public class StickyRuleTests { + + @Autowired LoadBalancerClient loadBalancerClient; + @Autowired DiscoveryClient discoveryClient; + + @Test + public void should_use_sticky_load_balancing_strategy_taken_from_Zookeeper_dependencies() { + //given: + System.setProperty("spring.cloud.zookeeper.dependency.ribbon.loadbalancer.checkping", "false"); + //expect: + thereAreTwoRegisteredServices(); + URI uri = getUriForAlias(); + Awaitility.await().until(uriMatchesTwice(uri)); + } + + private Callable uriMatchesTwice(final URI uri) { + return new Callable() { + @Override public Boolean call() throws Exception { + return uriMatches() && uriMatches(); + } + + private boolean uriMatches() { + return uri != null && uri.equals(getUriForAlias()); + } + }; + } + + private boolean thereAreTwoRegisteredServices() { + List instances = this.discoveryClient.getInstances("someAlias"); + return instances != null && instances.size() == 2; + } + + private URI getUriForAlias() { + ServiceInstance alias = this.loadBalancerClient.choose("someAlias"); + return alias != null ? alias.getUri() : null; + } + + + @Configuration + @EnableAutoConfiguration + @EnableDiscoveryClient + @Profile("loadbalancerclient") + static class Config { + + @Bean + @LoadBalanced RestTemplate loadBalancedRestTemplate() { + return new RestTemplate(); + } + + @Bean(destroyMethod = "close") TestingServer testingServer() throws Exception { + return new TestingServer(SocketUtils.findAvailableTcpPort()); + } + + @Bean ZookeeperProperties zookeeperProperties() throws Exception { + ZookeeperProperties zookeeperProperties = new ZookeeperProperties(); + zookeeperProperties.setConnectString("localhost:"+ testingServer().getPort()); + return zookeeperProperties; + } + + @Bean(initMethod = "start", destroyMethod = "stop") + TestServiceRegistrar serviceOne(CuratorFramework curatorFramework) { + return new TestServiceRegistrar(SocketUtils.findAvailableTcpPort(), curatorFramework); + } + + @Bean(initMethod = "start", destroyMethod = "stop") TestServiceRegistrar serviceTwo(CuratorFramework curatorFramework) { + return new TestServiceRegistrar(SocketUtils.findAvailableTcpPort(), curatorFramework); + } + + @Bean IPing noOpPing() { + return new NoOpPing(); + } + } +} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfigurationSpec.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfigurationSpec.groovy deleted file mode 100644 index e9f8f210..00000000 --- a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfigurationSpec.groovy +++ /dev/null @@ -1,50 +0,0 @@ -package org.springframework.cloud.zookeeper.discovery.dependency -import spock.lang.Specification -import spock.lang.Unroll - -class StubsConfigurationSpec extends Specification { - - @Unroll - def "should return empty colon separated dependency notation if empty or invalid path [#path] has been provided"() { - when: - StubsConfiguration stubsConfiguration = new StubsConfiguration(path) - then: - '' == stubsConfiguration.toColonSeparatedDependencyNotation() - where: - path << ['', 'pl/'] - } - - def "should properly parse invalid colon separated path into empty notation"() { - given: - String path = 'pl/a' - when: - StubsConfiguration stubsConfiguration = new StubsConfiguration(path) - then: - '' == stubsConfiguration.toColonSeparatedDependencyNotation() - '' == stubsConfiguration.stubsGroupId - '' == stubsConfiguration.stubsArtifactId - '' == stubsConfiguration.stubsClassifier - } - - def "should parse the path into group, artifact and classifier"() { - given: - String path = 'pl/a' - when: - StubsConfiguration stubsConfiguration = new StubsConfiguration(new StubsConfiguration.DependencyPath(path)) - then: - 'pl:a:stubs' == stubsConfiguration.toColonSeparatedDependencyNotation() - 'pl' == stubsConfiguration.stubsGroupId - 'a' == stubsConfiguration.stubsArtifactId - 'stubs' == stubsConfiguration.stubsClassifier - } - - def "should properly set group, artifact and classifier"() { - when: - StubsConfiguration stubsConfiguration = new StubsConfiguration('pl', 'a', 'superstubs') - then: - 'pl:a:superstubs' == stubsConfiguration.toColonSeparatedDependencyNotation() - 'pl' == stubsConfiguration.stubsGroupId - 'a' == stubsConfiguration.stubsArtifactId - 'superstubs' == stubsConfiguration.stubsClassifier - } -} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfigurationTests.java b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfigurationTests.java new file mode 100644 index 00000000..187f0f1c --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfigurationTests.java @@ -0,0 +1,68 @@ +package org.springframework.cloud.zookeeper.discovery.dependency; + +import org.junit.Test; + +import static org.assertj.core.api.BDDAssertions.then; + +/** + * @author Marcin Grzejszczak + */ +public class StubsConfigurationTests { + + @Test + public void should_return_empty_colon_separated_dependency_notation_if_empty_path_has_been_provided() { + //given: + String path = ""; + //when: + StubsConfiguration stubsConfiguration = new StubsConfiguration(path); + //then: + then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo(""); + } + @Test + public void should_return_empty_colon_separated_dependency_notation_if_invalid_path_has_been_provided() { + //given: + String path = "pl/"; + //when: + StubsConfiguration stubsConfiguration = new StubsConfiguration(path); + //then: + then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo(""); + } + + @Test + public void should_properly_parse_invalid_colon_separated_path_into_empty_notation() { + //given: + String path = "pl/a"; + //when: + StubsConfiguration stubsConfiguration = new StubsConfiguration(path); + //then: + then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo(""); + then(stubsConfiguration.getStubsGroupId()).isEqualTo(""); + then(stubsConfiguration.getStubsArtifactId()).isEqualTo(""); + then(stubsConfiguration.getStubsClassifier()).isEqualTo(""); + } + + @Test + public void should_parse_the_path_into_group_artifact_and_classifier() { + //given: + String path = "pl/a"; + //when: + StubsConfiguration stubsConfiguration = new StubsConfiguration(new StubsConfiguration.DependencyPath(path)); + //then: + then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo("pl:a:stubs"); + then(stubsConfiguration.getStubsGroupId()).isEqualTo("pl"); + then(stubsConfiguration.getStubsArtifactId()).isEqualTo("a"); + then(stubsConfiguration.getStubsClassifier()).isEqualTo("stubs"); + } + + @Test + public void should_properly_set_group_artifact_and_classifier() { + //when: + StubsConfiguration stubsConfiguration = new StubsConfiguration("pl", "a", "superstubs"); + //then: + then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo("pl:a:superstubs"); + then(stubsConfiguration.getStubsGroupId()).isEqualTo("pl"); + then(stubsConfiguration.getStubsArtifactId()).isEqualTo("a"); + then(stubsConfiguration.getStubsClassifier()).isEqualTo("superstubs"); + } + +} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesSpec.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesSpec.groovy deleted file mode 100644 index 4328c521..00000000 --- a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesSpec.groovy +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2013-2015 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.zookeeper.discovery.dependency - -import spock.lang.Specification -import spock.lang.Unroll - -class ZookeeperDependenciesSpec extends Specification { - - private static final ZookeeperDependency EXPECTED_DEPENDENCY = new ZookeeperDependency( - 'path', - LoadBalancerType.RANDOM, - 'contentTypeTemplate', - 'version', - [header: ['value']], - false, - "" - ) - private static final Map DEPENDENCIES = [ - alias: EXPECTED_DEPENDENCY - ] - - ZookeeperDependencies zookeeperDependencies = new ZookeeperDependencies(dependencies: DEPENDENCIES) - - @Unroll - def "should retrieve dependency [#expectedDependency] for path [#path]"() { - expect: - expectedDependency == zookeeperDependencies.getDependencyForPath(path) - where: - path || expectedDependency - 'unknownPath' || null - 'path' || EXPECTED_DEPENDENCY - } - - @Unroll - def "should retrieve dependency [#expectedDependency] for alias [#alias]"() { - expect: - expectedDependency == zookeeperDependencies.getDependencyForAlias(alias) - where: - alias || expectedDependency - 'unknownAlias' || null - 'alias' || EXPECTED_DEPENDENCY - } - - @Unroll - def "should retrieve alias [#expectedAlias] for path [#path]"() { - expect: - expectedAlias == zookeeperDependencies.getAliasForPath(path) - where: - path || expectedAlias - 'unknownPath' || '' - 'path' || 'alias' - } - - @Unroll - def "should retrieve path [#expectedPath] for alias [#alias]"() { - expect: - expectedPath == zookeeperDependencies.getPathForAlias(alias) - where: - alias || expectedPath - 'unknownAlias' || '' - 'alias' || 'path' - } - - def "should successfully replace version in content type template"() { - given: - ZookeeperDependency zookeeperDependency = new ZookeeperDependency( - contentTypeTemplate: 'application/vnd.some-service.$version+json', - version: 'v1' - ) - expect: - 'application/vnd.some-service.v1+json' == zookeeperDependency.contentTypeWithVersion - } -} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesTests.java b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesTests.java new file mode 100644 index 00000000..92fbd337 --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesTests.java @@ -0,0 +1,100 @@ +package org.springframework.cloud.zookeeper.discovery.dependency; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.BDDAssertions.then; + +/** + * @author Marcin Grzejszczak + */ +public class ZookeeperDependenciesTests { + + private static final ZookeeperDependency EXPECTED_DEPENDENCY = new ZookeeperDependency( + "path", + LoadBalancerType.RANDOM, + "contentTypeTemplate", + "version", + defaultHeader(), + false, + "" + ); + private static final Map DEPENDENCIES = defaultDependencies(); + + private static Map> defaultHeader() { + Map> map = new HashMap<>(); + List list = new ArrayList<>(); + list.add("value"); + map.put("header", list); + return map; + } + + private static Map defaultDependencies() { + Map map = new HashMap<>(); + map.put("alias", EXPECTED_DEPENDENCY); + return map; + } + + ZookeeperDependencies zookeeperDependencies = new ZookeeperDependencies(); + + @Before + public void setup() { + this.zookeeperDependencies.setDependencies(DEPENDENCIES); + } + + @Test public void should_retrieve_dependency_dependency_for_good_path() { + // expect: + then(this.zookeeperDependencies.getDependencyForPath("path")).isEqualTo(EXPECTED_DEPENDENCY); + } + + @Test public void should_retrieve_null_dependency_dependency_for_bad_path() { + // expect: + then(this.zookeeperDependencies.getDependencyForPath("unknownPath")).isNull(); + } + + @Test public void should_retrieve_dependency_for_good_alias() { + // expect: + then(this.zookeeperDependencies.getDependencyForAlias("alias")).isEqualTo(EXPECTED_DEPENDENCY); + } + + @Test public void should_retrieve_null_dependency_for_bad_alias() { + // expect: + then(this.zookeeperDependencies.getDependencyForAlias("unknownAlias")).isNull(); + } + + @Test public void should_retrieve_alias_for_good_path() { + // expect: + then(this.zookeeperDependencies.getAliasForPath("path")).isEqualTo("alias"); + } + + @Test public void should_retrieve_empty_alias_for_bad_path() { + // expect: + then(this.zookeeperDependencies.getAliasForPath("unkownPath")).isEmpty(); + } + + @Test public void should_retrieve_path_for_good_alias() { + // expect: + then(this.zookeeperDependencies.getPathForAlias("alias")).isEqualTo("path"); + } + + @Test public void should_retrieve_empty_path_for_bad_alias() { + // expect: + then(this.zookeeperDependencies.getPathForAlias("unknownAlias")).isEmpty(); + } + + @Test public void should_successfully_replace_version_in_content_type_template() { + // given: + ZookeeperDependency zookeeperDependency = new ZookeeperDependency(); + zookeeperDependency.setContentTypeTemplate("application/vnd.some-service.$version+json"); + zookeeperDependency.setVersion("v1"); + // expect: + then(zookeeperDependency.getContentTypeWithVersion()).isEqualTo("application/vnd.some-service.v1+json"); + } + +}