NamespaceOpenApiService extends AbstractOpenApiService { public OpenAppNamespaceDTO createAppNamespace(OpenAppNamespaceDTO appNamespaceDTO) { checkNotEmpty(appNamespaceDTO.getAppId(), "App id"); checkNotEmpty(appNamespaceDTO.getName(), "Name"); checkNotEmpty(appNamespaceDTO.getDataChangeCreatedBy(), "Created by"); if (Strings.isNullOrEmpty(appNamespaceDTO.getFormat())) { appNamespaceDTO.setFormat(ConfigFileFormat.Properties.getValue()); } String path = String.format("apps/%s/appnamespaces", escapePath(appNamespaceDTO.getAppId())); try (CloseableHttpResponse response = post(path, appNamespaceDTO)) { return gson.fromJson(EntityUtils.toString(response.getEntity()), OpenAppNamespaceDTO.class); } catch (Throwable ex) { throw new RuntimeException(String .format("Create app namespace: %s for appId: %s, format: %s failed", appNamespaceDTO.getName(), appNamespaceDTO.getAppId(), appNamespaceDTO.getFormat()), ex); } } NamespaceOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson); OpenNamespaceDTO getNamespace(String appId, String env, String clusterName, String namespaceName); List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName); OpenAppNamespaceDTO createAppNamespace(OpenAppNamespaceDTO appNamespaceDTO); OpenNamespaceLockDTO getNamespaceLock(String appId, String env, String clusterName, String namespaceName);  }
@Test public void testCreateAppNamespace() throws Exception { String someName = "someName"; String someCreatedBy = "someCreatedBy"; OpenAppNamespaceDTO appNamespaceDTO = new OpenAppNamespaceDTO(); appNamespaceDTO.setAppId(someAppId); appNamespaceDTO.setName(someName); appNamespaceDTO.setDataChangeCreatedBy(someCreatedBy); final ArgumentCaptor<HttpPost> request = ArgumentCaptor.forClass(HttpPost.class); namespaceOpenApiService.createAppNamespace(appNamespaceDTO); verify(httpClient, times(1)).execute(request.capture()); HttpPost post = request.getValue(); assertEquals(String.format("%s/apps/%s/appnamespaces", someBaseUrl, someAppId), post.getURI().toString()); }
@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); }