Migrated a couple of classes from groovy to java

This commit is contained in:
Marcin Grzejszczak
2016-04-27 15:14:52 +02:00
parent b3abb02e14
commit a7d3d4f6b4
9 changed files with 133 additions and 173 deletions

3
.gitignore vendored
View File

@@ -12,4 +12,5 @@ _site/
.idea
*.iml
*.swp
*.log
*.log
.checkstyle

View File

@@ -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()
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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}")
}
}

View File

@@ -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;
}
}

View File

@@ -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)
}
}

View File

@@ -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);
}
}

View File

@@ -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)
}
}

View File

@@ -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);
}
}