use of com.ctrip.framework.apollo.common.dto.NamespaceDTO 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());
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO 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());
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO 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());
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO 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());
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO 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());
}
Aggregations