You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2017/12/26 07:18:47 UTC

[incubator-servicecomb-java-chassis] 09/10: JAV-591 add CompletableFuture response mapper support

This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit f187a4b3f95e645c2a747571d7ce076a5fe993d2
Author: wujimin <wu...@huawei.com>
AuthorDate: Sun Dec 24 03:22:52 2017 +0800

    JAV-591 add CompletableFuture response mapper support
---
 ...letableFutureConsumerResponseMapperFactory.java | 41 ++++++++++++
 ...letableFutureProducerResponseMapperFactory.java | 41 ++++++++++++
 ...response.consumer.ConsumerResponseMapperFactory |  1 +
 ...response.producer.ProducerResponseMapperFactory |  1 +
 ...letableFutureConsumerResponseMapperFactory.java | 77 ++++++++++++++++++++++
 ...letableFutureProducerResponseMapperFactory.java | 77 ++++++++++++++++++++++
 6 files changed, 238 insertions(+)

diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/consumer/CompletableFutureConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/consumer/CompletableFutureConsumerResponseMapperFactory.java
new file mode 100644
index 0000000..8b2ce72
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/consumer/CompletableFutureConsumerResponseMapperFactory.java
@@ -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 io.servicecomb.swagger.invocation.response.consumer;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.concurrent.CompletableFuture;
+
+import io.servicecomb.swagger.invocation.response.ResponseMapperFactorys;
+
+public class CompletableFutureConsumerResponseMapperFactory implements ConsumerResponseMapperFactory {
+  @Override
+  public boolean isMatch(Type swaggerType, Type consumerType) {
+    if (!ParameterizedType.class.isAssignableFrom(consumerType.getClass())) {
+      return false;
+    }
+
+    return ((ParameterizedType) consumerType).getRawType().equals(CompletableFuture.class);
+  }
+
+  @Override
+  public ConsumerResponseMapper createResponseMapper(ResponseMapperFactorys<ConsumerResponseMapper> factorys,
+      Type swaggerType, Type consumerType) {
+    Type realConsumerType = ((ParameterizedType) consumerType).getActualTypeArguments()[0];
+    return factorys.createResponseMapper(swaggerType, realConsumerType);
+  }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/producer/CompletableFutureProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/producer/CompletableFutureProducerResponseMapperFactory.java
new file mode 100644
index 0000000..7375a3f
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/producer/CompletableFutureProducerResponseMapperFactory.java
@@ -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 io.servicecomb.swagger.invocation.response.producer;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.concurrent.CompletableFuture;
+
+import io.servicecomb.swagger.invocation.response.ResponseMapperFactorys;
+
+public class CompletableFutureProducerResponseMapperFactory implements ProducerResponseMapperFactory {
+  @Override
+  public boolean isMatch(Type swaggerType, Type producerType) {
+    if (!ParameterizedType.class.isAssignableFrom(producerType.getClass())) {
+      return false;
+    }
+
+    return ((ParameterizedType) producerType).getRawType().equals(CompletableFuture.class);
+  }
+
+  @Override
+  public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys,
+      Type swaggerType, Type producerType) {
+    Type realProducerType = ((ParameterizedType) producerType).getActualTypeArguments()[0];
+    return factorys.createResponseMapper(swaggerType, realProducerType);
+  }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapperFactory b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapperFactory
index 8fa2e27..299d5e6 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapperFactory
+++ b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapperFactory
@@ -16,4 +16,5 @@
 #
 
 io.servicecomb.swagger.invocation.response.consumer.CseResponseConsumerResponseMapperFactory
+io.servicecomb.swagger.invocation.response.consumer.CompletableFutureConsumerResponseMapperFactory
 io.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapperFactory
\ No newline at end of file
diff --git a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory
index 7f8db5d..5ed6641 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory
+++ b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory
@@ -16,4 +16,5 @@
 #
 
 io.servicecomb.swagger.invocation.response.producer.CseResponseProducerResponseMapperFactory
+io.servicecomb.swagger.invocation.response.producer.CompletableFutureProducerResponseMapperFactory
 io.servicecomb.swagger.invocation.response.producer.DefaultProducerResponseMapperFactory
\ No newline at end of file
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/consumer/TestCompletableFutureConsumerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/consumer/TestCompletableFutureConsumerResponseMapperFactory.java
new file mode 100644
index 0000000..529d062
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/consumer/TestCompletableFutureConsumerResponseMapperFactory.java
@@ -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 io.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.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.foundation.common.utils.ReflectUtils;
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.converter.ConverterMgr;
+import io.servicecomb.swagger.invocation.response.ResponseMapperFactorys;
+
+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/io/servicecomb/swagger/invocation/response/producer/TestCompletableFutureProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/producer/TestCompletableFutureProducerResponseMapperFactory.java
new file mode 100644
index 0000000..8d5cf00
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/producer/TestCompletableFutureProducerResponseMapperFactory.java
@@ -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 io.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.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.foundation.common.utils.ReflectUtils;
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.converter.ConverterMgr;
+import io.servicecomb.swagger.invocation.response.ResponseMapperFactorys;
+
+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"));
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.