Search in sources :

Example 1 with Item

use of com.ctrip.framework.apollo.biz.entity.Item in project apollo by ctripcorp.

the class ItemController method create.

@PreAcquireNamespaceLock
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public ItemDTO create(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
    Item entity = BeanUtils.transfrom(Item.class, dto);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
    if (managedEntity != null) {
        throw new BadRequestException("item already exist");
    } else {
        entity = itemService.save(entity);
        builder.createItem(entity);
    }
    dto = BeanUtils.transfrom(ItemDTO.class, entity);
    Commit commit = new Commit();
    commit.setAppId(appId);
    commit.setClusterName(clusterName);
    commit.setNamespaceName(namespaceName);
    commit.setChangeSets(builder.build());
    commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
    commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
    commitService.save(commit);
    return dto;
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Item

use of com.ctrip.framework.apollo.biz.entity.Item in project apollo by ctripcorp.

the class ItemController method delete.

@PreAcquireNamespaceLock
@RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE)
public void delete(@PathVariable("itemId") long itemId, @RequestParam String operator) {
    Item entity = itemService.findOne(itemId);
    if (entity == null) {
        throw new NotFoundException("item not found for itemId " + itemId);
    }
    itemService.delete(entity.getId(), operator);
    Namespace namespace = namespaceService.findOne(entity.getNamespaceId());
    Commit commit = new Commit();
    commit.setAppId(namespace.getAppId());
    commit.setClusterName(namespace.getClusterName());
    commit.setNamespaceName(namespace.getNamespaceName());
    commit.setChangeSets(new ConfigChangeContentBuilder().deleteItem(entity).build());
    commit.setDataChangeCreatedBy(operator);
    commit.setDataChangeLastModifiedBy(operator);
    commitService.save(commit);
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Item

use of com.ctrip.framework.apollo.biz.entity.Item in project apollo by ctripcorp.

the class NamespaceUnlockAspectTest method testNamespaceAddItem.

@Test
public void testNamespaceAddItem() {
    long namespaceId = 1;
    Namespace namespace = createNamespace(namespaceId);
    Release release = createRelease("{\"k1\":\"v1\"}");
    List<Item> items = Arrays.asList(createItem("k1", "v1"), createItem("k2", "v2"));
    when(releaseService.findLatestActiveRelease(namespace)).thenReturn(release);
    when(itemService.findItemsWithOrdered(namespaceId)).thenReturn(items);
    when(namespaceService.findParentNamespace(namespace)).thenReturn(null);
    boolean isModified = namespaceUnlockAspect.isModified(namespace);
    Assert.assertTrue(isModified);
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Release(com.ctrip.framework.apollo.biz.entity.Release) Test(org.junit.Test)

Example 4 with Item

use of com.ctrip.framework.apollo.biz.entity.Item in project apollo by ctripcorp.

the class NamespaceUnlockAspectTest method testNamespaceDeleteItem.

@Test
public void testNamespaceDeleteItem() {
    long namespaceId = 1;
    Namespace namespace = createNamespace(namespaceId);
    Release release = createRelease("{\"k1\":\"v1\"}");
    List<Item> items = Arrays.asList(createItem("k2", "v2"));
    when(releaseService.findLatestActiveRelease(namespace)).thenReturn(release);
    when(itemService.findItemsWithOrdered(namespaceId)).thenReturn(items);
    when(namespaceService.findParentNamespace(namespace)).thenReturn(null);
    boolean isModified = namespaceUnlockAspect.isModified(namespace);
    Assert.assertTrue(isModified);
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Release(com.ctrip.framework.apollo.biz.entity.Release) Test(org.junit.Test)

Example 5 with Item

use of com.ctrip.framework.apollo.biz.entity.Item in project apollo by ctripcorp.

the class NamespaceUnlockAspectTest method createItem.

private Item createItem(String key, String value) {
    Item item = new Item();
    item.setKey(key);
    item.setValue(value);
    return item;
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item)

Aggregations

Item (com.ctrip.framework.apollo.biz.entity.Item)30 Test (org.junit.Test)13 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)12 Release (com.ctrip.framework.apollo.biz.entity.Release)11 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)6 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)4 Commit (com.ctrip.framework.apollo.biz.entity.Commit)4 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)4 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)4 Sql (org.springframework.test.context.jdbc.Sql)4 Transactional (org.springframework.transaction.annotation.Transactional)4 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)3 AbstractUnitTest (com.ctrip.framework.apollo.biz.AbstractUnitTest)3 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)3 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)3 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)3 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)3 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)3 TestRestTemplate (org.springframework.boot.test.TestRestTemplate)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3