Search in sources :

Example 26 with ItemDTO

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

the class ItemController method createItem.

@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public OpenItemDTO createItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @RequestBody OpenItemDTO item, HttpServletRequest request) {
    RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(item.getKey(), item.getValue(), item.getDataChangeCreatedBy()), "key,value,dataChangeCreatedBy 字段不能为空");
    if (userService.findByUserId(item.getDataChangeCreatedBy()) == null) {
        throw new BadRequestException("用户不存在.");
    }
    ItemDTO toCreate = OpenApiBeanUtils.transformToItemDTO(item);
    // protect
    toCreate.setLineNum(0);
    toCreate.setId(0);
    toCreate.setDataChangeLastModifiedBy(toCreate.getDataChangeCreatedBy());
    toCreate.setDataChangeLastModifiedTime(null);
    toCreate.setDataChangeCreatedTime(null);
    ItemDTO createdItem = itemService.createItem(appId, Env.fromString(env), clusterName, namespaceName, toCreate);
    return OpenApiBeanUtils.transformFromItemDTO(createdItem);
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with ItemDTO

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

the class ItemController method deleteItem.

@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName, #env) ")
@DeleteMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}")
public void deleteItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable long itemId) {
    ItemDTO item = configService.loadItemById(Env.valueOf(env), itemId);
    NamespaceDTO namespace = namespaceService.loadNamespaceBaseInfo(appId, Env.valueOf(env), clusterName, namespaceName);
    // In case someone constructs an attack scenario
    if (namespace == null || item.getNamespaceId() != namespace.getId()) {
        throw new BadRequestException("Invalid request, item and namespace do not match!");
    }
    configService.deleteItem(Env.valueOf(env), itemId, userInfoHolder.getUser().getUserId());
}
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) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 28 with ItemDTO

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

the class PropertyResolver method buildNormalItem.

private ItemDTO buildNormalItem(Long id, Long namespaceId, String key, String value, String comment, int lineNum) {
    ItemDTO item = new ItemDTO(key, value, comment, lineNum);
    item.setId(id);
    item.setNamespaceId(namespaceId);
    return item;
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO)

Example 29 with ItemDTO

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

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

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