From 511ef88ccc9c6be5527cf2472aef5e8b440a82ec Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Mon, 13 Jul 2020 18:54:06 +0800 Subject: [PATCH] fix:#3311 Add specified Logger when constructing nacos resttemplate (#3312) --- .../naming/net/NamingHttpClientManager.java | 7 ++ .../http/AbstractHttpClientFactory.java | 12 ++- .../common/http/DefaultHttpClientFactory.java | 13 ++++ .../common/http/HttpClientBeanHolder.java | 8 +- .../client/AbstractNacosRestTemplate.java | 11 ++- .../http/client/DefaultHttpClientRequest.java | 7 -- .../http/client/NacosAsyncRestTemplate.java | 13 ++-- .../common/http/client/NacosRestTemplate.java | 16 ++-- .../common/NacosAsyncRestTemplate_ITCase.java | 61 ++++++++------- .../test/common/NacosRestTemplate_ITCase.java | 75 +++++++++++-------- ...NacosRestTemplate_Interceptors_ITCase.java | 37 +++++---- 11 files changed, 152 insertions(+), 108 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingHttpClientManager.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingHttpClientManager.java index 0ff5f2c5d03..a900c2b8c6e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingHttpClientManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingHttpClientManager.java @@ -16,11 +16,13 @@ package com.alibaba.nacos.client.naming.net; +import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.common.http.AbstractHttpClientFactory; import com.alibaba.nacos.common.http.HttpClientBeanHolder; import com.alibaba.nacos.common.http.HttpClientConfig; import com.alibaba.nacos.common.http.HttpClientFactory; import com.alibaba.nacos.common.http.client.NacosRestTemplate; +import org.slf4j.Logger; /** * http Manager. @@ -58,5 +60,10 @@ protected HttpClientConfig buildHttpClientConfig() { return HttpClientConfig.builder().setConTimeOutMillis(CON_TIME_OUT_MILLIS) .setReadTimeOutMillis(READ_TIME_OUT_MILLIS).setMaxRedirects(MAX_REDIRECTS).build(); } + + @Override + protected Logger assignLogger() { + return LogUtils.NAMING_LOGGER; + } } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java b/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java index afe9b742bce..b3ed2568dc8 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java @@ -23,6 +23,7 @@ import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.nio.client.HttpAsyncClients; +import org.slf4j.Logger; /** * AbstractHttpClientFactory Let the creator only specify the http client config. @@ -34,14 +35,14 @@ public abstract class AbstractHttpClientFactory implements HttpClientFactory { @Override public final NacosRestTemplate createNacosRestTemplate() { RequestConfig requestConfig = getRequestConfig(); - return new NacosRestTemplate( + return new NacosRestTemplate(assignLogger(), new DefaultHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(requestConfig).build())); } @Override public final NacosAsyncRestTemplate createNacosAsyncRestTemplate() { RequestConfig requestConfig = getRequestConfig(); - return new NacosAsyncRestTemplate(new DefaultAsyncHttpClientRequest( + return new NacosAsyncRestTemplate(assignLogger(), new DefaultAsyncHttpClientRequest( HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build())); } @@ -58,4 +59,11 @@ private RequestConfig getRequestConfig() { * @return HttpClientConfig */ protected abstract HttpClientConfig buildHttpClientConfig(); + + /** + * assign Logger. + * + * @return Logger + */ + protected abstract Logger assignLogger(); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/DefaultHttpClientFactory.java b/common/src/main/java/com/alibaba/nacos/common/http/DefaultHttpClientFactory.java index 383271af42b..c8440c23c8f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/DefaultHttpClientFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/DefaultHttpClientFactory.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.common.http; +import org.slf4j.Logger; + /** * default http client factory. * @@ -25,8 +27,19 @@ public class DefaultHttpClientFactory extends AbstractHttpClientFactory { private static final int TIMEOUT = Integer.getInteger("nacos.http.timeout", 5000); + private final Logger logger; + + public DefaultHttpClientFactory(Logger logger) { + this.logger = logger; + } + @Override protected HttpClientConfig buildHttpClientConfig() { return HttpClientConfig.builder().setConTimeOutMillis(TIMEOUT).setReadTimeOutMillis(TIMEOUT >> 1).build(); } + + @Override + protected Logger assignLogger() { + return logger; + } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientBeanHolder.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientBeanHolder.java index 18d28a72528..0e264985d46 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientBeanHolder.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientBeanHolder.java @@ -52,8 +52,8 @@ public void run() { }); } - public static NacosRestTemplate getNacosRestTemplate() { - return getNacosRestTemplate(new DefaultHttpClientFactory()); + public static NacosRestTemplate getNacosRestTemplate(Logger logger) { + return getNacosRestTemplate(new DefaultHttpClientFactory(logger)); } public static NacosRestTemplate getNacosRestTemplate(HttpClientFactory httpClientFactory) { @@ -75,8 +75,8 @@ public static NacosRestTemplate getNacosRestTemplate(HttpClientFactory httpClien return nacosRestTemplate; } - public static NacosAsyncRestTemplate getNacosAsyncRestTemplate() { - return getNacosAsyncRestTemplate(new DefaultHttpClientFactory()); + public static NacosAsyncRestTemplate getNacosAsyncRestTemplate(Logger logger) { + return getNacosAsyncRestTemplate(new DefaultHttpClientFactory(logger)); } public static NacosAsyncRestTemplate getNacosAsyncRestTemplate(HttpClientFactory httpClientFactory) { diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/AbstractNacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/AbstractNacosRestTemplate.java index 5c91f874257..7edd64e6ebf 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/AbstractNacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/AbstractNacosRestTemplate.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.common.constant.ResponseHandlerType; import com.alibaba.nacos.common.utils.JacksonUtils; import com.fasterxml.jackson.databind.JavaType; +import org.slf4j.Logger; import java.lang.reflect.Type; import java.util.HashMap; @@ -29,11 +30,19 @@ * * @author mai.jh */ +@SuppressWarnings("all") public abstract class AbstractNacosRestTemplate { private final Map responseHandlerMap = new HashMap(); - public AbstractNacosRestTemplate() { + protected final Logger logger; + + public AbstractNacosRestTemplate(Logger logger) { + this.logger = logger; + initDefaultResponseHandler(); + } + + private void initDefaultResponseHandler() { // init response handler responseHandlerMap.put(ResponseHandlerType.STRING_TYPE, new StringResponseHandler()); responseHandlerMap.put(ResponseHandlerType.RESTRESULT_TYPE, new RestResultResponseHandler()); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java index c31cd5cae8c..459211bb0eb 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java @@ -26,8 +26,6 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.CloseableHttpClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URI; @@ -41,8 +39,6 @@ @SuppressWarnings({"unchecked", "resource"}) public class DefaultHttpClientRequest implements HttpClientRequest { - private static final Logger LOGGER = LoggerFactory.getLogger(DefaultHttpClientRequest.class); - private final CloseableHttpClient client; public DefaultHttpClientRequest(CloseableHttpClient client) { @@ -54,9 +50,6 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity throws Exception { HttpRequestBase request = build(uri, httpMethod, requestHttpEntity); CloseableHttpResponse response = client.execute(request); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Request from server: " + request.getURI().toString()); - } return new DefaultClientHttpResponse(response); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java index 3c317f3ce85..f894c3a6dc9 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java @@ -24,7 +24,6 @@ import com.alibaba.nacos.common.model.RequestHttpEntity; import com.alibaba.nacos.common.utils.HttpMethod; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.lang.reflect.Type; import java.net.URI; @@ -39,12 +38,10 @@ */ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate { - private static final Logger LOGGER = LoggerFactory.getLogger(NacosAsyncRestTemplate.class); + private final AsyncHttpClientRequest clientRequest; - private AsyncHttpClientRequest clientRequest; - - public NacosAsyncRestTemplate(AsyncHttpClientRequest clientRequest) { - super(); + public NacosAsyncRestTemplate(Logger logger, AsyncHttpClientRequest clientRequest) { + super(logger); this.clientRequest = clientRequest; } @@ -335,8 +332,8 @@ public void postForm(String url, Header header, Map paramVal private void execute(String url, String httpMethod, RequestHttpEntity requestEntity, Type type, Callback callback) throws Exception { URI uri = HttpUtils.buildUri(url, requestEntity.getQuery()); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("HTTP " + httpMethod + " " + url); + if (logger.isDebugEnabled()) { + logger.debug("HTTP method: {}, url: {}, body: {}", httpMethod, uri, requestEntity.getBody()); } ResponseHandler responseHandler = super.selectResponseHandler(type); clientRequest.execute(uri, httpMethod, requestEntity, responseHandler, callback); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index d710eecd0f7..38d9ce2d8cd 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -26,7 +26,6 @@ import com.alibaba.nacos.common.utils.CollectionUtils; import com.alibaba.nacos.common.utils.HttpMethod; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.lang.reflect.Type; import java.net.URI; @@ -43,14 +42,12 @@ */ public class NacosRestTemplate extends AbstractNacosRestTemplate { - private static final Logger LOGGER = LoggerFactory.getLogger(NacosRestTemplate.class); - private final HttpClientRequest requestClient; private final List interceptors = new ArrayList(); - public NacosRestTemplate(HttpClientRequest requestClient) { - super(); + public NacosRestTemplate(Logger logger, HttpClientRequest requestClient) { + super(logger); this.requestClient = requestClient; } @@ -447,9 +444,10 @@ public List getInterceptors() { private HttpRestResult execute(String url, String httpMethod, RequestHttpEntity requestEntity, Type responseType) throws Exception { URI uri = HttpUtils.buildUri(url, requestEntity.getQuery()); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("HTTP " + httpMethod + " " + url); + if (logger.isDebugEnabled()) { + logger.debug("HTTP method: {}, url: {}, body: {}", httpMethod, uri, requestEntity.getBody()); } + ResponseHandler responseHandler = super.selectResponseHandler(responseType); HttpClientResponse response = null; try { @@ -464,8 +462,8 @@ private HttpRestResult execute(String url, String httpMethod, RequestHttp private HttpClientRequest requestClient() { if (CollectionUtils.isNotEmpty(interceptors)) { - if (LOGGER.isDebugEnabled()) { - LOGGER.info("Execute via interceptors :{}", interceptors); + if (logger.isDebugEnabled()) { + logger.debug("Execute via interceptors :{}", interceptors); } return new InterceptingHttpClientRequest(requestClient, interceptors.iterator()); } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java index 2e301ef6c04..3812641650d 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.test.common; import com.alibaba.nacos.Nacos; @@ -30,6 +31,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; +import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; @@ -45,49 +47,52 @@ @SuppressWarnings("all") @FixMethodOrder(MethodSorters.JVM) @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = Nacos.class, properties = { + "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class NacosAsyncRestTemplate_ITCase { - + @LocalServerPort private int port; - - private NacosAsyncRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(); - + + private NacosAsyncRestTemplate nacosRestTemplate = HttpClientBeanHolder + .getNacosAsyncRestTemplate(LoggerFactory.getLogger(NacosAsyncRestTemplate_ITCase.class)); + private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; + private String IP = null; - + @Before public void init() throws NacosException { IP = String.format("http://localhost:%d", port); } - + private class CallbackMap implements Callback { - + private HttpRestResult restResult; + private Throwable throwable; - + @Override public void onReceive(RestResult result) { restResult = (HttpRestResult) result; } - + @Override public void onError(Throwable throwable) { throwable = throwable; } - + public HttpRestResult getRestResult() { return restResult; } - + public Throwable getThrowable() { return throwable; } } - + @Test - public void test_url_post_form() throws Exception{ + public void test_url_post_form() throws Exception { String url = IP + CONFIG_INSTANCE_PATH + "/instance"; Map param = new HashMap<>(); param.put("serviceName", "app-test"); @@ -101,9 +106,9 @@ public void test_url_post_form() throws Exception{ System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); } - + @Test - public void test_url_put_form() throws Exception{ + public void test_url_put_form() throws Exception { String url = IP + CONFIG_INSTANCE_PATH + "/instance"; Map param = new HashMap<>(); param.put("serviceName", "app-test-change"); @@ -117,8 +122,8 @@ public void test_url_put_form() throws Exception{ System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); } - - + + @Test public void test_url_get() throws Exception { String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; @@ -132,9 +137,9 @@ public void test_url_get() throws Exception { Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); } - + @Test - public void test_url_by_map() throws Exception{ + public void test_url_by_map() throws Exception { String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; Map param = new HashMap<>(); param.put("serviceName", "app-test"); @@ -147,21 +152,19 @@ public void test_url_by_map() throws Exception{ Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); } - + @Test - public void test_url_delete() throws Exception{ + public void test_url_delete() throws Exception { String url = IP + CONFIG_INSTANCE_PATH + "/instance"; - Query query = Query.newInstance() - .addParam("ip", "11.11.11.11") - .addParam("port", "8080") - .addParam("serviceName", "app-test"); + Query query = Query.newInstance().addParam("ip", "11.11.11.11").addParam("port", "8080") + .addParam("serviceName", "app-test"); CallbackMap callbackMap = new CallbackMap<>(); - nacosRestTemplate.delete(url, Header.newInstance(), query, String.class, callbackMap); + nacosRestTemplate.delete(url, Header.newInstance(), query, String.class, callbackMap); Thread.sleep(2000); HttpRestResult restResult = callbackMap.getRestResult(); System.out.println(restResult.getData()); System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); } - + } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java index 2c7720fccbb..801089b85e9 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.test.common; import com.alibaba.nacos.Nacos; @@ -28,13 +29,16 @@ import com.alibaba.nacos.config.server.model.ConfigInfo4Beta; import com.alibaba.nacos.config.server.utils.JSONUtils; import com.alibaba.nacos.core.utils.GenericType; +import com.alibaba.nacos.test.smoke.nacosSmoke_ITCase; import com.fasterxml.jackson.core.type.TypeReference; +import org.apache.log4j.Logger; import org.junit.Assert; import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; +import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; @@ -43,78 +47,87 @@ import java.util.Map; /** - * NacosRestTemplate_ITCase + * NacosRestTemplate_ITCase * * @author mai.jh */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = Nacos.class, properties = { + "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @FixMethodOrder(MethodSorters.JVM) public class NacosRestTemplate_ITCase { - + @LocalServerPort private int port; - - private NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosRestTemplate(); - + + private NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder + .getNacosRestTemplate(LoggerFactory.getLogger(NacosRestTemplate_ITCase.class)); + private final String INSTANCE_PATH = "/nacos/v1/ns"; + private final String CONFIG_PATH = "/nacos/v1/cs"; + private String IP = null; - + @Before public void init() throws NacosException { IP = String.format("http://localhost:%d", port); } - + @Test - public void test_url_post_config() throws Exception { + public void test_url_post_config() throws Exception { String url = IP + CONFIG_PATH + "/configs"; Map param = new HashMap<>(); param.put("dataId", "test-1"); param.put("group", "DEFAULT_GROUP"); param.put("content", "aaa=b"); - HttpRestResult restResult = nacosRestTemplate.postForm(url, Header.newInstance(), Query.EMPTY, param, String.class); + HttpRestResult restResult = nacosRestTemplate + .postForm(url, Header.newInstance(), Query.EMPTY, param, String.class); Assert.assertTrue(restResult.ok()); System.out.println(restResult.getData()); System.out.println(restResult.getHeader()); } - + @Test - public void test_url_get_return_restResult() throws Exception{ + public void test_url_get_return_restResult() throws Exception { String url = IP + CONFIG_PATH + "/configs"; - Query query = Query.newInstance().addParam("beta", true).addParam("dataId","test-1").addParam("group", "DEFAULT_GROUP"); - HttpRestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), query, new TypeReference>(){}.getType()); + Query query = Query.newInstance().addParam("beta", true).addParam("dataId", "test-1") + .addParam("group", "DEFAULT_GROUP"); + HttpRestResult restResult = nacosRestTemplate + .get(url, Header.newInstance(), query, new TypeReference>() { + }.getType()); Assert.assertTrue(restResult.ok()); System.out.println(restResult.getData()); System.out.println(restResult.getHeader()); } - - + + @Test - public void test_url_post_form() throws Exception{ + public void test_url_post_form() throws Exception { String url = IP + INSTANCE_PATH + "/instance"; Map param = new HashMap<>(); param.put("serviceName", "app-test"); param.put("port", "8080"); param.put("ip", "11.11.11.11"); - HttpRestResult restResult = nacosRestTemplate.postForm(url, Header.newInstance(), Query.newInstance(), param, String.class); + HttpRestResult restResult = nacosRestTemplate + .postForm(url, Header.newInstance(), Query.newInstance(), param, String.class); Assert.assertTrue(restResult.ok()); System.out.println(restResult.getData()); } - + @Test - public void test_url_put_from() throws Exception{ + public void test_url_put_from() throws Exception { String url = IP + INSTANCE_PATH + "/instance"; Map param = new HashMap<>(); param.put("serviceName", "app-test-change"); param.put("port", "8080"); param.put("ip", "11.11.11.11"); - HttpRestResult restResult = nacosRestTemplate.putForm(url, Header.newInstance(), Query.newInstance(), param, String.class); + HttpRestResult restResult = nacosRestTemplate + .putForm(url, Header.newInstance(), Query.newInstance(), param, String.class); Assert.assertTrue(restResult.ok()); System.out.println(restResult.getData()); } - + @Test public void test_url_get() throws Exception { String url = IP + INSTANCE_PATH + "/instance/list"; @@ -124,7 +137,7 @@ public void test_url_get() throws Exception { Assert.assertEquals(restResult.getData().get("dom"), "app-test"); System.out.println(restResult.getData()); } - + @Test public void test_url_get_by_map() throws Exception { String url = IP + INSTANCE_PATH + "/instance/list"; @@ -135,17 +148,15 @@ public void test_url_get_by_map() throws Exception { Assert.assertEquals(restResult.getData().get("dom"), "app-test"); System.out.println(restResult.getData()); } - + @Test - public void test_url_delete() throws Exception{ + public void test_url_delete() throws Exception { String url = IP + INSTANCE_PATH + "/instance"; - Query query = Query.newInstance() - .addParam("ip", "11.11.11.11") - .addParam("port", "8080") - .addParam("serviceName", "app-test"); - HttpRestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, String.class); + Query query = Query.newInstance().addParam("ip", "11.11.11.11").addParam("port", "8080") + .addParam("serviceName", "app-test"); + HttpRestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, String.class); Assert.assertTrue(restResult.ok()); System.out.println(restResult); } - + } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_Interceptors_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_Interceptors_ITCase.java index 14a96dccf67..de36a71dfcb 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_Interceptors_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_Interceptors_ITCase.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.test.common; import com.alibaba.nacos.Nacos; @@ -31,6 +32,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; +import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; @@ -44,26 +46,28 @@ import java.util.Map; /** - * NacosRestTemplate_Interceptors_ITCase + * NacosRestTemplate_Interceptors_ITCase * * @author mai.jh */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = Nacos.class, properties = { + "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @FixMethodOrder(MethodSorters.JVM) public class NacosRestTemplate_Interceptors_ITCase { - + @LocalServerPort private int port; - - private NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosRestTemplate(); - + + private NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder + .getNacosRestTemplate(LoggerFactory.getLogger(NacosRestTemplate_Interceptors_ITCase.class)); + private final String CONFIG_PATH = "/nacos/v1/cs"; + private String IP = null; private class TerminationInterceptor implements HttpClientRequestInterceptor { - + @Override public HttpClientResponse intercept() { return new HttpClientResponse() { @@ -71,36 +75,36 @@ public HttpClientResponse intercept() { public Header getHeaders() { return Header.EMPTY; } - + @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("Stop request".getBytes()); } - + @Override public int getStatusCode() { return NacosException.SERVER_ERROR; } - + @Override public String getStatusText() { return null; } - + @Override public void close() throws IOException { } }; } - + @Override public boolean isIntercept(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) { return true; } } - + @Before public void init() throws NacosException { nacosRestTemplate.setInterceptors(Arrays.asList(new TerminationInterceptor())); @@ -108,13 +112,14 @@ public void init() throws NacosException { } @Test - public void test_url_post_config() throws Exception { + public void test_url_post_config() throws Exception { String url = IP + CONFIG_PATH + "/configs"; Map param = new HashMap<>(); param.put("dataId", "test-1"); param.put("group", "DEFAULT_GROUP"); param.put("content", "aaa=b"); - HttpRestResult restResult = nacosRestTemplate.postForm(url, Header.newInstance(), Query.EMPTY, param, String.class); + HttpRestResult restResult = nacosRestTemplate + .postForm(url, Header.newInstance(), Query.EMPTY, param, String.class); Assert.assertEquals(500, restResult.getCode()); Assert.assertEquals("Stop request", restResult.getData()); System.out.println(restResult.getData());