Skip to content

Commit edc66cc

Browse files
committed
refactor: improve code formatting and readability in test classes
1 parent 6f5ffe8 commit edc66cc

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public final class UpstreamCheckServiceTest {
105105
private SelectorConditionMapper selectorConditionMapper;
106106

107107
private SelectorHandleConverterFactor converterFactor;
108-
108+
109109
@Mock
110110
private DiscoveryUpstreamService discoveryUpstreamService;
111111

@@ -119,7 +119,8 @@ public final class UpstreamCheckServiceTest {
119119
public static void beforeClass() {
120120
loggerSpy = spy(LoggerFactory.getLogger(UpstreamCheckService.class));
121121
loggerFactoryMockedStatic = mockStatic(LoggerFactory.class);
122-
loggerFactoryMockedStatic.when(() -> LoggerFactory.getLogger(UpstreamCheckService.class)).thenReturn(loggerSpy);
122+
loggerFactoryMockedStatic.when(() -> LoggerFactory.getLogger(UpstreamCheckService.class))
123+
.thenReturn(loggerSpy);
123124
loggerFactoryMockedStatic.when(() -> LoggerFactory.getLogger(anyString())).thenReturn(loggerSpy);
124125
}
125126

@@ -133,12 +134,17 @@ public void setUp() {
133134
shenyuRegisterCenterConfig.setRegisterType("http");
134135
shenyuRegisterCenterConfig.getProps().setProperty(Constants.IS_CHECKED, "true");
135136
// get static variable reference by reflection
136-
upstreamMap = (Map<String, List<DivideUpstream>>) ReflectionTestUtils.getField(UpstreamCheckService.class, "UPSTREAM_MAP");
137-
zombieSet = (Set<ZombieUpstream>) ReflectionTestUtils.getField(UpstreamCheckService.class, "ZOMBIE_SET");
137+
upstreamMap = (Map<String, List<DivideUpstream>>) ReflectionTestUtils
138+
.getField(UpstreamCheckService.class, "UPSTREAM_MAP");
139+
upstreamMap.clear();
140+
zombieSet = (Set<ZombieUpstream>) ReflectionTestUtils.getField(UpstreamCheckService.class,
141+
"ZOMBIE_SET");
142+
zombieSet.clear();
138143
Map<String, SelectorHandleConverter> maps = new HashMap<>();
139144
maps.put(PluginEnum.DIVIDE.getName(), new DivideSelectorHandleConverter());
140145
converterFactor = new SelectorHandleConverterFactor(maps);
141-
upstreamCheckService = new UpstreamCheckService(selectorMapper, eventPublisher, pluginMapper, selectorConditionMapper,
146+
upstreamCheckService = new UpstreamCheckService(selectorMapper, eventPublisher, pluginMapper,
147+
selectorConditionMapper,
142148
shenyuRegisterCenterConfig, converterFactor, discoveryUpstreamService);
143149
}
144150

@@ -186,20 +192,24 @@ public void testRemoveByKey() {
186192

187193
@Test
188194
public void testSubmit() {
189-
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, ShenyuThreadFactory.create("scheduled-upstream-task", false));
190-
ReflectionTestUtils.setField(upstreamCheckService, "executor", executor);
191-
final DivideUpstream divideUpstream = DivideUpstream.builder()
192-
.upstreamUrl("divide-upstream-60")
193-
.weight(60)
194-
.build();
195-
// Test submit when selector name not exists
196-
testSubmitOnce(divideUpstream);
197-
// Test submit when selector name exists
198-
testSubmitOnce(divideUpstream);
199-
// Test service deleted
200-
divideUpstream.setStatus(false);
201-
testSubmitDeleted(divideUpstream);
202-
testSubmitDeleted(divideUpstream);
195+
try (MockedStatic<UpstreamCheckUtils> mocked = mockStatic(UpstreamCheckUtils.class)) {
196+
mocked.when(() -> UpstreamCheckUtils.checkUrl(anyString())).thenReturn(true);
197+
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1,
198+
ShenyuThreadFactory.create("scheduled-upstream-task", false));
199+
ReflectionTestUtils.setField(upstreamCheckService, "executor", executor);
200+
final DivideUpstream divideUpstream = DivideUpstream.builder()
201+
.upstreamUrl("divide-upstream-60")
202+
.weight(60)
203+
.build();
204+
// Test submit when selector name not exists
205+
testSubmitOnce(divideUpstream);
206+
// Test submit when selector name exists
207+
testSubmitOnce(divideUpstream);
208+
// Test service deleted
209+
divideUpstream.setStatus(false);
210+
testSubmitDeleted(divideUpstream);
211+
testSubmitDeleted(divideUpstream);
212+
}
203213
}
204214

205215
private void testSubmitOnce(final DivideUpstream divideUpstream) {
@@ -214,7 +224,8 @@ private void testSubmitDeleted(final DivideUpstream divideUpstream) {
214224

215225
@Test
216226
public void testReplace() {
217-
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, ShenyuThreadFactory.create("scheduled-upstream-task", false));
227+
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1,
228+
ShenyuThreadFactory.create("scheduled-upstream-task", false));
218229
ReflectionTestUtils.setField(upstreamCheckService, "executor", executor);
219230
final DivideUpstream divideUpstream = DivideUpstream.builder()
220231
.upstreamHost("localhost")
@@ -257,8 +268,10 @@ public void testFetchUpstreamData() {
257268
.status(0)
258269
.build();
259270
when(pluginMapper.selectByNames(anyList())).thenReturn(Lists.newArrayList(pluginDO));
260-
when(selectorMapper.findByPluginIds(anyList())).thenReturn(Lists.newArrayList(selectorDOWithUrlError, selectorDOWithUrlReachable));
261-
when(discoveryUpstreamService.findBySelectorId(anyString())).thenReturn(Lists.newArrayList(discoveryUpstreamData));
271+
when(selectorMapper.findByPluginIds(anyList()))
272+
.thenReturn(Lists.newArrayList(selectorDOWithUrlError, selectorDOWithUrlReachable));
273+
when(discoveryUpstreamService.findBySelectorId(anyString()))
274+
.thenReturn(Lists.newArrayList(discoveryUpstreamData));
262275
upstreamCheckService.fetchUpstreamData();
263276
assertTrue(upstreamMap.containsKey(MOCK_SELECTOR_NAME));
264277
assertEquals(2, upstreamMap.get(MOCK_SELECTOR_NAME).size());
@@ -271,7 +284,8 @@ public void testClose() {
271284
Properties properties = new Properties();
272285
properties.setProperty(Constants.IS_CHECKED, "true");
273286
shenyuRegisterCenterConfig.setProps(properties);
274-
upstreamCheckService = new UpstreamCheckService(selectorMapper, eventPublisher, pluginMapper, selectorConditionMapper,
287+
upstreamCheckService = new UpstreamCheckService(selectorMapper, eventPublisher, pluginMapper,
288+
selectorConditionMapper,
275289
shenyuRegisterCenterConfig, converterFactor, discoveryUpstreamService);
276290
upstreamCheckService.close();
277291
}

shenyu-spring-boot-starter/shenyu-spring-boot-starter-sync-data-center/shenyu-spring-boot-starter-sync-data-polaris/src/test/java/org/apache/shenyu/springboot/stater/sync/data/polaris/PolarisSyncDataConfigurationTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,18 @@
4343
* The test case for {@link PolarisSyncDataConfiguration}.
4444
*/
4545
@ExtendWith(SpringExtension.class)
46-
@SpringBootTest(
47-
classes = PolarisSyncDataConfiguration.class,
48-
properties = {
49-
"shenyu.sync.polaris.url=" + PolarisSyncDataConfigurationTest.URL,
50-
"shenyu.sync.polaris.namespace=default",
51-
"shenyu.sync.polaris.fileGroup=fileGroup"
52-
})
46+
@SpringBootTest(classes = PolarisSyncDataConfiguration.class, properties = {
47+
"shenyu.sync.polaris.url=" + PolarisSyncDataConfigurationTest.URL,
48+
"shenyu.sync.polaris.namespace=default",
49+
"shenyu.sync.polaris.fileGroup=fileGroup"
50+
})
5351
@EnableAutoConfiguration
5452
@MockBean(name = "shenyuConfig", value = ShenyuConfig.class, answer = CALLS_REAL_METHODS)
5553
public final class PolarisSyncDataConfigurationTest {
5654

5755
public static final String URL = "127.0.0.1:8093";
5856

59-
@Autowired
57+
@MockBean
6058
private SyncDataService syncDataService;
6159

6260
@Autowired
@@ -68,10 +66,13 @@ public void polarisConfigServiceTest() {
6866
assertNotNull(polarisConfig);
6967
final PolarisSyncDataConfiguration polarisSyncDataConfiguration = new PolarisSyncDataConfiguration();
7068

71-
final ConfigFileService configFileService = Assertions.assertDoesNotThrow(() -> polarisSyncDataConfiguration.polarisConfigServices(polarisConfig));
69+
final ConfigFileService configFileService = Assertions
70+
.assertDoesNotThrow(() -> polarisSyncDataConfiguration
71+
.polarisConfigServices(polarisConfig));
7272
assertInstanceOf(DefaultConfigFileService.class, configFileService);
7373
DefaultConfigFileService defaultConfigFileService = (DefaultConfigFileService) configFileService;
7474
final SDKContext sdkContext = defaultConfigFileService.getSDKContext();
75-
assertTrue(sdkContext.getConfig().getConfigFile().getServerConnector().getAddresses().contains(PolarisSyncDataConfigurationTest.URL));
75+
assertTrue(sdkContext.getConfig().getConfigFile().getServerConnector().getAddresses()
76+
.contains(PolarisSyncDataConfigurationTest.URL));
7677
}
7778
}

0 commit comments

Comments
 (0)