Search in sources :

Example 1 with Plugin1

use of co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin1 in project cdap by caskdata.

the class ArtifactHttpHandlerTest method testGetPlugins.

@Test
public void testGetPlugins() throws Exception {
    // add an app for plugins to extend
    Id.Artifact wordCount1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), addAppArtifact(wordCount1Id, WordCountApp.class).getStatusLine().getStatusCode());
    Id.Artifact wordCount2Id = Id.Artifact.from(Id.Namespace.DEFAULT, "wordcount", "2.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), addAppArtifact(wordCount2Id, WordCountApp.class).getStatusLine().getStatusCode());
    // add some plugins.
    // plugins-1.0.0 extends wordcount[1.0.0,2.0.0)
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
    Id.Artifact pluginsId1 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "1.0.0");
    Set<ArtifactRange> plugins1Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(pluginsId1, Plugin1.class, manifest, plugins1Parents).getStatusLine().getStatusCode());
    // plugin-2.0.0 extends wordcount[1.0.0,3.0.0)
    Id.Artifact pluginsId2 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "2.0.0");
    Set<ArtifactRange> plugins2Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("3.0.0")));
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(pluginsId2, Plugin1.class, manifest, plugins2Parents).getStatusLine().getStatusCode());
    ArtifactSummary plugins1Artifact = new ArtifactSummary("plugins", "1.0.0");
    ArtifactSummary plugins2Artifact = new ArtifactSummary("plugins", "2.0.0");
    // get plugin types, should be the same for both
    Set<String> expectedTypes = Sets.newHashSet("dummy", "callable");
    Set<String> actualTypes = getPluginTypes(wordCount1Id);
    Assert.assertEquals(expectedTypes, actualTypes);
    actualTypes = getPluginTypes(wordCount2Id);
    Assert.assertEquals(expectedTypes, actualTypes);
    // get plugin summaries. wordcount1 should see plugins from both plugin artifacts
    Set<PluginSummary> expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins1Artifact), new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact));
    Set<PluginSummary> actualSummaries = getPluginSummaries(wordCount1Id, "dummy");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins1Artifact), new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount1Id, "callable");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    // wordcount2 should only see plugins from plugins2 artifact
    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount2Id, "dummy");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact));
    actualSummaries = getPluginSummaries(wordCount2Id, "callable");
    Assert.assertEquals(expectedSummaries, actualSummaries);
    // get plugin info. Again, wordcount1 should see plugins from both artifacts
    Map<String, PluginPropertyField> p1Properties = ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true));
    Map<String, PluginPropertyField> p2Properties = ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false));
    Set<PluginInfo> expectedInfos = Sets.newHashSet(new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins1Artifact, p1Properties, new HashSet<String>()), new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact, p1Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "dummy", "Plugin1"));
    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins1Artifact, p2Properties, new HashSet<String>()), new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact, p2Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "callable", "Plugin2"));
    // while wordcount2 should only see plugins from plugins2 artifact
    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact, p1Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "dummy", "Plugin1"));
    expectedInfos = Sets.newHashSet(new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact, p2Properties, new HashSet<String>()));
    Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "callable", "Plugin2"));
}
Also used : Plugin2(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin2) Plugin1(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin1) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) WordCountApp(co.cask.cdap.WordCountApp) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginSummary(co.cask.cdap.proto.artifact.PluginSummary) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Plugin1

use of co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin1 in project cdap by caskdata.

the class ArtifactHttpHandlerTest method testPluginNamespaceIsolation.

