1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Polish spring-cloud-incubator/spring-cloud-alibaba#348 : @DubboTransported supports RestTemplate (part 1)

This commit is contained in:
mercyblitz
2019-02-19 15:05:25 +08:00
parent 0a4cd85255
commit fc8d8e3628
62 changed files with 3408 additions and 230 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.alibaba.dubbo.annotation.DubboTransported;
import org.springframework.cloud.alibaba.dubbo.service.EchoService;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
@@ -32,6 +33,13 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/**
* Dubbo Spring Cloud Bootstrap
@@ -53,6 +61,10 @@ public class DubboSpringCloudBootstrap {
@Lazy
private DubboFeignEchoService dubboFeignEchoService;
@Autowired
@LoadBalanced
private RestTemplate restTemplate;
@GetMapping(value = "/dubbo/call/echo")
public String dubboEcho(@RequestParam("message") String message) {
return echoService.echo(message);
@@ -71,16 +83,16 @@ public class DubboSpringCloudBootstrap {
@FeignClient("spring-cloud-alibaba-dubbo")
public interface FeignEchoService {
@GetMapping(value = "/echo")
@GetMapping(value = "/echo", consumes = APPLICATION_JSON_VALUE)
String echo(@RequestParam("message") String message);
}
@FeignClient("spring-cloud-alibaba-dubbo")
@DubboTransported
public interface DubboFeignEchoService {
@GetMapping(value = "/echo")
@DubboTransported
@GetMapping(value = "/echo", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
String echo(@RequestParam("message") String message);
}
@@ -96,6 +108,26 @@ public class DubboSpringCloudBootstrap {
};
}
@Bean
public ApplicationRunner restTemplateRunner() {
return arguments -> {
System.out.println(restTemplate.getForEntity("http://spring-cloud-alibaba-dubbo/echo?message=小马哥", String.class));
Map<String, Object> data = new HashMap<>();
data.put("name", "小马哥");
data.put("age", 33);
data.put("height", 173);
System.out.println(restTemplate.postForEntity("http://spring-cloud-alibaba-dubbo/toString", data, String.class));
};
}
@Bean
@LoadBalanced
@DubboTransported
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
new SpringApplicationBuilder(DubboSpringCloudBootstrap.class)
.run(args);

View File

@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Test;
/**
* {@link AbstractHttpRequestMatcher} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public abstract class AbstractHttpRequestMatcherTest<T extends AbstractHttpRequestMatcher> {
@Test
public abstract void testEqualsAndHashCode();
@Test
public abstract void testGetContent();
@Test
public abstract void testGetToStringInfix();
}

View File

@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import java.lang.reflect.Constructor;
/**
* {@link AbstractMediaTypeExpression} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public abstract class AbstractMediaTypeExpressionTest<T extends AbstractMediaTypeExpression> {
protected T createExpression(String expression) {
ResolvableType resolvableType = ResolvableType.forType(getClass().getGenericSuperclass());
Class<T> firstGenericType = (Class<T>) resolvableType.resolveGeneric(0);
Constructor<T> constructor = null;
try {
constructor = firstGenericType.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
return BeanUtils.instantiateClass(constructor, expression);
}
@Test
public void testGetMediaTypeAndNegated() {
// Normal
AbstractMediaTypeExpression expression = createExpression(MediaType.APPLICATION_JSON_VALUE);
Assert.assertEquals(MediaType.APPLICATION_JSON, expression.getMediaType());
Assert.assertFalse(expression.isNegated());
// Negated
expression = createExpression("!" + MediaType.APPLICATION_JSON_VALUE);
Assert.assertEquals(MediaType.APPLICATION_JSON, expression.getMediaType());
Assert.assertTrue(expression.isNegated());
}
@Test
public void testEqualsAndHashCode() {
Assert.assertEquals(createExpression(MediaType.APPLICATION_JSON_VALUE), createExpression(MediaType.APPLICATION_JSON_VALUE));
Assert.assertEquals(createExpression(MediaType.APPLICATION_JSON_VALUE).hashCode(),
createExpression(MediaType.APPLICATION_JSON_VALUE).hashCode());
}
@Test
public void testCompareTo() {
Assert.assertEquals(0,
createExpression(MediaType.APPLICATION_JSON_VALUE).compareTo(createExpression(MediaType.APPLICATION_JSON_VALUE)));
}
}

View File

@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.core.ResolvableType;
import java.lang.reflect.Constructor;
/**
* {@link AbstractNameValueExpression} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public abstract class AbstractNameValueExpressionTest<T extends AbstractNameValueExpression> {
protected T createExpression(String expression) {
ResolvableType resolvableType = ResolvableType.forType(getClass().getGenericSuperclass());
Class<T> firstGenericType = (Class<T>) resolvableType.resolveGeneric(0);
Constructor<T> constructor = null;
try {
constructor = firstGenericType.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
return BeanUtils.instantiateClass(constructor, expression);
}
@Test
public void testGetNameAndValue() {
// Normal Name and value
AbstractNameValueExpression expression = createExpression("a=1");
Assert.assertEquals("a", expression.getName());
Assert.assertFalse(expression.isNegated());
expression = createExpression("a=1");
Assert.assertEquals("a", expression.getName());
Assert.assertEquals("1", expression.getValue());
Assert.assertFalse(expression.isNegated());
// Negated Name
expression = createExpression("!a");
Assert.assertEquals("a", expression.getName());
Assert.assertTrue(expression.isNegated());
expression = createExpression("a!=1");
Assert.assertEquals("a", expression.getName());
Assert.assertEquals("1", expression.getValue());
Assert.assertTrue(expression.isNegated());
}
@Test
public void testEqualsAndHashCode() {
Assert.assertEquals(createExpression("a"), createExpression("a"));
Assert.assertEquals(createExpression("a").hashCode(), createExpression("a").hashCode());
Assert.assertEquals(createExpression("a=1"), createExpression("a = 1 "));
Assert.assertEquals(createExpression("a=1").hashCode(), createExpression("a = 1 ").hashCode());
Assert.assertNotEquals(createExpression("a"), createExpression("b"));
Assert.assertNotEquals(createExpression("a").hashCode(), createExpression("b").hashCode());
}
}

View File

@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.http.MediaType;
/**
* {@link ConsumeMediaTypeExpression} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ConsumeMediaTypeExpressionTest extends AbstractMediaTypeExpressionTest<ConsumeMediaTypeExpression> {
@Test
public void testMatch() {
ConsumeMediaTypeExpression expression = createExpression(MediaType.ALL_VALUE);
Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON_UTF8));
expression = createExpression(MediaType.APPLICATION_JSON_VALUE);
Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON_UTF8));
expression = createExpression(MediaType.APPLICATION_JSON_VALUE + ";q=0.7");
Assert.assertTrue(expression.match(MediaType.APPLICATION_JSON_UTF8));
expression = createExpression(MediaType.TEXT_HTML_VALUE);
Assert.assertFalse(expression.match(MediaType.APPLICATION_JSON_UTF8));
}
}

View File

@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.http.HttpRequest;
import static org.springframework.cloud.alibaba.dubbo.http.DefaultHttpRequest.builder;
/**
* {@link HeaderExpression} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class HeaderExpressionTest extends AbstractNameValueExpressionTest<HeaderExpression> {
@Test
public void testIsCaseSensitiveName() {
Assert.assertFalse(createExpression("a=1").isCaseSensitiveName());
Assert.assertFalse(createExpression("a=!1").isCaseSensitiveName());
Assert.assertFalse(createExpression("b=1").isCaseSensitiveName());
}
@Test
public void testMatch() {
HeaderExpression expression = createExpression("a=1");
HttpRequest request = builder().build();
Assert.assertFalse(expression.match(request));
request = builder().header("a", "").build();
Assert.assertFalse(expression.match(request));
request = builder().header("a", "2").build();
Assert.assertFalse(expression.match(request));
request = builder().header("", "1").build();
Assert.assertFalse(expression.match(request));
request = builder().header("a", "1").build();
Assert.assertTrue(expression.match(request));
}
}

View File

@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.springframework.http.HttpMethod;
import java.util.Arrays;
import java.util.HashSet;
/**
* {@link HttpRequestMethodsMatcher} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class HttpRequestMethodsMatcherTest extends AbstractHttpRequestMatcherTest<HttpRequestMethodsMatcher> {
HttpRequestMethodsMatcher matcher = new HttpRequestMethodsMatcher("GET");
@Override
public void testEqualsAndHashCode() {
Assert.assertEquals(new HashSet<>(Arrays.asList(HttpMethod.GET)), matcher.getMethods());
}
@Override
public void testGetContent() {
Assert.assertEquals(new HashSet<>(Arrays.asList(HttpMethod.GET)), matcher.getContent());
}
@Override
public void testGetToStringInfix() {
Assert.assertEquals(" || ", matcher.getToStringInfix());
}
}

View File

@@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.http.client.MockClientHttpRequest;
import java.net.URI;
import java.util.List;
import java.util.Map;
/**
* {@link HttpRequestParamsMatcher} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class HttpRequestParamsMatcherTest {
// @Test
// public void testGetParams() {
//
// HttpRequestParamsMatcher matcher = new HttpRequestParamsMatcher(
// "a ",
// "a =1",
// "b = 2",
// "b = 3 ",
// " c = 4 ",
// " d"
// );
//
// Map<String, List<String>> params = matcher.getParams();
// Assert.assertEquals(4, params.size());
// Assert.assertTrue(params.containsKey("a"));
// Assert.assertTrue(params.containsKey("b"));
// Assert.assertTrue(params.containsKey("c"));
// Assert.assertTrue(params.containsKey("d"));
// Assert.assertFalse(params.containsKey("e"));
//
// List<String> values = params.get("a");
// Assert.assertEquals(2, values.size());
// Assert.assertEquals("", values.get(0));
// Assert.assertEquals("1", values.get(1));
//
// values = params.get("b");
// Assert.assertEquals(2, values.size());
// Assert.assertEquals("2", values.get(0));
// Assert.assertEquals("3", values.get(1));
//
// values = params.get("c");
// Assert.assertEquals(1, values.size());
// Assert.assertEquals("4", values.get(0));
//
// values = params.get("d");
// Assert.assertEquals(1, values.size());
// Assert.assertEquals("", values.get(0));
// }
@Test
public void testEquals() {
HttpRequestParamsMatcher matcher = new HttpRequestParamsMatcher("a ", "a = 1");
MockClientHttpRequest request = new MockClientHttpRequest();
request.setURI(URI.create("http://dummy/?a"));
Assert.assertTrue(matcher.match(request));
request.setURI(URI.create("http://dummy/?a&a=1"));
Assert.assertTrue(matcher.match(request));
matcher = new HttpRequestParamsMatcher("a ", "a =1", "b", "b=2");
request.setURI(URI.create("http://dummy/?a&a=1&b"));
Assert.assertTrue(matcher.match(request));
request.setURI(URI.create("http://dummy/?a&a=1&b&b=2"));
Assert.assertTrue(matcher.match(request));
matcher = new HttpRequestParamsMatcher("a ", "a =1", "b", "b=2", "b = 3 ");
request.setURI(URI.create("http://dummy/?a&a=1&b&b=2&b=3"));
Assert.assertTrue(matcher.match(request));
request.setURI(URI.create("http://dummy/?d=1"));
Assert.assertFalse(matcher.match(request));
}
}

View File

@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.http.HttpRequest;
import static org.springframework.cloud.alibaba.dubbo.http.DefaultHttpRequest.builder;
/**
* {@link ParamExpression} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ParamExpressionTest extends AbstractNameValueExpressionTest<ParamExpression> {
@Test
public void testIsCaseSensitiveName() {
Assert.assertTrue(createExpression("a=1").isCaseSensitiveName());
Assert.assertTrue(createExpression("a=!1").isCaseSensitiveName());
Assert.assertTrue(createExpression("b=1").isCaseSensitiveName());
}
@Test
public void testMatch() {
ParamExpression expression = createExpression("a=1");
HttpRequest request = builder().build();
Assert.assertFalse(expression.match(request));
request = builder().param("a", "").build();
Assert.assertFalse(expression.match(request));
request = builder().param("a", "2").build();
Assert.assertFalse(expression.match(request));
request = builder().param("", "1").build();
Assert.assertFalse(expression.match(request));
request = builder().param("a", "1").build();
Assert.assertTrue(expression.match(request));
}
}

View File

@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.http.MediaType;
import java.util.Arrays;
/**
* {@link ProduceMediaTypeExpression} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ProduceMediaTypeExpressionTest extends AbstractMediaTypeExpressionTest<ProduceMediaTypeExpression> {
@Test
public void testMatch() {
ProduceMediaTypeExpression expression = createExpression(MediaType.APPLICATION_JSON_VALUE);
Assert.assertTrue(expression.match(Arrays.asList(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON)));
expression = createExpression(MediaType.APPLICATION_JSON_VALUE);
Assert.assertTrue(expression.match(Arrays.asList(MediaType.APPLICATION_XML)));
}
}

View File

@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.http.util;
import org.junit.Assert;
import org.junit.Test;
/**
* {@link HttpUtils} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class HttpUtilsTest {
@Test
public void testEncodeAndDecode() {
String whitespace = " ";
String encodedValue = HttpUtils.encode(" ");
String decodedValue = HttpUtils.decode(encodedValue);
Assert.assertEquals(whitespace, decodedValue);
}
}

View File

@@ -0,0 +1,136 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alibaba.dubbo.metadata;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* {@link RequestMetadata} Test
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class RequestMetadataTest {
private String method = "GET";
private String url = "/echo";
private Set<String> paramNames = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
private Set<String> headerNames = new LinkedHashSet<>(Arrays.asList("d", "e", "f"));
@Test
public void testEqualsAndHashCodeAndCompareTo() {
RequestMetadata metadata = new RequestMetadata();
RequestMetadata metadata2 = new RequestMetadata();
Assert.assertEquals(metadata, metadata2);
Assert.assertEquals(metadata.hashCode(), metadata2.hashCode());
metadata.setMethod(method);
metadata2.setMethod(method);
Assert.assertEquals(metadata, metadata2);
Assert.assertEquals(metadata.hashCode(), metadata2.hashCode());
metadata.setPath(url);
metadata2.setPath(url);
Assert.assertEquals(metadata, metadata2);
Assert.assertEquals(metadata.hashCode(), metadata2.hashCode());
metadata.addParam("a", "1").addParam("b", "2").addParam("c", "3");
metadata2.addParam("a", "1a").addParam("b", "2b").addParam("c", "3c");
Assert.assertEquals(metadata, metadata2);
Assert.assertEquals(metadata.hashCode(), metadata2.hashCode());
metadata.addHeader("d", "1").addHeader("e", "2").addHeader("f", "3");
metadata2.addHeader("d", "1").addHeader("e", "2");
Assert.assertEquals(metadata, metadata2);
Assert.assertEquals(metadata.hashCode(), metadata2.hashCode());
}
// @Test
// public void testBestMatch() {
//
// NavigableMap<RequestMetadata, RequestMetadata> requestMetadataMap = new TreeMap<>();
//
// RequestMetadata metadata = new RequestMetadata();
// metadata.setMethod(method);
//
// RequestMetadata metadata1 = new RequestMetadata();
// metadata1.setMethod(method);
// metadata1.setPath(url);
//
// RequestMetadata metadata2 = new RequestMetadata();
// metadata2.setMethod(method);
// metadata2.setPath(url);
// metadata2.setParams(paramNames);
//
// RequestMetadata metadata3 = new RequestMetadata();
// metadata3.setMethod(method);
// metadata3.setPath(url);
// metadata3.setParams(paramNames);
// metadata3.setHeaders(headerNames);
//
// requestMetadataMap.put(metadata, metadata);
// requestMetadataMap.put(metadata1, metadata1);
// requestMetadataMap.put(metadata2, metadata2);
// requestMetadataMap.put(metadata3, metadata3);
//
// RequestMetadata result = getBestMatch(requestMetadataMap, metadata);
// Assert.assertEquals(result, metadata);
//
// result = getBestMatch(requestMetadataMap, metadata1);
// Assert.assertEquals(result, metadata1);
//
// result = getBestMatch(requestMetadataMap, metadata2);
// Assert.assertEquals(result, metadata2);
//
// result = getBestMatch(requestMetadataMap, metadata3);
// Assert.assertEquals(result, metadata3);
//
// // REDO
// requestMetadataMap.clear();
//
// requestMetadataMap.put(metadata1, metadata1);
//
// result = getBestMatch(requestMetadataMap, metadata2);
// Assert.assertEquals(metadata1, result);
//
// requestMetadataMap.put(metadata2, metadata2);
//
// result = getBestMatch(requestMetadataMap, metadata3);
// Assert.assertEquals(metadata2, result);
//
// result = getBestMatch(requestMetadataMap, new RequestMetadata());
// Assert.assertNull(result);
//
// result = getBestMatch(requestMetadataMap, metadata);
// Assert.assertNull(result);
//
// }
}

View File

@@ -19,18 +19,23 @@ package org.springframework.cloud.alibaba.dubbo.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.util.Map;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
/**
* Default {@link EchoService}
@@ -43,14 +48,10 @@ import javax.ws.rs.QueryParam;
public class DefaultEchoService implements EchoService {
@Override
@GetMapping(value = "/echo"
// consumes = MediaType.APPLICATION_JSON_VALUE,
// produces = MediaType.APPLICATION_JSON_UTF8_VALUE
)
@GetMapping(value = "/echo", produces = APPLICATION_JSON_UTF8_VALUE)
@Path("/echo")
@GET
// @Consumes("application/json")
// @Produces("application/json;charset=UTF-8")
@Produces(APPLICATION_JSON_UTF8_VALUE)
public String echo(@RequestParam @QueryParam("message") String message) {
return RpcContext.getContext().getUrl() + " [echo] : " + message;
}
@@ -60,6 +61,39 @@ public class DefaultEchoService implements EchoService {
@Path("/plus")
@POST
public String plus(@RequestParam @QueryParam("a") int a, @RequestParam @QueryParam("b") int b) {
return null;
return String.valueOf(a + b);
}
@Override
@PostMapping("/toString")
@Path("/toString")
@POST
public String toString(@RequestBody Map<String, Object> data) {
return data.toString();
}
@Override
@GetMapping("/header")
@Path("/header")
@GET
public String header(@RequestHeader("h") @HeaderParam("h") String header) {
return String.valueOf(header);
}
@Override
@PostMapping("/form")
@Path("/form")
@POST
public String form(@RequestParam("f") @FormParam("f") String form) {
return String.valueOf(form);
}
@Override
@GetMapping("/paramAndHeader")
@Path("/paramAndHeader")
@GET
public String paramAndHeader(@RequestHeader("h") @HeaderParam("h") @RequestParam("p") @QueryParam("p") String param,
@RequestHeader("h") @HeaderParam("h") String header) {
return param + header;
}
}

View File

@@ -16,6 +16,8 @@
*/
package org.springframework.cloud.alibaba.dubbo.service;
import java.util.Map;
/**
* Echo Service
*
@@ -27,4 +29,11 @@ public interface EchoService {
String plus(int a, int b);
String toString(Map<String, Object> data);
String header(String header);
String form(String form);
String paramAndHeader(String param, String header);
}