Search in sources :

Example 1 with ItemDiffs

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());
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 2 with ItemDiffs

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;
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with 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());
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 4 with ItemDiffs

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));
    }
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) Env(com.ctrip.framework.apollo.portal.environment.Env)

Example 5 with ItemDiffs

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;
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) LinkedList(java.util.LinkedList)

Aggregations

ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)5 ItemDiffs (com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs)5 NamespaceIdentifier (com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier)5 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)2 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)2 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)2 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)2 Test (org.junit.Test)2 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)1 Env (com.ctrip.framework.apollo.portal.environment.Env)1 LinkedList (java.util.LinkedList)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1