Skip to content

Commit

Permalink
[type:feature] add_some_code_refactor_improve (apache#5613)
Browse files Browse the repository at this point in the history
* [type:feature] add_some_code_refactor_improve

* [type:feature] add_some_code_refactor_improve

* [type:feature] add_some_code_refactor_improve

* [type:feature] add_some_code_refactor_improve, e2e debug
  • Loading branch information
Aias00 authored Aug 15, 2024
1 parent a1c6502 commit 94553e4
Show file tree
Hide file tree
Showing 109 changed files with 330 additions and 432 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,13 @@ private <T> void onCommonChanged(final String configKeyPrefix, final List<T> cha
final List<String> changeNames = changedList.stream().map(mapperToKey).collect(Collectors.toList());
switch (eventType) {
case DELETE:
changedList.stream().map(mapperToKey).forEach(removeKey -> {
delConfig(configKeyPrefix + removeKey);
});
changedList.stream().map(mapperToKey).forEach(removeKey -> delConfig(configKeyPrefix + removeKey));
delChangedData(configKeyPrefix, changeNames);
break;
case REFRESH:
case MYSELF:
final List<String> configDataNames = this.getConfigDataNames(configKeyPrefix);
changedList.forEach(changedData -> {
publishConfig(configKeyPrefix + mapperToKey.apply(changedData), changedData);
});
changedList.forEach(changedData -> publishConfig(configKeyPrefix + mapperToKey.apply(changedData), changedData));

if (configDataNames != null && configDataNames.size() > changedList.size()) {
configDataNames.removeAll(changeNames);
Expand All @@ -125,9 +121,7 @@ private <T> void onCommonChanged(final String configKeyPrefix, final List<T> cha
publishConfig(configKeyPrefix + DefaultNodeConstants.LIST_STR, changeNames);
break;
default:
changedList.forEach(changedData -> {
publishConfig(configKeyPrefix + mapperToKey.apply(changedData), changedData);
});
changedList.forEach(changedData -> publishConfig(configKeyPrefix + mapperToKey.apply(changedData), changedData));
putChangeData(configKeyPrefix, changeNames);
break;
}
Expand Down Expand Up @@ -201,17 +195,13 @@ private <T> void onCommonMultiChanged(final List<T> changedList, final DataEvent
.collect(Collectors.groupingBy(mappingKey, Collectors.mapping(mappingValue, Collectors.toList())));
switch (eventType) {
case DELETE:
changedList.forEach(changedData -> {
delConfig(configKeyPrefix + mappingKey.apply(changedData) + DefaultNodeConstants.JOIN_POINT + mappingValue.apply(changedData));
});
changedList.forEach(changedData -> delConfig(configKeyPrefix + mappingKey.apply(changedData) + DefaultNodeConstants.JOIN_POINT + mappingValue.apply(changedData)));
this.delChangedMapToList(nameToIdMap, configKeyPrefix);
break;
case REFRESH:
case MYSELF:
default:
changedList.forEach(changedData -> {
publishConfig(configKeyPrefix + mappingKey.apply(changedData) + DefaultNodeConstants.JOIN_POINT + mappingValue.apply(changedData), changedData);
});
changedList.forEach(changedData -> publishConfig(configKeyPrefix + mappingKey.apply(changedData) + DefaultNodeConstants.JOIN_POINT + mappingValue.apply(changedData), changedData));
this.putChangedMapToList(nameToIdMap, configKeyPrefix);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -111,7 +113,7 @@ public void testOnAppAuthChangedDelete() {
@Test
public void testOnMetaDataChangedCreate() throws UnsupportedEncodingException {
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).build();
String metaDataPath = DefaultPathConstants.buildMetaDataPath(URLEncoder.encode(metaData.getPath(), "UTF-8"));
String metaDataPath = DefaultPathConstants.buildMetaDataPath(URLEncoder.encode(metaData.getPath(), StandardCharsets.UTF_8));

zookeeperDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.CREATE);
verify(zkClient, times(1)).createOrUpdate(metaDataPath, metaData, CreateMode.PERSISTENT);
Expand All @@ -123,7 +125,7 @@ public void testOnMetaDataChangedCreate() throws UnsupportedEncodingException {
@Test
public void testOnMetaDataChangedUpdate() throws UnsupportedEncodingException {
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).build();
String metaDataPath = DefaultPathConstants.buildMetaDataPath(URLEncoder.encode(metaData.getPath(), "UTF-8"));
String metaDataPath = DefaultPathConstants.buildMetaDataPath(URLEncoder.encode(metaData.getPath(), StandardCharsets.UTF_8));

zookeeperDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.UPDATE);
verify(zkClient, times(1)).createOrUpdate(metaDataPath, metaData, CreateMode.PERSISTENT);
Expand All @@ -135,7 +137,7 @@ public void testOnMetaDataChangedUpdate() throws UnsupportedEncodingException {
@Test
public void testOnMetaDataChangedDelete() throws UnsupportedEncodingException {
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).build();
String metaDataPath = DefaultPathConstants.buildMetaDataPath(URLEncoder.encode(metaData.getPath(), "UTF-8"));
String metaDataPath = DefaultPathConstants.buildMetaDataPath(URLEncoder.encode(metaData.getPath(), StandardCharsets.UTF_8));

when(zkClient.isExist(metaDataPath)).thenReturn(true);
zookeeperDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.DELETE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public DiscoveryConfigRegisterExecutorSubscriber(final DiscoveryService discover

@Override
public void executor(final Collection<DiscoveryConfigRegisterDTO> discoveryConfigRegisterDTOS) {
executorService.schedule(() -> {
discoveryConfigRegisterDTOS.forEach(discoveryService::registerDiscoveryConfig);
}, 2, TimeUnit.SECONDS);
executorService.schedule(() -> discoveryConfigRegisterDTOS.forEach(discoveryService::registerDiscoveryConfig), 2, TimeUnit.SECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ public DataType getType() {

@Override
public void executor(final Collection<MetaDataRegisterDTO> metaDataRegisterDTOList) {
metaDataRegisterDTOList.forEach(meta -> {
Optional.ofNullable(this.shenyuClientRegisterService.get(meta.getRpcType()))
.ifPresent(shenyuClientRegisterService -> {
synchronized (shenyuClientRegisterService) {
shenyuClientRegisterService.register(meta);
}
});
});
metaDataRegisterDTOList.forEach(meta -> Optional.ofNullable(this.shenyuClientRegisterService.get(meta.getRpcType()))
.ifPresent(shenyuClientRegisterService -> {
synchronized (shenyuClientRegisterService) {
shenyuClientRegisterService.register(meta);
}
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ private void copyHeaders(final HttpHeaders sourceHeaders, final HttpServletRespo
sourceHeaders.forEach((headerName, headerValues) -> {
String name = removeSpecial(headerName);
if (!response.containsHeader(name)) {
headerValues.forEach(headerValue -> {
response.addHeader(name, removeSpecial(headerValue));
});
headerValues.forEach(headerValue -> response.addHeader(name, removeSpecial(headerValue)));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public List<SelectorDO> findByNameAndPluginNames(final String name, final List<S
if (CollectionUtils.isEmpty(pluginDOList)) {
return Lists.newArrayList();
}
List<String> pluginIds = pluginDOList.stream().map(it -> it.getId()).collect(Collectors.toList());
List<String> pluginIds = pluginDOList.stream().map(BaseDO::getId).collect(Collectors.toList());
SelectorQuery selectorQuery = new SelectorQuery();
selectorQuery.setName(name);
selectorQuery.setPluginIds(pluginIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static UnZipResult unzip(final byte[] source) {
out.write(buffer, 0, offset);
}
String entryName = entry.getName();
itemList.add(new ZipItem(entryName, out.toString("UTF-8")));
itemList.add(new ZipItem(entryName, out.toString(StandardCharsets.UTF_8)));
} catch (IOException e) {
LOG.error("unzip error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testSecretPropertiesAssignValue() {
long randomLong = new Random().nextLong();
String randomKey = String.valueOf(Math.abs(randomLong));
if (LOG.isDebugEnabled()) {
LOG.debug("RandomKey is: " + randomKey);
LOG.debug("RandomKey is: {}", randomKey);
}
load(SecretPropertiesConfiguration.class, "shenyu.aes.secret.key=" + randomKey,
"shenyu.aes.secret.iv=" + randomKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.admin.config.properties;

import org.apache.shenyu.admin.AbstractConfigurationTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -28,7 +29,6 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.comparesEqualTo;
import static org.junit.Assert.assertEquals;

/**
* Test cases for HttpSyncProperties.
Expand All @@ -42,7 +42,7 @@ public void testDefault() {
HttpSyncProperties httpSyncProperties = getContext().getBean(HttpSyncProperties.class);
httpSyncProperties.setNotifyBatchSize(0);
assertThat(httpSyncProperties.isEnabled(), comparesEqualTo(true));
assertEquals(httpSyncProperties.getNotifyBatchSize(), 0);
Assertions.assertEquals(httpSyncProperties.getNotifyBatchSize(), 0);
assertThat(httpSyncProperties.getRefreshInterval(), comparesEqualTo(Duration.ofMinutes(5)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,10 @@ public void testCreateDiscovery() {
public void testCreateProxySelector() {
DiscoveryHandlerDTO discoveryHandlerDTO = new DiscoveryHandlerDTO();
ProxySelectorDTO proxySelectorDTO = new ProxySelectorDTO();
Assertions.assertThrows(NullPointerException.class, () -> {
defaultDiscoveryProcessor.createProxySelector(discoveryHandlerDTO, proxySelectorDTO);
});
Assertions.assertThrows(NullPointerException.class, () -> defaultDiscoveryProcessor.createProxySelector(discoveryHandlerDTO, proxySelectorDTO));
discoveryHandlerDTO.setDiscoveryId("id");

Assertions.assertThrows(ShenyuAdminException.class, () -> {
defaultDiscoveryProcessor.createProxySelector(discoveryHandlerDTO, proxySelectorDTO);
});
Assertions.assertThrows(ShenyuAdminException.class, () -> defaultDiscoveryProcessor.createProxySelector(discoveryHandlerDTO, proxySelectorDTO));
when(shenyuDiscoveryService.exists(anyString())).thenReturn(true);
doNothing().when(shenyuDiscoveryService).watch(anyString(), any(DataChangedEventListener.class));
doNothing().when(eventPublisher).publishEvent(any(Object.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,23 @@
*/

package org.apache.shenyu.admin.disruptor.subscriber;

import org.apache.shenyu.admin.service.register.ShenyuClientRegisterService;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.type.DataType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -54,14 +53,14 @@ public class MetadataExecutorSubscriberTest {

@Test
public void testGetType() {
assertEquals(DataType.META_DATA, metadataExecutorSubscriber.getType());
Assertions.assertEquals(DataType.META_DATA, metadataExecutorSubscriber.getType());
}

@Test
public void testExecutor() {
List<MetaDataRegisterDTO> list = new ArrayList<>();
metadataExecutorSubscriber.executor(list);
assertTrue(list.isEmpty());
Assertions.assertTrue(list.isEmpty());
list.add(MetaDataRegisterDTO.builder().appName("test").build());
ShenyuClientRegisterService service = mock(ShenyuClientRegisterService.class);
when(shenyuClientRegisterService.get(any())).thenReturn(service);
Expand Down
Loading

0 comments on commit 94553e4

Please sign in to comment.