use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ConfigServiceTest method testCompareTargetNamespaceHasNoItems.
@Test
public void testCompareTargetNamespaceHasNoItems() {
ItemDTO sourceItem1 = new ItemDTO("a", "b", "comment", 1);
List<ItemDTO> sourceItems = Collections.singletonList(sourceItem1);
String appId = "6666", env = "LOCAL", clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName = ConfigConsts.NAMESPACE_APPLICATION;
List<NamespaceIdentifier> namespaceIdentifiers = generateNamespaceIdentifier(appId, env, clusterName, namespaceName);
NamespaceDTO namespaceDTO = generateNamespaceDTO(appId, clusterName, namespaceName);
when(namespaceAPI.loadNamespace(appId, Env.valueOf(env), clusterName, namespaceName)).thenReturn(namespaceDTO);
when(itemAPI.findItems(appId, Env.valueOf(env), clusterName, namespaceName)).thenReturn(null);
UserInfo userInfo = new UserInfo();
userInfo.setUserId("test");
when(userInfoHolder.getUser()).thenReturn(userInfo);
List<ItemDiffs> itemDiffses = configService.compare(namespaceIdentifiers, sourceItems);
assertEquals(1, itemDiffses.size());
ItemDiffs itemDiffs = itemDiffses.get(0);
ItemChangeSets changeSets = itemDiffs.getDiffs();
assertEquals(0, changeSets.getUpdateItems().size());
assertEquals(0, changeSets.getDeleteItems().size());
List<ItemDTO> createItems = changeSets.getCreateItems();
ItemDTO createItem = createItems.get(0);
assertEquals(1, createItem.getLineNum());
assertEquals("a", createItem.getKey());
assertEquals("b", createItem.getValue());
assertEquals("comment", createItem.getComment());
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ConfigServiceTest method testUpdateConfigByText.
@Test
public void testUpdateConfigByText() {
String appId = "6666";
String clusterName = "default";
String namespaceName = "application";
long someNamespaceId = 123L;
NamespaceTextModel model = new NamespaceTextModel();
model.setEnv("DEV");
model.setNamespaceName(namespaceName);
model.setClusterName(clusterName);
model.setAppId(appId);
model.setConfigText("a=b\nb=c\nc=d\nd=e");
model.setFormat(ConfigFileFormat.Properties.getValue());
List<ItemDTO> itemDTOs = mockBaseItemHas3Key();
ItemChangeSets changeSets = new ItemChangeSets();
changeSets.addCreateItem(new ItemDTO("d", "c", "", 4));
NamespaceDTO someNamespaceDto = mock(NamespaceDTO.class);
when(someNamespaceDto.getId()).thenReturn(someNamespaceId);
when(namespaceAPI.loadNamespace(appId, model.getEnv(), clusterName, namespaceName)).thenReturn(someNamespaceDto);
when(itemAPI.findItems(appId, Env.DEV, clusterName, namespaceName)).thenReturn(itemDTOs);
when(resolver.resolve(someNamespaceId, model.getConfigText(), itemDTOs)).thenReturn(changeSets);
UserInfo userInfo = new UserInfo();
userInfo.setUserId("test");
when(userInfoHolder.getUser()).thenReturn(userInfo);
try {
configService.updateConfigItemByText(model);
} catch (Exception e) {
Assert.fail();
}
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class FileTextResolverTest method testCreateItem.
@Test
public void testCreateItem() {
ItemChangeSets changeSets = resolver.resolve(NAMESPACE, CONFIG_TEXT, Collections.emptyList());
Assert.assertEquals(1, changeSets.getCreateItems().size());
Assert.assertEquals(0, changeSets.getUpdateItems().size());
Assert.assertEquals(0, changeSets.getDeleteItems().size());
ItemDTO createdItem = changeSets.getCreateItems().get(0);
Assert.assertEquals(CONFIG_TEXT, createdItem.getValue());
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class FileTextResolverTest method testUpdateItem.
@Test
public void testUpdateItem() {
ItemDTO existedItem = new ItemDTO();
existedItem.setId(1000);
existedItem.setKey(ConfigConsts.CONFIG_FILE_CONTENT_KEY);
existedItem.setValue("before");
ItemChangeSets changeSets = resolver.resolve(NAMESPACE, CONFIG_TEXT, Collections.singletonList(existedItem));
Assert.assertEquals(0, changeSets.getCreateItems().size());
Assert.assertEquals(1, changeSets.getUpdateItems().size());
Assert.assertEquals(0, changeSets.getDeleteItems().size());
ItemDTO updatedItem = changeSets.getUpdateItems().get(0);
Assert.assertEquals(CONFIG_TEXT, updatedItem.getValue());
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ConfigsImportService method importItems.
private void importItems(String appId, Env env, String clusterName, String namespaceName, String configText, NamespaceDTO namespaceDTO, String operator) {
List<ItemDTO> toImportItems = gson.fromJson(configText, GsonType.ITEM_DTOS);
toImportItems.parallelStream().forEach(newItem -> {
String key = newItem.getKey();
newItem.setNamespaceId(namespaceDTO.getId());
newItem.setDataChangeCreatedBy(operator);
newItem.setDataChangeLastModifiedBy(operator);
newItem.setDataChangeCreatedTime(new Date());
newItem.setDataChangeLastModifiedTime(new Date());
if (StringUtils.hasText(key)) {
// create or update normal item
try {
ItemDTO oldItem = itemService.loadItem(env, appId, clusterName, namespaceName, key);
newItem.setId(oldItem.getId());
// existed
itemService.updateItem(appId, env, clusterName, namespaceName, newItem);
} catch (Exception e) {
if (e instanceof HttpStatusCodeException && ((HttpStatusCodeException) e).getStatusCode().equals(HttpStatus.NOT_FOUND)) {
// not existed
itemService.createItem(appId, env, clusterName, namespaceName, newItem);
} else {
LOGGER.error("Load or update item error. appId = {}, env = {}, cluster = {}, namespace = {}", appId, env, clusterName, namespaceDTO, e);
}
}
} else if (StringUtils.hasText(newItem.getComment())) {
// create comment item
itemService.createCommentItem(appId, env, clusterName, namespaceName, newItem);
}
});
}
Aggregations