You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2019/07/04 09:38:13 UTC
[servicecomb-java-chassis] 03/07: [SCB-1212[WIP][WEAK] consumer
response mapper switch to new mechanism
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 6c108b3e68ffed0ca1801db9056c97b424149257
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Jul 3 21:51:13 2019 +0800
[SCB-1212[WIP][WEAK] consumer response mapper switch to new mechanism
---
...letableFutureConsumerResponseMapperFactory.java | 6 +-
.../CseResponseConsumerResponseMapperFactory.java | 4 +-
.../consumer/DefaultConsumerResponseMapper.java | 9 +-
.../DefaultConsumerResponseMapperFactory.java | 16 +---
.../OptionalConsumerResponseMapperFactory.java | 6 +-
...letableFutureConsumerResponseMapperFactory.java | 76 -----------------
.../TestConsumerResponseMapperFactorys.java | 96 ++++++++++++++++++++++
...stCseResponseConsumerResponseMapperFactory.java | 43 ----------
.../TestDefaultConsumerResponseMapper.java | 37 ---------
.../TestDefaultConsumerResponseMapperFactory.java | 51 ------------
10 files changed, 108 insertions(+), 236 deletions(-)
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CompletableFutureConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CompletableFutureConsumerResponseMapperFactory.java
index 742958d..eabe5fd 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CompletableFutureConsumerResponseMapperFactory.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CompletableFutureConsumerResponseMapperFactory.java
@@ -24,7 +24,7 @@ import org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys
public class CompletableFutureConsumerResponseMapperFactory implements ConsumerResponseMapperFactory {
@Override
- public boolean isMatch(Type swaggerType, Type consumerType) {
+ public boolean isMatch(Type consumerType) {
if (!ParameterizedType.class.isAssignableFrom(consumerType.getClass())) {
return false;
}
@@ -34,8 +34,8 @@ public class CompletableFutureConsumerResponseMapperFactory implements ConsumerR
@Override
public ConsumerResponseMapper createResponseMapper(ResponseMapperFactorys<ConsumerResponseMapper> factorys,
- Type swaggerType, Type consumerType) {
+ Type consumerType) {
Type realConsumerType = ((ParameterizedType) consumerType).getActualTypeArguments()[0];
- return factorys.createResponseMapper(swaggerType, realConsumerType);
+ return factorys.createResponseMapper(realConsumerType);
}
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CseResponseConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CseResponseConsumerResponseMapperFactory.java
index 86c294b..34fcafd 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CseResponseConsumerResponseMapperFactory.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/CseResponseConsumerResponseMapperFactory.java
@@ -25,13 +25,13 @@ public class CseResponseConsumerResponseMapperFactory implements ConsumerRespons
private static final ConsumerResponseMapper SAME = response -> response;
@Override
- public boolean isMatch(Type swaggerType, Type consumerType) {
+ public boolean isMatch(Type consumerType) {
return Response.class.equals(consumerType);
}
@Override
public ConsumerResponseMapper createResponseMapper(ResponseMapperFactorys<ConsumerResponseMapper> factorys,
- Type swaggerType, Type consumerType) {
+ Type consumerType) {
return SAME;
}
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapper.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapper.java
index e43e37e..d430050 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapper.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapper.java
@@ -17,17 +17,10 @@
package org.apache.servicecomb.swagger.invocation.response.consumer;
import org.apache.servicecomb.swagger.invocation.Response;
-import org.apache.servicecomb.swagger.invocation.converter.Converter;
public class DefaultConsumerResponseMapper implements ConsumerResponseMapper {
- private Converter converter;
-
- public DefaultConsumerResponseMapper(Converter converter) {
- this.converter = converter;
- }
-
@Override
public Object mapResponse(Response response) {
- return converter.convert(response.getResult());
+ return response.getResult();
}
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapperFactory.java
index bdd9643..0dd56c0 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapperFactory.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/DefaultConsumerResponseMapperFactory.java
@@ -18,32 +18,22 @@ package org.apache.servicecomb.swagger.invocation.response.consumer;
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 DefaultConsumerResponseMapperFactory implements ConsumerResponseMapperFactory {
- 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 consumerType) {
+ public boolean isMatch(Type consumerType) {
return true;
}
@Override
public ConsumerResponseMapper createResponseMapper(ResponseMapperFactorys<ConsumerResponseMapper> factorys,
- Type swaggerType, Type consumerType) {
- Converter converter = converterMgr.findConverter(swaggerType, consumerType);
- return new DefaultConsumerResponseMapper(converter);
+ Type consumerType) {
+ return new DefaultConsumerResponseMapper();
}
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/OptionalConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/OptionalConsumerResponseMapperFactory.java
index 29b95dc..8c08afd 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/OptionalConsumerResponseMapperFactory.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/consumer/OptionalConsumerResponseMapperFactory.java
@@ -24,7 +24,7 @@ import org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys
public class OptionalConsumerResponseMapperFactory implements ConsumerResponseMapperFactory {
@Override
- public boolean isMatch(Type swaggerType, Type consumerType) {
+ public boolean isMatch(Type consumerType) {
if (!ParameterizedType.class.isAssignableFrom(consumerType.getClass())) {
return false;
}
@@ -34,9 +34,9 @@ public class OptionalConsumerResponseMapperFactory implements ConsumerResponseMa
@Override
public ConsumerResponseMapper createResponseMapper(ResponseMapperFactorys<ConsumerResponseMapper> factorys,
- Type swaggerType, Type consumerType) {
+ Type consumerType) {
Type realConsumerType = ((ParameterizedType) consumerType).getActualTypeArguments()[0];
- ConsumerResponseMapper realMapper = factorys.createResponseMapper(swaggerType, realConsumerType);
+ ConsumerResponseMapper realMapper = factorys.createResponseMapper(realConsumerType);
return new OptionalConsumerResponseMapper(realMapper);
}
}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestCompletableFutureConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestCompletableFutureConsumerResponseMapperFactory.java
deleted file mode 100644
index ea4cc49..0000000
--- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestCompletableFutureConsumerResponseMapperFactory.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.consumer;
-
-import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.List;
-import java.util.concurrent.CompletableFuture;
-
-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 TestCompletableFutureConsumerResponseMapperFactory {
- CompletableFutureConsumerResponseMapperFactory factory = new CompletableFutureConsumerResponseMapperFactory();
-
- ConverterMgr converterMgr = new ConverterMgr();
-
- ResponseMapperFactorys<ConsumerResponseMapper> factorys =
- new ResponseMapperFactorys<>(ConsumerResponseMapperFactory.class, converterMgr);
-
- public CompletableFuture<String[]> consumer() {
- return null;
- }
-
- public List<String> swagger() {
- return null;
- }
-
- @Test
- public void isMatch_true() {
- Method method = ReflectUtils.findMethod(this.getClass(), "consumer");
- 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 consumerMethod = ReflectUtils.findMethod(this.getClass(), "consumer");
- Method swaggerMethod = ReflectUtils.findMethod(this.getClass(), "swagger");
- ConsumerResponseMapper mapper = factory
- .createResponseMapper(factorys, swaggerMethod.getGenericReturnType(), consumerMethod.getGenericReturnType());
-
- Response response = Response.ok(Arrays.asList("a", "b"));
- String[] arr = (String[]) mapper.mapResponse(response);
- Assert.assertThat(arr, Matchers.arrayContaining("a", "b"));
- }
-}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestConsumerResponseMapperFactorys.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestConsumerResponseMapperFactorys.java
new file mode 100644
index 0000000..d9e64a3
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestConsumerResponseMapperFactorys.java
@@ -0,0 +1,96 @@
+/*
+ * 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.consumer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.servicecomb.swagger.engine.SwaggerConsumer;
+import org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation;
+import org.apache.servicecomb.swagger.engine.SwaggerEnvironment;
+import org.apache.servicecomb.swagger.generator.SwaggerGenerator;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.swagger.annotations.ApiResponse;
+import io.swagger.models.Swagger;
+
+public class TestConsumerResponseMapperFactorys {
+ interface ConsumerResponseForTest {
+ String normal();
+
+ CompletableFuture<String> async();
+
+ @ApiResponse(code = 200, message = "", response = String.class)
+ Response scbResponse();
+
+ @ApiResponse(code = 200, message = "", response = String.class)
+ javax.ws.rs.core.Response jaxrsResponse();
+
+ Optional<String> optional();
+ }
+
+ SwaggerEnvironment environment = new SwaggerEnvironment();
+
+ SwaggerConsumer swaggerConsumer;
+
+ String result = "abc";
+
+ Response response = Response.ok(result);
+
+ @Before
+ public void setup() {
+ Swagger swagger = SwaggerGenerator.generate(ConsumerResponseForTest.class);
+ swaggerConsumer = environment.createConsumer(ConsumerResponseForTest.class, swagger);
+ }
+
+ @Test
+ public void should_mapper_to_normal_string() {
+ SwaggerConsumerOperation operation = swaggerConsumer.findOperation("normal");
+ assertThat(operation.getResponseMapper()).isInstanceOf(DefaultConsumerResponseMapper.class);
+ Assert.assertEquals(result, operation.getResponseMapper().mapResponse(response));
+ }
+
+ @Test
+ public void should_mapper_to_completableFuture_element_string() {
+ SwaggerConsumerOperation operation = swaggerConsumer.findOperation("async");
+ assertThat(operation.getResponseMapper()).isInstanceOf(DefaultConsumerResponseMapper.class);
+ Assert.assertEquals(result, operation.getResponseMapper().mapResponse(response));
+ }
+
+ @Test
+ public void should_mapper_to_scbResponse_string() {
+ SwaggerConsumerOperation operation = swaggerConsumer.findOperation("scbResponse");
+ assertThat(operation.getResponseMapper().getClass().getName())
+ .startsWith(CseResponseConsumerResponseMapperFactory.class.getName());
+ Response scbResponse = (Response) operation.getResponseMapper().mapResponse(response);
+ Assert.assertEquals(result, scbResponse.getResult());
+ }
+
+ @Test
+ public void should_mapper_to_optional_string() {
+ SwaggerConsumerOperation operation = swaggerConsumer.findOperation("optional");
+ assertThat(operation.getResponseMapper()).isInstanceOf(OptionalConsumerResponseMapper.class);
+ @SuppressWarnings("unchecked")
+ Optional<String> optional = (Optional<String>) operation.getResponseMapper().mapResponse(response);
+ Assert.assertEquals(result, optional.get());
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestCseResponseConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestCseResponseConsumerResponseMapperFactory.java
deleted file mode 100644
index 4238628..0000000
--- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestCseResponseConsumerResponseMapperFactory.java
+++ /dev/null
@@ -1,43 +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.consumer;
-
-import org.apache.servicecomb.swagger.invocation.Response;
-import org.junit.Assert;
-import org.junit.Test;
-
-public class TestCseResponseConsumerResponseMapperFactory {
- CseResponseConsumerResponseMapperFactory factory = new CseResponseConsumerResponseMapperFactory();
-
- @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() {
- ConsumerResponseMapper mapper = factory.createResponseMapper(null, null, Response.class);
- Response response = Response.ok(null);
-
- Assert.assertSame(response, mapper.mapResponse(response));
- }
-}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestDefaultConsumerResponseMapper.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestDefaultConsumerResponseMapper.java
deleted file mode 100644
index d88be49..0000000
--- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestDefaultConsumerResponseMapper.java
+++ /dev/null
@@ -1,37 +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.consumer;
-
-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 TestDefaultConsumerResponseMapper {
- ConverterMgr mgr = new ConverterMgr();
-
- Converter converter = mgr.findConverter(Integer.class, String.class);
-
- DefaultConsumerResponseMapper mapper = new DefaultConsumerResponseMapper(converter);
-
- @Test
- public void mapResponse() {
- Object result = mapper.mapResponse(Response.ok(1));
- Assert.assertEquals("1", result);
- }
-}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestDefaultConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestDefaultConsumerResponseMapperFactory.java
deleted file mode 100644
index 18991d9..0000000
--- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/consumer/TestDefaultConsumerResponseMapperFactory.java
+++ /dev/null
@@ -1,51 +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.consumer;
-
-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 TestDefaultConsumerResponseMapperFactory {
- static ConverterMgr mgr = new ConverterMgr();
-
- static DefaultConsumerResponseMapperFactory factory = new DefaultConsumerResponseMapperFactory();
-
- @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() {
- ConsumerResponseMapper mapper = factory.createResponseMapper(null, Integer.class, String.class);
- Object result = mapper.mapResponse(Response.ok(1));
- Assert.assertEquals("1", result);
- }
-}