use of io.cdap.cdap.proto.BatchApplicationDetail in project cdap by caskdata.
the class AppLifecycleHttpHandler method getApplicationDetails.
/**
* Gets {@link ApplicationDetail} for a set of applications. It expects a post body as a array of object, with each
* object specifying the applciation id and an optional version. E.g.
*
* <pre>
* {@code
* [
* {"appId":"XYZ", "version":"1.2.3"},
* {"appId":"ABC"},
* {"appId":"FOO", "version":"2.3.4"},
* ]
* }
* </pre>
* The response will be an array of {@link BatchApplicationDetail} object, which either indicates a success (200) or
* failure for each of the requested application in the same order as the request.
*/
@POST
@Path("/appdetail")
public void getApplicationDetails(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace) throws Exception {
List<ApplicationId> appIds = decodeAndValidateBatchApplication(validateNamespace(namespace), request);
Map<ApplicationId, ApplicationDetail> details = applicationLifecycleService.getAppDetails(appIds);
List<BatchApplicationDetail> result = new ArrayList<>();
for (ApplicationId appId : appIds) {
ApplicationDetail detail = details.get(appId);
if (detail == null) {
result.add(new BatchApplicationDetail(new NotFoundException(appId)));
} else {
result.add(new BatchApplicationDetail(detail));
}
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result));
}
use of io.cdap.cdap.proto.BatchApplicationDetail in project cdap by caskdata.
the class AppLifecycleHttpHandlerTest method testListAndGet.
@Test
public void testListAndGet() throws Exception {
// deploy without name to testnamespace1
deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
// deploy with name to testnamespace2
String ns2AppName = AllProgramsApp.NAME + "2";
Id.Namespace ns2 = Id.Namespace.from(TEST_NAMESPACE2);
Id.Artifact ns2ArtifactId = Id.Artifact.from(ns2, AllProgramsApp.class.getSimpleName(), "1.0.0-SNAPSHOT");
HttpResponse response = addAppArtifact(ns2ArtifactId, AllProgramsApp.class);
Assert.assertEquals(200, response.getResponseCode());
Id.Application appId = Id.Application.from(ns2, ns2AppName);
response = deploy(appId, new AppRequest<>(ArtifactSummary.from(ns2ArtifactId.toArtifactId())));
Assert.assertEquals(200, response.getResponseCode());
// deploy with name and version to testnamespace2
ApplicationId app1 = new ApplicationId(TEST_NAMESPACE2, ns2AppName, VERSION1);
response = deploy(app1, new AppRequest<>(ArtifactSummary.from(ns2ArtifactId.toArtifactId())));
Assert.assertEquals(200, response.getResponseCode());
// verify testnamespace1 has 1 app
List<JsonObject> apps = getAppList(TEST_NAMESPACE1);
Assert.assertEquals(1, apps.size());
// verify testnamespace2 has 2 app
apps = getAppList(TEST_NAMESPACE2);
Assert.assertEquals(2, apps.size());
// get and verify app details in testnamespace1
JsonObject result = getAppDetails(TEST_NAMESPACE1, AllProgramsApp.NAME);
ApplicationSpecification spec = Specifications.from(new AllProgramsApp());
Assert.assertEquals(AllProgramsApp.NAME, result.get("name").getAsString());
Assert.assertEquals(AllProgramsApp.DESC, result.get("description").getAsString());
// Validate the datasets
JsonArray datasets = result.get("datasets").getAsJsonArray();
Assert.assertEquals(spec.getDatasets().size(), datasets.size());
Assert.assertTrue(StreamSupport.stream(datasets.spliterator(), false).map(JsonObject.class::cast).map(obj -> obj.get("name").getAsString()).allMatch(dataset -> spec.getDatasets().containsKey(dataset)));
// Validate the programs
JsonArray programs = result.get("programs").getAsJsonArray();
int totalPrograms = Arrays.stream(io.cdap.cdap.api.app.ProgramType.values()).mapToInt(type -> spec.getProgramsByType(type).size()).reduce(0, (l, r) -> l + r);
Assert.assertEquals(totalPrograms, programs.size());
Assert.assertTrue(StreamSupport.stream(programs.spliterator(), false).map(JsonObject.class::cast).allMatch(obj -> {
String type = obj.get("type").getAsString().toUpperCase();
io.cdap.cdap.api.app.ProgramType programType = io.cdap.cdap.api.app.ProgramType.valueOf(type);
return spec.getProgramsByType(programType).contains(obj.get("name").getAsString());
}));
// get and verify app details in testnamespace2
List<BatchApplicationDetail> appDetails = getAppDetails(TEST_NAMESPACE2, Arrays.asList(ImmutablePair.of(ns2AppName, null), ImmutablePair.of(ns2AppName, VERSION1)));
Assert.assertEquals(2, appDetails.size());
Assert.assertTrue(appDetails.stream().allMatch(d -> d.getStatusCode() == 200));
ApplicationDetail appDetail = appDetails.get(0).getDetail();
Assert.assertNotNull(appDetail);
Assert.assertEquals(ns2AppName, appDetail.getName());
Assert.assertEquals(ApplicationId.DEFAULT_VERSION, appDetail.getAppVersion());
appDetail = appDetails.get(1).getDetail();
Assert.assertNotNull(appDetail);
Assert.assertEquals(ns2AppName, appDetail.getName());
Assert.assertEquals(VERSION1, appDetail.getAppVersion());
// delete app in testnamespace1
response = doDelete(getVersionedAPIPath("apps/", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(200, response.getResponseCode());
// delete app in testnamespace2
response = doDelete(getVersionedAPIPath("apps/", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2));
Assert.assertEquals(200, response.getResponseCode());
deleteArtifact(ns2ArtifactId, 200);
// verify testnamespace2 has 0 app
apps = getAppList(TEST_NAMESPACE2);
Assert.assertEquals(0, apps.size());
}
use of io.cdap.cdap.proto.BatchApplicationDetail in project cdap by caskdata.
the class AppFabricTestBase method getAppDetails.
/**
* Gets a list of {@link BatchApplicationDetail} from the give set of application version
*
* @param namespace the namespace to read from
* @param appVersions list of appId and version pair.
*/
protected List<BatchApplicationDetail> getAppDetails(String namespace, Collection<ImmutablePair<String, String>> appVersions) throws Exception {
List<Map<String, String>> request = appVersions.stream().map(e -> (e.getSecond() == null) ? Collections.singletonMap("appId", e.getFirst()) : ImmutableMap.of("appId", e.getFirst(), "version", e.getSecond())).collect(Collectors.toList());
HttpResponse response = doPost(getVersionedAPIPath("appdetail", Constants.Gateway.API_VERSION_3_TOKEN, namespace), GSON.toJson(request));
assertResponseCode(200, response);
Assert.assertEquals("application/json", getFirstHeaderValue(response, HttpHeaderNames.CONTENT_TYPE.toString()));
return readResponse(response, new TypeToken<List<BatchApplicationDetail>>() {
}.getType());
}
Aggregations