ItemOpenApiService extends AbstractOpenApiService { public OpenItemDTO createItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) { if (Strings.isNullOrEmpty(clusterName)) { clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT; } if (Strings.isNullOrEmpty(namespaceName)) { namespaceName = ConfigConsts.NAMESPACE_APPLICATION; } checkNotEmpty(appId, "App id"); checkNotEmpty(env, "Env"); checkNotEmpty(itemDTO.getKey(), "Item key"); checkNotEmpty(itemDTO.getDataChangeCreatedBy(), "Item created by"); String path = String.format("envs/%s/apps/%s/clusters/%s/namespaces/%s/items", escapePath(env), escapePath(appId), escapePath(clusterName), escapePath(namespaceName)); try (CloseableHttpResponse response = post(path, itemDTO)) { return gson.fromJson(EntityUtils.toString(response.getEntity()), OpenItemDTO.class); } catch (Throwable ex) { throw new RuntimeException(String .format("Create item: %s for appId: %s, cluster: %s, namespace: %s in env: %s failed", itemDTO.getKey(), appId, clusterName, namespaceName, env), ex); } } ItemOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson); OpenItemDTO getItem(String appId, String env, String clusterName, String namespaceName, String key); OpenItemDTO createItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO); void updateItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO); void createOrUpdateItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO); void removeItem(String appId, String env, String clusterName, String namespaceName, String key, String operator);  }
@Test public void testCreateItem() throws Exception { String someKey = "someKey"; String someValue = "someValue"; String someCreatedBy = "someCreatedBy"; OpenItemDTO itemDTO = new OpenItemDTO(); itemDTO.setKey(someKey); itemDTO.setValue(someValue); itemDTO.setDataChangeCreatedBy(someCreatedBy); final ArgumentCaptor<HttpPost> request = ArgumentCaptor.forClass(HttpPost.class); itemOpenApiService.createItem(someAppId, someEnv, someCluster, someNamespace, itemDTO); verify(httpClient, times(1)).execute(request.capture()); HttpPost post = request.getValue(); assertEquals(String .format("%s/envs/%s/apps/%s/clusters/%s/namespaces/%s/items", someBaseUrl, someEnv, someAppId, someCluster, someNamespace), post.getURI().toString()); StringEntity entity = (StringEntity) post.getEntity(); assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue()); assertEquals(gson.toJson(itemDTO), EntityUtils.toString(entity)); }
@Test(expected = RuntimeException.class) public void testCreateItemWithError() throws Exception { String someKey = "someKey"; String someValue = "someValue"; String someCreatedBy = "someCreatedBy"; OpenItemDTO itemDTO = new OpenItemDTO(); itemDTO.setKey(someKey); itemDTO.setValue(someValue); itemDTO.setDataChangeCreatedBy(someCreatedBy); when(statusLine.getStatusCode()).thenReturn(400); itemOpenApiService.createItem(someAppId, someEnv, someCluster, someNamespace, itemDTO); }