Skip to content

Commit

Permalink
[SCB-1305] add IT and ITSCBAsyncRestTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshenggh authored and liubao68 committed Jun 20, 2019
1 parent 4083f5b commit 6d5d662
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

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> {
Expand All @@ -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;
Expand All @@ -51,6 +55,7 @@ public void init() {
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() {
Expand All @@ -76,4 +81,8 @@ public RestTemplate getZuulRestTemplate() {
public String getTransport() {
return transport;
}

public CseAsyncRestTemplate getCseAsyncRestTemplate() {
return cseAsyncRestTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -514,4 +518,14 @@ public void floatForm_require_springmvc_rt() {
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 6d5d662

Please sign in to comment.