Search in sources :

Example 6 with ApplicationContextAwareUtils

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

the class RouterLabelFeignInterceptorTest method testResolveRouterLabel.

@Test
public void testResolveRouterLabel() {
    RouterLabelFeignInterceptor routerLabelFeignInterceptor = new RouterLabelFeignInterceptor(Collections.singletonList(routerLabelResolver), metadataLocalProperties, routerRuleLabelResolver);
    // mock request template
    RequestTemplate requestTemplate = new RequestTemplate();
    String headerUidKey = "uid";
    String headerUidValue = "1000";
    requestTemplate.header(headerUidKey, headerUidValue);
    String peerService = "peerService";
    Target.EmptyTarget<Object> target = Target.EmptyTarget.create(Object.class, peerService);
    requestTemplate.feignTarget(target);
    // mock ApplicationContextAwareUtils#getProperties
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        String testService = "callerService";
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(testService);
        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);
        // mock MetadataContextHolder#get
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            // mock custom resolved labels from request
            Map<String, String> customResolvedLabels = new HashMap<>();
            customResolvedLabels.put("k2", "v2");
            customResolvedLabels.put("k3", "v3");
            when(routerLabelResolver.resolve(requestTemplate)).thenReturn(customResolvedLabels);
            // mock expression rule labels
            Set<String> expressionKeys = new HashSet<>();
            expressionKeys.add("${http.header.uid}");
            expressionKeys.add("${http.header.name}");
            when(routerRuleLabelResolver.getExpressionLabelKeys(MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE, peerService)).thenReturn(expressionKeys);
            // mock local metadata
            Map<String, String> localMetadata = new HashMap<>();
            localMetadata.put("k3", "v31");
            localMetadata.put("k4", "v4");
            when(metadataLocalProperties.getContent()).thenReturn(localMetadata);
            routerLabelFeignInterceptor.apply(requestTemplate);
            Collection<String> routerLabels = requestTemplate.headers().get(RouterConstants.ROUTER_LABEL_HEADER);
            Assert.assertNotNull(routerLabels);
            for (String value : routerLabels) {
                Map<String, String> labels = unescape(JacksonUtils.deserialize2Map(value));
                Assert.assertEquals("v1", labels.get("k1"));
                Assert.assertEquals("v22", labels.get("k2"));
                Assert.assertEquals("v3", labels.get("k3"));
                Assert.assertEquals("v4", labels.get("k4"));
                Assert.assertEquals(headerUidValue, labels.get("${http.header.uid}"));
                Assert.assertEquals("", labels.get("${http.header.name}"));
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Target(feign.Target) RequestTemplate(feign.RequestTemplate) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ApplicationContextAwareUtils

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

the class PolarisLoadBalancerCompositeRuleTest method testBuildMetadataRouteRequest.

@Test
public void testBuildMetadataRouteRequest() {
    when(polarisMetadataRouterProperties.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(MetadataRouter.ROUTER_TYPE_METADATA);
        Assert.assertEquals(1, routerMetadata.size());
        Assert.assertEquals(0, request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY).size());
        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 8 with ApplicationContextAwareUtils

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

the class PolarisLoadBalancerCompositeRuleTest method testBuildRuleBasedRouteRequest.

@Test
public void testBuildRuleBasedRouteRequest() {
    when(polarisRuleBasedRouterProperties.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(RuleBasedRouter.ROUTER_TYPE_RULE_BASED);
        Assert.assertEquals(1, routerMetadata.size());
        Assert.assertEquals(0, request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA).size());
        Assert.assertEquals(0, request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY).size());
        Assert.assertEquals(1, request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size());
        Assert.assertEquals("true", 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 9 with ApplicationContextAwareUtils

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

the class PolarisLoadBalancerCompositeRuleTest method testRouter.

@Test
public void testRouter() {
    when(polarisRuleBasedRouterProperties.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);
        ProcessRoutersResponse assembleResponse = assembleProcessRoutersResponse();
        when(routerAPI.processRouters(any())).thenReturn(assembleResponse);
        List<Server> servers = compositeRule.doRouter(assembleServers(), assembleRouterContext());
        Assert.assertEquals(assembleResponse.getServiceInstances().getInstances().size(), servers.size());
    }
}
Also used : ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) 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