Search in sources :

Example 36 with ArtifactVersion

use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.

the class ArtifactRepositoryTest method testNamespaceIsolation.

@Test
public void testNamespaceIsolation() throws Exception {
    // create system app artifact
    Id.Artifact systemAppArtifactId = Id.Artifact.from(Id.Namespace.SYSTEM, "PluginTest", "1.0.0");
    File jar = createAppJar(PluginTestApp.class, new File(systemArtifactsDir1, "PluginTest-1.0.0.jar"), createManifest(ManifestFields.EXPORT_PACKAGE, PluginTestRunnable.class.getPackage().getName()));
    artifactRepository.addSystemArtifacts();
    Assert.assertTrue(jar.delete());
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(systemAppArtifactId.getNamespace().getId(), systemAppArtifactId.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    NamespaceId namespace1 = Ids.namespace("ns1");
    NamespaceId namespace2 = Ids.namespace("ns2");
    Id.Artifact pluginArtifactId1 = Id.Artifact.from(namespace1.toId(), "myPlugin", "1.0");
    Id.Artifact pluginArtifactId2 = Id.Artifact.from(namespace2.toId(), "myPlugin", "1.0");
    try {
        // create plugin artifact in namespace1 that extends the system artifact
        // There should be two plugins there (TestPlugin and TestPlugin2).
        Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, TestPlugin.class.getPackage().getName());
        File jarFile = createPluginJar(TestPlugin.class, new File(tmpDir, "myPlugin-1.0.jar"), manifest);
        artifactRepository.addArtifact(pluginArtifactId1, jarFile, parents);
        // create plugin artifact in namespace2 that extends the system artifact
        artifactRepository.addArtifact(pluginArtifactId2, jarFile, parents);
        // check that only plugins from the artifact in the namespace are returned, and not plugins from both
        SortedMap<ArtifactDescriptor, Set<PluginClass>> extensions = artifactRepository.getPlugins(namespace1, systemAppArtifactId);
        Assert.assertEquals(1, extensions.keySet().size());
        Assert.assertEquals(2, extensions.values().iterator().next().size());
        extensions = artifactRepository.getPlugins(namespace2, systemAppArtifactId);
        Assert.assertEquals(1, extensions.keySet().size());
        Assert.assertEquals(2, extensions.values().iterator().next().size());
    } finally {
        artifactRepository.clear(NamespaceId.SYSTEM);
        artifactRepository.clear(namespace1);
        artifactRepository.clear(namespace2);
    }
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) PluginTestRunnable(co.cask.cdap.internal.app.runtime.artifact.app.plugin.PluginTestRunnable) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Manifest(java.util.jar.Manifest) File(java.io.File) Test(org.junit.Test)

Example 37 with ArtifactVersion

use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.

the class ArtifactRepositoryTest method testGrandparentsAreInvalid.

@Test(expected = InvalidArtifactException.class)
public void testGrandparentsAreInvalid() throws Exception {
    // create child artifact
    Id.Artifact childId = Id.Artifact.from(Id.Namespace.DEFAULT, "child", "1.0.0");
    Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
    File jarFile = createPluginJar(Plugin1.class, new File(tmpDir, "child-1.0.0.jar"), manifest);
    // add the artifact
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(APP_ARTIFACT_ID.getNamespace().getId(), APP_ARTIFACT_ID.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    artifactRepository.addArtifact(childId, jarFile, parents);
    // try and create grandchild, should throw exception
    Id.Artifact grandchildId = Id.Artifact.from(Id.Namespace.DEFAULT, "grandchild", "1.0.0");
    manifest = createManifest(ManifestFields.EXPORT_PACKAGE, Plugin2.class.getPackage().getName());
    jarFile = createPluginJar(Plugin2.class, new File(tmpDir, "grandchild-1.0.0.jar"), manifest);
    parents = ImmutableSet.of(new ArtifactRange(childId.getNamespace().getId(), childId.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    artifactRepository.addArtifact(grandchildId, jarFile, parents);
}
Also used : TestPlugin2(co.cask.cdap.internal.app.plugins.test.TestPlugin2) Plugin2(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin2) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Manifest(java.util.jar.Manifest) File(java.io.File) Test(org.junit.Test)

Example 38 with ArtifactVersion

use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.

the class ArtifactRangeTest method testIsInRange.

@Test
public void testIsInRange() {
    ArtifactRange range = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "test", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("0.0.9")));
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("0.9.0")));
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("0.9")));
    // 1 < 1.0 < 1.0.0-SNAPSHOT < 1.0.0
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("1")));
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("1.0")));
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("1.0.0-SNAPSHOT")));
    Assert.assertTrue(range.versionIsInRange(new ArtifactVersion("1.0.0")));
    Assert.assertTrue(range.versionIsInRange(new ArtifactVersion("1.0.1-SNAPSHOT")));
    Assert.assertTrue(range.versionIsInRange(new ArtifactVersion("1.0.1")));
    Assert.assertTrue(range.versionIsInRange(new ArtifactVersion("1.1.0")));
    // 2 < 2.0 < 2.0.0-SNAPSHOT < 2.0.0
    Assert.assertTrue(range.versionIsInRange(new ArtifactVersion("2")));
    Assert.assertTrue(range.versionIsInRange(new ArtifactVersion("2.0")));
    Assert.assertTrue(range.versionIsInRange(new ArtifactVersion("2.0.0-SNAPSHOT")));
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("2.0.0")));
    Assert.assertFalse(range.versionIsInRange(new ArtifactVersion("2.0.1")));
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Test(org.junit.Test)

