Search in sources :

Example 1 with TestMacroEvaluator

use of co.cask.cdap.internal.app.runtime.plugin.TestMacroEvaluator 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

ArtifactId (co.cask.cdap.api.artifact.ArtifactId)1 Plugin (co.cask.cdap.api.plugin.Plugin)1 PluginClass (co.cask.cdap.api.plugin.PluginClass)1 PluginContext (co.cask.cdap.api.plugin.PluginContext)1 PluginProperties (co.cask.cdap.api.plugin.PluginProperties)1 TestPlugin (co.cask.cdap.internal.app.plugins.test.TestPlugin)1 DefaultPluginContext (co.cask.cdap.internal.app.runtime.DefaultPluginContext)1 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)1 TestMacroEvaluator (co.cask.cdap.internal.app.runtime.plugin.TestMacroEvaluator)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 Test (org.junit.Test)1