use of co.cask.cdap.proto.artifact.ArtifactPropertiesRequest in project cdap by caskdata.
the class ArtifactHttpHandlerTest method testBatchProperties.
@Test
public void testBatchProperties() throws Exception {
ArtifactId wordcountId1 = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordcountId1), WordCountApp.class).getStatusLine().getStatusCode());
addArtifactProperties(Id.Artifact.fromEntityId(wordcountId1), ImmutableMap.of("k1", "v1", "k2", "v2"));
ArtifactId wordcountId2 = NamespaceId.DEFAULT.artifact("wc", "2.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordcountId2), WordCountApp.class).getStatusLine().getStatusCode());
addArtifactProperties(Id.Artifact.fromEntityId(wordcountId2), ImmutableMap.of("k2", "v20", "k3", "v30"));
List<String> props = ImmutableList.of("k1", "k2", "k3");
List<ArtifactPropertiesRequest> requestList = ImmutableList.of(new ArtifactPropertiesRequest(wordcountId1.getArtifact(), wordcountId1.getVersion(), ArtifactScope.USER, props), new ArtifactPropertiesRequest(wordcountId2.getArtifact(), wordcountId2.getVersion(), ArtifactScope.USER, props));
URL url = getEndPoint(String.format("%s/namespaces/%s/artifactproperties", Constants.Gateway.API_VERSION_3, NamespaceId.DEFAULT.getNamespace())).toURL();
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(requestList)).build();
co.cask.common.http.HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
List<ArtifactSummaryProperties> actual = GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<List<ArtifactSummaryProperties>>() {
}.getType());
List<ArtifactSummaryProperties> expected = ImmutableList.of(new ArtifactSummaryProperties(wordcountId1.getArtifact(), wordcountId1.getVersion(), ArtifactScope.USER, ImmutableMap.of("k1", "v1", "k2", "v2")), new ArtifactSummaryProperties(wordcountId2.getArtifact(), wordcountId2.getVersion(), ArtifactScope.USER, ImmutableMap.of("k2", "v20", "k3", "v30")));
Assert.assertEquals(expected, actual);
}
use of co.cask.cdap.proto.artifact.ArtifactPropertiesRequest in project cdap by caskdata.
the class ArtifactHttpHandler method getArtifactProperties.
@POST
@Path("/namespaces/{namespace-id}/artifactproperties")
public void getArtifactProperties(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
NamespaceId namespace = validateAndGetNamespace(namespaceId);
List<ArtifactPropertiesRequest> propertyRequests;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
propertyRequests = GSON.fromJson(reader, BATCH_ARTIFACT_PROPERTIES_REQUEST);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Unable to parse request: " + e.getMessage(), e);
}
List<ArtifactSummaryProperties> result = new ArrayList<>(propertyRequests.size());
for (ArtifactPropertiesRequest propertiesRequest : propertyRequests) {
NamespaceId requestNamespace = propertiesRequest.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : namespace;
ArtifactId artifactId = validateAndGetArtifactId(requestNamespace, propertiesRequest.getName(), propertiesRequest.getVersion());
ArtifactDetail artifactDetail;
try {
artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
} catch (ArtifactNotFoundException e) {
continue;
}
Map<String, String> properties = artifactDetail.getMeta().getProperties();
Map<String, String> filteredProperties = new HashMap<>(propertiesRequest.getProperties().size());
for (String propertyKey : propertiesRequest.getProperties()) {
if (properties.containsKey(propertyKey)) {
filteredProperties.put(propertyKey, properties.get(propertyKey));
}
}
result.add(new ArtifactSummaryProperties(propertiesRequest.getName(), propertiesRequest.getVersion(), propertiesRequest.getScope(), filteredProperties));
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result, BATCH_ARTIFACT_PROPERTIES_RESPONSE));
}
Aggregations