Add unit tests for package com.alibaba.nacos.config.server.service.dump (#11532)
* Add unit test for DumpAllProcessor * [#11531] Add unit tests for package com.alibaba.nacos.config.server.service.dump ———— Add unit test for DumpAllProcessor
This commit is contained in:
parent
d40190ee24
commit
4d76eb7c31
@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.config.server.service.dump.processor;
|
||||
|
||||
import com.alibaba.nacos.common.utils.MD5Utils;
|
||||
import com.alibaba.nacos.config.server.model.CacheItem;
|
||||
import com.alibaba.nacos.config.server.model.ConfigInfoWrapper;
|
||||
import com.alibaba.nacos.config.server.service.ConfigCacheService;
|
||||
import com.alibaba.nacos.config.server.service.dump.ExternalDumpService;
|
||||
import com.alibaba.nacos.config.server.service.dump.disk.ConfigDiskServiceFactory;
|
||||
import com.alibaba.nacos.config.server.service.dump.task.DumpAllTask;
|
||||
import com.alibaba.nacos.config.server.service.repository.ConfigInfoBetaPersistService;
|
||||
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
|
||||
import com.alibaba.nacos.config.server.service.repository.ConfigInfoTagPersistService;
|
||||
import com.alibaba.nacos.config.server.utils.GroupKey2;
|
||||
import com.alibaba.nacos.persistence.datasource.DataSourceService;
|
||||
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
|
||||
import com.alibaba.nacos.persistence.model.Page;
|
||||
import com.alibaba.nacos.plugin.datasource.constants.CommonConstant;
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DumpAllProcessorTest extends TestCase {
|
||||
|
||||
@Mock
|
||||
DynamicDataSource dynamicDataSource;
|
||||
|
||||
@Mock
|
||||
DataSourceService dataSourceService;
|
||||
|
||||
@Mock
|
||||
ConfigInfoBetaPersistService configInfoBetaPersistService;
|
||||
|
||||
@Mock
|
||||
ConfigInfoTagPersistService configInfoTagPersistService;
|
||||
|
||||
DumpAllProcessor dumpAllProcessor;
|
||||
|
||||
ExternalDumpService dumpService;
|
||||
|
||||
MockedStatic<DynamicDataSource> dynamicDataSourceMockedStatic;
|
||||
|
||||
@Mock
|
||||
ConfigInfoPersistService configInfoPersistService;
|
||||
|
||||
MockedStatic<EnvUtil> envUtilMockedStatic;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
dynamicDataSourceMockedStatic = Mockito.mockStatic(DynamicDataSource.class);
|
||||
envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class);
|
||||
dumpAllProcessor = new DumpAllProcessor(configInfoPersistService);
|
||||
when(EnvUtil.getNacosHome()).thenReturn(System.getProperty("user.home"));
|
||||
when(EnvUtil.getProperty(eq(CommonConstant.NACOS_PLUGIN_DATASOURCE_LOG), eq(Boolean.class),
|
||||
eq(false))).thenReturn(false);
|
||||
dynamicDataSourceMockedStatic.when(DynamicDataSource::getInstance).thenReturn(dynamicDataSource);
|
||||
|
||||
when(dynamicDataSource.getDataSource()).thenReturn(dataSourceService);
|
||||
|
||||
dumpService = new ExternalDumpService(configInfoPersistService, null, null, null, configInfoBetaPersistService,
|
||||
configInfoTagPersistService, null);
|
||||
|
||||
dumpAllProcessor = new DumpAllProcessor(configInfoPersistService);
|
||||
}
|
||||
|
||||
private static int newConfigCount = 1;
|
||||
|
||||
private ConfigInfoWrapper createNewConfig(int id) {
|
||||
ConfigInfoWrapper configInfoWrapper = new ConfigInfoWrapper();
|
||||
String dataId = "dataIdTime" + newConfigCount;
|
||||
configInfoWrapper.setDataId(dataId);
|
||||
String group = "groupTime" + newConfigCount;
|
||||
configInfoWrapper.setGroup(group);
|
||||
String tenant = "tenantTime" + newConfigCount;
|
||||
configInfoWrapper.setTenant(tenant);
|
||||
String content = "content " + newConfigCount;
|
||||
configInfoWrapper.setContent(content);
|
||||
configInfoWrapper.setId(id);
|
||||
newConfigCount++;
|
||||
return configInfoWrapper;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDumpAll() throws IOException {
|
||||
ConfigInfoWrapper configInfoWrapper1 = createNewConfig(1);
|
||||
ConfigInfoWrapper configInfoWrapper2 = createNewConfig(2);
|
||||
long timestamp = System.currentTimeMillis();
|
||||
configInfoWrapper1.setLastModified(timestamp);
|
||||
configInfoWrapper2.setLastModified(timestamp);
|
||||
Page<ConfigInfoWrapper> page = new Page<>();
|
||||
page.setTotalCount(2);
|
||||
page.setPagesAvailable(2);
|
||||
page.setPageNumber(1);
|
||||
ArrayList<ConfigInfoWrapper> list = Stream.of(configInfoWrapper1, configInfoWrapper2)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
page.setPageItems(list);
|
||||
|
||||
Mockito.when(configInfoPersistService.findConfigMaxId()).thenReturn(2L);
|
||||
Mockito.when(configInfoPersistService.findAllConfigInfoFragment(0, 1000))
|
||||
.thenReturn(page);
|
||||
|
||||
String groupKey1 = GroupKey2.getKey(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(), configInfoWrapper1.getTenant());
|
||||
String groupKey2 = GroupKey2.getKey(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(), configInfoWrapper2.getTenant());
|
||||
// For config 1, assign a latter time, to make sure that it would be updated.
|
||||
// For config 2, assign an earlier time, to make sure that it is not be updated.
|
||||
String md51 = MD5Utils.md5Hex(configInfoWrapper1.getContent(), "UTF-8");
|
||||
String md52 = MD5Utils.md5Hex(configInfoWrapper2.getContent(), "UTF-8");
|
||||
long latterTimestamp = timestamp + 999;
|
||||
long earlierTimestamp = timestamp - 999;
|
||||
String encryptedDataKey = "testEncryptedDataKey";
|
||||
ConfigCacheService.updateMd5(groupKey1, md51, latterTimestamp, encryptedDataKey);
|
||||
ConfigCacheService.updateMd5(groupKey2, md52, earlierTimestamp, encryptedDataKey);
|
||||
|
||||
DumpAllTask dumpAllTask = new DumpAllTask();
|
||||
boolean process = dumpAllProcessor.process(dumpAllTask);
|
||||
Assert.assertTrue(process);
|
||||
|
||||
//Check cache
|
||||
CacheItem contentCache1 = ConfigCacheService.getContentCache(GroupKey2.getKey(
|
||||
configInfoWrapper1.getDataId(),
|
||||
configInfoWrapper1.getGroup(),
|
||||
configInfoWrapper1.getTenant()));
|
||||
Assert.assertEquals(md51, contentCache1.getConfigCache().getMd5Utf8());
|
||||
// check if config1 is updated
|
||||
Assert.assertTrue(timestamp < contentCache1.getConfigCache().getLastModifiedTs());
|
||||
//check disk
|
||||
String contentFromDisk1 = ConfigDiskServiceFactory.getInstance().getContent(
|
||||
configInfoWrapper1.getDataId(),
|
||||
configInfoWrapper1.getGroup(),
|
||||
configInfoWrapper1.getTenant());
|
||||
Assert.assertEquals(configInfoWrapper1.getContent(), contentFromDisk1);
|
||||
|
||||
//Check cache
|
||||
CacheItem contentCache2 = ConfigCacheService.getContentCache(GroupKey2.getKey(
|
||||
configInfoWrapper2.getDataId(),
|
||||
configInfoWrapper2.getGroup(),
|
||||
configInfoWrapper2.getTenant()));
|
||||
Assert.assertEquals(MD5Utils.md5Hex(configInfoWrapper2.getContent(), "UTF-8"), contentCache2.getConfigCache().getMd5Utf8());
|
||||
// check if config2 is updated
|
||||
Assert.assertEquals(timestamp, contentCache2.getConfigCache().getLastModifiedTs());
|
||||
//check disk
|
||||
String contentFromDisk2 = ConfigDiskServiceFactory.getInstance().getContent(
|
||||
configInfoWrapper2.getDataId(),
|
||||
configInfoWrapper2.getGroup(),
|
||||
configInfoWrapper2.getTenant());
|
||||
Assert.assertEquals(configInfoWrapper2.getContent(), contentFromDisk2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user