Search in sources :

Example 1 with OpenAppNamespaceDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO in project apollo by ctripcorp.

the class NamespaceControllerWithAuthorizationTest method testCreateAppNamespace.

/**
 * test method {@link NamespaceController#createAppNamespace(String, OpenAppNamespaceDTO)}.
 */
@Ignore("need admin server for this case")
@Test
@Sql(scripts = "/sql/openapi/NamespaceControllerTest.testCreateAppNamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateAppNamespace() {
    final String appId = "consumer-test-app-id-0";
    final String namespaceName = "create-app-namespace-success";
    // query
    {
        ResponseEntity<String> responseEntity = restTemplate.exchange(url("/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces"), HttpMethod.GET, new HttpEntity<>(HTTP_HEADERS_WITH_TOKEN), String.class, "DEV", appId, "default");
        String responseEntityBody = responseEntity.getBody();
        assertNotNull(responseEntityBody);
        assertFalse(responseEntityBody.contains(namespaceName));
    }
    // create it
    final OpenAppNamespaceDTO dto = new OpenAppNamespaceDTO();
    dto.setAppId(appId);
    dto.setName(namespaceName);
    dto.setFormat(ConfigFileFormat.Properties.getValue());
    dto.setDataChangeCreatedBy("apollo");
    restTemplate.exchange(this.url("/openapi/v1/apps/{appId}/appnamespaces"), HttpMethod.POST, new HttpEntity<>(dto, HTTP_HEADERS_WITH_TOKEN), OpenAppNamespaceDTO.class, dto.getAppId());
    // query again to confirm
    {
        ResponseEntity<OpenNamespaceDTO[]> responseEntity = restTemplate.getForEntity("/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces", OpenNamespaceDTO[].class, "DEV", appId, "default");
        OpenNamespaceDTO[] openNamespaceDTOS = responseEntity.getBody();
        assertNotNull(openNamespaceDTOS);
        assertEquals(1, Arrays.stream(openNamespaceDTOS).filter(openNamespaceDTO -> namespaceName.equals(openNamespaceDTO.getNamespaceName())).count());
    }
}
Also used : OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) Arrays(java.util.Arrays) Assert.assertNotNull(org.junit.Assert.assertNotNull) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Assert.assertTrue(org.junit.Assert.assertTrue) ConfigFileFormat(com.ctrip.framework.apollo.core.enums.ConfigFileFormat) Test(org.junit.Test) UUID(java.util.UUID) Sql(org.springframework.test.context.jdbc.Sql) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) OpenNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO) Assert.fail(org.junit.Assert.fail) ResponseEntity(org.springframework.http.ResponseEntity) InputValidator(com.ctrip.framework.apollo.common.utils.InputValidator) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) OpenNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO) Ignore(org.junit.Ignore) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 2 with OpenAppNamespaceDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO in project apollo by ctripcorp.

the class NamespaceControllerWithAuthorizationTest method testCreateAppNamespaceUnauthorized.

/**
 * test method {@link NamespaceController#createAppNamespace(String, OpenAppNamespaceDTO)}.
 */
@Test
@Sql(scripts = "/sql/openapi/NamespaceControllerTest.testCreateAppNamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateAppNamespaceUnauthorized() {
    OpenAppNamespaceDTO dto = new OpenAppNamespaceDTO();
    dto.setAppId("consumer-test-app-id-0");
    dto.setName("namespace-0");
    dto.setFormat(ConfigFileFormat.Properties.getValue());
    dto.setDataChangeCreatedBy("apollo");
    try {
        restTemplate.postForEntity(url("/openapi/v1/apps/{appId}/appnamespaces"), dto, OpenAppNamespaceDTO.class, dto.getAppId());
        Assert.fail("should throw");
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 3 with OpenAppNamespaceDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO in project apollo by ctripcorp.

the class NamespaceControllerWithAuthorizationTest method testCreateAppNamespaceInvalidNamespaceName.

/**
 * test method {@link NamespaceController#createAppNamespace(String, OpenAppNamespaceDTO)}. Just
 * for check Authorization is ok.
 */
@Test
@Sql(scripts = "/sql/openapi/NamespaceControllerTest.testCreateAppNamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateAppNamespaceInvalidNamespaceName() {
    OpenAppNamespaceDTO dto = new OpenAppNamespaceDTO();
    dto.setAppId("consumer-test-app-id-0");
    dto.setName("invalid name");
    dto.setFormat(ConfigFileFormat.Properties.getValue());
    dto.setDataChangeCreatedBy("apollo");
    try {
        restTemplate.exchange(this.url("/openapi/v1/apps/{appId}/appnamespaces"), HttpMethod.POST, new HttpEntity<>(dto, HTTP_HEADERS_WITH_TOKEN), OpenAppNamespaceDTO.class, dto.getAppId());
        Assert.fail("should throw");
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
        String result = e.getResponseBodyAsString();
        assertTrue(result.contains(InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
        assertTrue(result.contains(InputValidator.INVALID_NAMESPACE_NAMESPACE_MESSAGE));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 4 with OpenAppNamespaceDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO in project apollo by ctripcorp.

the class NamespaceControllerTest method shouldFailWhenAppNamespaceNameIsInvalid.

@Test
public void shouldFailWhenAppNamespaceNameIsInvalid() {
    Assert.assertTrue(consumerPermissionValidator.hasCreateNamespacePermission(null, null));
    OpenAppNamespaceDTO dto = new OpenAppNamespaceDTO();
    dto.setAppId("appId");
    dto.setName("invalid name");
    dto.setFormat(ConfigFileFormat.Properties.getValue());
    dto.setDataChangeCreatedBy("apollo");
    try {
        restTemplate.postForEntity(url("/openapi/v1/apps/{appId}/appnamespaces"), dto, OpenAppNamespaceDTO.class, dto.getAppId());
        Assert.fail("should throw");
    } catch (HttpClientErrorException e) {
        String result = e.getResponseBodyAsString();
        Assert.assertThat(result, containsString(InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
        Assert.assertThat(result, containsString(InputValidator.INVALID_NAMESPACE_NAMESPACE_MESSAGE));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 5 with OpenAppNamespaceDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO in project apollo by ctripcorp.

the class NamespaceOpenApiServiceTest method testCreateAppNamespaceWithError.

@Test(expected = RuntimeException.class)
public void testCreateAppNamespaceWithError() throws Exception {
    String someName = "someName";
    String someCreatedBy = "someCreatedBy";
    OpenAppNamespaceDTO appNamespaceDTO = new OpenAppNamespaceDTO();
    appNamespaceDTO.setAppId(someAppId);
    appNamespaceDTO.setName(someName);
    appNamespaceDTO.setDataChangeCreatedBy(someCreatedBy);
    when(statusLine.getStatusCode()).thenReturn(400);
    namespaceOpenApiService.createAppNamespace(appNamespaceDTO);
}
Also used : OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) Test(org.junit.Test)

Aggregations

OpenAppNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO)7 Test (org.junit.Test)7 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)5 Sql (org.springframework.test.context.jdbc.Sql)4 InputValidator (com.ctrip.framework.apollo.common.utils.InputValidator)1 ConfigFileFormat (com.ctrip.framework.apollo.core.enums.ConfigFileFormat)1 OpenNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO)1 Arrays (java.util.Arrays)1 UUID (java.util.UUID)1 HttpPost (org.apache.http.client.methods.HttpPost)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1 Ignore (org.junit.Ignore)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1