Search in sources :

Example 6 with ClusterDTO

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

the class ConfigsExportServiceTest method testNamespaceExportImport.

@Test
public void testNamespaceExportImport() throws FileNotFoundException {
    String filePath = "/tmp/apollo.zip";
    File file = new File(filePath);
    if (file.exists()) {
        file.delete();
    }
    // export config
    UserInfo userInfo = genUser();
    when(userInfoHolder.getUser()).thenReturn(userInfo);
    Env env = Env.DEV;
    String appId1 = "app1";
    String appId2 = "app2";
    App app1 = genApp(appId1, appId1, "org1", "org2");
    App app2 = genApp(appId2, appId2, "org1", "org2");
    List<App> exportApps = Lists.newArrayList(app1, app2);
    String appNamespaceName1 = "ns1";
    String appNamespaceName2 = "ns2";
    AppNamespace app1Namespace1 = genAppNamespace(appId1, appNamespaceName1, false);
    AppNamespace app1Namespace2 = genAppNamespace(appId1, appNamespaceName2, true);
    AppNamespace app2Namespace1 = genAppNamespace(appId2, appNamespaceName1, false);
    List<AppNamespace> appNamespaces = Lists.newArrayList(app1Namespace1, app1Namespace2, app2Namespace1);
    String clusterName1 = "c1";
    String clusterName2 = "c2";
    ClusterDTO app1Cluster1 = genCluster(clusterName1, appId1);
    ClusterDTO app1Cluster2 = genCluster(clusterName2, appId1);
    ClusterDTO app2Cluster1 = genCluster(clusterName1, appId2);
    ClusterDTO app2Cluster2 = genCluster(clusterName2, appId2);
    List<ClusterDTO> app1Clusters = Lists.newArrayList(app1Cluster1, app1Cluster2);
    List<ClusterDTO> app2Clusters = Lists.newArrayList(app2Cluster1, app2Cluster2);
    ItemBO item1 = genItem("k1", "v1");
    ItemBO item2 = genItem("k2", "v2");
    List<ItemBO> items = Lists.newArrayList(item1, item2);
    String namespaceName1 = "namespace1";
    String namespaceName2 = "namespace2";
    NamespaceBO app1Cluster1Namespace1 = genNamespace(app1, app1Cluster1, items, namespaceName1);
    NamespaceBO app1Cluster1Namespace2 = genNamespace(app1, app1Cluster1, items, namespaceName2);
    List<NamespaceBO> app1Cluster1Namespace = Lists.newArrayList(app1Cluster1Namespace1, app1Cluster1Namespace2);
    NamespaceBO app1Cluster2Namespace1 = genNamespace(app1, app1Cluster2, items, namespaceName1);
    List<NamespaceBO> app1Cluster2Namespace = Lists.newArrayList(app1Cluster2Namespace1);
    NamespaceBO app2Cluster1Namespace1 = genNamespace(app2, app1Cluster1, items, namespaceName1);
    List<NamespaceBO> app2Cluster1Namespace = Lists.newArrayList(app2Cluster1Namespace1);
    NamespaceBO app2Cluster2Namespace1 = genNamespace(app2, app1Cluster2, items, namespaceName1);
    NamespaceBO app2Cluster2Namespace2 = genNamespace(app2, app1Cluster2, items, namespaceName2);
    List<NamespaceBO> app2Cluster2Namespace = Lists.newArrayList(app2Cluster2Namespace1, app2Cluster2Namespace2);
    when(appService.findAll()).thenReturn(exportApps);
    when(appNamespaceService.findAll()).thenReturn(appNamespaces);
    when(permissionValidator.isAppAdmin(any())).thenReturn(true);
    when(clusterService.findClusters(env, appId1)).thenReturn(app1Clusters);
    when(clusterService.findClusters(env, appId2)).thenReturn(app2Clusters);
    when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName1)).thenReturn(app1Cluster1Namespace);
    when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2)).thenReturn(app1Cluster2Namespace);
    when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1)).thenReturn(app2Cluster1Namespace);
    when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2)).thenReturn(app2Cluster2Namespace);
    FileOutputStream fileOutputStream = new FileOutputStream("/tmp/apollo.zip");
    configsExportService.exportData(fileOutputStream, Lists.newArrayList(Env.DEV));
    // import config
    when(appNamespaceService.findByAppIdAndName(any(), any())).thenReturn(null);
    when(appNamespaceService.importAppNamespaceInLocal(any())).thenReturn(app1Namespace1);
    when(appService.load(any())).thenReturn(null);
    when(appService.load(any(), any())).thenThrow(new RuntimeException());
    when(clusterService.loadCluster(any(), any(), any())).thenThrow(new RuntimeException());
    when(namespaceService.loadNamespaceBaseInfo(any(), any(), any(), any())).thenThrow(new RuntimeException());
    when(namespaceService.createNamespace(any(), any())).thenReturn(genNamespaceDTO(1));
    when(itemService.findItems(any(), any(), any(), any())).thenReturn(Lists.newArrayList());
    HttpStatusCodeException itemNotFoundException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
    when(itemService.loadItem(any(), any(), any(), any(), anyString())).thenThrow(itemNotFoundException);
    FileInputStream fileInputStream = new FileInputStream("/tmp/apollo.zip");
    ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
    try {
        configsImportService.importDataFromZipFile(Lists.newArrayList(Env.DEV), zipInputStream, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    verify(appNamespaceService, times(3)).importAppNamespaceInLocal(any());
    verify(applicationEventPublisher, times(3)).publishEvent(any());
    verify(appService, times(2)).createAppInRemote(any(), any());
    verify(clusterService, times(4)).createCluster(any(), any());
    verify(namespaceService, times(6)).createNamespace(any(), any());
    verify(roleInitializationService, times(6)).initNamespaceRoles(any(), any(), anyString());
    verify(roleInitializationService, times(6)).initNamespaceEnvRoles(any(), any(), anyString());
    verify(itemService, times(12)).createItem(any(), any(), any(), any(), any());
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) ItemBO(com.ctrip.framework.apollo.portal.entity.bo.ItemBO) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Env(com.ctrip.framework.apollo.portal.environment.Env) FileInputStream(java.io.FileInputStream) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) FileNotFoundException(java.io.FileNotFoundException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ZipInputStream(java.util.zip.ZipInputStream) FileOutputStream(java.io.FileOutputStream) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) File(java.io.File) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 7 with ClusterDTO

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

