Skip to content

Commit

Permalink
refactor: use composite instead of extend OpenAppDTO
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilople committed Aug 29, 2023
1 parent af53e3b commit 9558c84
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ private void checkHttpResponseStatus(HttpResponse response) {
throw new ApolloOpenApiException(status.getStatusCode(), status.getReasonPhrase(), message);
}

protected void checkNotNull(Object value, String name) {
Preconditions.checkArgument(null != value, name + " should not be null");
}

protected void checkNotEmpty(String value, String name) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(value), name + " should not be null or empty");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ public AppOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson)

@Override
public void createApp(OpenCreateAppDTO req) {
checkNotEmpty(req.getAppId(), "App id");
checkNotEmpty(req.getName(), "App name");
OpenAppDTO app = req.getApp();
checkNotNull(app, "App");
checkNotEmpty(app.getAppId(), "App id");
checkNotEmpty(app.getName(), "App name");
OpenApiPathBuilder pathBuilder = OpenApiPathBuilder.newBuilder()
.customResource("apps");

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Set;

public class OpenCreateAppDTO extends OpenAppDTO {
public class OpenCreateAppDTO {

/**
* The application owner has project administrator permission by default.
Expand All @@ -33,6 +33,8 @@ public class OpenCreateAppDTO extends OpenAppDTO {
*/
private boolean assignAppRoleToSelf;

private OpenAppDTO app;

public Set<String> getAdmins() {
return admins;
}
Expand All @@ -49,21 +51,20 @@ public void setAssignAppRoleToSelf(boolean assignAppRoleToSelf) {
this.assignAppRoleToSelf = assignAppRoleToSelf;
}

public OpenAppDTO getApp() {
return app;
}

public void setApp(OpenAppDTO app) {
this.app = app;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("OpenCreateAppDTO{");
sb.append("name='").append(getName()).append('\'');
sb.append(", appId='").append(getAppId()).append('\'');
sb.append(", orgId='").append(getOrgId()).append('\'');
sb.append(", orgName='").append(getOrgName()).append('\'');
sb.append(", ownerName='").append(getOwnerName()).append('\'');
sb.append(", ownerEmail='").append(getOwnerEmail()).append('\'');
sb.append("assignAppRoleToSelf='").append(assignAppRoleToSelf).append('\'');
sb.append(", admins='").append(admins).append('\'');
sb.append(", assignAppRoleToSelf='").append(assignAppRoleToSelf).append('\'');
sb.append(", dataChangeCreatedBy='").append(dataChangeCreatedBy).append('\'');
sb.append(", dataChangeLastModifiedBy='").append(dataChangeLastModifiedBy).append('\'');
sb.append(", dataChangeCreatedTime=").append(dataChangeCreatedTime);
sb.append(", dataChangeLastModifiedTime=").append(dataChangeLastModifiedTime);
sb.append(", app=").append(app);
sb.append('}');
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.ctrip.framework.apollo.openapi.dto.OpenCreateAppDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenReleaseDTO;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
Expand Down Expand Up @@ -68,13 +67,15 @@ ApolloOpenApiClient newClient() {

void createApp(String appId, String ownerName, boolean assignAppRoleToSelf, String ... admins) {
{
OpenAppDTO app = new OpenAppDTO();
app.setName("openapi create app 测试名字 " + appId);
app.setAppId(appId);
app.setOwnerName(ownerName);
app.setOwnerEmail(ownerName + "@apollo.apollo");
app.setOrgId("orgIdFromOpenapi");
app.setOrgName("orgNameFromOpenapi");
OpenCreateAppDTO req = new OpenCreateAppDTO();
req.setName("openapi create app 测试名字 " + appId);
req.setAppId(appId);
req.setOwnerName(ownerName);
req.setOwnerEmail(ownerName + "@apollo.apollo");
req.setOrgId("orgIdFromOpenapi");
req.setOrgName("orgNameFromOpenapi");
req.setApp(app);
req.setAdmins(new HashSet<>(Arrays.asList(admins)));
req.setAssignAppRoleToSelf(assignAppRoleToSelf);
log.info("create app {}, ownerName {} assignAppRoleToSelf {}", appId, ownerName, assignAppRoleToSelf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,37 @@ public void testGetEnvClusterInfoWithError() throws Exception {
appOpenApiService.getEnvClusterInfo(someAppId);
}

@Test(expected = RuntimeException.class)
public void testCreateAppNullApp() throws Exception {
OpenCreateAppDTO req = new OpenCreateAppDTO();
appOpenApiService.createApp(req);
}

@Test(expected = RuntimeException.class)
public void testCreateAppEmptyAppId() throws Exception {
OpenCreateAppDTO req = new OpenCreateAppDTO();
req.setApp(new OpenAppDTO());
appOpenApiService.createApp(req);
}

@Test(expected = RuntimeException.class)
public void testCreateAppEmptyAppName() throws Exception {
OpenAppDTO app = new OpenAppDTO();
app.setAppId("appId1");

OpenCreateAppDTO req = new OpenCreateAppDTO();
req.setAppId("appId1");
req.setApp(app);
appOpenApiService.createApp(req);
}

@Test(expected = RuntimeException.class)
public void testCreateAppFail() throws Exception {
OpenAppDTO app = new OpenAppDTO();
app.setAppId("appId1");
app.setName("name1");

OpenCreateAppDTO req = new OpenCreateAppDTO();
req.setAppId("appId1");
req.setName("name1");
req.setApp(app);
req.setAdmins(new HashSet<>(Arrays.asList("user1", "user2")));

when(statusLine.getStatusCode()).thenReturn(400);
Expand All @@ -102,9 +115,12 @@ public void testCreateAppFail() throws Exception {

@Test
public void testCreateAppSuccess() throws Exception {
OpenAppDTO app = new OpenAppDTO();
app.setAppId("appId1");
app.setName("name1");

OpenCreateAppDTO req = new OpenCreateAppDTO();
req.setAppId("appId1");
req.setName("name1");
req.setApp(app);
req.setAdmins(new HashSet<>(Arrays.asList("user1", "user2")));

when(statusLine.getStatusCode()).thenReturn(200);
Expand Down

0 comments on commit 9558c84

Please sign in to comment.