Search in sources :

Example 16 with Plugin

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

the class ArtifactRepositoryTest method testMacroPlugin.

@Test
public void testMacroPlugin() throws Exception {
    File pluginDir = TMP_FOLDER.newFolder();
    addPluginArtifact();
    SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = getPlugins();
    copyArtifacts(pluginDir, plugins);
    // set up test macro evaluator's substitutions
    Map<String, String> propertySubstitutions = ImmutableMap.<String, String>builder().put("expansiveHostname", "${hostname}/${path}:${port}").put("hostname", "${one}").put("path", "${two}").put("port", "${three}").put("one", "${host${hostScopeMacro}}").put("hostScopeMacro", "-local").put("host-local", "${l}${o}${c}${a}${l}${hostSuffix}").put("l", "l").put("o", "o").put("c", "c").put("a", "a").put("hostSuffix", "host").put("two", "${filename${fileTypeMacro}}").put("three", "${firstPortDigit}${secondPortDigit}").put("filename", "index").put("fileTypeMacro", "-html").put("filename-html", "index.html").put("filename-php", "index.php").put("firstPortDigit", "8").put("secondPortDigit", "0").put("aBoolean", "true").put("aByte", "101").put("aChar", "k").put("aDouble", "64.0").put("aFloat", "52.0").put("anInt", "42").put("aLong", "32").put("aShort", "81").build();
    // Instantiate the plugins and execute them
    try (PluginInstantiator instantiator = new PluginInstantiator(cConf, appClassLoader, pluginDir)) {
        for (Map.Entry<ArtifactDescriptor, Set<PluginClass>> entry : plugins.entrySet()) {
            for (PluginClass pluginClass : entry.getValue()) {
                Plugin pluginInfo = new Plugin(new ArrayList<ArtifactId>(), entry.getKey().getArtifactId(), pluginClass, PluginProperties.builder().add("class.name", TEST_EMPTY_CLASS).add("nullableLongFlag", "10").add("host", "${expansiveHostname}").add("aBoolean", "${aBoolean}").add("aByte", "${aByte}").add("aChar", "${aChar}").add("aDouble", "${aDouble}").add("anInt", "${anInt}").add("aFloat", "${aFloat}").add("aLong", "${aLong}").add("aShort", "${aShort}").build());
                TestMacroEvaluator testMacroEvaluator = new TestMacroEvaluator(propertySubstitutions, new HashMap<String, String>());
                Callable<String> plugin = instantiator.newInstance(pluginInfo, testMacroEvaluator);
                Assert.assertEquals("localhost/index.html:80,true,101,k,64.0,52.0,42,32,81", plugin.call());
                String pluginId = "5";
                PluginContext pluginContext = new DefaultPluginContext(instantiator, NamespaceId.DEFAULT.app("abc").worker("w"), ImmutableMap.of(pluginId, pluginInfo));
                PluginProperties resolvedProperties = pluginContext.getPluginProperties(pluginId, testMacroEvaluator);
                Map<String, String> expected = new HashMap<>();
                expected.put("class.name", TEST_EMPTY_CLASS);
                expected.put("nullableLongFlag", "10");
                expected.put("host", "localhost/index.html:80");
                expected.put("aBoolean", "true");
                expected.put("aByte", "101");
                expected.put("aChar", "k");
                expected.put("aDouble", "64.0");
                expected.put("anInt", "42");
                expected.put("aFloat", "52.0");
                expected.put("aLong", "32");
                expected.put("aShort", "81");
                Assert.assertEquals(expected, resolvedProperties.getProperties());
            }
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) TestMacroEvaluator(co.cask.cdap.internal.app.runtime.plugin.TestMacroEvaluator) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) PluginContext(co.cask.cdap.api.plugin.PluginContext) DefaultPluginContext(co.cask.cdap.internal.app.runtime.DefaultPluginContext) HashMap(java.util.HashMap) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) PluginClass(co.cask.cdap.api.plugin.PluginClass) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) PluginProperties(co.cask.cdap.api.plugin.PluginProperties) DefaultPluginContext(co.cask.cdap.internal.app.runtime.DefaultPluginContext) Plugin(co.cask.cdap.api.plugin.Plugin) TestPlugin(co.cask.cdap.internal.app.plugins.test.TestPlugin) Test(org.junit.Test)

Aggregations

Plugin (co.cask.cdap.api.plugin.Plugin)16 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)7 Map (java.util.Map)7 PluginClass (co.cask.cdap.api.plugin.PluginClass)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 File (java.io.File)4 HashSet (java.util.HashSet)4 TestPlugin (co.cask.cdap.internal.app.plugins.test.TestPlugin)3 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)3 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 SortedMap (java.util.SortedMap)3 Test (org.junit.Test)3 StreamSpecification (co.cask.cdap.api.data.stream.StreamSpecification)2 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)2