Search in sources :

Example 1 with ItemChangeSets

use of com.ctrip.framework.apollo.common.dto.ItemChangeSets in project apollo by ctripcorp.

the class PropertyResolver method resolve.

@Override
public ItemChangeSets resolve(long namespaceId, String configText, List<ItemDTO> baseItems) {
    Map<Integer, ItemDTO> oldLineNumMapItem = BeanUtils.mapByKey("lineNum", baseItems);
    Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems);
    // remove comment and blank item map.
    oldKeyMapItem.remove("");
    String[] newItems = configText.split(ITEM_SEPARATOR);
    Set<String> repeatKeys = new HashSet<>();
    if (isHasRepeatKey(newItems, repeatKeys)) {
        throw new BadRequestException(String.format("Config text has repeated keys: %s, please check your input.", repeatKeys.toString()));
    }
    ItemChangeSets changeSets = new ItemChangeSets();
    // use for delete blank and comment item
    Map<Integer, String> newLineNumMapItem = new HashMap<>();
    int lineCounter = 1;
    for (String newItem : newItems) {
        newItem = newItem.trim();
        newLineNumMapItem.put(lineCounter, newItem);
        ItemDTO oldItemByLine = oldLineNumMapItem.get(lineCounter);
        // comment item
        if (isCommentItem(newItem)) {
            handleCommentLine(namespaceId, oldItemByLine, newItem, lineCounter, changeSets);
        // blank item
        } else if (isBlankItem(newItem)) {
            handleBlankLine(namespaceId, oldItemByLine, lineCounter, changeSets);
        // normal item
        } else {
            handleNormalLine(namespaceId, oldKeyMapItem, newItem, lineCounter, changeSets);
        }
        lineCounter++;
    }
    deleteCommentAndBlankItem(oldLineNumMapItem, newLineNumMapItem, changeSets);
    deleteNormalKVItem(oldKeyMapItem, changeSets);
    return changeSets;
}
Also used : HashMap(java.util.HashMap) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) HashSet(java.util.HashSet)

Example 2 with ItemChangeSets

use of com.ctrip.framework.apollo.common.dto.ItemChangeSets 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 3 with ItemChangeSets

use of com.ctrip.framework.apollo.common.dto.ItemChangeSets 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();
    }
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) NamespaceTextModel(com.ctrip.framework.apollo.portal.entity.model.NamespaceTextModel) 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) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 4 with ItemChangeSets

use of com.ctrip.framework.apollo.common.dto.ItemChangeSets 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());
}
Also used : ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 5 with ItemChangeSets

use of com.ctrip.framework.apollo.common.dto.ItemChangeSets 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());
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Aggregations

ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)28 Test (org.junit.Test)18 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)15 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)14 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)8 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)5 ItemDiffs (com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs)5 NamespaceIdentifier (com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier)5 Item (com.ctrip.framework.apollo.biz.entity.Item)3 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)3 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)3 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)3 Sql (org.springframework.test.context.jdbc.Sql)3 RestTemplate (org.springframework.web.client.RestTemplate)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)2 Env (com.ctrip.framework.apollo.portal.environment.Env)2 HashMap (java.util.HashMap)2 ConfigTextResolver (com.ctrip.framework.apollo.portal.component.txtresolver.ConfigTextResolver)1 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)1