Search in sources :

Example 1 with ApplicationContextAwareUtils

use of com.tencent.cloud.common.util.ApplicationContextAwareUtils in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRuleTest method testBuildNearbyRouteRequest.

@Test
public void testBuildNearbyRouteRequest() {
    when(polarisNearByRouterProperties.isEnabled()).thenReturn(true);
    when(polarisLoadBalancerProperties.getStrategy()).thenReturn("");
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(testCallerService);
        setTransitiveMetadata();
        PolarisLoadBalancerCompositeRule compositeRule = new PolarisLoadBalancerCompositeRule(routerAPI, polarisLoadBalancerProperties, polarisNearByRouterProperties, polarisMetadataRouterProperties, polarisRuleBasedRouterProperties, config);
        ServiceInstances serviceInstances = assembleServiceInstances();
        PolarisRouterContext routerContext = assembleRouterContext();
        ProcessRoutersRequest request = compositeRule.buildProcessRoutersRequest(serviceInstances, routerContext);
        Map<String, String> routerMetadata = request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY);
        Assert.assertEquals(0, request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA).size());
        Assert.assertEquals(1, routerMetadata.size());
        Assert.assertEquals("true", routerMetadata.get(NearbyRouter.ROUTER_ENABLED));
        Assert.assertEquals(1, request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size());
        Assert.assertEquals("false", request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).get(RuleBasedRouter.ROUTER_ENABLED));
    }
}
Also used : DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with ApplicationContextAwareUtils

use of com.tencent.cloud.common.util.ApplicationContextAwareUtils in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerInterceptorTest method testRouterContext.

@Test
public void testRouterContext() throws Exception {
    String callerService = "callerService";
    String calleeService = "calleeService";
    HttpRequest request = new MockedHttpRequest("http://" + calleeService + "/user/get");
    // mock local metadata
    Map<String, String> localMetadata = new HashMap<>();
    localMetadata.put("k1", "v1");
    localMetadata.put("k2", "v2");
    when(metadataLocalProperties.getContent()).thenReturn(localMetadata);
    // mock custom resolved from request
    Map<String, String> customResolvedLabels = new HashMap<>();
    customResolvedLabels.put("k2", "v22");
    customResolvedLabels.put("k4", "v4");
    when(routerLabelResolver.resolve(request, null)).thenReturn(customResolvedLabels);
    // mock expression rule labels
    Set<String> expressionKeys = new HashSet<>();
    expressionKeys.add("${http.method}");
    expressionKeys.add("${http.uri}");
    when(routerRuleLabelResolver.getExpressionLabelKeys(callerService, callerService, calleeService)).thenReturn(expressionKeys);
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(callerService);
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock transitive metadata
        Map<String, String> transitiveLabels = new HashMap<>();
        transitiveLabels.put("k1", "v1");
        transitiveLabels.put("k2", "v22");
        when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels);
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            PolarisLoadBalancerInterceptor polarisLoadBalancerInterceptor = new PolarisLoadBalancerInterceptor(loadBalancerClient, loadBalancerRequestFactory, Collections.singletonList(routerLabelResolver), metadataLocalProperties, routerRuleLabelResolver);
            PolarisRouterContext routerContext = polarisLoadBalancerInterceptor.genRouterContext(request, null, calleeService);
            verify(metadataLocalProperties).getContent();
            verify(routerRuleLabelResolver).getExpressionLabelKeys(callerService, callerService, calleeService);
            verify(routerLabelResolver).resolve(request, null);
            Assert.assertEquals("v1", routerContext.getLabels(PolarisRouterContext.TRANSITIVE_LABELS).get("k1"));
            Assert.assertEquals("v22", routerContext.getLabels(PolarisRouterContext.TRANSITIVE_LABELS).get("k2"));
            Assert.assertEquals("v1", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("k1"));
            Assert.assertEquals("v22", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("k2"));
            Assert.assertEquals("v4", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("k4"));
            Assert.assertEquals("GET", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("${http.method}"));
            Assert.assertEquals("/user/get", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("${http.uri}"));
        }
    }
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HashMap(java.util.HashMap) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ApplicationContextAwareUtils

use of com.tencent.cloud.common.util.ApplicationContextAwareUtils in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerInterceptorTest method testProxyRibbonLoadBalance.

