Skip to content

Commit

Permalink
feat(openapi): create app
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilople authored and nobodyiam committed Aug 12, 2023
1 parent 265fe49 commit 7b41ecd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@

import com.ctrip.framework.apollo.openapi.dto.OpenAppDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenEnvClusterDTO;
import java.util.Collections;
import java.util.List;

/**
* @author wxq
*/
public interface AppOpenApiService {

default void createApp(OpenAppDTO openAppDTO) {
throw new UnsupportedOperationException();

Check warning on line 30 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/api/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/api/AppOpenApiService.java#L30

Added line #L30 was not covered by tests
}

default void createApp(String env, OpenAppDTO openAppDTO) {
throw new UnsupportedOperationException();

Check warning on line 34 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/api/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/api/AppOpenApiService.java#L34

Added line #L34 was not covered by tests
}

List<OpenEnvClusterDTO> getEnvClusterInfo(String appId);

List<OpenAppDTO> getAllApps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ private ApolloOpenApiClient(String portalUrl, String token, RequestConfig reques
releaseService = new ReleaseOpenApiService(client, baseUrl, GSON);
}

public void createApp(OpenAppDTO openAppDTO) {
appService.createApp(openAppDTO);
}

Check warning on line 70 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/ApolloOpenApiClient.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/ApolloOpenApiClient.java#L69-L70

Added lines #L69 - L70 were not covered by tests

public void createApp(String env, OpenAppDTO openAppDTO) {
appService.createApp(env, openAppDTO);
}

Check warning on line 74 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/ApolloOpenApiClient.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/ApolloOpenApiClient.java#L73-L74

Added lines #L73 - L74 were not covered by tests

/**
* Get the environment and cluster information
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,44 @@ public AppOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson)
super(client, baseUrl, gson);
}

@Override
public void createApp(OpenAppDTO openAppDTO) {
checkNotEmpty(openAppDTO.getAppId(), "App id");
checkNotEmpty(openAppDTO.getName(), "App name");
OpenApiPathBuilder pathBuilder = OpenApiPathBuilder.newBuilder()
.customResource("apps/create");

Check warning on line 47 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java#L44-L47

Added lines #L44 - L47 were not covered by tests

try (CloseableHttpResponse response = post(pathBuilder, openAppDTO)) {
gson.fromJson(EntityUtils.toString(response.getEntity()), void.class);
} catch (Throwable ex) {
throw new RuntimeException(
String.format("Create app: %s for appId: %s failed", openAppDTO.getName(),
openAppDTO.getAppId()), ex);
}
}

Check warning on line 56 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java#L49-L56

Added lines #L49 - L56 were not covered by tests

@Override
public void createApp(String env, OpenAppDTO openAppDTO) {
checkNotEmpty(env, "Env");
checkNotEmpty(openAppDTO.getAppId(), "App id");
checkNotEmpty(openAppDTO.getName(), "App name");
OpenApiPathBuilder pathBuilder = OpenApiPathBuilder.newBuilder()
.envPathVal(env)
.customResource("apps/create")

Check warning on line 65 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java#L60-L65

Added lines #L60 - L65 were not covered by tests
;

try (CloseableHttpResponse response = post(pathBuilder, openAppDTO)) {
gson.fromJson(EntityUtils.toString(response.getEntity()), void.class);
} catch (Throwable ex) {
throw new RuntimeException(String.format(

Check warning on line 71 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java#L68-L71

Added lines #L68 - L71 were not covered by tests
"Create app: %s for appId: %s in env: %s failed",
openAppDTO.getName(),
openAppDTO.getAppId(),

Check warning on line 74 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java#L73-L74

Added lines #L73 - L74 were not covered by tests
env
), ex);
}
}

Check warning on line 78 in apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java

View check run for this annotation

Codecov / codecov/patch

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AppOpenApiService.java#L77-L78

Added lines #L77 - L78 were not covered by tests

@Override
public List<OpenEnvClusterDTO> getEnvClusterInfo(String appId) {
checkNotEmpty(appId, "App id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
*/
package com.ctrip.framework.apollo.openapi.client;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.ctrip.framework.apollo.openapi.dto.OpenAppDTO;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class ApolloOpenApiClientTest {

Expand All @@ -32,11 +38,67 @@ public void testCreate() {
assertEquals(someToken, client.getToken());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testCreateWithInvalidUrl() {
String someInvalidUrl = "someInvalidUrl";
String someToken = "someToken";

ApolloOpenApiClient.newBuilder().withPortalUrl(someInvalidUrl).withToken(someToken).build();
assertThrows(
IllegalArgumentException.class,
() -> ApolloOpenApiClient.newBuilder().withPortalUrl(someInvalidUrl).withToken(someToken).build()
);
}

@Test
@Disabled("only for integration test")
public void testCreateApp() {
String someUrl = "http://localhost:8070";
// String someToken = "0627b87948c30517157e8b2a9565e473b5a97323a50128f584838ed10559d3fd";
String someToken = "9d0a241e9cb2300f302a875b1195340b2b6f56373cf5ca5d006a3f4e1a46b3ef";

ApolloOpenApiClient client = ApolloOpenApiClient.newBuilder()
.withPortalUrl(someUrl)
.withToken(someToken)
.withReadTimeout(200 * 1000)
.withConnectTimeout(200 * 1000)
.build();

final String appId = "openapi-create-app";
OpenAppDTO openAppDTO = new OpenAppDTO();
openAppDTO.setName("openapi create app 测试名字");
openAppDTO.setAppId(appId);
openAppDTO.setOwnerName("user-test-xxx1");
openAppDTO.setOwnerEmail("[email protected]");
openAppDTO.setOrgId("orgId1");
openAppDTO.setOrgName("orgName1");
// client.createApp(openAppDTO);

List<OpenAppDTO> list = client.getAppsByIds(Collections.singletonList(appId));
}

@Test
@Disabled("only for integration test")
public void testCreateAppWithEnv() {
String someUrl = "http://localhost:8070";
String someToken = "9d0a241e9cb2300f302a875b1195340b2b6f56373cf5ca5d006a3f4e1a46b3ef";

ApolloOpenApiClient client = ApolloOpenApiClient.newBuilder()
.withPortalUrl(someUrl)
.withToken(someToken)
.withReadTimeout(200 * 1000)
.withConnectTimeout(200 * 1000)
.build();

final String appId = "openapi-create-app-in-dev";
OpenAppDTO openAppDTO = new OpenAppDTO();
openAppDTO.setName("openapi create app 测试名字");
openAppDTO.setAppId(appId);
openAppDTO.setOwnerName("user-test-xxx1");
openAppDTO.setOwnerEmail("[email protected]");
openAppDTO.setOrgId("orgId1");
openAppDTO.setOrgName("orgName1");
client.createApp("dev", openAppDTO);

List<OpenAppDTO> list = client.getAppsByIds(Collections.singletonList(appId));
}
}

0 comments on commit 7b41ecd

Please sign in to comment.