diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java index 5bcbbd21a67b..bb9e4a8f1f72 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java @@ -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); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestServiceImpl.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java similarity index 78% rename from dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestServiceImpl.java rename to dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java index e9d31dcac353..37ec29a6d4e7 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java @@ -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(); + } +} diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/IDemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/IDemoService.java deleted file mode 100644 index 47dbfb76cba1..000000000000 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/IDemoService.java +++ /dev/null @@ -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(); -} diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index c2f2360c1447..df86ab20d95b 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -25,6 +25,7 @@ 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 void tearDown() { 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 exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url)); + Invoker 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 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 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 exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl)); + Exporter 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 void testExport() { @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 exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl)); + Exporter 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 void testNettyServer() { @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 exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl)); + Exporter 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 void testErrorHandler() { @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 exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl)); + Exporter 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 void testInvoke() { @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 exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl)); + Exporter 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 void testFilter() { @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 exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl)); + Exporter 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 void testRpcContextFilter() { @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)); }); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java deleted file mode 100644 index 7ab27cadfb37..000000000000 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java +++ /dev/null @@ -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 exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url)); - Invoker 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 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 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(); - } -} diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestService.java deleted file mode 100644 index fe0d5884408f..000000000000 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestService.java +++ /dev/null @@ -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); - -}