Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
385 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ervice/src/test/java/com/ctrip/apollo/adminservice/controller/AbstractControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
...service/src/test/java/com/ctrip/apollo/adminservice/controller/ItemSetControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
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.properties.hibernate.show_sql=true | ||
spring.h2.console.enabled = true | ||
spring.h2.console.settings.web-allow-others=true | ||
|
6 changes: 6 additions & 0 deletions
6
apollo-adminservice/src/test/resources/controller/test-itemset-cleanup.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DELETE FROM Item; | ||
DELETE FROM Namespace; | ||
DELETE FROM AppNamespace; | ||
DELETE FROM Cluster; | ||
DELETE FROM App; | ||
|
7 changes: 7 additions & 0 deletions
7
apollo-adminservice/src/test/resources/controller/test-itemset.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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'); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.