@Test
public void testProxyRibbonLoadBalance() throws Exception {
    String callerService = "callerService";
    String calleeService = "calleeService";
    HttpRequest request = new MockedHttpRequest("http://" + calleeService + "/user/get");
    // mock local metadata
    Map<String, String> localMetadata = new HashMap<>();
    localMetadata.put("k1", "v1");
    localMetadata.put("k2", "v2");
    when(metadataLocalProperties.getContent()).thenReturn(localMetadata);
    // mock custom resolved from request
    Map<String, String> customResolvedLabels = new HashMap<>();
    customResolvedLabels.put("k3", "v3");
    customResolvedLabels.put("k4", "v4");
    when(routerLabelResolver.resolve(request, null)).thenReturn(customResolvedLabels);
    // mock expression rule labels
    Set<String> expressionKeys = new HashSet<>();
    expressionKeys.add("${http.method}");
    expressionKeys.add("${http.uri}");
    when(routerRuleLabelResolver.getExpressionLabelKeys(callerService, callerService, calleeService)).thenReturn(expressionKeys);
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(callerService);
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock transitive metadata
        Map<String, String> transitiveLabels = new HashMap<>();
        transitiveLabels.put("k1", "v1");
        transitiveLabels.put("k2", "v22");
        when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels);
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            LoadBalancerRequest<ClientHttpResponse> loadBalancerRequest = new MockedLoadBalancerRequest<>();
            when(loadBalancerRequestFactory.createRequest(request, null, null)).thenReturn(loadBalancerRequest);
            PolarisLoadBalancerInterceptor polarisLoadBalancerInterceptor = new PolarisLoadBalancerInterceptor(loadBalancerClient, loadBalancerRequestFactory, Collections.singletonList(routerLabelResolver), metadataLocalProperties, routerRuleLabelResolver);
            polarisLoadBalancerInterceptor.intercept(request, null, null);
            verify(metadataLocalProperties).getContent();
            verify(routerRuleLabelResolver).getExpressionLabelKeys(callerService, callerService, calleeService);
            verify(routerLabelResolver).resolve(request, null);
        }
    }
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HashMap(java.util.HashMap) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ApplicationContextAwareUtils

use of com.tencent.cloud.common.util.ApplicationContextAwareUtils in project spring-cloud-tencent by Tencent.

the class PolarisFeignLoadBalancerTest method testHasRouterContext.

@Test
public void testHasRouterContext() {
    DefaultClientConfigImpl config = new DefaultClientConfigImpl();
    config.loadDefaultValues();
    ILoadBalancer loadBalancer = new SimpleLoadBalancer();
    ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
    PolarisFeignLoadBalancer polarisFeignLoadBalancer = new PolarisFeignLoadBalancer(loadBalancer, config, serverIntrospector);
    Map<String, String> labels = new HashMap<>();
    labels.put("k1", "v1");
    labels.put("k2", "v2");
    List<String> headerValues = new ArrayList<>();
    headerValues.add(JacksonUtils.serialize2Json(labels));
    Map<String, Collection<String>> headers = new HashMap<>();
    headers.put(RouterConstants.ROUTER_LABEL_HEADER, headerValues);
    // mock ApplicationContextAwareUtils#getProperties
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn("unit-test");
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock MetadataContextHolder#get
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            PolarisRouterContext routerContext = polarisFeignLoadBalancer.buildRouterContext(headers);
            Assert.assertNotNull(routerContext);
            Map<String, String> routerLabels = routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS);
            Assert.assertNotNull(routerLabels);
            Assert.assertEquals("v1", routerLabels.get("k1"));
            Assert.assertEquals("v2", routerLabels.get("k2"));
            Assert.assertNull(routerLabels.get("k3"));
        }
    }
}
Also used : SimpleLoadBalancer(com.tencent.cloud.polaris.router.SimpleLoadBalancer) HashMap(java.util.HashMap) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) ArrayList(java.util.ArrayList) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) Mockito.anyString(org.mockito.Mockito.anyString) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) Collection(java.util.Collection) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 5 with ApplicationContextAwareUtils

use of com.tencent.cloud.common.util.ApplicationContextAwareUtils in project spring-cloud-tencent by Tencent.

the class PolarisFeignLoadBalancerTest method testHasNoneRouterContext.

@Test
public void testHasNoneRouterContext() {
    DefaultClientConfigImpl config = new DefaultClientConfigImpl();
    config.loadDefaultValues();
    ILoadBalancer loadBalancer = new SimpleLoadBalancer();
    ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
    PolarisFeignLoadBalancer polarisFeignLoadBalancer = new PolarisFeignLoadBalancer(loadBalancer, config, serverIntrospector);
    Map<String, Collection<String>> headers = new HashMap<>();
    // mock ApplicationContextAwareUtils#getProperties
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn("unit-test");
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock MetadataContextHolder#get
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            PolarisRouterContext routerContext = polarisFeignLoadBalancer.buildRouterContext(headers);
            Assert.assertNull(routerContext);
        }
    }
}
Also used : SimpleLoadBalancer(com.tencent.cloud.polaris.router.SimpleLoadBalancer) HashMap(java.util.HashMap) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) DefaultServerIntrospector(org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector) Mockito.anyString(org.mockito.Mockito.anyString) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) Collection(java.util.Collection) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Aggregations

ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)9 Test (org.junit.Test)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 MetadataContext (com.tencent.cloud.common.metadata.MetadataContext)5 MetadataContextHolder (com.tencent.cloud.common.metadata.MetadataContextHolder)5 HashMap (java.util.HashMap)5 PolarisRouterContext (com.tencent.cloud.polaris.router.PolarisRouterContext)3 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)3 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)3 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)3 HashSet (java.util.HashSet)3 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)2 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)2 SimpleLoadBalancer (com.tencent.cloud.polaris.router.SimpleLoadBalancer)2 Collection (java.util.Collection)2 Mockito.anyString (org.mockito.Mockito.anyString)2 DefaultServerIntrospector (org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector)2 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)2 HttpRequest (org.springframework.http.HttpRequest)2 Server (com.netflix.loadbalancer.Server)1