Example 39 with ArtifactVersion

use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.

the class AppFabricTestHelper method deployApplicationWithManager.

public static ApplicationWithPrograms deployApplicationWithManager(Id.Namespace namespace, Class<?> appClass, Supplier<File> folderSupplier, Config config) throws Exception {
    ensureNamespaceExists(namespace.toEntityId());
    Location deployedJar = createAppJar(appClass, folderSupplier);
    ArtifactVersion artifactVersion = new ArtifactVersion(String.format("1.0.%d", System.currentTimeMillis()));
    ArtifactId artifactId = new ArtifactId(appClass.getSimpleName(), artifactVersion, ArtifactScope.USER);
    ArtifactDescriptor artifactDescriptor = new ArtifactDescriptor(artifactId, deployedJar);
    ArtifactRepository artifactRepository = getInjector().getInstance(ArtifactRepository.class);
    artifactRepository.addArtifact(Artifacts.toArtifactId(namespace.toEntityId(), artifactId).toId(), new File(deployedJar.toURI()));
    AppDeploymentInfo info = new AppDeploymentInfo(artifactDescriptor, namespace.toEntityId(), appClass.getName(), null, null, config == null ? null : new Gson().toJson(config));
    return getLocalManager().deploy(info).get();
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) AppDeploymentInfo(co.cask.cdap.internal.app.deploy.pipeline.AppDeploymentInfo) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) Gson(com.google.gson.Gson) ArtifactRepository(co.cask.cdap.internal.app.runtime.artifact.ArtifactRepository) File(java.io.File) Location(org.apache.twill.filesystem.Location)

Example 40 with ArtifactVersion

use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.

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);
    Id.Artifact parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
    int limitNumber = Integer.valueOf(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, 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();
            pluginInfos.add(new PluginInfo(pluginClass.getName(), pluginClass.getType(), pluginClass.getDescription(), pluginClass.getClassName(), pluginArtifactSummary, pluginClass.getProperties(), pluginClass.getEndpoints()));
        }
        responder.sendJson(HttpResponseStatus.OK, 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.");
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactVersionRange(co.cask.cdap.api.artifact.ArtifactVersionRange) IOException(java.io.IOException) PluginEndpoint(co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint) ArtifactSortOrder(co.cask.cdap.proto.artifact.ArtifactSortOrder) Predicate(com.google.common.base.Predicate) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)50 Test (org.junit.Test)36 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)33 Id (co.cask.cdap.proto.Id)24 NamespaceId (co.cask.cdap.proto.id.NamespaceId)24 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)18 PluginClass (co.cask.cdap.api.plugin.PluginClass)15 ArtifactId (co.cask.cdap.proto.id.ArtifactId)14 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)12 File (java.io.File)11 Manifest (java.util.jar.Manifest)10 HashSet (java.util.HashSet)9 ArtifactDescriptor (co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 Set (java.util.Set)6 Location (org.apache.twill.filesystem.Location)6 Map (java.util.Map)5 AppDeploymentInfo (co.cask.cdap.internal.app.deploy.pipeline.AppDeploymentInfo)4