use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class AdminAppTestRun method testAdminService.
@Test
public void testAdminService() throws Exception {
// Start the service
ServiceManager serviceManager = appManager.getServiceManager(AdminApp.SERVICE_NAME).start();
String namespaceX = "x";
try {
URI serviceURI = serviceManager.getServiceURL(10, TimeUnit.SECONDS).toURI();
// dataset nn should not exist
HttpResponse response = executeHttp(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("false", response.getResponseBodyAsString());
// create nn as a table
response = executeHttp(HttpRequest.put(serviceURI.resolve("create/nn/table").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
// now nn should exist
response = executeHttp(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("true", response.getResponseBodyAsString());
// create it again as a fileset -> should fail with conflict
response = executeHttp(HttpRequest.put(serviceURI.resolve("create/nn/fileSet").toURL()).build());
Assert.assertEquals(409, response.getResponseCode());
// get the type for xx -> not found
response = executeHttp(HttpRequest.get(serviceURI.resolve("type/xx").toURL()).build());
Assert.assertEquals(404, response.getResponseCode());
// get the type for nn -> table
response = executeHttp(HttpRequest.get(serviceURI.resolve("type/nn").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("table", response.getResponseBodyAsString());
// update xx's properties -> should get not-found
Map<String, String> nnProps = TableProperties.builder().setTTL(1000L).build().getProperties();
response = executeHttp(HttpRequest.put(serviceURI.resolve("update/xx").toURL()).withBody(GSON.toJson(nnProps)).build());
Assert.assertEquals(404, response.getResponseCode());
// update nn's properties
response = executeHttp(HttpRequest.put(serviceURI.resolve("update/nn").toURL()).withBody(GSON.toJson(nnProps)).build());
Assert.assertEquals(200, response.getResponseCode());
// get properties for xx -> not found
response = executeHttp(HttpRequest.get(serviceURI.resolve("props/xx").toURL()).build());
Assert.assertEquals(404, response.getResponseCode());
// get properties for nn and validate
response = executeHttp(HttpRequest.get(serviceURI.resolve("props/nn").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
Map<String, String> returnedProps = GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Map<String, String>>() {
}.getType());
Assert.assertEquals(nnProps, returnedProps);
// write some data to the table
DataSetManager<Table> nnManager = getDataset("nn");
nnManager.get().put(new Put("x", "y", "z"));
nnManager.flush();
// in a new tx, validate that data is in table
Assert.assertFalse(nnManager.get().get(new Get("x")).isEmpty());
Assert.assertEquals("z", nnManager.get().get(new Get("x", "y")).getString("y"));
nnManager.flush();
// truncate xx -> not found
response = executeHttp(HttpRequest.post(serviceURI.resolve("truncate/xx").toURL()).build());
Assert.assertEquals(404, response.getResponseCode());
// truncate nn
response = executeHttp(HttpRequest.post(serviceURI.resolve("truncate/nn").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
// validate table is empty
Assert.assertTrue(nnManager.get().get(new Get("x")).isEmpty());
nnManager.flush();
// delete nn
response = executeHttp(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
// delete again -> not found
response = executeHttp(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
Assert.assertEquals(404, response.getResponseCode());
// delete xx which never existed -> not found
response = executeHttp(HttpRequest.delete(serviceURI.resolve("delete/xx").toURL()).build());
Assert.assertEquals(404, response.getResponseCode());
// exists should now return false for nn
response = executeHttp(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("false", response.getResponseBodyAsString());
Assert.assertNull(getDataset("nn").get());
// test Admin.namespaceExists()
HttpRequest request = HttpRequest.get(serviceURI.resolve("namespaces/y").toURL()).build();
response = executeHttp(request);
Assert.assertEquals(404, response.getResponseCode());
// test Admin.getNamespaceSummary()
NamespaceMeta namespaceXMeta = new NamespaceMeta.Builder().setName(namespaceX).setGeneration(10L).build();
getNamespaceAdmin().create(namespaceXMeta);
request = HttpRequest.get(serviceURI.resolve("namespaces/" + namespaceX).toURL()).build();
response = executeHttp(request);
NamespaceSummary namespaceSummary = GSON.fromJson(response.getResponseBodyAsString(), NamespaceSummary.class);
NamespaceSummary expectedX = new NamespaceSummary(namespaceXMeta.getName(), namespaceXMeta.getDescription(), namespaceXMeta.getGeneration());
Assert.assertEquals(expectedX, namespaceSummary);
// test ArtifactManager.listArtifacts()
ArtifactId pluginArtifactId = new NamespaceId(namespaceX).artifact("r1", "1.0.0");
// add a plugin artifact to namespace X
addPluginArtifact(pluginArtifactId, ADMIN_APP_ARTIFACT, DummyPlugin.class);
// no plugins should be listed in the default namespace, but the app artifact should
request = HttpRequest.get(serviceURI.resolve("namespaces/default/plugins").toURL()).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
Type setType = new TypeToken<Set<ArtifactSummary>>() {
}.getType();
Assert.assertEquals(Collections.singleton(ADMIN_ARTIFACT_SUMMARY), GSON.fromJson(response.getResponseBodyAsString(), setType));
// the plugin should be listed in namespace X
request = HttpRequest.get(serviceURI.resolve("namespaces/x/plugins").toURL()).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
ArtifactSummary expected = new ArtifactSummary(pluginArtifactId.getArtifact(), pluginArtifactId.getVersion());
Assert.assertEquals(Collections.singleton(expected), GSON.fromJson(response.getResponseBodyAsString(), setType));
} finally {
serviceManager.stop();
if (getNamespaceAdmin().exists(new NamespaceId(namespaceX))) {
getNamespaceAdmin().delete(new NamespaceId(namespaceX));
}
}
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class DynamicPluginServiceTestRun method initTest.
@Before
public void initTest() throws Exception {
ArtifactId appArtifactId = NamespaceId.DEFAULT.artifact("dynamicPlugin", "1.0.0");
addAppArtifact(appArtifactId, DynamicPluginServiceApp.class);
ArtifactId pluginArtifactId = NamespaceId.DEFAULT.artifact("plugins", "1.0.0");
addPluginArtifact(pluginArtifactId, appArtifactId, ConstantFunction.class, DelegatingFunction.class, MacroFunction.class);
ApplicationId appId = NamespaceId.DEFAULT.app("dynamicPluginService");
ArtifactSummary summary = new ArtifactSummary(appArtifactId.getArtifact(), appArtifactId.getVersion());
AppRequest<Void> appRequest = new AppRequest<>(summary);
ApplicationManager appManager = deployApplication(appId, appRequest);
serviceManager = appManager.getServiceManager(DynamicPluginServiceApp.SERVICE_NAME);
serviceManager.startAndWaitForGoodRun(ProgramRunStatus.RUNNING, 2, TimeUnit.MINUTES);
baseURI = serviceManager.getServiceURL(1, TimeUnit.MINUTES).toURI();
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class UnitTestManager method deployApplication.
@Override
public ApplicationManager deployApplication(NamespaceId namespace, Class<? extends Application> applicationClz, @Nullable Config configObject, File... bundleEmbeddedJars) throws AccessException {
Preconditions.checkNotNull(applicationClz, "Application class cannot be null.");
Type configType = Artifacts.getConfigType(applicationClz);
try {
ArtifactId artifactId = new ArtifactId(namespace.getNamespace(), applicationClz.getSimpleName(), "1.0-SNAPSHOT");
addAppArtifact(artifactId, applicationClz, new Manifest(), bundleEmbeddedJars);
if (configObject == null) {
configObject = (Config) TypeToken.of(configType).getRawType().newInstance();
}
Application app = applicationClz.newInstance();
MockAppConfigurer configurer = new MockAppConfigurer(app);
app.configure(configurer, new DefaultApplicationContext<>(configObject));
ApplicationId applicationId = new ApplicationId(namespace.getNamespace(), configurer.getName());
ArtifactSummary artifactSummary = new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion());
appFabricClient.deployApplication(Id.Application.fromEntityId(applicationId), new AppRequest(artifactSummary, configObject));
return appManagerFactory.create(applicationId);
} catch (AccessException e) {
throw e;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class TestFrameworkTestBase method deployWithArtifact.
/**
* Deploys an {@link Application} using the given artifact jar with an optional config object.
*/
protected static <T> ApplicationManager deployWithArtifact(NamespaceId namespaceId, Class<? extends Application> appClass, File artifactJar, @Nullable T config) throws Exception {
ArtifactId artifactId = new ArtifactId(namespaceId.getNamespace(), appClass.getSimpleName(), "1.0-SNAPSHOT");
addArtifact(artifactId, artifactJar);
AppRequest<T> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), config);
return deployApplication(namespaceId.app(appClass.getSimpleName()), appRequest);
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class ArtifactHttpHandler method getArtifactPlugin.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/extensions/{plugin-type}/plugins/{plugin-name}")
public void getArtifactPlugin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @PathParam("plugin-name") String pluginName, @QueryParam("scope") @DefaultValue("user") final String scope, @QueryParam("artifactName") final String pluginArtifactName, @QueryParam("artifactVersion") String pluginVersion, @QueryParam("artifactScope") final String pluginScope, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException, InvalidArtifactRangeException {
NamespaceId namespace = Ids.namespace(namespaceId);
NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
final NamespaceId pluginArtifactNamespace = validateAndGetScopedNamespace(namespace, pluginScope);
ArtifactId parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
int limitNumber = Integer.parseInt(limit);
limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
Predicate<ArtifactId> predicate = new Predicate<ArtifactId>() {
@Override
public boolean apply(ArtifactId input) {
// by default, the scoped namespace is for USER scope
return (((pluginScope == null && NamespaceId.SYSTEM.equals(input.getParent())) || pluginArtifactNamespace.equals(input.getParent())) && (pluginArtifactName == null || pluginArtifactName.equals(input.getArtifact())) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
}
};
try {
SortedMap<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(namespace, Id.Artifact.fromEntityId(parentArtifactId), pluginType, pluginName, predicate, limitNumber, sortOrder);
List<PluginInfo> pluginInfos = Lists.newArrayList();
// flatten the map
for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
PluginClass pluginClass = pluginsEntry.getValue();
try {
capabilityReader.checkAllEnabled(pluginClass.getRequirements().getCapabilities());
} catch (CapabilityNotAvailableException e) {
LOG.debug("Skipping plugin {} because of disabled capability", pluginClass, e);
continue;
}
pluginInfos.add(new PluginInfo(pluginClass, pluginArtifactSummary));
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(pluginInfos));
} catch (PluginNotExistsException e) {
responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
} catch (IOException e) {
LOG.error("Exception looking up plugins for artifact {}", parentArtifactId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
}
}
Aggregations