diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/DefaultDependencyWatcherSpringISpec.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/DefaultDependencyWatcherSpringISpec.groovy deleted file mode 100644 index 08df1699..00000000 --- a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/DefaultDependencyWatcherSpringISpec.groovy +++ /dev/null @@ -1,143 +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.watcher - -import org.apache.curator.framework.CuratorFramework -import org.apache.curator.framework.CuratorFrameworkFactory -import org.apache.curator.retry.ExponentialBackoffRetry -import org.apache.curator.test.TestingServer -import org.apache.curator.x.discovery.ServiceCache -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.autoconfigure.EnableAutoConfiguration -import org.springframework.boot.test.SpringApplicationContextLoader -import org.springframework.cloud.client.loadbalancer.LoadBalanced -import org.springframework.cloud.zookeeper.discovery.CustomZookeeperServiceDiscovery -import org.springframework.cloud.zookeeper.discovery.PollingUtils -import org.springframework.cloud.zookeeper.discovery.ZookeeperServiceDiscovery -import org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier -import org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.context.annotation.Profile -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer -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 - -@ContextConfiguration(classes = Config, loader = SpringApplicationContextLoader) -@ActiveProfiles('watcher') -class DefaultDependencyWatcherSpringISpec extends Specification implements PollingUtils { - - @Autowired AssertableDependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier - @Autowired AssertableDependencyWatcherListener dependencyWatcherListener - @Autowired ZookeeperServiceDiscovery serviceDiscovery - PollingConditions conditions - - def setup() { - conditions = new PollingConditions() - } - - def 'should verify that presence of a dependency has been checked'() { - expect: - dependencyPresenceOnStartupVerifier.startupPresenceVerified - } - - def 'should verify that dependency watcher listener is successfully registered and operational'() { - when: - serviceDiscovery.serviceDiscovery.unregisterService(serviceDiscovery.serviceInstance) - - then: - conditions.eventually willPass { - assert dependencyWatcherListener.dependencyState == DependencyState.DISCONNECTED - } - } - - @Configuration - @EnableAutoConfiguration - @Profile('watcher') - static class Config { - - @Bean - @LoadBalanced - RestTemplate loadBalancedRestTemplate() { - return new RestTemplate() - } - - @Bean - static PropertySourcesPlaceholderConfigurer propertiesConfigurer() { - return new PropertySourcesPlaceholderConfigurer() - } - - @Bean(destroyMethod = 'close') - TestingServer testingServer() { - return new TestingServer(SocketUtils.findAvailableTcpPort()) - } - - @Bean - ZookeeperServiceDiscovery zookeeperServiceDiscovery() { - return new MyZookeeperServiceDiscovery(curatorFramework()) - } - - @Bean(initMethod = 'start', destroyMethod = 'close') - CuratorFramework curatorFramework() { - return CuratorFrameworkFactory.newClient(testingServer().connectString, new ExponentialBackoffRetry(20, 20, 500)) - } - - @Bean - DependencyWatcherListener dependencyWatcherListener() { - return new AssertableDependencyWatcherListener() - } - - @Bean - DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier() { - return new AssertableDependencyPresenceOnStartupVerifier() - } - - } - - static class MyZookeeperServiceDiscovery extends CustomZookeeperServiceDiscovery { - MyZookeeperServiceDiscovery(CuratorFramework curator) { - super('testInstance', curator) - } - } - - static class AssertableDependencyWatcherListener implements DependencyWatcherListener { - - DependencyState dependencyState = DependencyState.CONNECTED - - @Override - void stateChanged(String dependencyName, DependencyState newState) { - dependencyState = newState - } - } - - static class AssertableDependencyPresenceOnStartupVerifier extends DependencyPresenceOnStartupVerifier { - - boolean startupPresenceVerified = false - - AssertableDependencyPresenceOnStartupVerifier() { - super(new LogMissingDependencyChecker()) - } - - @Override - void verifyDependencyPresence(String dependencyName, ServiceCache serviceCache, boolean required) { - startupPresenceVerified = true - } - } -} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/DefaultDependencyWatcherSpringTests.java b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/DefaultDependencyWatcherSpringTests.java new file mode 100644 index 00000000..ef06b076 --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/DefaultDependencyWatcherSpringTests.java @@ -0,0 +1,132 @@ +package org.springframework.cloud.zookeeper.discovery.watcher; + +import java.util.concurrent.Callable; + +import com.jayway.awaitility.Awaitility; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.test.TestingServer; +import org.apache.curator.x.discovery.ServiceCache; +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.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.zookeeper.discovery.CustomZookeeperServiceDiscovery; +import org.springframework.cloud.zookeeper.discovery.ZookeeperServiceDiscovery; +import org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier; +import org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.SocketUtils; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.BDDAssertions.then; + +/** + * @author Marcin Grzejszczak + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = DefaultDependencyWatcherSpringTests.Config.class) +@ActiveProfiles("watcher") +public class DefaultDependencyWatcherSpringTests { + + @Autowired AssertableDependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier; + @Autowired AssertableDependencyWatcherListener dependencyWatcherListener; + @Autowired ZookeeperServiceDiscovery serviceDiscovery; + + @Test public void should_verify_that_presence_of_a_dependency_has_been_checked() { + then(this.dependencyPresenceOnStartupVerifier.startupPresenceVerified).isTrue(); + } + + @Test public void should_verify_that_dependency_watcher_listener_is_successfully_registered_and_operational() + throws Exception { + //when: + this.serviceDiscovery.getServiceDiscovery().unregisterService(this.serviceDiscovery.getServiceInstance()); + + //then: + Awaitility.await().until(new Callable() { + @Override public Boolean call() throws Exception { + then(DefaultDependencyWatcherSpringTests.this.dependencyWatcherListener.dependencyState).isEqualTo(DependencyState.DISCONNECTED); + return true; + } + }); + } + + @Configuration + @EnableAutoConfiguration + @Profile("watcher") + static class Config { + + @Bean + @LoadBalanced RestTemplate loadBalancedRestTemplate() { + return new RestTemplate(); + } + + @Bean + static PropertySourcesPlaceholderConfigurer propertiesConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + @Bean(destroyMethod = "close") TestingServer testingServer() throws Exception { + return new TestingServer(SocketUtils.findAvailableTcpPort()); + } + + @Bean ZookeeperServiceDiscovery zookeeperServiceDiscovery() throws Exception { + return new MyZookeeperServiceDiscovery(curatorFramework()); + } + + @Bean(initMethod = "start", destroyMethod = "close") + CuratorFramework curatorFramework() throws Exception { + return CuratorFrameworkFactory + .newClient(testingServer().getConnectString(), new ExponentialBackoffRetry(20, 20, 500)); + } + + @Bean + DependencyWatcherListener dependencyWatcherListener() { + return new AssertableDependencyWatcherListener(); + } + + @Bean DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier() { + return new AssertableDependencyPresenceOnStartupVerifier(); + } + + } + + static class MyZookeeperServiceDiscovery extends CustomZookeeperServiceDiscovery { + MyZookeeperServiceDiscovery(CuratorFramework curator) { + super("testInstance", curator); + } + } + + static class AssertableDependencyWatcherListener implements DependencyWatcherListener { + + DependencyState dependencyState = DependencyState.CONNECTED; + + @Override + public void stateChanged(String dependencyName, DependencyState newState) { + dependencyState = newState; + } + } + + static class AssertableDependencyPresenceOnStartupVerifier extends DependencyPresenceOnStartupVerifier { + + boolean startupPresenceVerified = false; + + AssertableDependencyPresenceOnStartupVerifier() { + super(new LogMissingDependencyChecker()); + } + + @Override public void verifyDependencyPresence(String dependencyName, + ServiceCache serviceCache, boolean required) { + startupPresenceVerified = true; + } + } +} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifierSpec.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifierSpec.groovy deleted file mode 100644 index 48f92ea6..00000000 --- a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifierSpec.groovy +++ /dev/null @@ -1,38 +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.watcher.presence - -import org.apache.curator.x.discovery.ServiceCache -import spock.lang.Specification - -class DependencyPresenceOnStartupVerifierSpec extends Specification { - - private static final String SERVICE_NAME = 'service01' - - def 'should check optional dependency using optional dependency checker'() { - given: - PresenceChecker optionalDependencyChecker = Mock() - DependencyPresenceOnStartupVerifier dependencyVerifier = new DependencyPresenceOnStartupVerifier(optionalDependencyChecker) { - } - ServiceCache serviceCache = Mock() - serviceCache.instances >> [] - when: - dependencyVerifier.verifyDependencyPresence(SERVICE_NAME, serviceCache, false) - then: - 1 * optionalDependencyChecker.checkPresence(SERVICE_NAME, serviceCache.instances) - } - -} diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifierTests.java b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifierTests.java new file mode 100644 index 00000000..88f1f611 --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifierTests.java @@ -0,0 +1,32 @@ +package org.springframework.cloud.zookeeper.discovery.watcher.presence; + +import java.util.Collections; + +import org.apache.curator.x.discovery.ServiceCache; +import org.junit.Test; + +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; + +/** + * @author Marcin Grzejszczak + */ +public class DependencyPresenceOnStartupVerifierTests { + + private static final String SERVICE_NAME = "service01"; + + @Test public void should_check_optional_dependency_using_optional_dependency_checker() { + //given: + PresenceChecker optionalDependencyChecker = mock(PresenceChecker.class); + DependencyPresenceOnStartupVerifier dependencyVerifier = new DependencyPresenceOnStartupVerifier(optionalDependencyChecker) { + }; + ServiceCache serviceCache = mock(ServiceCache.class); + given(serviceCache.getInstances()).willReturn(Collections.emptyList()); + //when: + dependencyVerifier.verifyDependencyPresence(SERVICE_NAME, serviceCache, false); + //then: + then(optionalDependencyChecker).should().checkPresence(SERVICE_NAME, serviceCache.getInstances()); + } + +}