Search in sources :

Example 11 with ItemChangeSets

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

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

the class ItemSetControllerTest method testItemSetUpdated.

@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetUpdated() {
    String appId = "someAppId";
    AppDTO app = restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
    ClusterDTO cluster = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default", ClusterDTO.class);
    NamespaceDTO namespace = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
    Assert.assertEquals("someAppId", app.getAppId());
    Assert.assertEquals("default", cluster.getName());
    Assert.assertEquals("application", namespace.getNamespaceName());
    ItemChangeSets createChangeSet = new ItemChangeSets();
    createChangeSet.setDataChangeLastModifiedBy("created");
    RestTemplate createdRestTemplate = (new TestRestTemplate()).getRestTemplate();
    createdRestTemplate.setMessageConverters(restTemplate.getMessageConverters());
    int createdSize = 3;
    for (int i = 0; i < createdSize; i++) {
        ItemDTO item = new ItemDTO();
        item.setNamespaceId(namespace.getId());
        item.setKey("key_" + i);
        item.setValue("created_value_" + i);
        createChangeSet.addCreateItem(item);
    }
    ResponseEntity<Void> response = createdRestTemplate.postForEntity("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset", createChangeSet, Void.class);
    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    ItemDTO[] items = createdRestTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items", ItemDTO[].class);
    ItemChangeSets updateChangeSet = new ItemChangeSets();
    updateChangeSet.setDataChangeLastModifiedBy("updated");
    RestTemplate updatedRestTemplate = (new TestRestTemplate()).getRestTemplate();
    updatedRestTemplate.setMessageConverters(restTemplate.getMessageConverters());
    int updatedSize = 2;
    for (int i = 0; i < updatedSize; i++) {
        items[i].setValue("updated_value_" + i);
        updateChangeSet.addUpdateItem(items[i]);
    }
    response = updatedRestTemplate.postForEntity("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset", updateChangeSet, Void.class);
    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
    Assert.assertEquals(createdSize, savedItems.size());
    Item item0 = savedItems.get(0);
    Assert.assertEquals("key_0", item0.getKey());
    Assert.assertEquals("updated_value_0", item0.getValue());
    Assert.assertEquals("created", item0.getDataChangeCreatedBy());
    Assert.assertEquals("updated", item0.getDataChangeLastModifiedBy());
    Assert.assertNotNull(item0.getDataChangeCreatedTime());
    Assert.assertNotNull(item0.getDataChangeLastModifiedTime());
}
Also used : TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) Item(com.ctrip.framework.apollo.biz.entity.Item) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) AppDTO(com.ctrip.framework.apollo.common.dto.AppDTO) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 13 with ItemChangeSets

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

the class ItemSetControllerTest method testItemSetDeleted.

@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetDeleted() {
    String appId = "someAppId";
    AppDTO app = restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
    ClusterDTO cluster = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default", ClusterDTO.class);
    NamespaceDTO namespace = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
    Assert.assertEquals("someAppId", app.getAppId());
    Assert.assertEquals("default", cluster.getName());
    Assert.assertEquals("application", namespace.getNamespaceName());
    ItemChangeSets createChangeSet = new ItemChangeSets();
    createChangeSet.setDataChangeLastModifiedBy("created");
    RestTemplate createdTemplate = (new TestRestTemplate()).getRestTemplate();
    createdTemplate.setMessageConverters(restTemplate.getMessageConverters());
    int createdSize = 3;
    for (int i = 0; i < createdSize; i++) {
        ItemDTO item = new ItemDTO();
        item.setNamespaceId(namespace.getId());
        item.setKey("key_" + i);
        item.setValue("created_value_" + i);
        createChangeSet.addCreateItem(item);
    }
    ResponseEntity<Void> response = createdTemplate.postForEntity("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset", createChangeSet, Void.class);
    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    ItemDTO[] items = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items", ItemDTO[].class);
    ItemChangeSets deleteChangeSet = new ItemChangeSets();
    deleteChangeSet.setDataChangeLastModifiedBy("deleted");
    RestTemplate deletedTemplate = (new TestRestTemplate()).getRestTemplate();
    deletedTemplate.setMessageConverters(restTemplate.getMessageConverters());
    int deletedSize = 1;
    for (int i = 0; i < deletedSize; i++) {
        items[i].setValue("deleted_value_" + i);
        deleteChangeSet.addDeleteItem(items[i]);
    }
    response = deletedTemplate.postForEntity("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset", deleteChangeSet, Void.class);
    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
    Assert.assertEquals(createdSize - deletedSize, savedItems.size());
    Item item0 = savedItems.get(0);
    Assert.assertEquals("key_1", item0.getKey());
    Assert.assertEquals("created_value_1", item0.getValue());
    Assert.assertEquals("created", item0.getDataChangeCreatedBy());
    Assert.assertNotNull(item0.getDataChangeCreatedTime());
}
Also used : TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) Item(com.ctrip.framework.apollo.biz.entity.Item) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) AppDTO(com.ctrip.framework.apollo.common.dto.AppDTO) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 14 with ItemChangeSets

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

