Search in sources :

Example 31 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStoreTest method testDelete.

@Test
public void testDelete() throws Exception {
    // write an artifact with an app
    Id.Artifact parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
    ApplicationClass appClass = new ApplicationClass(InspectionApp.class.getName(), "", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(appClass).build());
    writeArtifact(parentId, artifactMeta, "parent contents");
    // write a child artifact that extends the parent with some plugins
    Id.Artifact childId = Id.Artifact.from(Id.Namespace.DEFAULT, "myplugins", "1.0.0");
    List<PluginClass> plugins = ImmutableList.of(new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()), new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(parentId.getNamespace().getId(), parentId.getName(), new ArtifactVersion("0.1.0"), new ArtifactVersion("2.0.0")));
    artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parents);
    writeArtifact(childId, artifactMeta, "child contents");
    // check parent has plugins from the child
    Assert.assertFalse(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    // delete the child artifact
    artifactStore.delete(childId);
    // shouldn't be able to get artifact detail
    try {
        artifactStore.getArtifact(childId);
        Assert.fail();
    } catch (ArtifactNotFoundException e) {
    // expected
    }
    // shouldn't see it in the list
    List<ArtifactDetail> artifactList = artifactStore.getArtifacts(parentId.getNamespace().toEntityId());
    Assert.assertEquals(1, artifactList.size());
    Assert.assertEquals(parentId.getName(), artifactList.get(0).getDescriptor().getArtifactId().getName());
    // shouldn't see any more plugins for parent
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    // delete parent
    artifactStore.delete(parentId);
    // nothing should be in the list
    Assert.assertTrue(artifactStore.getArtifacts(parentId.getNamespace().toEntityId()).isEmpty());
    // shouldn't be able to see app class either
    Assert.assertTrue(artifactStore.getApplicationClasses(NamespaceId.DEFAULT, appClass.getClassName()).isEmpty());
}
Also used : ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactId(co.cask.cdap.proto.id.ArtifactId) Id(co.cask.cdap.common.id.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) InspectionApp(co.cask.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp) Test(org.junit.Test)

Example 32 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStoreTest method testGetArtifacts.

