Search in sources :

Example 36 with ItemDTO

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

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

the class ItemSetService method updateSet.

@Transactional
public ItemChangeSets updateSet(String appId, String clusterName, String namespaceName, ItemChangeSets changeSet) {
    String operator = changeSet.getDataChangeLastModifiedBy();
    ConfigChangeContentBuilder configChangeContentBuilder = new ConfigChangeContentBuilder();
    if (!CollectionUtils.isEmpty(changeSet.getCreateItems())) {
        for (ItemDTO item : changeSet.getCreateItems()) {
            Item entity = BeanUtils.transform(Item.class, item);
            entity.setDataChangeCreatedBy(operator);
            entity.setDataChangeLastModifiedBy(operator);
            Item createdItem = itemService.save(entity);
            configChangeContentBuilder.createItem(createdItem);
        }
        auditService.audit("ItemSet", null, Audit.OP.INSERT, operator);
    }
    if (!CollectionUtils.isEmpty(changeSet.getUpdateItems())) {
        for (ItemDTO item : changeSet.getUpdateItems()) {
            Item entity = BeanUtils.transform(Item.class, item);
            Item managedItem = itemService.findOne(entity.getId());
            if (managedItem == null) {
                throw new NotFoundException(String.format("item not found.(key=%s)", entity.getKey()));
            }
            Item beforeUpdateItem = BeanUtils.transform(Item.class, managedItem);
            // protect. only value,comment,lastModifiedBy,lineNum can be modified
            managedItem.setValue(entity.getValue());
            managedItem.setComment(entity.getComment());
            managedItem.setLineNum(entity.getLineNum());
            managedItem.setDataChangeLastModifiedBy(operator);
            Item updatedItem = itemService.update(managedItem);
            configChangeContentBuilder.updateItem(beforeUpdateItem, updatedItem);
        }
        auditService.audit("ItemSet", null, Audit.OP.UPDATE, operator);
    }
    if (!CollectionUtils.isEmpty(changeSet.getDeleteItems())) {
        for (ItemDTO item : changeSet.getDeleteItems()) {
            Item deletedItem = itemService.delete(item.getId(), operator);
            configChangeContentBuilder.deleteItem(deletedItem);
        }
        auditService.audit("ItemSet", null, Audit.OP.DELETE, operator);
    }
    if (configChangeContentBuilder.hasContent()) {
        createCommit(appId, clusterName, namespaceName, configChangeContentBuilder.build(), changeSet.getDataChangeLastModifiedBy());
    }
    return changeSet;
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 38 with ItemDTO

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

the class ConfigsImportService method importNamespace.

private void importNamespace(final String appId, final Env env, final String clusterName, final String namespaceName, final String configText, final String format, boolean ignoreConflictNamespace, String operator) {
    NamespaceDTO namespaceDTO;
    try {
        namespaceDTO = namespaceService.loadNamespaceBaseInfo(appId, env, clusterName, namespaceName);
    } catch (Exception e) {
        // not existed
        namespaceDTO = null;
    }
    if (namespaceDTO == null) {
        namespaceDTO = new NamespaceDTO();
        namespaceDTO.setAppId(appId);
        namespaceDTO.setClusterName(clusterName);
        namespaceDTO.setNamespaceName(namespaceName);
        namespaceDTO.setDataChangeCreatedBy(operator);
        namespaceDTO.setDataChangeLastModifiedBy(operator);
        namespaceDTO = namespaceService.createNamespace(env, namespaceDTO);
        roleInitializationService.initNamespaceRoles(appId, namespaceName, operator);
        roleInitializationService.initNamespaceEnvRoles(appId, namespaceName, operator);
    }
    List<ItemDTO> itemDTOS = itemService.findItems(appId, env, clusterName, namespaceName);
    // skip import if target namespace has existed items
    if (!CollectionUtils.isEmpty(itemDTOS) && ignoreConflictNamespace) {
        return;
    }
    importItems(appId, env, clusterName, namespaceName, configText, namespaceDTO, operator);
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) ServerException(java.rmi.ServerException) IOException(java.io.IOException) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException)

Example 39 with ItemDTO

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

the class ItemService method parseChangeSets.

private ItemChangeSets parseChangeSets(NamespaceIdentifier namespace, List<ItemDTO> sourceItems) {
    ItemChangeSets changeSets = new ItemChangeSets();
    List<ItemDTO> targetItems = itemAPI.findItems(namespace.getAppId(), namespace.getEnv(), namespace.getClusterName(), namespace.getNamespaceName());
    long namespaceId = getNamespaceId(namespace);
    if (CollectionUtils.isEmpty(targetItems)) {
        // all source items is added
        int lineNum = 1;
        for (ItemDTO sourceItem : sourceItems) {
            changeSets.addCreateItem(buildItem(namespaceId, lineNum++, sourceItem));
        }
    } else {
        Map<String, ItemDTO> targetItemMap = BeanUtils.mapByKey("key", targetItems);
        String key, sourceValue, sourceComment;
        ItemDTO targetItem = null;
        // append to last
        int maxLineNum = targetItems.size();
        for (ItemDTO sourceItem : sourceItems) {
            key = sourceItem.getKey();
            sourceValue = sourceItem.getValue();
            sourceComment = sourceItem.getComment();
            targetItem = targetItemMap.get(key);
            if (targetItem == null) {
                // added items
                changeSets.addCreateItem(buildItem(namespaceId, ++maxLineNum, sourceItem));
            } else if (isModified(sourceValue, targetItem.getValue(), sourceComment, targetItem.getComment())) {
                // modified items
                targetItem.setValue(sourceValue);
                targetItem.setComment(sourceComment);
                changeSets.addUpdateItem(targetItem);
            }
        }
    }
    return changeSets;
}
Also used : ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO)

Example 40 with ItemDTO

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

the class ItemService method createItem.

public ItemDTO createItem(String appId, Env env, String clusterName, String namespaceName, ItemDTO item) {
    NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
    if (namespace == null) {
        throw new BadRequestException("namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
    }
    item.setNamespaceId(namespace.getId());
    ItemDTO itemDTO = itemAPI.createItem(appId, env, clusterName, namespaceName, item);
    Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE, String.format("%s+%s+%s+%s", appId, env, clusterName, namespaceName));
    return itemDTO;
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Aggregations

ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)44 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)14 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)12 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)11 Test (org.junit.Test)11 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)7 Item (com.ctrip.framework.apollo.biz.entity.Item)6 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)6 HashMap (java.util.HashMap)6 ItemBO (com.ctrip.framework.apollo.portal.entity.bo.ItemBO)5 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)4 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)4 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Sql (org.springframework.test.context.jdbc.Sql)4 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)3 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)3 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)3 RestTemplate (org.springframework.web.client.RestTemplate)3