@Test
public void testPluginNamespaceIsolation() throws Exception {
    // add a system artifact. currently can't do this through the rest api (by design)
    // so bypass it and use the repository directly
    Id.Artifact systemId = Id.Artifact.from(Id.Namespace.SYSTEM, "wordcount", "1.0.0");
    File systemArtifact = buildAppArtifact(WordCountApp.class, "wordcount-1.0.0.jar");
    artifactRepository.addArtifact(systemId, systemArtifact, Sets.<ArtifactRange>newHashSet());
    Set<ArtifactRange> parents = Sets.newHashSet(new ArtifactRange(systemId.getNamespace().getId(), systemId.getName(), systemId.getVersion(), true, systemId.getVersion(), true));
    Id.Namespace namespace1 = Id.Namespace.from("ns1");
    Id.Namespace namespace2 = Id.Namespace.from("ns2");
    createNamespace(namespace1.getId());
    createNamespace(namespace2.getId());
    try {
        // add some plugins in namespace1. Will contain Plugin1 and Plugin2
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
        Id.Artifact pluginsId1 = Id.Artifact.from(namespace1, "plugins1", "1.0.0");
        Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(pluginsId1, Plugin1.class, manifest, parents).getStatusLine().getStatusCode());
        // add some plugins in namespace2. Will contain Plugin1 and Plugin2
        manifest = new Manifest();
        manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
        Id.Artifact pluginsId2 = Id.Artifact.from(namespace2, "plugins2", "1.0.0");
        Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(pluginsId2, Plugin1.class, manifest, parents).getStatusLine().getStatusCode());
        ArtifactSummary artifact1 = new ArtifactSummary(pluginsId1.getName(), pluginsId1.getVersion().getVersion(), ArtifactScope.USER);
        ArtifactSummary artifact2 = new ArtifactSummary(pluginsId2.getName(), pluginsId2.getVersion().getVersion(), ArtifactScope.USER);
        PluginSummary summary1Namespace1 = new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact1);
        PluginSummary summary2Namespace1 = new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact1);
        PluginSummary summary1Namespace2 = new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact2);
        PluginSummary summary2Namespace2 = new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact2);
        PluginInfo info1Namespace1 = new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact1, ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true)), new HashSet<String>());
        PluginInfo info2Namespace1 = new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact1, ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false)), new HashSet<String>());
        PluginInfo info1Namespace2 = new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact2, ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true)), new HashSet<String>());
        PluginInfo info2Namespace2 = new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact2, ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false)), new HashSet<String>());
        Id.Artifact namespace1Artifact = Id.Artifact.from(namespace1, systemId.getName(), systemId.getVersion());
        Id.Artifact namespace2Artifact = Id.Artifact.from(namespace2, systemId.getName(), systemId.getVersion());
        // should see same types in both namespaces
        Assert.assertEquals(ImmutableSet.of("dummy", "callable"), getPluginTypes(namespace1Artifact, ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of("dummy", "callable"), getPluginTypes(namespace2Artifact, ArtifactScope.SYSTEM));
        // should see that plugins in namespace1 come only from the namespace1 artifact
        Assert.assertEquals(ImmutableSet.of(summary1Namespace1), getPluginSummaries(namespace1Artifact, "dummy", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(summary2Namespace1), getPluginSummaries(namespace1Artifact, "callable", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info1Namespace1), getPluginInfos(namespace1Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info2Namespace1), getPluginInfos(namespace1Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));
        // should see that plugins in namespace2 come only from the namespace2 artifact
        Assert.assertEquals(ImmutableSet.of(summary1Namespace2), getPluginSummaries(namespace2Artifact, "dummy", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(summary2Namespace2), getPluginSummaries(namespace2Artifact, "callable", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info1Namespace2), getPluginInfos(namespace2Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
        Assert.assertEquals(ImmutableSet.of(info2Namespace2), getPluginInfos(namespace2Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));
    } finally {
        deleteNamespace("iso1");
        deleteNamespace("iso2");
    }
}
Also used : Plugin2(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin2) Plugin1(co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin1) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginSummary(co.cask.cdap.proto.artifact.PluginSummary) File(java.io.File) Test(org.junit.Test)

Aggregations

ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)2 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)2 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)2 Plugin1 (co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin1)2 Plugin2 (co.cask.cdap.internal.app.runtime.artifact.plugin.Plugin2)2 Id (co.cask.cdap.proto.Id)2 PluginInfo (co.cask.cdap.proto.artifact.PluginInfo)2 PluginSummary (co.cask.cdap.proto.artifact.PluginSummary)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 Manifest (java.util.jar.Manifest)2 Test (org.junit.Test)2 WordCountApp (co.cask.cdap.WordCountApp)1 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)1 File (java.io.File)1 HashSet (java.util.HashSet)1