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/06/20 03:50:17 UTC

[servicecomb-java-chassis] 02/02: [SCB-1305] add IT and ITSCBAsyncRestTemplate

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

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

commit 6d5d6622791ee05c21afa152f580a9c1ac7f78fc
Author: wangsheng <wa...@huawei.com>
AuthorDate: Wed Jun 19 10:08:43 2019 +0800

    [SCB-1305] add IT and ITSCBAsyncRestTemplate
---
 .../java/org/apache/servicecomb/it/Consumers.java  |  9 +++
 .../it/extend/engine/ITAsyncClientHttpRequest.java | 44 +++++++++++++
 .../engine/ITAsyncClientHttpRequestFactory.java    | 38 +++++++++++
 .../it/extend/engine/ITSCBAsyncRestTemplate.java   | 77 ++++++++++++++++++++++
 .../servicecomb/it/testcase/TestDefaultValue.java  | 14 ++++
 .../reference/async/CseAsyncClientHttpRequest.java |  2 +-
 6 files changed, 183 insertions(+), 1 deletion(-)

diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/Consumers.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/Consumers.java
index ea24dad..8a56980 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/Consumers.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/Consumers.java
@@ -18,8 +18,10 @@ package org.apache.servicecomb.it;
 
 import org.apache.servicecomb.it.extend.engine.GateRestTemplate;
 import org.apache.servicecomb.it.extend.engine.ITInvoker;
+import org.apache.servicecomb.it.extend.engine.ITSCBAsyncRestTemplate;
 import org.apache.servicecomb.it.extend.engine.ITSCBRestTemplate;
 import org.apache.servicecomb.it.junit.ITJUnitUtils;
