use of com.ctrip.framework.apollo.common.dto.ItemDTO 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.ItemDTO in project apollo by ctripcorp.
the class ItemController method update.
@PreAcquireNamespaceLock
@PutMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}")
public ItemDTO update(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @PathVariable("itemId") long itemId, @RequestBody ItemDTO itemDTO) {
Item managedEntity = itemService.findOne(itemId);
if (managedEntity == null) {
throw new NotFoundException("item not found for itemId " + itemId);
}
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
// In case someone constructs an attack scenario
if (namespace == null || namespace.getId() != managedEntity.getNamespaceId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
}
Item entity = BeanUtils.transform(Item.class, itemDTO);
ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
Item beforeUpdateItem = BeanUtils.transform(Item.class, managedEntity);
// protect. only value,comment,lastModifiedBy can be modified
managedEntity.setValue(entity.getValue());
managedEntity.setComment(entity.getComment());
managedEntity.setDataChangeLastModifiedBy(entity.getDataChangeLastModifiedBy());
entity = itemService.update(managedEntity);
builder.updateItem(beforeUpdateItem, entity);
itemDTO = BeanUtils.transform(ItemDTO.class, entity);
if (builder.hasContent()) {
Commit commit = new Commit();
commit.setAppId(appId);
commit.setClusterName(clusterName);
commit.setNamespaceName(namespaceName);
commit.setChangeSets(builder.build());
commit.setDataChangeCreatedBy(itemDTO.getDataChangeLastModifiedBy());
commit.setDataChangeLastModifiedBy(itemDTO.getDataChangeLastModifiedBy());
commitService.save(commit);
}
return itemDTO;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ItemsComparator method copyItem.
private ItemDTO copyItem(ItemDTO sourceItem) {
ItemDTO copiedItem = new ItemDTO();
copiedItem.setKey(sourceItem.getKey());
copiedItem.setValue(sourceItem.getValue());
copiedItem.setComment(sourceItem.getComment());
return copiedItem;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO 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;
}
use of com.ctrip.framework.apollo.common.dto.ItemDTO in project apollo by ctripcorp.
the class ServerItemOpenApiService method updateItem.
@Override
public void updateItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) {
ItemDTO toUpdateItem = itemService.loadItem(Env.valueOf(env), appId, clusterName, namespaceName, itemDTO.getKey());
// protect. only value,comment,lastModifiedBy can be modified
toUpdateItem.setComment(itemDTO.getComment());
toUpdateItem.setValue(itemDTO.getValue());
toUpdateItem.setDataChangeLastModifiedBy(itemDTO.getDataChangeLastModifiedBy());
itemService.updateItem(appId, Env.valueOf(env), clusterName, namespaceName, toUpdateItem);
}
Aggregations