use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.
the class DefaultArtifactRepository method addArtifact.
@Override
public ArtifactDetail addArtifact(final Id.Artifact artifactId, final File artifactFile, @Nullable Set<ArtifactRange> parentArtifacts, @Nullable Set<PluginClass> additionalPlugins, Map<String, String> properties) throws Exception {
if (additionalPlugins != null) {
validatePluginSet(additionalPlugins);
}
parentArtifacts = parentArtifacts == null ? Collections.emptySet() : parentArtifacts;
CloseableClassLoader parentClassLoader = null;
EntityImpersonator entityImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
if (!parentArtifacts.isEmpty()) {
validateParentSet(artifactId, parentArtifacts);
parentClassLoader = createParentClassLoader(artifactId, parentArtifacts, entityImpersonator);
}
try {
ArtifactClasses artifactClasses = inspectArtifact(artifactId, artifactFile, additionalPlugins, parentClassLoader);
ArtifactMeta meta = new ArtifactMeta(artifactClasses, parentArtifacts, properties);
ArtifactDetail artifactDetail = artifactStore.write(artifactId, meta, Files.newInputStreamSupplier(artifactFile), entityImpersonator);
ArtifactDescriptor descriptor = artifactDetail.getDescriptor();
// info hides some fields that are available in detail, such as the location of the artifact
ArtifactInfo artifactInfo = new ArtifactInfo(descriptor.getArtifactId(), artifactDetail.getMeta().getClasses(), artifactDetail.getMeta().getProperties());
// add system metadata for artifacts
writeSystemMetadata(artifactId.toEntityId(), artifactInfo);
return artifactDetail;
} finally {
Closeables.closeQuietly(parentClassLoader);
}
}
use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.
the class ArtifactHttpHandlerTest method testAddAndGet.
@Test
public void testAddAndGet() throws Exception {
File wordCountArtifact = buildAppArtifact(WordCountApp.class, "wordcount.jar");
File configTestArtifact = buildAppArtifact(ConfigTestApp.class, "cfgtest.jar");
// add 2 versions of the same app that doesn't use config
ArtifactId wordcountId1 = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
ArtifactId wordcountId2 = NamespaceId.DEFAULT.artifact("wordcount", "2.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(wordcountId1), Files.newInputStreamSupplier(wordCountArtifact), null).getStatusLine().getStatusCode());
Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(wordcountId2), Files.newInputStreamSupplier(wordCountArtifact), null).getStatusLine().getStatusCode());
// and 1 version of another app that uses a config
ArtifactId configTestAppId = NamespaceId.DEFAULT.artifact("cfgtest", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(configTestAppId), Files.newInputStreamSupplier(configTestArtifact), null).getStatusLine().getStatusCode());
// test get /artifacts endpoint
Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0"), new ArtifactSummary("wordcount", "2.0.0"), new ArtifactSummary("cfgtest", "1.0.0"));
Set<ArtifactSummary> actualArtifacts = getArtifacts(NamespaceId.DEFAULT);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts/wordcount endpoint
expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0"), new ArtifactSummary("wordcount", "2.0.0"));
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount");
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts/cfgtest/versions/1.0.0 endpoint
Schema appConfigSchema = schemaGenerator.generate(ConfigTestApp.ConfigClass.class);
ArtifactInfo actualInfo = getArtifact(configTestAppId);
Assert.assertEquals("cfgtest", actualInfo.getName());
Assert.assertEquals("1.0.0", actualInfo.getVersion());
ApplicationClass appClass = new ApplicationClass(ConfigTestApp.class.getName(), "", appConfigSchema);
Assert.assertTrue(actualInfo.getClasses().getApps().contains(appClass));
}
use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.
the class ArtifactHttpHandlerTest method testSystemArtifacts.
@Test
public void testSystemArtifacts() throws Exception {
// add the app in the default namespace
File systemArtifact = buildAppArtifact(WordCountApp.class, "wordcount-1.0.0.jar");
ArtifactId defaultId = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(defaultId), Files.newInputStreamSupplier(systemArtifact), null).getStatusLine().getStatusCode());
// add system artifact
addSystemArtifacts();
// test get /artifacts
Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER), new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
Set<ArtifactSummary> actualArtifacts = getArtifacts(NamespaceId.DEFAULT);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts?scope=system
expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.SYSTEM);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts?scope=user
expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER));
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.USER);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts/wordcount?scope=user
expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER));
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.USER);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts/wordcount?scope=system
expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.SYSTEM);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts/wordcount/versions/1.0.0?scope=user
ArtifactInfo actualInfo = getArtifact(defaultId, ArtifactScope.USER);
Assert.assertEquals("wordcount", actualInfo.getName());
Assert.assertEquals("1.0.0", actualInfo.getVersion());
// test get /artifacts/wordcount/versions/1.0.0?scope=system
actualInfo = getArtifact(defaultId, ArtifactScope.SYSTEM);
Assert.assertEquals("wordcount", actualInfo.getName());
Assert.assertEquals("1.0.0", actualInfo.getVersion());
try {
ArtifactId systemId = NamespaceId.SYSTEM.artifact("wordcount", "1.0.0");
artifactRepository.addArtifact(Id.Artifact.fromEntityId(systemId), systemArtifact, new HashSet<ArtifactRange>(), null);
Assert.fail();
} catch (Exception e) {
// no-op - expected exception while adding artifact in system namespace
}
cleanupSystemArtifactsDirectory();
}
use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.
the class ArtifactHttpHandlerTest method testSystemArtifactIsolation.
/**
* Tests that system artifacts can be accessed in a user namespace only if appropriately scoped.
*/
@Test
public void testSystemArtifactIsolation() throws Exception {
// add the app in the default namespace
ArtifactId defaultId = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
// add system artifact wordcount
addSystemArtifacts();
// test get /artifacts
Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
Set<ArtifactSummary> actualArtifacts = getArtifacts(NamespaceId.DEFAULT);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts?scope=system
expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.SYSTEM);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts?scope=user
expectedArtifacts = Sets.newHashSet();
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.USER);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts/wordcount?scope=user
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.USER);
Assert.assertNull(actualArtifacts);
// test get /artifacts/wordcount?scope=system
expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.SYSTEM);
Assert.assertEquals(expectedArtifacts, actualArtifacts);
// test get /artifacts/wordcount/versions/1.0.0?scope=user
ArtifactInfo actualInfo = getArtifact(defaultId, ArtifactScope.USER);
Assert.assertEquals(null, actualInfo);
// test get /artifacts/wordcount/versions/1.0.0?scope=system
actualInfo = getArtifact(defaultId, ArtifactScope.SYSTEM);
Assert.assertEquals("wordcount", actualInfo.getName());
Assert.assertEquals("1.0.0", actualInfo.getVersion());
// test delete /default/artifacts/wordcount/versions/1.0.0
deleteArtifact(Id.Artifact.fromEntityId(defaultId), 404);
cleanupSystemArtifactsDirectory();
}
Aggregations