the class ItemSetControllerTest method testItemSetCreated.

@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetCreated() {
    String appId = "someAppId";
    AppDTO app = restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
    ClusterDTO cluster = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default", ClusterDTO.class);
    NamespaceDTO namespace = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
    Assert.assertEquals("someAppId", app.getAppId());
    Assert.assertEquals("default", cluster.getName());
    Assert.assertEquals("application", namespace.getNamespaceName());
    ItemChangeSets itemSet = new ItemChangeSets();
    itemSet.setDataChangeLastModifiedBy("created");
    RestTemplate createdTemplate = (new TestRestTemplate()).getRestTemplate();
    createdTemplate.setMessageConverters(restTemplate.getMessageConverters());
    int createdSize = 3;
    for (int i = 0; i < createdSize; i++) {
        ItemDTO item = new ItemDTO();
        item.setNamespaceId(namespace.getId());
        item.setKey("key_" + i);
        item.setValue("created_value_" + i);
        itemSet.addCreateItem(item);
    }
    ResponseEntity<Void> response = createdTemplate.postForEntity("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset", itemSet, Void.class);
    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
    Assert.assertEquals(createdSize, items.size());
    Item item0 = items.get(0);
    Assert.assertEquals("key_0", item0.getKey());
    Assert.assertEquals("created_value_0", item0.getValue());
    Assert.assertEquals("created", item0.getDataChangeCreatedBy());
    Assert.assertNotNull(item0.getDataChangeCreatedTime());
}
Also used : TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) Item(com.ctrip.framework.apollo.biz.entity.Item) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) AppDTO(com.ctrip.framework.apollo.common.dto.AppDTO) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 15 with ItemChangeSets

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

the class ItemsComparator method compareIgnoreBlankAndCommentItem.

public ItemChangeSets compareIgnoreBlankAndCommentItem(long baseNamespaceId, List<ItemDTO> baseItems, List<ItemDTO> targetItems) {
    List<ItemDTO> filteredSourceItems = filterBlankAndCommentItem(baseItems);
    List<ItemDTO> filteredTargetItems = filterBlankAndCommentItem(targetItems);
    Map<String, ItemDTO> sourceItemMap = BeanUtils.mapByKey("key", filteredSourceItems);
    Map<String, ItemDTO> targetItemMap = BeanUtils.mapByKey("key", filteredTargetItems);
    ItemChangeSets changeSets = new ItemChangeSets();
    for (ItemDTO item : targetItems) {
        String key = item.getKey();
        ItemDTO sourceItem = sourceItemMap.get(key);
        if (sourceItem == null) {
            // add
            ItemDTO copiedItem = copyItem(item);
            copiedItem.setNamespaceId(baseNamespaceId);
            changeSets.addCreateItem(copiedItem);
        } else if (!Objects.equals(sourceItem.getValue(), item.getValue())) {
            // update
            // only value & comment can be update
            sourceItem.setValue(item.getValue());
            sourceItem.setComment(item.getComment());
            changeSets.addUpdateItem(sourceItem);
        }
    }
    for (ItemDTO item : baseItems) {
        String key = item.getKey();
        ItemDTO targetItem = targetItemMap.get(key);
        if (targetItem == null) {
            // delete
            changeSets.addDeleteItem(item);
        }
    }
    return changeSets;
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets)

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