ClusterOpenApiService extends AbstractOpenApiService { public OpenClusterDTO createCluster(String env, OpenClusterDTO openClusterDTO) { checkNotEmpty(openClusterDTO.getAppId(), "App id"); checkNotEmpty(env, "Env"); checkNotEmpty(openClusterDTO.getName(), "Cluster name"); checkNotEmpty(openClusterDTO.getDataChangeCreatedBy(), "Created by"); String path = String.format("envs/%s/apps/%s/clusters", escapePath(env), escapePath(openClusterDTO.getAppId())); try (CloseableHttpResponse response = post(path, openClusterDTO)) { return gson.fromJson(EntityUtils.toString(response.getEntity()), OpenClusterDTO.class); } catch (Throwable ex) { throw new RuntimeException(String .format("Create cluster: %s for appId: %s in env: %s failed", openClusterDTO.getName(), openClusterDTO.getAppId(), env), ex); } } ClusterOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson); OpenClusterDTO getCluster(String appId, String env, String clusterName); OpenClusterDTO createCluster(String env, OpenClusterDTO openClusterDTO);  }
@Test public void testCreateCluster() throws Exception { String someCluster = "someCluster"; String someCreatedBy = "someCreatedBy"; OpenClusterDTO clusterDTO = new OpenClusterDTO(); clusterDTO.setAppId(someAppId); clusterDTO.setName(someCluster); clusterDTO.setDataChangeCreatedBy(someCreatedBy); final ArgumentCaptor<HttpPost> request = ArgumentCaptor.forClass(HttpPost.class); clusterOpenApiService.createCluster(someEnv, clusterDTO); verify(httpClient, times(1)).execute(request.capture()); HttpPost post = request.getValue(); assertEquals(String .format("%s/envs/%s/apps/%s/clusters", someBaseUrl, someEnv, someAppId), post.getURI().toString()); StringEntity entity = (StringEntity) post.getEntity(); assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue()); assertEquals(gson.toJson(clusterDTO), EntityUtils.toString(entity)); }
@Test(expected = RuntimeException.class) public void testCreateClusterWithError() throws Exception { String someCluster = "someCluster"; String someCreatedBy = "someCreatedBy"; OpenClusterDTO clusterDTO = new OpenClusterDTO(); clusterDTO.setAppId(someAppId); clusterDTO.setName(someCluster); clusterDTO.setDataChangeCreatedBy(someCreatedBy); when(statusLine.getStatusCode()).thenReturn(400); clusterOpenApiService.createCluster(someEnv, clusterDTO); }