use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.
the class ConfigControllerTest method testFuzzySearchConfig.
@Test
public void testFuzzySearchConfig() throws Exception {
List<ConfigInfo> configInfoList = new ArrayList<>();
ConfigInfo configInfo = new ConfigInfo("test", "test", "test");
configInfoList.add(configInfo);
Page<ConfigInfo> page = new Page<>();
page.setTotalCount(15);
page.setPageNumber(1);
page.setPagesAvailable(2);
page.setPageItems(configInfoList);
Map<String, Object> configAdvanceInfo = new HashMap<>(8);
when(persistService.findConfigInfoLike4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(page);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH).param("search", "blur").param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "").param("config_tags", "").param("pageNo", "1").param("pageSize", "10");
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
List resultList = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("pageItems").toString(), List.class);
ConfigInfo resConfigInfo = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("pageItems").get(0).toString(), ConfigInfo.class);
Assert.assertEquals(configInfoList.size(), resultList.size());
Assert.assertEquals(configInfo.getDataId(), resConfigInfo.getDataId());
Assert.assertEquals(configInfo.getGroup(), resConfigInfo.getGroup());
Assert.assertEquals(configInfo.getContent(), resConfigInfo.getContent());
}
use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.
the class ConfigControllerTest method testSearchConfig.
@Test
public void testSearchConfig() throws Exception {
List<ConfigInfo> configInfoList = new ArrayList<>();
ConfigInfo configInfo = new ConfigInfo("test", "test", "test");
configInfoList.add(configInfo);
Page<ConfigInfo> page = new Page<>();
page.setTotalCount(15);
page.setPageNumber(1);
page.setPagesAvailable(2);
page.setPageItems(configInfoList);
Map<String, Object> configAdvanceInfo = new HashMap<>(8);
when(persistService.findConfigInfo4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(page);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH).param("search", "accurate").param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "").param("config_tags", "").param("pageNo", "1").param("pageSize", "10");
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
JsonNode pageItemsNode = JacksonUtils.toObj(actualValue).get("pageItems");
List resultList = JacksonUtils.toObj(pageItemsNode.toString(), List.class);
ConfigInfo resConfigInfo = JacksonUtils.toObj(pageItemsNode.get(0).toString(), ConfigInfo.class);
Assert.assertEquals(configInfoList.size(), resultList.size());
Assert.assertEquals(configInfo.getDataId(), resConfigInfo.getDataId());
Assert.assertEquals(configInfo.getGroup(), resConfigInfo.getGroup());
Assert.assertEquals(configInfo.getContent(), resConfigInfo.getContent());
}
use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.
the class EmbeddedStoragePersistServiceImpl method updateConfigInfo.
@Override
public void updateConfigInfo(final ConfigInfo configInfo, final String srcIp, final String srcUser, final Timestamp time, final Map<String, Object> configAdvanceInfo, final boolean notify) {
try {
ConfigInfo oldConfigInfo = findConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
final String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
oldConfigInfo.setTenant(tenantTmp);
String appNameTmp = oldConfigInfo.getAppName();
// otherwise, the appName of db is used. Empty string is required to clear appName
if (configInfo.getAppName() == null) {
configInfo.setAppName(appNameTmp);
}
updateConfigInfoAtomic(configInfo, srcIp, srcUser, time, configAdvanceInfo);
String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
if (configTags != null) {
// Delete all tags and recreate them
removeTagByIdAtomic(oldConfigInfo.getId());
addConfigTagsRelation(oldConfigInfo.getId(), configTags, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
}
insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp, srcUser, time, "U");
EmbeddedStorageContextUtils.onModifyConfigInfo(configInfo, srcIp, time);
databaseOperate.blockUpdate();
} finally {
EmbeddedStorageContextUtils.cleanAllContext();
}
}
use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.
the class EmbeddedStoragePersistServiceImpl method batchInsertOrUpdate.
@Override
public Map<String, Object> batchInsertOrUpdate(List<ConfigAllInfo> configInfoList, String srcUser, String srcIp, Map<String, Object> configAdvanceInfo, Timestamp time, boolean notify, SameConfigPolicy policy) throws NacosException {
int succCount = 0;
int skipCount = 0;
List<Map<String, String>> failData = null;
List<Map<String, String>> skipData = null;
final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
if (t != null) {
throw new NacosRuntimeException(0, t);
}
};
for (int i = 0; i < configInfoList.size(); i++) {
ConfigAllInfo configInfo = configInfoList.get(i);
try {
ParamUtils.checkParam(configInfo.getDataId(), configInfo.getGroup(), "datumId", configInfo.getContent());
} catch (Throwable e) {
DEFAULT_LOG.error("data verification failed", e);
throw e;
}
ConfigInfo configInfo2Save = new ConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), configInfo.getAppName(), configInfo.getContent());
configInfo2Save.setEncryptedDataKey(configInfo.getEncryptedDataKey() == null ? "" : configInfo.getEncryptedDataKey());
String type = configInfo.getType();
if (StringUtils.isBlank(type)) {
// simple judgment of file type based on suffix
if (configInfo.getDataId().contains(SPOT)) {
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1);
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
type = fileTypeEnum.getFileType();
} else {
type = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(null).getFileType();
}
}
if (configAdvanceInfo == null) {
configAdvanceInfo = new HashMap<>(16);
}
configAdvanceInfo.put("type", type);
configAdvanceInfo.put("desc", configInfo.getDesc());
try {
ConfigInfo foundCfg = findConfigInfo(configInfo2Save.getDataId(), configInfo2Save.getGroup(), configInfo2Save.getTenant());
if (foundCfg != null) {
throw new Throwable("DuplicateKeyException: config already exists, should be overridden");
}
addConfigInfo(srcIp, srcUser, configInfo2Save, time, configAdvanceInfo, notify, callFinally);
succCount++;
} catch (Throwable e) {
if (!StringUtils.contains(e.toString(), "DuplicateKeyException")) {
throw new NacosException(NacosException.SERVER_ERROR, e);
}
// uniqueness constraint conflict
if (SameConfigPolicy.ABORT.equals(policy)) {
failData = new ArrayList<>();
skipData = new ArrayList<>();
Map<String, String> faileditem = new HashMap<>(2);
faileditem.put("dataId", configInfo2Save.getDataId());
faileditem.put("group", configInfo2Save.getGroup());
failData.add(faileditem);
for (int j = (i + 1); j < configInfoList.size(); j++) {
ConfigInfo skipConfigInfo = configInfoList.get(j);
Map<String, String> skipitem = new HashMap<>(2);
skipitem.put("dataId", skipConfigInfo.getDataId());
skipitem.put("group", skipConfigInfo.getGroup());
skipData.add(skipitem);
}
break;
} else if (SameConfigPolicy.SKIP.equals(policy)) {
skipCount++;
if (skipData == null) {
skipData = new ArrayList<>();
}
Map<String, String> skipitem = new HashMap<>(2);
skipitem.put("dataId", configInfo2Save.getDataId());
skipitem.put("group", configInfo2Save.getGroup());
skipData.add(skipitem);
} else if (SameConfigPolicy.OVERWRITE.equals(policy)) {
succCount++;
updateConfigInfo(configInfo2Save, srcIp, srcUser, time, configAdvanceInfo, notify);
}
}
}
Map<String, Object> result = new HashMap<>(4);
result.put("succCount", succCount);
result.put("skipCount", skipCount);
if (failData != null && !failData.isEmpty()) {
result.put("failData", failData);
}
if (skipData != null && !skipData.isEmpty()) {
result.put("skipData", skipData);
}
return result;
}
use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.
the class EmbeddedStoragePersistServiceImpl method findConfigInfoLike4Page.
@Override
public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int pageSize, final String dataId, final String group, final String tenant, final Map<String, Object> configAdvanceInfo) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
final String appName = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("appName");
final String content = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("content");
final String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
String sqlCountRows = "SELECT count(*) FROM config_info";
String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info";
StringBuilder where = new StringBuilder(" WHERE ");
List<String> params = new ArrayList<String>();
params.add(generateLikeArgument(tenantTmp));
if (StringUtils.isNotBlank(configTags)) {
sqlCountRows = "SELECT count(*) FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id ";
sqlFetchRows = "SELECT a.ID,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info a LEFT JOIN " + "config_tags_relation b ON a.id=b.id ";
where.append(" a.tenant_id LIKE ? ");
if (!StringUtils.isBlank(dataId)) {
where.append(" AND a.data_id LIKE ? ");
params.add(generateLikeArgument(dataId));
}
if (!StringUtils.isBlank(group)) {
where.append(" AND a.group_id LIKE ? ");
params.add(generateLikeArgument(group));
}
if (!StringUtils.isBlank(appName)) {
where.append(" AND a.app_name = ? ");
params.add(appName);
}
if (!StringUtils.isBlank(content)) {
where.append(" AND a.content LIKE ? ");
params.add(generateLikeArgument(content));
}
where.append(" AND b.tag_name IN (");
String[] tagArr = configTags.split(",");
for (int i = 0; i < tagArr.length; i++) {
if (i != 0) {
where.append(", ");
}
where.append('?');
params.add(tagArr[i]);
}
where.append(") ");
} else {
where.append(" tenant_id LIKE ? ");
if (!StringUtils.isBlank(dataId)) {
where.append(" AND data_id LIKE ? ");
params.add(generateLikeArgument(dataId));
}
if (!StringUtils.isBlank(group)) {
where.append(" AND group_id LIKE ? ");
params.add(generateLikeArgument(group));
}
if (!StringUtils.isBlank(appName)) {
where.append(" AND app_name = ? ");
params.add(appName);
}
if (!StringUtils.isBlank(content)) {
where.append(" AND content LIKE ? ");
params.add(generateLikeArgument(content));
}
}
PaginationHelper<ConfigInfo> helper = createPaginationHelper();
Page<ConfigInfo> page = helper.fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize, CONFIG_INFO_ROW_MAPPER);
for (ConfigInfo configInfo : page.getPageItems()) {
Pair<String, String> pair = EncryptionHandler.decryptHandler(configInfo.getDataId(), configInfo.getEncryptedDataKey(), configInfo.getContent());
configInfo.setContent(pair.getSecond());
}
return page;
}
Aggregations