use of co.cask.cdap.api.artifact.ApplicationClass in project cdap by caskdata.
the class ArtifactInspectorTest method inspectAppsAndPlugins.
@Test
public void inspectAppsAndPlugins() throws Exception {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, InspectionApp.class.getPackage().getName());
File appFile = createJar(InspectionApp.class, new File(TMP_FOLDER.newFolder(), "InspectionApp-1.0.0.jar"), manifest);
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "InspectionApp", "1.0.0");
Location artifactLocation = Locations.toLocation(appFile);
try (CloseableClassLoader artifactClassLoader = classLoaderFactory.createClassLoader(artifactLocation, new EntityImpersonator(artifactId.toEntityId(), new DefaultImpersonator(CConfiguration.create(), null)))) {
ArtifactClasses classes = artifactInspector.inspectArtifact(artifactId, appFile, artifactClassLoader);
// check app classes
Set<ApplicationClass> expectedApps = ImmutableSet.of(new ApplicationClass(InspectionApp.class.getName(), "", new ReflectionSchemaGenerator(false).generate(InspectionApp.AConfig.class)));
Assert.assertEquals(expectedApps, classes.getApps());
// check plugin classes
PluginClass expectedPlugin = new PluginClass(InspectionApp.PLUGIN_TYPE, InspectionApp.PLUGIN_NAME, InspectionApp.PLUGIN_DESCRIPTION, InspectionApp.AppPlugin.class.getName(), "pluginConf", ImmutableMap.of("y", new PluginPropertyField("y", "", "double", true, true), "isSomething", new PluginPropertyField("isSomething", "", "boolean", true, false)));
Assert.assertEquals(ImmutableSet.of(expectedPlugin), classes.getPlugins());
}
}
use of co.cask.cdap.api.artifact.ApplicationClass in project cdap by caskdata.
the class ArtifactStoreTest method testAddGetSingleArtifact.
@Test
public void testAddGetSingleArtifact() throws Exception {
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "myplugins", "1.0.0");
PluginClass plugin1 = new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of());
PluginClass plugin2 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
PluginClass plugin3 = new PluginClass("btype", "plugin3", "", "c.c.c.plugin3", "cfg", ImmutableMap.<String, PluginPropertyField>of());
Set<PluginClass> plugins = ImmutableSet.of(plugin1, plugin2, plugin3);
ApplicationClass appClass = new ApplicationClass(InspectionApp.class.getName(), "", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).addApp(appClass).build());
String artifactContents = "my artifact contents";
writeArtifact(artifactId, artifactMeta, artifactContents);
ArtifactDetail artifactDetail = artifactStore.getArtifact(artifactId);
assertEqual(artifactId, artifactMeta, artifactContents, artifactDetail);
// test that plugins in the artifact show up when getting plugins for that artifact
Map<ArtifactDescriptor, Set<PluginClass>> pluginsMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, artifactId);
Assert.assertEquals(1, pluginsMap.size());
Assert.assertTrue(pluginsMap.containsKey(artifactDetail.getDescriptor()));
Set<PluginClass> expected = ImmutableSet.copyOf(plugins);
Set<PluginClass> actual = ImmutableSet.copyOf(pluginsMap.get(artifactDetail.getDescriptor()));
Assert.assertEquals(expected, actual);
// test plugins for the specific type
pluginsMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, artifactId, "atype");
Assert.assertEquals(1, pluginsMap.size());
Assert.assertTrue(pluginsMap.containsKey(artifactDetail.getDescriptor()));
expected = ImmutableSet.of(plugin1, plugin2);
actual = ImmutableSet.copyOf(pluginsMap.get(artifactDetail.getDescriptor()));
Assert.assertEquals(expected, actual);
// test plugins for specific type and name
Map<ArtifactDescriptor, PluginClass> pluginClasses = artifactStore.getPluginClasses(NamespaceId.DEFAULT.getNamespaceId(), artifactId, "btype", "plugin3", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(1, pluginClasses.size());
Assert.assertTrue(pluginClasses.containsKey(artifactDetail.getDescriptor()));
Assert.assertEquals(plugin3, pluginClasses.get(artifactDetail.getDescriptor()));
}
use of co.cask.cdap.api.artifact.ApplicationClass in project cdap by caskdata.
the class ArtifactStoreTest method testPluginNamespaceIsolation.
@Test
public void testPluginNamespaceIsolation() throws Exception {
// write some system artifact
Id.Artifact systemAppArtifact = Id.Artifact.from(Id.Namespace.SYSTEM, "app", "1.0.0");
ArtifactMeta systemAppMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(new ApplicationClass("co.cask.class", "desc", null)).build());
writeArtifact(systemAppArtifact, systemAppMeta, "app contents");
Set<ArtifactRange> usableBy = ImmutableSet.of(new ArtifactRange(systemAppArtifact.getNamespace().getId(), systemAppArtifact.getName(), systemAppArtifact.getVersion(), true, systemAppArtifact.getVersion(), true));
PluginClass plugin = new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of());
// write a plugins artifact in namespace1
NamespaceId namespace1 = Ids.namespace("ns1");
Id.Artifact artifact1 = Id.Artifact.from(namespace1.toId(), "plugins1", "1.0.0");
ArtifactMeta meta1 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin).build(), usableBy);
String contents1 = "plugin1 contents";
writeArtifact(artifact1, meta1, contents1);
// write a plugins artifact in namespace2
NamespaceId namespace2 = Ids.namespace("ns2");
Id.Artifact artifact2 = Id.Artifact.from(namespace2.toId(), "plugins2", "1.0.0");
ArtifactMeta meta2 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin).build(), usableBy);
String contents2 = "plugin2 contents";
writeArtifact(artifact2, meta2, contents2);
try {
// this should only get plugins from artifact1
SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactStore.getPluginClasses(namespace1, systemAppArtifact);
Assert.assertEquals(1, plugins.size());
ArtifactDescriptor artifactDescriptor = plugins.firstKey();
Assert.assertEquals(artifact1.toArtifactId(), artifactDescriptor.getArtifactId());
assertContentsEqual(contents1, artifactDescriptor.getLocation());
// this should only get plugins from artifact2
plugins = artifactStore.getPluginClasses(namespace2, systemAppArtifact);
Assert.assertEquals(1, plugins.size());
artifactDescriptor = plugins.firstKey();
Assert.assertEquals(artifact2.toArtifactId(), artifactDescriptor.getArtifactId());
assertContentsEqual(contents2, artifactDescriptor.getLocation());
} finally {
artifactStore.clear(namespace1);
artifactStore.clear(namespace2);
artifactStore.clear(NamespaceId.SYSTEM);
}
}
use of co.cask.cdap.api.artifact.ApplicationClass in project cdap by caskdata.
the class ArtifactStoreTest method testGetAppClasses.
@Test
public void testGetAppClasses() throws Exception {
// create 2 versions of the same artifact with the same app class
Id.Artifact app1v1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "appA", "1.0.0");
ApplicationClass inspectionClass1 = new ApplicationClass(InspectionApp.class.getName(), "v1", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(inspectionClass1).build());
writeArtifact(app1v1Id, artifactMeta, "my artifact contents");
ArtifactDetail app1v1Detail = artifactStore.getArtifact(app1v1Id);
Id.Artifact app1v2Id = Id.Artifact.from(Id.Namespace.DEFAULT, "appA", "2.0.0");
ApplicationClass inspectionClass2 = new ApplicationClass(InspectionApp.class.getName(), "v2", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(inspectionClass2).build());
writeArtifact(app1v2Id, artifactMeta, "my artifact contents");
ArtifactDetail app1v2Detail = artifactStore.getArtifact(app1v2Id);
// create a different artifact with the same app class
Id.Artifact app2v1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "appB", "1.0.0");
artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(inspectionClass1).build());
writeArtifact(app2v1Id, artifactMeta, "other contents");
ArtifactDetail app2v1Detail = artifactStore.getArtifact(app2v1Id);
// create another artifact with a different app class
Id.Artifact app3v1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "appC", "1.0.0");
ApplicationClass wordCountClass1 = new ApplicationClass(WordCountApp.class.getName(), "v1", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(wordCountClass1).build());
writeArtifact(app3v1Id, artifactMeta, "wc contents");
ArtifactDetail app3v1Detail = artifactStore.getArtifact(app3v1Id);
// test getting all app classes in the namespace
Map<ArtifactDescriptor, List<ApplicationClass>> appClasses = artifactStore.getApplicationClasses(NamespaceId.DEFAULT);
Map<ArtifactDescriptor, List<ApplicationClass>> expected = ImmutableMap.<ArtifactDescriptor, List<ApplicationClass>>of(app1v1Detail.getDescriptor(), ImmutableList.of(inspectionClass1), app1v2Detail.getDescriptor(), ImmutableList.of(inspectionClass2), app2v1Detail.getDescriptor(), ImmutableList.of(inspectionClass1), app3v1Detail.getDescriptor(), ImmutableList.of(wordCountClass1));
Assert.assertEquals(expected, appClasses);
// test getting all app classes by class name
Map<ArtifactDescriptor, ApplicationClass> appArtifacts = artifactStore.getApplicationClasses(NamespaceId.DEFAULT, InspectionApp.class.getName());
Map<ArtifactDescriptor, ApplicationClass> expectedAppArtifacts = ImmutableMap.of(app1v1Detail.getDescriptor(), inspectionClass1, app1v2Detail.getDescriptor(), inspectionClass2, app2v1Detail.getDescriptor(), inspectionClass1);
Assert.assertEquals(expectedAppArtifacts, appArtifacts);
appArtifacts = artifactStore.getApplicationClasses(NamespaceId.DEFAULT, WordCountApp.class.getName());
expectedAppArtifacts = ImmutableMap.of(app3v1Detail.getDescriptor(), wordCountClass1);
Assert.assertEquals(expectedAppArtifacts, appArtifacts);
Assert.assertTrue(artifactStore.getApplicationClasses(Ids.namespace("ghost")).isEmpty());
}
use of co.cask.cdap.api.artifact.ApplicationClass in project cdap by caskdata.
the class ArtifactClientTestRun method testArtifacts.
@Test
public void testArtifacts() throws Exception {
// add 2 versions of an artifact with an application
ArtifactId myapp1Id = NamespaceId.DEFAULT.artifact("myapp", "1.0.0");
ArtifactId myapp2Id = NamespaceId.DEFAULT.artifact("myapp", "2.0.0");
LocalLocationFactory locationFactory = new LocalLocationFactory(TMP_FOLDER.newFolder());
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.BUNDLE_VERSION, "2.0.0");
final Location appJarLoc = AppJarHelper.createDeploymentJar(locationFactory, MyApp.class, manifest);
InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return appJarLoc.getInputStream();
}
};
artifactClient.add(myapp1Id.getParent(), myapp1Id.getArtifact(), inputSupplier, myapp1Id.getVersion());
// add some properties
Map<String, String> myapp1Properties = ImmutableMap.of("k1", "v1");
artifactClient.writeProperties(myapp1Id, myapp1Properties);
// let it derive version from jar manifest, which has bundle-version at 2.0.0
artifactClient.add(myapp2Id.getParent(), myapp2Id.getArtifact(), inputSupplier, null, null);
// add some properties
Map<String, String> myapp2Properties = ImmutableMap.of("k1", "v1", "k2", "v2");
artifactClient.writeProperties(myapp2Id, myapp2Properties);
// add an artifact that contains a plugin, but only extends myapp-2.0.0
ArtifactId pluginId = NamespaceId.DEFAULT.artifact("myapp-plugins", "2.0.0");
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
final Location pluginJarLoc = PluginJarHelper.createPluginJar(locationFactory, manifest, Plugin1.class);
inputSupplier = new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return pluginJarLoc.getInputStream();
}
};
Set<ArtifactRange> parents = Sets.newHashSet(new ArtifactRange(myapp2Id.getParent().getNamespace(), myapp2Id.getArtifact(), new ArtifactVersion(myapp2Id.getVersion()), new ArtifactVersion("3.0.0")));
Set<PluginClass> additionalPlugins = Sets.newHashSet(new PluginClass("jdbc", "mysql", "", "com.mysql.jdbc.Driver", null, Collections.<String, PluginPropertyField>emptyMap()));
artifactClient.add(pluginId.getParent(), pluginId.getArtifact(), inputSupplier, pluginId.getVersion(), parents, additionalPlugins);
ArtifactSummary myapp1Summary = new ArtifactSummary(myapp1Id.getArtifact(), myapp1Id.getVersion());
ArtifactSummary myapp2Summary = new ArtifactSummary(myapp2Id.getArtifact(), myapp2Id.getVersion());
ArtifactSummary pluginArtifactSummary = new ArtifactSummary(pluginId.getArtifact(), pluginId.getVersion());
Set<ArtifactSummary> artifacts = Sets.newHashSet(artifactClient.list(NamespaceId.DEFAULT));
Assert.assertEquals(Sets.newHashSet(myapp1Summary, myapp2Summary, pluginArtifactSummary), artifacts);
// list all artifacts named 'myapp'
Assert.assertEquals(Sets.newHashSet(myapp1Summary, myapp2Summary), Sets.newHashSet(artifactClient.listVersions(NamespaceId.DEFAULT, myapp1Id.getArtifact())));
// list all artifacts named 'myapp-plugins'
Assert.assertEquals(Sets.newHashSet(pluginArtifactSummary), Sets.newHashSet(artifactClient.listVersions(NamespaceId.DEFAULT, pluginId.getArtifact())));
// artifacts should be in user scope
try {
artifactClient.listVersions(NamespaceId.DEFAULT, pluginId.getArtifact(), ArtifactScope.SYSTEM);
Assert.fail();
} catch (ArtifactNotFoundException e) {
// expected
}
// get info about specific artifacts
Schema myAppConfigSchema = new ReflectionSchemaGenerator(false).generate(MyApp.Conf.class);
ArtifactClasses myAppClasses = ArtifactClasses.builder().addApp(new ApplicationClass(MyApp.class.getName(), "", myAppConfigSchema)).build();
// test get myapp-1.0.0
ArtifactInfo myapp1Info = new ArtifactInfo(myapp1Id.getArtifact(), myapp1Id.getVersion(), ArtifactScope.USER, myAppClasses, myapp1Properties);
Assert.assertEquals(myapp1Info, artifactClient.getArtifactInfo(myapp1Id));
// test get myapp-2.0.0
ArtifactInfo myapp2Info = new ArtifactInfo(myapp2Id.getArtifact(), myapp2Id.getVersion(), ArtifactScope.USER, myAppClasses, myapp2Properties);
Assert.assertEquals(myapp2Info, artifactClient.getArtifactInfo(myapp2Id));
// test overwriting properties
myapp2Properties = ImmutableMap.of("k1", "v3", "k5", "v5");
artifactClient.writeProperties(myapp2Id, myapp2Properties);
Assert.assertEquals(myapp2Properties, artifactClient.getArtifactInfo(myapp2Id).getProperties());
// test deleting property
artifactClient.deleteProperty(myapp2Id, "k1");
Assert.assertEquals(ImmutableMap.of("k5", "v5"), artifactClient.getArtifactInfo(myapp2Id).getProperties());
// test writing property
artifactClient.writeProperty(myapp2Id, "k5", "v4");
Assert.assertEquals(ImmutableMap.of("k5", "v4"), artifactClient.getArtifactInfo(myapp2Id).getProperties());
// test deleting properties
artifactClient.deleteProperties(myapp2Id);
Assert.assertTrue(artifactClient.getArtifactInfo(myapp2Id).getProperties().isEmpty());
// test get myapp-plugins-2.0.0
Map<String, PluginPropertyField> props = ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false));
ArtifactClasses pluginClasses = ArtifactClasses.builder().addPlugin(new PluginClass("callable", "plugin1", "p1 description", Plugin1.class.getName(), "conf", props)).addPlugins(additionalPlugins).build();
ArtifactInfo pluginArtifactInfo = new ArtifactInfo(pluginId.getArtifact(), pluginId.getVersion(), ArtifactScope.USER, pluginClasses, ImmutableMap.<String, String>of());
Assert.assertEquals(pluginArtifactInfo, artifactClient.getArtifactInfo(pluginId));
// test get all app classes in namespace
Set<ApplicationClassSummary> expectedSummaries = ImmutableSet.of(new ApplicationClassSummary(myapp1Summary, MyApp.class.getName()), new ApplicationClassSummary(myapp2Summary, MyApp.class.getName()));
Set<ApplicationClassSummary> appClassSummaries = Sets.newHashSet(artifactClient.getApplicationClasses(NamespaceId.DEFAULT));
Assert.assertEquals(expectedSummaries, appClassSummaries);
// test get all app classes in namespace with name MyApp.class.getName()
Set<ApplicationClassInfo> appClassInfos = Sets.newHashSet(artifactClient.getApplicationClasses(NamespaceId.DEFAULT, MyApp.class.getName()));
Set<ApplicationClassInfo> expectedInfos = ImmutableSet.of(new ApplicationClassInfo(myapp1Summary, MyApp.class.getName(), myAppConfigSchema), new ApplicationClassInfo(myapp2Summary, MyApp.class.getName(), myAppConfigSchema));
Assert.assertEquals(expectedInfos, appClassInfos);
// test get plugin types for myapp-1.0.0. should be empty, since plugins only extends versions [2.0.0 - 3.0.0)
Assert.assertTrue(artifactClient.getPluginTypes(myapp1Id).isEmpty());
// test get plugin types for myapp-2.0.0
Assert.assertEquals(Lists.newArrayList("callable", "jdbc"), artifactClient.getPluginTypes(myapp2Id));
// test get plugins of type callable for myapp-2.0.0
PluginSummary pluginSummary = new PluginSummary("plugin1", "callable", "p1 description", Plugin1.class.getName(), pluginArtifactSummary);
Assert.assertEquals(Sets.newHashSet(pluginSummary), Sets.newHashSet(artifactClient.getPluginSummaries(myapp2Id, "callable")));
// no plugins of type "runnable"
Assert.assertTrue(artifactClient.getPluginSummaries(myapp2Id, "runnable").isEmpty());
// test get plugin details for plugin1 for myapp-2.0.0
PluginInfo pluginInfo = new PluginInfo("plugin1", "callable", "p1 description", Plugin1.class.getName(), pluginArtifactSummary, props, new HashSet<String>());
Assert.assertEquals(Sets.newHashSet(pluginInfo), Sets.newHashSet(artifactClient.getPluginInfo(myapp2Id, "callable", "plugin1")));
}
Aggregations