Migrated a couple of classes from groovy to java
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,4 +12,5 @@ _site/
|
||||
.idea
|
||||
*.iml
|
||||
*.swp
|
||||
*.log
|
||||
*.log
|
||||
.checkstyle
|
||||
@@ -1,37 +0,0 @@
|
||||
package org.springframework.cloud.zookeeper
|
||||
|
||||
import org.apache.curator.framework.CuratorFramework
|
||||
import org.apache.curator.test.TestingServer
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
import spock.lang.Specification
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@ContextConfiguration(classes = [ TestConfig, ZookeeperAutoConfiguration ])
|
||||
class ZookeeperAutoConfigurationSpec extends Specification {
|
||||
|
||||
@Autowired(required = false)
|
||||
CuratorFramework curator
|
||||
|
||||
def 'should successfully inject Curator as a Spring bean'() {
|
||||
expect:
|
||||
curator != null
|
||||
}
|
||||
|
||||
static class TestConfig {
|
||||
@Bean
|
||||
ZookeeperProperties zookeeperProperties() throws Exception {
|
||||
ZookeeperProperties properties = new ZookeeperProperties()
|
||||
properties.connectString = testingServer().connectString
|
||||
return properties
|
||||
}
|
||||
|
||||
@Bean
|
||||
TestingServer testingServer() throws Exception {
|
||||
return new TestingServer()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.springframework.cloud.zookeeper;
|
||||
|
||||
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.context.annotation.Bean;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ZookeeperAutoConfigurationTests.TestConfig.class, ZookeeperAutoConfiguration.class })
|
||||
public class ZookeeperAutoConfigurationTests {
|
||||
|
||||
@Autowired(required = false) CuratorFramework curator;
|
||||
|
||||
@Test
|
||||
public void should_successfully_inject_Curator_as_a_Spring_bean() {
|
||||
assertNotNull(this.curator);
|
||||
}
|
||||
|
||||
static class TestConfig {
|
||||
@Bean
|
||||
ZookeeperProperties zookeeperProperties() throws Exception {
|
||||
ZookeeperProperties properties = new ZookeeperProperties();
|
||||
properties.setConnectString(testingServer().getConnectString());
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Bean TestingServer testingServer() throws Exception {
|
||||
return new TestingServer();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,47 +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.test
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.PackageScope
|
||||
import org.apache.curator.test.TestingServer
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced
|
||||
import org.springframework.cloud.zookeeper.ZookeeperProperties
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.util.SocketUtils
|
||||
import org.springframework.web.client.RestTemplate
|
||||
|
||||
@PackageScope
|
||||
@CompileStatic
|
||||
@Configuration
|
||||
class CommonTestConfig {
|
||||
|
||||
@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}")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.springframework.cloud.zookeeper.discovery.test;
|
||||
|
||||
import org.apache.curator.test.TestingServer;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.zookeeper.ZookeeperProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.SocketUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class CommonTestConfig {
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +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.test
|
||||
|
||||
import org.springframework.web.client.RestTemplate
|
||||
|
||||
class TestRibbonClient extends TestServiceRestClient {
|
||||
|
||||
private final String thisAppName
|
||||
|
||||
TestRibbonClient(RestTemplate restTemplate) {
|
||||
super(restTemplate)
|
||||
this.thisAppName = 'someName'
|
||||
}
|
||||
|
||||
TestRibbonClient(RestTemplate restTemplate, String thisAppName) {
|
||||
super(restTemplate)
|
||||
this.thisAppName = thisAppName
|
||||
}
|
||||
|
||||
String thisHealthCheck() {
|
||||
return restTemplate.getForObject("http://$thisAppName/health", String)
|
||||
}
|
||||
|
||||
Integer thisPort() {
|
||||
return restTemplate.getForObject("http://$thisAppName/port", Integer)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.springframework.cloud.zookeeper.discovery.test;
|
||||
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class TestRibbonClient extends TestServiceRestClient {
|
||||
|
||||
private final String thisAppName;
|
||||
|
||||
TestRibbonClient(RestTemplate restTemplate) {
|
||||
super(restTemplate);
|
||||
this.thisAppName = "someName";
|
||||
}
|
||||
|
||||
TestRibbonClient(RestTemplate restTemplate, String thisAppName) {
|
||||
super(restTemplate);
|
||||
this.thisAppName = thisAppName;
|
||||
}
|
||||
|
||||
String thisHealthCheck() {
|
||||
return this.restTemplate
|
||||
.getForObject("http://" + this.thisAppName + "/health", String.class);
|
||||
}
|
||||
|
||||
Integer thisPort() {
|
||||
return this.restTemplate
|
||||
.getForObject("http://" + this.thisAppName + "/port", Integer.class);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +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.test
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.util.logging.Commons
|
||||
import org.springframework.web.client.RestTemplate
|
||||
|
||||
@CompileStatic
|
||||
@Commons
|
||||
class TestServiceRestClient {
|
||||
|
||||
final RestTemplate restTemplate;
|
||||
|
||||
TestServiceRestClient(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate
|
||||
}
|
||||
|
||||
public <T> T callService(String alias, String endpoint, Class<T> clazz) {
|
||||
String url = "http://$alias/$endpoint"
|
||||
log.info("Calling [$url]")
|
||||
return restTemplate.getForObject(url, clazz)
|
||||
}
|
||||
|
||||
String callService(String alias, String endpoint) {
|
||||
return callService(alias, endpoint, String)
|
||||
}
|
||||
|
||||
String callOnUrl(String url, String endpoint) {
|
||||
return new RestTemplate().getForObject("http://$url/$endpoint", String)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.springframework.cloud.zookeeper.discovery.test;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class TestServiceRestClient {
|
||||
private static final Log log = LogFactory.getLog(TestServiceRestClient.class);
|
||||
|
||||
protected final RestTemplate restTemplate;
|
||||
|
||||
TestServiceRestClient(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
public <T> T callService(String alias, String endpoint, Class<T> clazz) {
|
||||
String url = "http://" + alias +"/" + endpoint;
|
||||
log.info("Calling [" + url + "]");
|
||||
return this.restTemplate.getForObject(url, clazz);
|
||||
}
|
||||
|
||||
String callService(String alias, String endpoint) {
|
||||
return callService(alias, endpoint, String.class);
|
||||
}
|
||||
|
||||
String callOnUrl(String url, String endpoint) {
|
||||
return new RestTemplate().getForObject("http://" + url + "/" + endpoint, String.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user