@Test
public void testGetArtifacts() throws Exception {
    // add 1 version of another artifact1
    Id.Artifact artifact1V1 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact1", "1.0.0");
    String contents1V1 = "first contents v1";
    PluginClass plugin1V1 = new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of());
    ArtifactMeta meta1V1 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin1V1).build());
    writeArtifact(artifact1V1, meta1V1, contents1V1);
    // add 2 versions of an artifact2
    Id.Artifact artifact2V1 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact2", "0.1.0");
    Id.Artifact artifact2V2 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact2", "0.1.1");
    Id.Artifact artifact2V3 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact2", "0.1.1-SNAPSHOT");
    String contents2V1 = "second contents v1";
    String contents2V2 = "second contents v2";
    String contents2V3 = "second contents v3";
    PluginClass plugin2V1 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
    PluginClass plugin2V2 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
    PluginClass plugin2V3 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
    ArtifactMeta meta2V1 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2V1).build());
    ArtifactMeta meta2V2 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2V2).build());
    ArtifactMeta meta2V3 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2V3).build());
    writeArtifact(artifact2V1, meta2V1, contents2V1);
    writeArtifact(artifact2V2, meta2V2, contents2V2);
    writeArtifact(artifact2V3, meta2V3, contents2V3);
    // test we get 1 version of artifact1 and 2 versions of artifact2
    List<ArtifactDetail> artifact1Versions = artifactStore.getArtifacts(artifact1V1.getNamespace().toEntityId(), artifact1V1.getName(), Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(1, artifact1Versions.size());
    assertEqual(artifact1V1, meta1V1, contents1V1, artifact1Versions.get(0));
    List<ArtifactDetail> artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(3, artifact2Versions.size());
    assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(0));
    assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(1));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(2));
    // test get 2 versions of artifact 2
    artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 2, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(2, artifact2Versions.size());
    assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(0));
    assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(1));
    // test get sorted version of artifact 2
    artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 3, ArtifactSortOrder.DESC);
    Assert.assertEquals(3, artifact2Versions.size());
    assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(0));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(1));
    assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(2));
    // test get sorted and limited version of artifact 2
    artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 2, ArtifactSortOrder.DESC);
    Assert.assertEquals(2, artifact2Versions.size());
    assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(0));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(1));
    artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 3, ArtifactSortOrder.ASC);
    Assert.assertEquals(3, artifact2Versions.size());
    assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(0));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(1));
    assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(2));
    // test we get all 3 in the getArtifactSummaries() call for the namespace
    List<ArtifactDetail> artifactVersions = artifactStore.getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(4, artifactVersions.size());
    assertEqual(artifact1V1, meta1V1, contents1V1, artifactVersions.get(0));
    assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(1));
    assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(2));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(3));
    // test get using a range
    // this range should get everything
    ArtifactRange range = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "artifact2", new ArtifactVersion("0.1.0"), new ArtifactVersion("0.1.2"));
    artifactVersions = artifactStore.getArtifacts(range, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(3, artifactVersions.size());
    assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
    assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(1));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(2));
    // test get one version
    artifactVersions = artifactStore.getArtifacts(range, 1, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(1, artifactVersions.size());
    assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
    // test get sorted versions
    artifactVersions = artifactStore.getArtifacts(range, 3, ArtifactSortOrder.DESC);
    Assert.assertEquals(3, artifact2Versions.size());
    assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(0));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(1));
    assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(2));
    artifactVersions = artifactStore.getArtifacts(range, 3, ArtifactSortOrder.ASC);
    Assert.assertEquals(3, artifact2Versions.size());
    assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(1));
    assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(2));
    // this range should get just v0.1.1
    range = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "artifact2", new ArtifactVersion("0.1.1"), new ArtifactVersion("1.0.0"));
    artifactVersions = artifactStore.getArtifacts(range, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(1, artifactVersions.size());
    assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(0));
    // this range should get just v0.1.0 and v0.1.1-SNAPSHOT
    range = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "artifact2", new ArtifactVersion("0.0.0"), new ArtifactVersion("0.1.1"));
    artifactVersions = artifactStore.getArtifacts(range, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(2, artifactVersions.size());
    assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
    assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(1));
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) ArtifactId(co.cask.cdap.proto.id.ArtifactId) Id(co.cask.cdap.common.id.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 33 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStoreTest method testUniversalPlugin.

@Test
public void testUniversalPlugin() throws Exception {
    // First, deploy an artifact in the SYSTEM scope that doesn't have any plugin inside.
    ArtifactId artifactId = NamespaceId.SYSTEM.artifact("artifact", "1.0.0");
    writeArtifact(Id.Artifact.fromEntityId(artifactId), new ArtifactMeta(ArtifactClasses.builder().build()), "test");
    // Deploy an artifact that has a plugin in the DEFAULT scope, but without any parent artifact
    PluginClass pluginClass1 = new PluginClass("type1", "plugin1", "plugin1", "plugin1", null, Collections.emptyMap());
    ArtifactId pluginArtifactId1 = NamespaceId.DEFAULT.artifact("plugin-artifact1", "0.0.1");
    writeArtifact(Id.Artifact.fromEntityId(pluginArtifactId1), new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginClass1).build()), "test");
    // Get the available plugins for the artifact, should get the plugin1
    SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(artifactId));
    Assert.assertEquals(1, plugins.size());
    List<PluginClass> pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
    Assert.assertEquals(1, pluginsClasses.size());
    Assert.assertEquals(pluginClass1, pluginsClasses.get(0));
    // Get the available plugins for the plugin artifact itself, should also get the plugin1
    plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(pluginArtifactId1));
    Assert.assertEquals(1, plugins.size());
    pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
    Assert.assertEquals(1, pluginsClasses.size());
    Assert.assertEquals(pluginClass1, pluginsClasses.get(0));
    // Deploy an artifact that has a plugin in the DEFAULT scope with a parent artifact
    PluginClass pluginClass2 = new PluginClass("type2", "plugin2", "plugin2", "plugin2", null, Collections.emptyMap());
    ArtifactId pluginArtifactId2 = NamespaceId.DEFAULT.artifact("plugin-artifact2", "0.0.1");
    ArtifactRange parentArtifactRange = new ArtifactRange(artifactId.getNamespace(), artifactId.getArtifact(), ArtifactVersionRange.parse("[1.0.0,2.0.0)"));
    writeArtifact(Id.Artifact.fromEntityId(pluginArtifactId2), new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginClass2).build(), Collections.singleton(parentArtifactRange)), "test");
    // Get the available plugins for the artifact again, should get plugin1 and plugin2
    plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(artifactId));
    Assert.assertEquals(2, plugins.size());
    // Get and verify the plugins.
    pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
    Assert.assertEquals(2, pluginsClasses.size());
    // The plugins are sorted by the ArtifactDescriptor, hence order is guaranteed
    Assert.assertEquals(Arrays.asList(pluginClass1, pluginClass2), pluginsClasses);
    // Get available plugin by type.
    for (PluginClass pluginClass : Arrays.asList(pluginClass1, pluginClass2)) {
        plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(artifactId), pluginClass.getType());
        Assert.assertEquals(1, plugins.size());
        pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
        Assert.assertEquals(1, pluginsClasses.size());
        Assert.assertEquals(pluginClass, pluginsClasses.get(0));
    }
    // Get plugins by parent ArtifactRange
    for (PluginClass pluginClass : Arrays.asList(pluginClass1, pluginClass2)) {
        SortedMap<ArtifactDescriptor, PluginClass> result = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactRange, pluginClass.getType(), pluginClass.getName(), null, 10, ArtifactSortOrder.UNORDERED);
        Assert.assertEquals(1, result.size());
        Assert.assertEquals(pluginClass, result.values().stream().findFirst().get());
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) PluginClass(co.cask.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 34 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactSelectorTest method testSelection.

@Test
public void testSelection() {
    SortedMap<ArtifactId, PluginClass> plugins = new TreeMap<>();
    // doesn't matter what this is, since we only select on artifact id.
    PluginClass pluginClass = new PluginClass("type", "name", "desc", "com.company.class", "field", ImmutableMap.<String, PluginPropertyField>of());
    // put every combination of abc or def as name, 1.0.0 or 2.0.0 as version, and system or user as scope
    plugins.put(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.USER), pluginClass);
    plugins.put(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), pluginClass);
    // test scope only
    ArtifactSelector selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, null, null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, null, null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    // test name only
    selector = new ArtifactSelector("type", "name", null, "abc", null);
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", null, "def", null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", null, "xyz", null);
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test version only
    selector = new ArtifactSelector("type", "name", null, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", null, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", null, null, new ArtifactVersionRange(new ArtifactVersion("3.0.0"), true, new ArtifactVersion("3.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test range only
    selector = new ArtifactSelector("type", "name", null, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", null, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", null, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test name + version
    selector = new ArtifactSelector("type", "name", null, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", null, "abc", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", null, "def", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", null, "def", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", null, "xyz", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    try {
        selector = new ArtifactSelector("type", "name", null, "abc", new ArtifactVersionRange(new ArtifactVersion("3.0.0"), true, new ArtifactVersion("3.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test name + scope
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, "abc", null);
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, "def", null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, "abc", null);
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, "def", null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, "xyz", null);
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test version + scope
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("3.0.0"), true, new ArtifactVersion("3.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test name + range
    selector = new ArtifactSelector("type", "name", null, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", null, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", null, "def", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test scope + range
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test name + version + scope
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, "def", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", ArtifactScope.USER, "xyz", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
    // test name + scope + range
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, "def", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector("type", "name", ArtifactScope.USER, "def", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    try {
        selector = new ArtifactSelector("type", "name", ArtifactScope.SYSTEM, "abc", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
        selector.select(plugins);
    } catch (Exception e) {
    // expected
    }
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) ArtifactVersionRange(co.cask.cdap.api.artifact.ArtifactVersionRange) PluginClass(co.cask.cdap.api.plugin.PluginClass) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 35 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactRepository method validatePluginSet.

/**
   * Validates the set of plugins for an artifact. Checks that the pair of plugin type and name are unique among
   * all plugins in an artifact.
   *
   * @param plugins the set of plugins to validate
   * @throws InvalidArtifactException if there is more than one class with the same type and name
   */
@VisibleForTesting
static void validatePluginSet(Set<PluginClass> plugins) throws InvalidArtifactException {
    boolean isInvalid = false;
    StringBuilder errMsg = new StringBuilder("Invalid plugins field.");
    Set<ImmutablePair<String, String>> existingPlugins = new HashSet<>();
    Set<ImmutablePair<String, String>> dupes = new HashSet<>();
    for (PluginClass plugin : plugins) {
        ImmutablePair<String, String> typeAndName = ImmutablePair.of(plugin.getType(), plugin.getName());
        if (!existingPlugins.add(typeAndName) && !dupes.contains(typeAndName)) {
            errMsg.append(" Only one plugin with type '");
            errMsg.append(typeAndName.getFirst());
            errMsg.append("' and name '");
            errMsg.append(typeAndName.getSecond());
            errMsg.append("' can be present.");
            dupes.add(typeAndName);
            isInvalid = true;
        }
    }
    // "Invalid plugins. Only one plugin with type 'source' and name 'table' can be present."
    if (isInvalid) {
        throw new InvalidArtifactException(errMsg.toString());
    }
}
Also used : ImmutablePair(co.cask.cdap.common.utils.ImmutablePair) PluginClass(co.cask.cdap.api.plugin.PluginClass) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

PluginClass (co.cask.cdap.api.plugin.PluginClass)76 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)46 HashMap (java.util.HashMap)37 Test (org.junit.Test)24 ArtifactId (co.cask.cdap.proto.id.ArtifactId)22 NamespaceId (co.cask.cdap.proto.id.NamespaceId)21 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)19 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)17 Id (co.cask.cdap.common.id.Id)17 Set (java.util.Set)14 ImmutableSet (com.google.common.collect.ImmutableSet)13 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)11 Map (java.util.Map)11 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)10 SortedMap (java.util.SortedMap)10 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)9 File (java.io.File)9 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)8 HashSet (java.util.HashSet)8 ImmutableMap (com.google.common.collect.ImmutableMap)7