+import org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncRestTemplate;
 import org.springframework.web.client.RestTemplate;
 
 public class Consumers<INTF> {
@@ -39,6 +41,8 @@ public class Consumers<INTF> {
 
   private String transport;
 
+  private CseAsyncRestTemplate cseAsyncRestTemplate;
+
   public Consumers(String schemaId, Class<INTF> intfCls) {
     this.schemaId = schemaId;
     this.intfCls = intfCls;
@@ -51,6 +55,7 @@ public class Consumers<INTF> {
     scbRestTemplate = new ITSCBRestTemplate(schemaId).init();
     edgeRestTemplate = GateRestTemplate.createEdgeRestTemplate(schemaId).init();
     zuulRestTemplate = null;// GateRestTemplate.createZuulRestTemplate(schemaId).init();
+    this.cseAsyncRestTemplate = new ITSCBAsyncRestTemplate(schemaId).init();
   }
 
   public String getSchemaId() {
@@ -76,4 +81,8 @@ public class Consumers<INTF> {
   public String getTransport() {
     return transport;
   }
+
+  public CseAsyncRestTemplate getCseAsyncRestTemplate() {
+    return cseAsyncRestTemplate;
+  }
 }
\ No newline at end of file
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITAsyncClientHttpRequest.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITAsyncClientHttpRequest.java
new file mode 100644
index 0000000..5989700
--- /dev/null
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITAsyncClientHttpRequest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.it.extend.engine;
+
+import java.net.URI;
+
+import org.apache.servicecomb.core.CseContext;
+import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
+import org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncClientHttpRequest;
+import org.springframework.http.HttpMethod;
+
+public class ITAsyncClientHttpRequest extends CseAsyncClientHttpRequest {
+  private String transport;
+
+  public ITAsyncClientHttpRequest(URI uri, HttpMethod method, String transport) {
+    super(uri, method);
+    this.transport = transport;
+  }
+
+  @Override
+  protected ReferenceConfig findReferenceConfig(String microserviceName) {
+    ReferenceConfig referenceConfig = CseContext.getInstance().getConsumerProviderManager()
+        .createReferenceConfig(microserviceName);
+    if (transport != null) {
+      referenceConfig.setTransport(transport);
+    }
+    return referenceConfig;
+  }
+}
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITAsyncClientHttpRequestFactory.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITAsyncClientHttpRequestFactory.java
new file mode 100644
index 0000000..53053b1
--- /dev/null
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITAsyncClientHttpRequestFactory.java
@@ -0,0 +1,38 @@
+/*
+ * 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.it.extend.engine;
+
+import java.net.URI;
+
+import org.apache.servicecomb.it.junit.ITJUnitUtils;
+import org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncClientHttpRequestFactory;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.client.AsyncClientHttpRequest;
+
+public class ITAsyncClientHttpRequestFactory extends CseAsyncClientHttpRequestFactory {
+  private String transport;
+
+  public ITAsyncClientHttpRequestFactory() {
+    this.transport = ITJUnitUtils.getTransport();
+  }
+
+  @Override
+  public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) {
+    return new ITAsyncClientHttpRequest(uri, httpMethod, transport);
+  }
+}
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBAsyncRestTemplate.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBAsyncRestTemplate.java
new file mode 100644
index 0000000..9a7dfb2
--- /dev/null
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBAsyncRestTemplate.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 org.apache.servicecomb.it.extend.engine;
+
+import java.util.Optional;
+
+import org.apache.servicecomb.core.definition.MicroserviceVersionMeta;
+import org.apache.servicecomb.core.definition.SchemaMeta;
+import org.apache.servicecomb.it.junit.ITJUnitUtils;
+import org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncRestTemplate;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule;
+import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
+
+public class ITSCBAsyncRestTemplate extends CseAsyncRestTemplate {
+  private String urlPrefix;
+
+  private String schemaId;
+
+  private String basePath;
+
+  private MicroserviceInstance instance;
+
+  public ITSCBAsyncRestTemplate(String schemaId) {
+    this.schemaId = schemaId;
+  }
+
+  public ITSCBAsyncRestTemplate init() {
+    String producerName = ITJUnitUtils.getProducerName();
+    MicroserviceVersionRule microserviceVersionRule = RegistryUtils.getServiceRegistry().getAppManager()
+        .getOrCreateMicroserviceVersionRule(RegistryUtils.getAppId(), producerName,
+            DefinitionConst.VERSION_RULE_ALL);
+    MicroserviceVersionMeta microserviceVersionMeta = microserviceVersionRule.getLatestMicroserviceVersion();
+    SchemaMeta schemaMeta = microserviceVersionMeta.getMicroserviceMeta().ensureFindSchemaMeta(schemaId);
+    basePath = schemaMeta.getSwagger().getBasePath();
+    urlPrefix = String.format("cse://%s%s", producerName, basePath);
+    instance = RegistryUtils.getServiceRegistry().getAppManager()
+        .getOrCreateMicroserviceManager(RegistryUtils.getAppId())
+        .getOrCreateMicroserviceVersions(producerName).getPulledInstances().get(0);
+
+    setUriTemplateHandler(new ITUriTemplateHandler(urlPrefix));
+    setAsyncRequestFactory(new ITAsyncClientHttpRequestFactory());
+
+    return this;
+  }
+
+  public String getBasePath() {
+    return basePath;
+  }
+
+  public String getUrlPrefix() {
+    return urlPrefix;
+  }
+
+  public String getAddress(String transport) {
+    Optional<String> addressHolder = instance.getEndpoints().stream()
+        .filter(endpoint -> endpoint.startsWith(transport))
+        .findFirst();
+    return addressHolder.get();
+  }
+}
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java
index d0b8039..1f39a6f 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java
@@ -17,10 +17,14 @@
 package org.apache.servicecomb.it.testcase;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 
 import org.apache.servicecomb.it.Consumers;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.junit.Test;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
 
 public class TestDefaultValue {
   interface DefaultValueIntf {
@@ -514,4 +518,14 @@ public class TestDefaultValue {
     assertEquals(defaultFloat,
         consumersSpringmvc.getSCBRestTemplate().postForObject("/floatFormRequire", null, float.class), 0.0f);
   }
+
+  @Test
+  public void stringHeader_springmvc_cart() {
+    HttpHeaders headers = new HttpHeaders();
+    headers.add("input", "setHeader");
+    HttpEntity<String> requestEntity = new HttpEntity<String>(null, headers);
+    assertNotEquals(defaultStr,
+        consumersSpringmvc.getCseAsyncRestTemplate()
+            .exchange("/stringHeader", HttpMethod.GET, requestEntity, String.class));
+  }
 }
diff --git a/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncClientHttpRequest.java b/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncClientHttpRequest.java
index d66a0c6..3512c87 100644
--- a/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncClientHttpRequest.java
+++ b/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncClientHttpRequest.java
@@ -40,7 +40,7 @@ public class CseAsyncClientHttpRequest extends CseClientHttpRequest implements A
   CseAsyncClientHttpRequest() {
   }
 
-  CseAsyncClientHttpRequest(URI uri, HttpMethod method) {
+  protected CseAsyncClientHttpRequest(URI uri, HttpMethod method) {
     this.setUri(uri);
     this.setMethod(method);
   }