Merge RestProtocolTest.java (#3597)

This commit is contained in:
huazhongming 2019-03-07 16:26:17 +08:00 committed by Huxing Zhang
parent af8cbe5496
commit fe049b8ddd
6 changed files with 131 additions and 218 deletions

View File

@ -16,15 +16,26 @@
*/
package org.apache.dubbo.rpc.protocol.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
public class DemoService implements IDemoService {
@Override
public Integer hello(Integer a, Integer b) {
return a + b;
}
@Override
public String error() {
throw new RuntimeException();
}
@Path("/demoService")
public interface DemoService {
@GET
@Path("/hello")
Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b);
@GET
@Path("/error")
String error();
@POST
@Path("/say")
@Consumes({MediaType.TEXT_PLAIN})
String sayHello(String name);
}

View File

@ -1,36 +1,42 @@
/*
* 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.apache.dubbo.rpc.protol.rest;
/**
* RestServiceImpl
*/
public class RestServiceImpl implements RestService {
private boolean called;
public String sayHello(String name) {
called = true;
return "Hello, " + name;
}
public boolean isCalled() {
return called;
}
}
/*
* 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.apache.dubbo.rpc.protocol.rest;
public class DemoServiceImpl implements DemoService {
private boolean called;
public String sayHello(String name) {
called = true;
return "Hello, " + name;
}
public boolean isCalled() {
return called;
}
@Override
public Integer hello(Integer a, Integer b) {
return a + b;
}
@Override
public String error() {
throw new RuntimeException();
}
}

View File

@ -1,32 +0,0 @@
/*
* 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.apache.dubbo.rpc.protocol.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("/demoService")
public interface IDemoService {
@GET
@Path("/hello")
Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b);
@GET
@Path("/error")
String error();
}

View File

@ -25,6 +25,7 @@ import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
@ -49,16 +50,55 @@ public class RestProtocolTest {
protocol.destroy();
}
@Test
public void testRestProtocol() {
URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say?version=1.0.0");
DemoServiceImpl server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url));
Invoker<DemoService> invoker = protocol.refer(DemoService.class, url);
Assertions.assertFalse(server.isCalled());
DemoService client = proxy.getProxy(invoker);
String result = client.sayHello("haha");
Assertions.assertTrue(server.isCalled());
Assertions.assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}
@Test
public void testRestProtocolWithContextPath() {
DemoServiceImpl server = new DemoServiceImpl();
Assertions.assertFalse(server.isCalled());
URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0");
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url));
url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0");
Invoker<DemoService> invoker = protocol.refer(DemoService.class, url);
DemoService client = proxy.getProxy(invoker);
String result = client.sayHello("haha");
Assertions.assertTrue(server.isCalled());
Assertions.assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}
@Test
public void testExport() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
RpcContext.getContext().setAttachment("timeout", "200");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl));
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, exportUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, exportUrl));
Integer echoString = demoService.hello(1, 2);
assertThat(echoString, is(3));
@ -68,14 +108,14 @@ public class RestProtocolTest {
@Test
public void testNettyServer() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(new DemoServiceImpl(), DemoService.class, nettyUrl));
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));
Integer echoString = demoService.hello(10, 10);
assertThat(echoString, is(20));
@ -86,27 +126,27 @@ public class RestProtocolTest {
@Test
public void testServletWithoutWebConfig() {
Assertions.assertThrows(RpcException.class, () -> {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet");
protocol.export(proxy.getInvoker(server, IDemoService.class, servletUrl));
protocol.export(proxy.getInvoker(server, DemoService.class, servletUrl));
});
}
@Test
public void testErrorHandler() {
Assertions.assertThrows(RpcException.class, () -> {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));
demoService.error();
});
@ -114,12 +154,12 @@ public class RestProtocolTest {
@Test
public void testInvoke() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl));
RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3});
@ -129,15 +169,15 @@ public class RestProtocolTest {
@Test
public void testFilter() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
.addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.support.LoggingFilter");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));
Integer result = demoService.hello(1, 2);
@ -148,16 +188,16 @@ public class RestProtocolTest {
@Test
public void testRpcContextFilter() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
// use RpcContextFilter
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
.addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.RpcContextFilter");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));
IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));
String value = null;
// put a null value into attachment.
@ -172,12 +212,12 @@ public class RestProtocolTest {
@Test
public void testRegFail() {
Assertions.assertThrows(RuntimeException.class, () -> {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter");
protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));
});
}

View File

@ -1,76 +0,0 @@
/*
* 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.apache.dubbo.rpc.protol.rest;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* RestProtocolTest
*/
public class RestProtocolTest {
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
private ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
@Test
public void testRestProtocol() {
URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say1?version=1.0.0");
RestServiceImpl server = new RestServiceImpl();
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, RestService.class);
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);
Exporter<RestService> exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url));
Invoker<RestService> invoker = protocol.refer(RestService.class, url); Assertions.assertFalse(server.isCalled());
RestService client = proxyFactory.getProxy(invoker);
String result = client.sayHello("haha");
Assertions.assertTrue(server.isCalled());
Assertions.assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}
@Test
public void testRestProtocolWithContextPath() {
RestServiceImpl server = new RestServiceImpl();
Assertions.assertFalse(server.isCalled());
URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0");
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, RestService.class);
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);
Exporter<RestService> exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url));
url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0");
Invoker<RestService> invoker = protocol.refer(RestService.class, url);
RestService client = proxyFactory.getProxy(invoker);
String result = client.sayHello("haha");
Assertions.assertTrue(server.isCalled());
Assertions.assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}
}

View File

@ -1,36 +0,0 @@
/*
* 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.apache.dubbo.rpc.protol.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
/**
* RestService
*/
@Path("/rest")
public interface RestService {
@POST
@Path("/say1")
@Consumes({MediaType.TEXT_PLAIN})
String sayHello(String name);
}