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());
}
}
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());
}
}
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));
}
}
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));
}
}
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);
}
Aggregations