the class ClusterControllerTest method shouldFailWhenRequestBodyInvalid.

@Test
public void shouldFailWhenRequestBodyInvalid() {
    ClusterDTO cluster = new ClusterDTO();
    cluster.setAppId("valid");
    cluster.setName("notBlank");
    ResponseEntity<ClusterDTO> response = restTemplate.postForEntity(baseUrl() + "/apps/{appId}/clusters", cluster, ClusterDTO.class, cluster.getAppId());
    ClusterDTO createdCluster = response.getBody();
    Assert.assertNotNull(createdCluster);
    Assert.assertEquals(cluster.getAppId(), createdCluster.getAppId());
    Assert.assertEquals(cluster.getName(), createdCluster.getName());
    cluster.setName("invalid app name");
    try {
        restTemplate.postForEntity(baseUrl() + "/apps/{appId}/clusters", cluster, ClusterDTO.class, cluster.getAppId());
        Assert.fail("Should throw");
    } catch (HttpClientErrorException e) {
        Assert.assertThat(new String(e.getResponseBodyAsByteArray()), containsString(InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with ClusterDTO

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

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

Example 10 with ClusterDTO

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

ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)12 Test (org.junit.Test)6 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)4 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)4 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)4 Sql (org.springframework.test.context.jdbc.Sql)4 Item (com.ctrip.framework.apollo.biz.entity.Item)3 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)3 RestTemplate (org.springframework.web.client.RestTemplate)3 OpenEnvClusterDTO (com.ctrip.framework.apollo.openapi.dto.OpenEnvClusterDTO)2 Env (com.ctrip.framework.apollo.portal.environment.Env)2 LinkedList (java.util.LinkedList)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)2 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)1 App (com.ctrip.framework.apollo.common.entity.App)1 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)1 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)1 ServiceException (com.ctrip.framework.apollo.common.exception.ServiceException)1