parent
c60268a21f
commit
bd1297b07e
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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 com.tencent.cloud.rpc.enhancement.resttemplate;
|
||||
|
||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class BlockingLoadBalancerClientAspectTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@Mock
|
||||
private ProceedingJoinPoint proceedingJoinPoint;
|
||||
|
||||
private BlockingLoadBalancerClientAspect aspect = new BlockingLoadBalancerClientAspect();
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
MetadataLocalProperties metadataLocalProperties = mock(MetadataLocalProperties.class);
|
||||
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
|
||||
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
|
||||
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Throwable {
|
||||
ServiceInstance serviceInstance = mock(ServiceInstance.class);
|
||||
doReturn("0.0.0.0").when(serviceInstance).getHost();
|
||||
doReturn(80).when(serviceInstance).getPort();
|
||||
doReturn(new Object[]{ serviceInstance }).when(proceedingJoinPoint).getArgs();
|
||||
aspect.invoke(proceedingJoinPoint);
|
||||
aspect.pointcut();
|
||||
assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST)).isEqualTo("0.0.0.0");
|
||||
assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT)).isEqualTo("80");
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,142 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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 com.tencent.cloud.rpc.enhancement.resttemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.polaris.api.config.Configuration;
|
||||
import com.tencent.polaris.api.config.global.APIConfig;
|
||||
import com.tencent.polaris.api.config.global.GlobalConfig;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class EnhancedPolarisRestTemplateReporterTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@Mock
|
||||
private RpcEnhancementReporterProperties reporterProperties;
|
||||
@Mock
|
||||
private SDKContext sdkContext;
|
||||
@Mock
|
||||
private ConsumerAPI consumerAPI;
|
||||
@Mock
|
||||
private CircuitBreakAPI circuitBreakAPI;
|
||||
@Mock
|
||||
private ClientHttpRequestExecution mockClientHttpRequestExecution;
|
||||
@Mock
|
||||
private ClientHttpResponse mockClientHttpResponse;
|
||||
@Mock
|
||||
private HttpRequest mockHttpRequest;
|
||||
@Mock
|
||||
private HttpHeaders mockHttpHeaders;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
MetadataLocalProperties metadataLocalProperties = mock(MetadataLocalProperties.class);
|
||||
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
|
||||
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
|
||||
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() throws IOException, URISyntaxException {
|
||||
|
||||
APIConfig apiConfig = mock(APIConfig.class);
|
||||
doReturn("0.0.0.0").when(apiConfig).getBindIP();
|
||||
|
||||
GlobalConfig globalConfig = mock(GlobalConfig.class);
|
||||
doReturn(apiConfig).when(globalConfig).getAPI();
|
||||
|
||||
Configuration configuration = mock(Configuration.class);
|
||||
doReturn(globalConfig).when(configuration).getGlobal();
|
||||
|
||||
doReturn(configuration).when(sdkContext).getConfig();
|
||||
|
||||
ClientHttpResponse actualResult;
|
||||
final byte[] inputBody = null;
|
||||
|
||||
URI uri = new URI("http://0.0.0.0/");
|
||||
doReturn(uri).when(mockHttpRequest).getURI();
|
||||
doReturn(HttpMethod.GET).when(mockHttpRequest).getMethod();
|
||||
doReturn(mockHttpHeaders).when(mockHttpRequest).getHeaders();
|
||||
doReturn(mockClientHttpResponse).when(mockClientHttpRequestExecution).execute(mockHttpRequest, inputBody);
|
||||
|
||||
EnhancedPolarisRestTemplateReporter reporter = new EnhancedPolarisRestTemplateReporter(reporterProperties, sdkContext, consumerAPI, circuitBreakAPI);
|
||||
actualResult = reporter.intercept(mockHttpRequest, inputBody, mockClientHttpRequestExecution);
|
||||
assertSame(mockClientHttpResponse, actualResult);
|
||||
|
||||
doReturn(true).when(reporterProperties).isEnabled();
|
||||
actualResult = reporter.intercept(mockHttpRequest, inputBody, mockClientHttpRequestExecution);
|
||||
assertSame(mockClientHttpResponse, actualResult);
|
||||
|
||||
doThrow(new SocketTimeoutException()).when(mockClientHttpRequestExecution).execute(mockHttpRequest, inputBody);
|
||||
assertThrows(SocketTimeoutException.class, () -> reporter.intercept(mockHttpRequest, inputBody, mockClientHttpRequestExecution));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,231 +0,0 @@
|
||||
///*
|
||||
// * Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
// *
|
||||
// * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
// *
|
||||
// * Licensed under the BSD 3-Clause License (the "License");
|
||||
// * you may not use this file except in compliance with the License.
|
||||
// * You may obtain a copy of the License at
|
||||
// *
|
||||
// * https://opensource.org/licenses/BSD-3-Clause
|
||||
// *
|
||||
// * 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 com.tencent.cloud.rpc.enhancement.resttemplate;
|
||||
//
|
||||
//import java.io.IOException;
|
||||
//import java.io.InputStream;
|
||||
//import java.net.URI;
|
||||
//import java.util.HashMap;
|
||||
//import java.util.Map;
|
||||
//
|
||||
//import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
//import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
//import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
//import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
//import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
//import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
//import org.junit.jupiter.api.AfterAll;
|
||||
//import org.junit.jupiter.api.BeforeAll;
|
||||
//import org.junit.jupiter.api.BeforeEach;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.junit.jupiter.api.extension.ExtendWith;
|
||||
//import org.mockito.InjectMocks;
|
||||
//import org.mockito.Mock;
|
||||
//import org.mockito.MockedStatic;
|
||||
//import org.mockito.Mockito;
|
||||
//import org.mockito.junit.jupiter.MockitoExtension;
|
||||
//
|
||||
//import org.springframework.context.ApplicationContext;
|
||||
//import org.springframework.http.HttpHeaders;
|
||||
//import org.springframework.http.HttpMethod;
|
||||
//import org.springframework.http.HttpStatus;
|
||||
//import org.springframework.http.client.AbstractClientHttpResponse;
|
||||
//import org.springframework.http.client.ClientHttpResponse;
|
||||
//import org.springframework.web.client.DefaultResponseErrorHandler;
|
||||
//import org.springframework.web.client.ResponseErrorHandler;
|
||||
//
|
||||
//import static org.assertj.core.api.Assertions.assertThat;
|
||||
//import static org.mockito.ArgumentMatchers.any;
|
||||
//import static org.mockito.ArgumentMatchers.anyString;
|
||||
//import static org.mockito.Mockito.mock;
|
||||
//import static org.mockito.Mockito.times;
|
||||
//import static org.mockito.Mockito.verify;
|
||||
//import static org.mockito.Mockito.when;
|
||||
//
|
||||
///**
|
||||
// * Test for {@link EnhancedRestTemplateReporter}.
|
||||
// *
|
||||
// * @author lepdou 2022-09-06
|
||||
// */
|
||||
//@ExtendWith(MockitoExtension.class)
|
||||
//public class EnhancedRestTemplateReporterTest {
|
||||
//
|
||||
// private static MockedStatic<MetadataContextHolder> mockedMetadataContextHolder;
|
||||
// private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
// @Mock
|
||||
// private ConsumerAPI consumerAPI;
|
||||
// @Mock
|
||||
// private RpcEnhancementReporterProperties reporterProperties;
|
||||
// @Mock
|
||||
// private ResponseErrorHandler delegate;
|
||||
// @InjectMocks
|
||||
// private EnhancedRestTemplateReporter enhancedRestTemplateReporter;
|
||||
//
|
||||
// @InjectMocks
|
||||
// private EnhancedRestTemplateReporter enhancedRestTemplateReporter2;
|
||||
//
|
||||
// @BeforeAll
|
||||
// static void beforeAll() {
|
||||
// mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
// mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
// .thenReturn("caller");
|
||||
// MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
|
||||
//
|
||||
// // mock transitive metadata
|
||||
// Map<String, String> loadBalancerContext = new HashMap<>();
|
||||
// loadBalancerContext.put("host", "1.1.1.1");
|
||||
// loadBalancerContext.put("port", "8080");
|
||||
// loadBalancerContext.put("startMillis", String.valueOf(System.currentTimeMillis()));
|
||||
// when(metadataContext.getLoadbalancerMetadata()).thenReturn(loadBalancerContext);
|
||||
//
|
||||
// mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class);
|
||||
// mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
|
||||
// }
|
||||
//
|
||||
// @AfterAll
|
||||
// static void afterAll() {
|
||||
// mockedApplicationContextAwareUtils.close();
|
||||
// mockedMetadataContextHolder.close();
|
||||
// }
|
||||
//
|
||||
// @BeforeEach
|
||||
// void setUp() {
|
||||
// enhancedRestTemplateReporter.setDelegateHandler(delegate);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testSetApplicationContext() {
|
||||
// ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
//
|
||||
// // test no ResponseErrorHandler
|
||||
// when(applicationContext.getBeanNamesForType(any(Class.class)))
|
||||
// .thenReturn(new String[] {"enhancedRestTemplateReporter"});
|
||||
// enhancedRestTemplateReporter2.setApplicationContext(applicationContext);
|
||||
// assertThat(enhancedRestTemplateReporter2.getDelegateHandler()).isInstanceOf(DefaultResponseErrorHandler.class);
|
||||
//
|
||||
// // test one other ResponseErrorHandler
|
||||
// when(applicationContext.getBeanNamesForType(any(Class.class)))
|
||||
// .thenReturn(new String[] {"enhancedRestTemplateReporter", "mockedResponseErrorHandler"});
|
||||
// when(applicationContext.getBean(anyString())).thenReturn(mock(MockedResponseErrorHandler.class));
|
||||
// enhancedRestTemplateReporter2.setApplicationContext(applicationContext);
|
||||
// assertThat(enhancedRestTemplateReporter2.getDelegateHandler()).isInstanceOf(MockedResponseErrorHandler.class);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testHasError() throws IOException {
|
||||
// when(delegate.hasError(any())).thenReturn(true);
|
||||
//
|
||||
// MockedClientHttpResponse response = new MockedClientHttpResponse();
|
||||
// assertThat(enhancedRestTemplateReporter.hasError(response)).isTrue();
|
||||
//
|
||||
// String realHasError = response.getHeaders().getFirst(EnhancedRestTemplateReporter.HEADER_HAS_ERROR);
|
||||
// assertThat(realHasError).isEqualTo("true");
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testHandleHasError() throws IOException {
|
||||
// when(reporterProperties.isEnabled()).thenReturn(true);
|
||||
// when(delegate.hasError(any())).thenReturn(true);
|
||||
//
|
||||
// MockedClientHttpResponse response = new MockedClientHttpResponse();
|
||||
// enhancedRestTemplateReporter.hasError(response);
|
||||
//
|
||||
// URI uri = mock(URI.class);
|
||||
// enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response);
|
||||
//
|
||||
// verify(consumerAPI, times(1)).updateServiceCallResult(any());
|
||||
// verify(delegate).handleError(uri, HttpMethod.GET, response);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testHandleHasNotError() throws IOException {
|
||||
// when(reporterProperties.isEnabled()).thenReturn(true);
|
||||
// when(delegate.hasError(any())).thenReturn(false);
|
||||
//
|
||||
// MockedClientHttpResponse response = new MockedClientHttpResponse();
|
||||
// enhancedRestTemplateReporter.hasError(response);
|
||||
//
|
||||
// URI uri = mock(URI.class);
|
||||
// enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response);
|
||||
//
|
||||
// verify(consumerAPI, times(1)).updateServiceCallResult(any());
|
||||
// verify(delegate, times(0)).handleError(uri, HttpMethod.GET, response);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testReportSwitchOff() throws IOException {
|
||||
// when(reporterProperties.isEnabled()).thenReturn(false);
|
||||
// when(delegate.hasError(any())).thenReturn(true);
|
||||
//
|
||||
// MockedClientHttpResponse response = new MockedClientHttpResponse();
|
||||
// enhancedRestTemplateReporter.hasError(response);
|
||||
//
|
||||
// URI uri = mock(URI.class);
|
||||
// enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response);
|
||||
//
|
||||
// verify(consumerAPI, times(0)).updateServiceCallResult(any());
|
||||
// verify(delegate).handleError(uri, HttpMethod.GET, response);
|
||||
// }
|
||||
//
|
||||
// static class MockedClientHttpResponse extends AbstractClientHttpResponse {
|
||||
//
|
||||
// private final HttpHeaders headers;
|
||||
//
|
||||
// MockedClientHttpResponse() {
|
||||
// this.headers = new HttpHeaders();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public int getRawStatusCode() {
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String getStatusText() {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void close() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public InputStream getBody() throws IOException {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public HttpHeaders getHeaders() {
|
||||
// return headers;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public HttpStatus getStatusCode() throws IOException {
|
||||
// return HttpStatus.OK;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private static class MockedResponseErrorHandler extends DefaultResponseErrorHandler {
|
||||
//
|
||||
// @Override
|
||||
// public void handleError(@NonNull ClientHttpResponse response) {
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//}
|
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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 com.tencent.cloud.rpc.enhancement.webclient;
|
||||
|
||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class PolarisLoadBalancerClientRequestTransformerTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
|
||||
private PolarisLoadBalancerClientRequestTransformer transformer = new PolarisLoadBalancerClientRequestTransformer();
|
||||
|
||||
@Mock
|
||||
private ClientRequest clientRequest;
|
||||
|
||||
@Mock
|
||||
private ServiceInstance serviceInstance;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
MetadataLocalProperties metadataLocalProperties = mock(MetadataLocalProperties.class);
|
||||
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
|
||||
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
|
||||
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Throwable {
|
||||
doReturn("test").when(serviceInstance).getServiceId();
|
||||
transformer.transformRequest(clientRequest, serviceInstance);
|
||||
assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID)).isEqualTo("test");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue