|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making tRPC available. |
| 3 | + * |
| 4 | + * Copyright (C) 2023 THL A29 Limited, a Tencent company. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * If you have downloaded a copy of the tRPC source code from Tencent, |
| 8 | + * please note that tRPC source code is licensed under the Apache 2.0 License, |
| 9 | + * A copy of the Apache 2.0 License can be found in the LICENSE file. |
| 10 | + */ |
| 11 | + |
| 12 | +package com.tencent.trpc.spring.context.configuration; |
| 13 | + |
| 14 | +import com.tencent.trpc.core.common.ConfigManager; |
| 15 | +import com.tencent.trpc.core.common.config.BackendConfig; |
| 16 | +import com.tencent.trpc.core.common.config.ClientConfig; |
| 17 | +import com.tencent.trpc.core.common.config.ServerConfig; |
| 18 | +import com.tencent.trpc.core.common.config.ServiceConfig; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class AddFilterTRpcConfigManagerCustomizerTest { |
| 26 | + |
| 27 | + private static final String FILTER_ONE = "filter1"; |
| 28 | + |
| 29 | + private static final String FILTER_TWO = "filter2"; |
| 30 | + |
| 31 | + private static final String FILTER_THREE = "filter3"; |
| 32 | + |
| 33 | + private static final String FILTER_FOUR = "filter4"; |
| 34 | + |
| 35 | + private static final String BACKEND_MAP_KEY = "backend"; |
| 36 | + |
| 37 | + private static final String SERVER_FILTER_ONE = "server-filter1"; |
| 38 | + |
| 39 | + private static final String SERVER_FILTER_TWO = "server-filter2"; |
| 40 | + |
| 41 | + private static final String SERVICE_FILTER_THREE = "service-filter3"; |
| 42 | + |
| 43 | + private static final String SERVICE_FILTER_FOUR = "service-filter4"; |
| 44 | + |
| 45 | + private static final String SERVICE_BACKEND_MAP_KEY = "service-backend"; |
| 46 | + |
| 47 | + private static final Integer ORDER_VALUE = 1024; |
| 48 | + |
| 49 | + private static AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setUp() { |
| 53 | + addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testConstructor() { |
| 58 | + Assert.assertNotNull(addFilterTRpcConfigManagerCustomizer); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testAddClientFilters() { |
| 63 | + AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addClientFilters( |
| 64 | + FILTER_ONE, FILTER_TWO); |
| 65 | + Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testAddServerFilters() { |
| 70 | + AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addServerFilters( |
| 71 | + FILTER_ONE, FILTER_TWO); |
| 72 | + Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testCustomize() { |
| 77 | + ConfigManager instance = ConfigManager.getInstance(); |
| 78 | + ClientConfig clientConfig = new ClientConfig(); |
| 79 | + List<String> list = Arrays.asList(FILTER_ONE, FILTER_TWO); |
| 80 | + clientConfig.setFilters(list); |
| 81 | + instance.setClientConfig(clientConfig); |
| 82 | + addFilterTRpcConfigManagerCustomizer.customize(instance); |
| 83 | + List<String> filters = instance.getClientConfig().getFilters(); |
| 84 | + |
| 85 | + Assert.assertEquals(filters.size(), list.size()); |
| 86 | + Assert.assertEquals(list, instance.getClientConfig().getFilters()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testGetOrder() { |
| 91 | + Assert.assertEquals(Integer.MAX_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); |
| 92 | + addFilterTRpcConfigManagerCustomizer = new TestAddFilterTRpcConfigManagerCustomizer(); |
| 93 | + Assert.assertEquals((long) ORDER_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); |
| 94 | + } |
| 95 | + |
| 96 | + static final class TestAddFilterTRpcConfigManagerCustomizer extends AddFilterTRpcConfigManagerCustomizer { |
| 97 | + |
| 98 | + @Override |
| 99 | + public int getOrder() { |
| 100 | + return ORDER_VALUE; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testConstructorWithNullEmpty() { |
| 106 | + addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(null, null); |
| 107 | + addFilterTRpcConfigManagerCustomizer.addClientFilters(FILTER_ONE, FILTER_TWO); |
| 108 | + addFilterTRpcConfigManagerCustomizer.addServerFilters(SERVER_FILTER_ONE, SERVER_FILTER_TWO); |
| 109 | + |
| 110 | + ClientConfig clientConfig = new ClientConfig(); |
| 111 | + BackendConfig backendConfig = new BackendConfig(); |
| 112 | + backendConfig.setFilters(Arrays.asList(FILTER_THREE, FILTER_FOUR)); |
| 113 | + clientConfig.getBackendConfigMap().put(BACKEND_MAP_KEY, backendConfig); |
| 114 | + |
| 115 | + ServerConfig serverConfig = new ServerConfig(); |
| 116 | + ServiceConfig serviceConfig = new ServiceConfig(); |
| 117 | + serviceConfig.setFilters(Arrays.asList(SERVICE_FILTER_THREE, SERVICE_FILTER_FOUR)); |
| 118 | + serverConfig.getServiceMap().put(SERVICE_BACKEND_MAP_KEY, serviceConfig); |
| 119 | + |
| 120 | + ConfigManager configManager = ConfigManager.getInstance(); |
| 121 | + configManager.setClientConfig(clientConfig); |
| 122 | + configManager.setServerConfig(serverConfig); |
| 123 | + |
| 124 | + addFilterTRpcConfigManagerCustomizer.customize(configManager); |
| 125 | + List<String> clientExpected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE, FILTER_FOUR); |
| 126 | + // backendConfig filters test |
| 127 | + Assert.assertEquals(clientExpected.size(), backendConfig.getFilters().size()); |
| 128 | + // serviceConfig filters test |
| 129 | + List<String> serverExpected = Arrays.asList(SERVER_FILTER_ONE, SERVER_FILTER_TWO, SERVICE_FILTER_THREE, |
| 130 | + SERVICE_FILTER_FOUR); |
| 131 | + Assert.assertEquals(serverExpected.size(), serviceConfig.getFilters().size()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testMerge() { |
| 136 | + // add client filter |
| 137 | + addFilterTRpcConfigManagerCustomizer.addClientFilters(FILTER_ONE, FILTER_TWO); |
| 138 | + |
| 139 | + ClientConfig clientConfig = new ClientConfig(); |
| 140 | + BackendConfig backendConfig = new BackendConfig(); |
| 141 | + |
| 142 | + backendConfig.setFilters(Arrays.asList(FILTER_THREE, FILTER_FOUR)); |
| 143 | + clientConfig.getBackendConfigMap().put(BACKEND_MAP_KEY, backendConfig); |
| 144 | + |
| 145 | + ConfigManager configManager = ConfigManager.getInstance(); |
| 146 | + configManager.setClientConfig(clientConfig); |
| 147 | + |
| 148 | + // call customize method |
| 149 | + addFilterTRpcConfigManagerCustomizer.customize(configManager); |
| 150 | + |
| 151 | + List<String> expected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE, FILTER_FOUR); |
| 152 | + Assert.assertEquals(expected, backendConfig.getFilters()); |
| 153 | + |
| 154 | + expected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE); |
| 155 | + Assert.assertNotEquals(expected, backendConfig.getFilters()); |
| 156 | + } |
| 157 | +} |
0 commit comments