This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch weak-contract-type in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit f944fbf53d39f5e285bfa8760b12d4cda6cfeff6 Author: wujimin <[email protected]> AuthorDate: Wed Jul 3 21:53:07 2019 +0800 [SCB-1212[WIP][WEAK] producer response mapper switch to new mechanism --- ...letableFutureProducerResponseMapperFactory.java | 6 +- .../CseResponseProducerResponseMapperFactory.java | 4 +- .../producer/DefaultProducerResponseMapper.java | 10 +- .../DefaultProducerResponseMapperFactory.java | 16 +-- .../OptionalProducerResponseMapperFactory.java | 6 +- ...letableFutureProducerResponseMapperFactory.java | 76 -------------- ...stCseResponseProducerResponseMapperFactory.java | 45 --------- .../TestDefaultProducerResponseMapper.java | 40 -------- .../TestDefaultProducerResponseMapperFactory.java | 55 ---------- .../TestProducerResponseMapperFactorys.java | 111 +++++++++++++++++++++ 10 files changed, 123 insertions(+), 246 deletions(-) diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CompletableFutureProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CompletableFutureProducerResponseMapperFactory.java index 2191608..5476719 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CompletableFutureProducerResponseMapperFactory.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CompletableFutureProducerResponseMapperFactory.java @@ -24,7 +24,7 @@ import org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys public class CompletableFutureProducerResponseMapperFactory implements ProducerResponseMapperFactory { @Override - public boolean isMatch(Type swaggerType, Type producerType) { + public boolean isMatch(Type producerType) { if (!ParameterizedType.class.isAssignableFrom(producerType.getClass())) { return false; } @@ -34,8 +34,8 @@ public class CompletableFutureProducerResponseMapperFactory implements ProducerR @Override public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys, - Type swaggerType, Type producerType) { + Type producerType) { Type realProducerType = ((ParameterizedType) producerType).getActualTypeArguments()[0]; - return factorys.createResponseMapper(swaggerType, realProducerType); + return factorys.createResponseMapper(realProducerType); } } diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CseResponseProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CseResponseProducerResponseMapperFactory.java index b736522..4434872 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CseResponseProducerResponseMapperFactory.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/CseResponseProducerResponseMapperFactory.java @@ -25,13 +25,13 @@ public class CseResponseProducerResponseMapperFactory implements ProducerRespons private static final ProducerResponseMapper SAME = (status, response) -> (Response) response; @Override - public boolean isMatch(Type swaggerType, Type producerType) { + public boolean isMatch(Type producerType) { return producerType.equals(Response.class); } @Override public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys, - Type swaggerType, Type producerType) { + Type producerType) { return SAME; } } diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapper.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapper.java index 2d35e7c..aa4f2ce 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapper.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapper.java @@ -19,18 +19,10 @@ package org.apache.servicecomb.swagger.invocation.response.producer; import javax.ws.rs.core.Response.StatusType; import org.apache.servicecomb.swagger.invocation.Response; -import org.apache.servicecomb.swagger.invocation.converter.Converter; public class DefaultProducerResponseMapper implements ProducerResponseMapper { - private Converter converter; - - public DefaultProducerResponseMapper(Converter converter) { - this.converter = converter; - } - @Override public Response mapResponse(StatusType status, Object response) { - Object swaggerResult = converter.convert(response); - return Response.create(status, swaggerResult); + return Response.create(status, response); } } diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapperFactory.java index 5e28204..dbf1df3 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapperFactory.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/DefaultProducerResponseMapperFactory.java @@ -18,32 +18,22 @@ package org.apache.servicecomb.swagger.invocation.response.producer; import java.lang.reflect.Type; -import org.apache.servicecomb.swagger.invocation.converter.Converter; -import org.apache.servicecomb.swagger.invocation.converter.ConverterMgr; import org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys; public class DefaultProducerResponseMapperFactory implements ProducerResponseMapperFactory { - private ConverterMgr converterMgr; - - @Override - public void setConverterMgr(ConverterMgr converterMgr) { - this.converterMgr = converterMgr; - } - @Override public int getOrder() { return Integer.MAX_VALUE; } @Override - public boolean isMatch(Type swaggerType, Type producerType) { + public boolean isMatch(Type producerType) { return true; } @Override public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys, - Type swaggerType, Type producerType) { - Converter converter = converterMgr.findConverter(producerType, swaggerType); - return new DefaultProducerResponseMapper(converter); + Type producerType) { + return new DefaultProducerResponseMapper(); } } diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/OptionalProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/OptionalProducerResponseMapperFactory.java index 4e0fcd6..b7b45a1 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/OptionalProducerResponseMapperFactory.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/producer/OptionalProducerResponseMapperFactory.java @@ -24,7 +24,7 @@ import org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys public class OptionalProducerResponseMapperFactory implements ProducerResponseMapperFactory { @Override - public boolean isMatch(Type swaggerType, Type producerType) { + public boolean isMatch(Type producerType) { if (!ParameterizedType.class.isAssignableFrom(producerType.getClass())) { return false; } @@ -34,9 +34,9 @@ public class OptionalProducerResponseMapperFactory implements ProducerResponseMa @Override public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys, - Type swaggerType, Type producerType) { + Type producerType) { Type realProducerType = ((ParameterizedType) producerType).getActualTypeArguments()[0]; - ProducerResponseMapper realMapper = factorys.createResponseMapper(swaggerType, realProducerType); + ProducerResponseMapper realMapper = factorys.createResponseMapper(realProducerType); return new OptionalProducerResponseMapper(realMapper); } } diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestCompletableFutureProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestCompletableFutureProducerResponseMapperFactory.java deleted file mode 100644 index a1c8c47..0000000 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestCompletableFutureProducerResponseMapperFactory.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.servicecomb.swagger.invocation.response.producer; - -import java.lang.reflect.Method; -import java.util.List; -import java.util.concurrent.CompletableFuture; - -import javax.ws.rs.core.Response.Status; - -import org.apache.servicecomb.foundation.common.utils.ReflectUtils; -import org.apache.servicecomb.swagger.invocation.Response; -import org.apache.servicecomb.swagger.invocation.converter.ConverterMgr; -import org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys; -import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; - -public class TestCompletableFutureProducerResponseMapperFactory { - CompletableFutureProducerResponseMapperFactory factory = new CompletableFutureProducerResponseMapperFactory(); - - ConverterMgr converterMgr = new ConverterMgr(); - - ResponseMapperFactorys<ProducerResponseMapper> factorys = - new ResponseMapperFactorys<>(ProducerResponseMapperFactory.class, converterMgr); - - public CompletableFuture<String[]> producer() { - return null; - } - - public List<String> swagger() { - return null; - } - - @Test - public void isMatch_true() { - Method method = ReflectUtils.findMethod(this.getClass(), "producer"); - Assert.assertTrue(factory.isMatch(null, method.getGenericReturnType())); - } - - @Test - public void isMatch_Parameterized_false() { - Method method = ReflectUtils.findMethod(this.getClass(), "swagger"); - Assert.assertFalse(factory.isMatch(null, method.getGenericReturnType())); - } - - @Test - public void isMatch_false() { - Assert.assertFalse(factory.isMatch(null, String.class)); - } - - @Test - public void completableFuture() { - Method producerMethod = ReflectUtils.findMethod(this.getClass(), "producer"); - Method swaggerMethod = ReflectUtils.findMethod(this.getClass(), "swagger"); - ProducerResponseMapper mapper = factory - .createResponseMapper(factorys, swaggerMethod.getGenericReturnType(), producerMethod.getGenericReturnType()); - - Response response = mapper.mapResponse(Status.OK, new String[] {"a", "b"}); - Assert.assertThat(response.getResult(), Matchers.contains("a", "b")); - } -} diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestCseResponseProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestCseResponseProducerResponseMapperFactory.java deleted file mode 100644 index 22f2ddc..0000000 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestCseResponseProducerResponseMapperFactory.java +++ /dev/null @@ -1,45 +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.servicecomb.swagger.invocation.response.producer; - -import javax.ws.rs.core.Response.Status; - -import org.apache.servicecomb.swagger.invocation.Response; -import org.junit.Assert; -import org.junit.Test; - -public class TestCseResponseProducerResponseMapperFactory { - CseResponseProducerResponseMapperFactory factory = new CseResponseProducerResponseMapperFactory(); - - @Test - public void isMatch_true() { - Assert.assertTrue(factory.isMatch(String.class, Response.class)); - } - - @Test - public void isMatch_false() { - Assert.assertFalse(factory.isMatch(String.class, String.class)); - } - - @Test - public void createResponseMapper() { - ProducerResponseMapper mapper = factory.createResponseMapper(null, null, Response.class); - Response response = Response.ok(null); - - Assert.assertSame(response, mapper.mapResponse(Status.OK, response)); - } -} diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestDefaultProducerResponseMapper.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestDefaultProducerResponseMapper.java deleted file mode 100644 index c9e980b..0000000 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestDefaultProducerResponseMapper.java +++ /dev/null @@ -1,40 +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.servicecomb.swagger.invocation.response.producer; - -import javax.ws.rs.core.Response.Status; - -import org.apache.servicecomb.swagger.invocation.Response; -import org.apache.servicecomb.swagger.invocation.converter.Converter; -import org.apache.servicecomb.swagger.invocation.converter.ConverterMgr; -import org.junit.Assert; -import org.junit.Test; - -public class TestDefaultProducerResponseMapper { - ConverterMgr mgr = new ConverterMgr(); - - Converter converter = mgr.findConverter(Integer.class, String.class); - - DefaultProducerResponseMapper mapper = new DefaultProducerResponseMapper(converter); - - @Test - public void mapResponse() { - Response result = mapper.mapResponse(Status.OK, 1); - Assert.assertSame(Status.OK, result.getStatus()); - Assert.assertEquals("1", result.getResult()); - } -} diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestDefaultProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestDefaultProducerResponseMapperFactory.java deleted file mode 100644 index 42aaac9..0000000 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestDefaultProducerResponseMapperFactory.java +++ /dev/null @@ -1,55 +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.servicecomb.swagger.invocation.response.producer; - -import javax.ws.rs.core.Response.Status; - -import org.apache.servicecomb.swagger.invocation.Response; -import org.apache.servicecomb.swagger.invocation.converter.ConverterMgr; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class TestDefaultProducerResponseMapperFactory { - static ConverterMgr mgr = new ConverterMgr(); - - static DefaultProducerResponseMapperFactory factory = new DefaultProducerResponseMapperFactory(); - - @BeforeClass - public static void setup() { - factory.setConverterMgr(mgr); - } - - @Test - public void getOrder() { - Assert.assertEquals(Integer.MAX_VALUE, factory.getOrder()); - } - - @Test - public void isMatch() { - Assert.assertTrue(factory.isMatch(null, null)); - } - - @Test - public void createResponseMapper() { - ProducerResponseMapper mapper = factory.createResponseMapper(null, String.class, Integer.class); - Response result = mapper.mapResponse(Status.OK, 1); - - Assert.assertSame(Status.OK, result.getStatus()); - Assert.assertEquals("1", result.getResult()); - } -} diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestProducerResponseMapperFactorys.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestProducerResponseMapperFactorys.java new file mode 100644 index 0000000..d06a541 --- /dev/null +++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/producer/TestProducerResponseMapperFactorys.java @@ -0,0 +1,111 @@ +/* + * 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.servicecomb.swagger.invocation.response.producer; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import javax.ws.rs.core.Response.Status; + +import org.apache.servicecomb.swagger.engine.SwaggerEnvironment; +import org.apache.servicecomb.swagger.engine.SwaggerProducer; +import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation; +import org.apache.servicecomb.swagger.generator.SwaggerGenerator; +import org.apache.servicecomb.swagger.invocation.Response; +import org.junit.BeforeClass; +import org.junit.Test; + +import io.swagger.annotations.ApiResponse; +import io.swagger.models.Swagger; + +public class TestProducerResponseMapperFactorys { + static class ResponseForTest { + public String normal() { + return "normal"; + } + + public CompletableFuture<String> async() { + return CompletableFuture.completedFuture("async"); + } + + @ApiResponse(code = 200, message = "", response = String.class) + public Response scbResponse() { + return Response.ok("scb"); + } + + @ApiResponse(code = 200, message = "", response = String.class) + public javax.ws.rs.core.Response jaxrsResponse() { + return javax.ws.rs.core.Response.ok("jaxrs").build(); + } + + public Optional<String> optional() { + return Optional.of("optional"); + } + } + + static SwaggerEnvironment environment = new SwaggerEnvironment(); + + static SwaggerProducer swaggerProducer; + + static ResponseForTest instance = new ResponseForTest(); + + static String result = "abc"; + + static Response response = Response.ok(result); + + @BeforeClass + public static void setup() { + Swagger swagger = SwaggerGenerator.generate(ResponseForTest.class); + swaggerProducer = environment.createProducer(instance, swagger); + } + + @Test + public void should_mapper_to_normal_string() { + SwaggerProducerOperation operation = swaggerProducer.findOperation("normal"); + assertThat(operation.getResponseMapper()).isInstanceOf(DefaultProducerResponseMapper.class); + assertThat((String) operation.getResponseMapper().mapResponse(Status.OK, instance.normal()).getResult()) + .isEqualTo("normal"); + } + + @Test + public void should_mapper_to_completableFuture_element_string() throws ExecutionException, InterruptedException { + SwaggerProducerOperation operation = swaggerProducer.findOperation("async"); + assertThat(operation.getResponseMapper()).isInstanceOf(DefaultProducerResponseMapper.class); + assertThat((String) operation.getResponseMapper().mapResponse(Status.OK, instance.async().get()).getResult()) + .isEqualTo("async"); + } + + @Test + public void should_mapper_to_scbResponse_string() { + SwaggerProducerOperation operation = swaggerProducer.findOperation("scbResponse"); + assertThat(operation.getResponseMapper().getClass().getName()) + .startsWith(CseResponseProducerResponseMapperFactory.class.getName()); + assertThat((String) operation.getResponseMapper().mapResponse(Status.OK, instance.scbResponse()).getResult()) + .isEqualTo("scb"); + } + + @Test + public void should_mapper_to_optional_string() { + SwaggerProducerOperation operation = swaggerProducer.findOperation("optional"); + assertThat(operation.getResponseMapper()).isInstanceOf(OptionalProducerResponseMapper.class); + assertThat((String) operation.getResponseMapper().mapResponse(Status.OK, instance.optional()).getResult()) + .isEqualTo("optional"); + } +}
