use of com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs 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.portal.entity.vo.ItemDiffs in project apollo by ctripcorp.
the class ItemController method diff.
@PostMapping(value = "/namespaces/{namespaceName}/diff", consumes = { "application/json" })
public List<ItemDiffs> diff(@RequestBody NamespaceSyncModel model) {
checkModel(!model.isInvalid());
List<ItemDiffs> itemDiffs = configService.compare(model.getSyncToNamespaces(), model.getSyncItems());
for (ItemDiffs diff : itemDiffs) {
NamespaceIdentifier namespace = diff.getNamespace();
if (namespace == null) {
continue;
}
if (permissionValidator.shouldHideConfigToCurrentUser(namespace.getAppId(), namespace.getEnv().name(), namespace.getNamespaceName())) {
diff.setDiffs(new ItemChangeSets());
diff.setExtInfo("You are not this project's administrator, nor you have edit or release permission for the namespace in environment: " + namespace.getEnv());
}
}
return itemDiffs;
}
use of com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs in project apollo by ctripcorp.
the class ConfigServiceTest method testCompare.
@Test
public void testCompare() {
// not modified
ItemDTO sourceItem1 = new ItemDTO("a", "b", "comment", 1);
// new item
ItemDTO sourceItem2 = new ItemDTO("newKey", "c", "comment", 2);
// update value
ItemDTO sourceItem3 = new ItemDTO("c", "newValue", "comment", 3);
// update comment
ItemDTO sourceItem4 = new ItemDTO("d", "b", "newComment", 4);
List<ItemDTO> sourceItems = Arrays.asList(sourceItem1, sourceItem2, sourceItem3, sourceItem4);
ItemDTO targetItem1 = new ItemDTO("a", "b", "comment", 1);
ItemDTO targetItem2 = new ItemDTO("c", "oldValue", "comment", 2);
ItemDTO targetItem3 = new ItemDTO("d", "b", "oldComment", 3);
List<ItemDTO> targetItems = Arrays.asList(targetItem1, targetItem2, targetItem3);
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(targetItems);
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.getDeleteItems().size());
assertEquals(2, changeSets.getUpdateItems().size());
assertEquals(1, changeSets.getCreateItems().size());
NamespaceIdentifier namespaceIdentifier = itemDiffs.getNamespace();
assertEquals(appId, namespaceIdentifier.getAppId());
assertEquals(Env.valueOf("LOCAL"), namespaceIdentifier.getEnv());
assertEquals(clusterName, namespaceIdentifier.getClusterName());
assertEquals(namespaceName, namespaceIdentifier.getNamespaceName());
ItemDTO createdItem = changeSets.getCreateItems().get(0);
assertEquals("newKey", createdItem.getKey());
assertEquals("c", createdItem.getValue());
assertEquals("comment", createdItem.getComment());
assertEquals(4, createdItem.getLineNum());
List<ItemDTO> updateItems = changeSets.getUpdateItems();
ItemDTO updateItem1 = updateItems.get(0);
ItemDTO updateItem2 = updateItems.get(1);
assertEquals("c", updateItem1.getKey());
assertEquals("newValue", updateItem1.getValue());
assertEquals("comment", updateItem1.getComment());
assertEquals(2, updateItem1.getLineNum());
assertEquals("d", updateItem2.getKey());
assertEquals("b", updateItem2.getValue());
assertEquals("newComment", updateItem2.getComment());
assertEquals(3, updateItem2.getLineNum());
}
use of com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs in project apollo by ctripcorp.
the class ItemService method syncItems.
public void syncItems(List<NamespaceIdentifier> comparedNamespaces, List<ItemDTO> sourceItems) {
List<ItemDiffs> itemDiffs = compare(comparedNamespaces, sourceItems);
for (ItemDiffs itemDiff : itemDiffs) {
NamespaceIdentifier namespaceIdentifier = itemDiff.getNamespace();
ItemChangeSets changeSets = itemDiff.getDiffs();
changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
String appId = namespaceIdentifier.getAppId();
Env env = namespaceIdentifier.getEnv();
String clusterName = namespaceIdentifier.getClusterName();
String namespaceName = namespaceIdentifier.getNamespaceName();
itemAPI.updateItemsByChangeSet(appId, env, clusterName, namespaceName, changeSets);
Tracer.logEvent(TracerEventType.SYNC_NAMESPACE, String.format("%s+%s+%s+%s", appId, env, clusterName, namespaceName));
}
}
use of com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs in project apollo by ctripcorp.
the class ItemService method compare.
public List<ItemDiffs> compare(List<NamespaceIdentifier> comparedNamespaces, List<ItemDTO> sourceItems) {
List<ItemDiffs> result = new LinkedList<>();
for (NamespaceIdentifier namespace : comparedNamespaces) {
ItemDiffs itemDiffs = new ItemDiffs(namespace);
try {
itemDiffs.setDiffs(parseChangeSets(namespace, sourceItems));
} catch (BadRequestException e) {
itemDiffs.setDiffs(new ItemChangeSets());
itemDiffs.setExtInfo("该集群下没有名为 " + namespace.getNamespaceName() + " 的namespace");
}
result.add(itemDiffs);
}
return result;
}
Aggregations