Commit 44375b3b by Jason Song

Merge pull request #83 from yiming187/item_set

Add ItemChangeSet Integration Test
parents c99fd43f 58c1a20c
...@@ -3,7 +3,6 @@ package com.ctrip.apollo.adminservice.controller; ...@@ -3,7 +3,6 @@ package com.ctrip.apollo.adminservice.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
...@@ -19,9 +18,8 @@ public class ItemSetController { ...@@ -19,9 +18,8 @@ public class ItemSetController {
private ItemSetService itemSetService; private ItemSetService itemSetService;
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/itemset", method = RequestMethod.POST) @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/itemset", method = RequestMethod.POST)
public ResponseEntity<Void> create(@PathVariable String appId, @PathVariable String clusterName, public ResponseEntity<Void> create(@RequestBody ItemChangeSets changeSet) {
@PathVariable String namespaceName, @RequestBody ItemChangeSets changeSet) { itemSetService.updateSet(changeSet);
itemSetService.updateSet(appId, clusterName, namespaceName, changeSet);
return ResponseEntity.status(HttpStatus.OK).build(); return ResponseEntity.status(HttpStatus.OK).build();
} }
} }
package com.ctrip.apollo.adminservice.controller;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.AdminServiceTestConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AdminServiceTestConfiguration.class)
@WebIntegrationTest(randomPort = true)
public abstract class AbstractControllerTest {
RestTemplate restTemplate = new TestRestTemplate();
@Value("${local.server.port}")
int port;
}
...@@ -2,36 +2,20 @@ package com.ctrip.apollo.adminservice.controller; ...@@ -2,36 +2,20 @@ package com.ctrip.apollo.adminservice.controller;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.AdminServiceTestConfiguration;
import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.repository.AppRepository; import com.ctrip.apollo.biz.repository.AppRepository;
import com.ctrip.apollo.biz.utils.BeanUtils; import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.AppDTO; import com.ctrip.apollo.core.dto.AppDTO;
@RunWith(SpringJUnit4ClassRunner.class) public class AppControllerTest extends AbstractControllerTest{
@SpringApplicationConfiguration(classes = AdminServiceTestConfiguration.class)
@WebIntegrationTest(randomPort = true)
public class AppControllerTest {
RestTemplate restTemplate = new TestRestTemplate();
@Autowired @Autowired
AppRepository appRepository; AppRepository appRepository;
@Value("${local.server.port}")
private int port;
private String getBaseAppUrl(){ private String getBaseAppUrl(){
return "http://localhost:"+port+"/apps/"; return "http://localhost:"+port+"/apps/";
} }
......
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.NamespaceDTO;
public class ItemSetControllerTest extends AbstractControllerTest {
@Autowired
ItemRepository itemRepository;
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/test-itemset-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.setModifyBy("created");
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 =
restTemplate.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());
}
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/test-itemset-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.setModifyBy("created");
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 = restTemplate.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 udpateChangeSet = new ItemChangeSets();
udpateChangeSet.setModifyBy("updated");
int updatedSize = 2;
for (int i = 0; i < updatedSize; i++) {
items[i].setValue("updated_value_" + i);
udpateChangeSet.addUpdateItem(items[i]);
}
response = restTemplate.postForEntity(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
+ "/namespaces/" + namespace.getNamespaceName() + "/itemset",
udpateChangeSet, 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());
}
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/test-itemset-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.setModifyBy("created");
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 = restTemplate.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.setModifyBy("deleted");
int deletedSize = 1;
for (int i = 0; i < deletedSize; i++) {
items[i].setValue("deleted_value_" + i);
deleteChangeSet.addDeleteItem(items[i]);
}
response = restTemplate.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());
}
}
spring.datasource.url = jdbc:h2:mem:~/fxapolloconfigdb;mode=mysql;DB_CLOSE_ON_EXIT=FALSE spring.datasource.url = jdbc:h2:mem:~/apolloconfigdb;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.properties.hibernate.show_sql=true
spring.h2.console.enabled = true spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true spring.h2.console.settings.web-allow-others=true
DELETE FROM Item;
DELETE FROM Namespace;
DELETE FROM AppNamespace;
DELETE FROM Cluster;
DELETE FROM App;
INSERT INTO App (AppId, Name, OwnerName, OwnerEmail) VALUES ('someAppId','someAppName','someOwnerName','someOwnerName@ctrip.com');
INSERT INTO Cluster (AppId, Name) VALUES ('someAppId', 'default');
INSERT INTO AppNamespace (AppId, Name) VALUES ('someAppId', 'application');
INSERT INTO Namespace (AppId, ClusterName, NamespaceName) VALUES ('someAppId', 'default', 'application');
...@@ -20,8 +20,8 @@ public abstract class BaseEntity { ...@@ -20,8 +20,8 @@ public abstract class BaseEntity {
@GeneratedValue @GeneratedValue
private long id; private long id;
@Column(name = "IsDeleted") @Column(name = "IsDeleted", columnDefinition="Bit default '0'")
protected boolean isDeleted; protected boolean isDeleted = false;
@Column(name = "DataChange_CreatedBy") @Column(name = "DataChange_CreatedBy")
private String dataChangeCreatedBy; private String dataChangeCreatedBy;
......
...@@ -4,51 +4,43 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -4,51 +4,43 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Item; import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.ItemRepository; import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.biz.utils.BeanUtils; import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemChangeSets; import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import java.util.Date;
@Service @Service
public class ItemSetService { public class ItemSetService {
@Autowired @Autowired
private ItemRepository itemRepository; private ItemRepository itemRepository;
@Autowired
private NamespaceRepository namespaceRepository;
public void updateSet(String appId, String clusterName, String namespaceName, ItemChangeSets changeSet) {
Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName, namespaceName);
String modifyBy = changeSet.getModifyBy(); public void updateSet(ItemChangeSets changeSet) {
for (ItemDTO item : changeSet.getCreateItems()) { if (changeSet.getCreateItems() != null) {
Item entity = BeanUtils.transfrom(Item.class, item); for (ItemDTO item : changeSet.getCreateItems()) {
Item entity = BeanUtils.transfrom(Item.class, item);
entity.setNamespaceId(namespace.getId()); entity.setDataChangeCreatedBy(changeSet.getModifyBy());
entity.setDataChangeCreatedBy(modifyBy); itemRepository.save(entity);
entity.setDataChangeCreatedTime(new Date()); }
entity.setDataChangeLastModifiedBy(modifyBy);
itemRepository.save(entity);
} }
for (ItemDTO item : changeSet.getUpdateItems()) { if (changeSet.getUpdateItems() != null) {
Item entity = BeanUtils.transfrom(Item.class, item); for (ItemDTO item : changeSet.getUpdateItems()) {
Item managedItem = itemRepository.findOne(entity.getId()); Item entity = BeanUtils.transfrom(Item.class, item);
if (managedItem != null){ Item managedItem = itemRepository.findOne(entity.getId());
BeanUtils.copyEntityProperties(entity, managedItem, "id", "namespaceId", "key", "dataChangeCreatedBy", "dataChangeCreatedTime"); BeanUtils.copyEntityProperties(entity, managedItem);
managedItem.setDataChangeLastModifiedBy(modifyBy); managedItem.setDataChangeLastModifiedBy(changeSet.getModifyBy());
itemRepository.save(managedItem);
} }
itemRepository.save(managedItem);
} }
for (ItemDTO item : changeSet.getDeletedItems()) { if (changeSet.getDeleteItems() != null) {
Item entity = BeanUtils.transfrom(Item.class, item); for (ItemDTO item : changeSet.getDeleteItems()) {
entity.setDataChangeLastModifiedBy(modifyBy); Item entity = BeanUtils.transfrom(Item.class, item);
itemRepository.delete(entity.getId()); entity.setDataChangeLastModifiedBy(changeSet.getModifyBy());
itemRepository.save(entity);
itemRepository.delete(item.getId());
}
} }
} }
} }
...@@ -209,12 +209,22 @@ public class BeanUtils { ...@@ -209,12 +209,22 @@ public class BeanUtils {
} }
/** /**
* The copy will ignore <em>id</em> field
* *
* @param source * @param source
* @param target * @param target
*/ */
public static void copyEntityProperties(Object source, Object target, String... ignoreProperties) { public static void copyProperties(Object source, Object target, String... ignoreProperties) {
org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties); org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties);
} }
/**
* The copy will ignore <em>BaseEntity</em> field
*
* @param source
* @param target
*/
public static void copyEntityProperties(Object source, Object target) {
org.springframework.beans.BeanUtils.copyProperties(source, target, "id", "dataChangeCreatedBy",
"dataChangeCreatedTime", "dataChangeLastModifiedBy", "dataChangeLastModifiedTime");
}
} }
...@@ -8,7 +8,6 @@ import org.springframework.data.web.PageableHandlerMethodArgumentResolver; ...@@ -8,7 +8,6 @@ import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration @Configuration
......
spring.datasource.url = jdbc:h2:mem:~/fxapolloconfigdb;mode=mysql spring.datasource.url = jdbc:h2:mem:~/apolloconfigdb;mode=mysql
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.h2.console.enabled = true spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true spring.h2.console.settings.web-allow-others=true
hibernate.globally_quoted_identifiers=true spring.jpa.properties.hibernate.show_sql=true
hibernate.show_sql=true \ No newline at end of file
\ No newline at end of file
...@@ -11,9 +11,9 @@ public class ItemChangeSets { ...@@ -11,9 +11,9 @@ public class ItemChangeSets {
private String modifyBy; private String modifyBy;
private List<ItemDTO> createItems = new LinkedList<>(); private List<ItemDTO> createItems = new LinkedList<>();
private List<ItemDTO> updateItems = new LinkedList<>(); private List<ItemDTO> updateItems = new LinkedList<>();
private List<ItemDTO> deletedItems = new LinkedList<>(); private List<ItemDTO> deleteItems = new LinkedList<>();
public void addCreatedItem(ItemDTO item) { public void addCreateItem(ItemDTO item) {
createItems.add(item); createItems.add(item);
} }
...@@ -21,11 +21,10 @@ public class ItemChangeSets { ...@@ -21,11 +21,10 @@ public class ItemChangeSets {
updateItems.add(item); updateItems.add(item);
} }
public void addDeletedItem(ItemDTO item) { public void addDeleteItem(ItemDTO item) {
deletedItems.add(item); deleteItems.add(item);
} }
public List<ItemDTO> getCreateItems() { public List<ItemDTO> getCreateItems() {
return createItems; return createItems;
} }
...@@ -34,8 +33,8 @@ public class ItemChangeSets { ...@@ -34,8 +33,8 @@ public class ItemChangeSets {
return updateItems; return updateItems;
} }
public List<ItemDTO> getDeletedItems() { public List<ItemDTO> getDeleteItems() {
return deletedItems; return deleteItems;
} }
public void setCreateItems(List<ItemDTO> createItems) { public void setCreateItems(List<ItemDTO> createItems) {
...@@ -46,8 +45,8 @@ public class ItemChangeSets { ...@@ -46,8 +45,8 @@ public class ItemChangeSets {
this.updateItems = updateItems; this.updateItems = updateItems;
} }
public void setDeletedItems(List<ItemDTO> deletedItems) { public void setDeleteItems(List<ItemDTO> deleteItems) {
this.deletedItems = deletedItems; this.deleteItems = deleteItems;
} }
public String getModifyBy() { public String getModifyBy() {
......
...@@ -4,11 +4,10 @@ import com.ctrip.apollo.core.dto.ItemChangeSets; ...@@ -4,11 +4,10 @@ import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.utils.StringUtils; import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.util.BeanUtils; import com.ctrip.apollo.portal.util.BeanUtils;
import com.sun.tools.javac.util.Assert;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -43,7 +42,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -43,7 +42,7 @@ public class PropertyResolver implements ConfigTextResolver {
ItemChangeSets changeSets = new ItemChangeSets(); ItemChangeSets changeSets = new ItemChangeSets();
result.setChangeSets(changeSets); result.setChangeSets(changeSets);
Map<Integer, String> newLineNumMapItem = new HashedMap();//use for delete blank and comment item Map<Integer, String> newLineNumMapItem = new HashMap();//use for delete blank and comment item
int lineCounter = 1; int lineCounter = 1;
for (String newItem : newItems) { for (String newItem : newItems) {
newItem = newItem.trim(); newItem = newItem.trim();
...@@ -82,13 +81,13 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -82,13 +81,13 @@ public class PropertyResolver implements ConfigTextResolver {
String oldComment = oldItemByLine == null ? "" : oldItemByLine.getComment(); String oldComment = oldItemByLine == null ? "" : oldItemByLine.getComment();
//create comment. implement update comment by delete old comment and create new comment //create comment. implement update comment by delete old comment and create new comment
if (!(isCommentItem(oldItemByLine) && newItem.equals(oldComment))) { if (!(isCommentItem(oldItemByLine) && newItem.equals(oldComment))) {
changeSets.addCreatedItem(buildCommentItem(0l, newItem, lineCounter)); changeSets.addCreateItem(buildCommentItem(0l, newItem, lineCounter));
} }
} }
private void handleBlankLine(ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) { private void handleBlankLine(ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) {
if (!isBlankItem(oldItem)) { if (!isBlankItem(oldItem)) {
changeSets.addCreatedItem(buildBlankItem(0l, lineCounter)); changeSets.addCreateItem(buildBlankItem(0l, lineCounter));
} }
} }
...@@ -110,7 +109,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -110,7 +109,7 @@ public class PropertyResolver implements ConfigTextResolver {
ItemDTO oldItem = keyMapOldItem.get(newKey); ItemDTO oldItem = keyMapOldItem.get(newKey);
if (oldItem == null) {//new item if (oldItem == null) {//new item
changeSets.addCreatedItem(buildNormalItem(0l, newKey, newValue, "", lineCounter)); changeSets.addCreateItem(buildNormalItem(0l, newKey, newValue, "", lineCounter));
} else if (!newValue.equals(oldItem.getValue())){//update item } else if (!newValue.equals(oldItem.getValue())){//update item
changeSets.addUpdateItem( changeSets.addUpdateItem(
buildNormalItem(oldItem.getId(), newKey, newValue, oldItem.getComment(), buildNormalItem(oldItem.getId(), newKey, newValue, oldItem.getComment(),
...@@ -139,7 +138,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -139,7 +138,7 @@ public class PropertyResolver implements ConfigTextResolver {
private void deleteNormalKVItem(Map<String, ItemDTO> baseKeyMapItem, ItemChangeSets changeSets) { private void deleteNormalKVItem(Map<String, ItemDTO> baseKeyMapItem, ItemChangeSets changeSets) {
//surplus item is to be deleted //surplus item is to be deleted
for (Map.Entry<String, ItemDTO> entry : baseKeyMapItem.entrySet()) { for (Map.Entry<String, ItemDTO> entry : baseKeyMapItem.entrySet()) {
changeSets.addDeletedItem(entry.getValue()); changeSets.addDeleteItem(entry.getValue());
} }
} }
...@@ -156,7 +155,7 @@ public class PropertyResolver implements ConfigTextResolver { ...@@ -156,7 +155,7 @@ public class PropertyResolver implements ConfigTextResolver {
//2.old is comment by now is not exist or modified //2.old is comment by now is not exist or modified
if ((isBlankItem(oldItem) && !isBlankItem(newItem)) if ((isBlankItem(oldItem) && !isBlankItem(newItem))
|| isCommentItem(oldItem) && (newItem == null || !newItem.equals(oldItem))) { || isCommentItem(oldItem) && (newItem == null || !newItem.equals(oldItem))) {
changeSets.addDeletedItem(oldItem); changeSets.addDeleteItem(oldItem);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment