use of co.cask.cdap.internal.app.runtime.artifact.plugin.p5.TestData in project cdap by caskdata.
the class ArtifactHttpHandlerTest method testPluginWithEndpoints.
@Test
public void testPluginWithEndpoints() 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, "testartifact", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.getCode(), addAppArtifact(wordCount2Id, WordCountApp.class).getStatusLine().getStatusCode());
// add some plugins.
// plugins-3.0.0 extends wordcount[1.0.0,2.0.0)
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, CallablePlugin.class.getPackage().getName());
Id.Artifact plugins3Id = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins3", "1.0.0");
Set<ArtifactRange> plugins3Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(plugins3Id, CallablePlugin.class, manifest, plugins3Parents).getStatusLine().getStatusCode());
Set<PluginInfo> expectedInfos = Sets.newHashSet(new PluginInfo("CallablePlugin", "interactive", "This is plugin with endpoint", CallablePlugin.class.getName(), new ArtifactSummary("plugins3", "1.0.0"), ImmutableMap.<String, PluginPropertyField>of(), ImmutableSet.<String>of("ping")));
Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "interactive", "CallablePlugin"));
// test plugin with endpoint
Assert.assertEquals("hello", GSON.fromJson(callPluginMethod(plugins3Id, "interactive", "CallablePlugin", "ping", "user", ArtifactScope.USER, 200).getResponseBodyAsString(), String.class));
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, CallingPlugin.class.getPackage().getName());
Id.Artifact plugins4Id = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins4", "1.0.0");
// we also add test artifact as parent for calling plugin,
// when callable plugin is loaded by calling plugin's method,
// it will try with "testArtifact" parent - wouldn't be able to load
// then it will load using "wordcount" parent and succeed
Set<ArtifactRange> plugins4Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")), new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "testartifact", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(plugins4Id, CallingPlugin.class, manifest, plugins4Parents).getStatusLine().getStatusCode());
// test plugin with endpoint having endpoint-context parameter
Assert.assertEquals("hi user", GSON.fromJson(callPluginMethod(plugins4Id, "interactive", "CallingPlugin", "ping", "user", ArtifactScope.USER, 200).getResponseBodyAsString(), String.class));
// test plugin that accepts list of data and aggregates and returns result map
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, PluginWithPojo.class.getPackage().getName());
Id.Artifact plugins5Id = Id.Artifact.from(Id.Namespace.DEFAULT, "aggregator", "1.0.0");
Set<ArtifactRange> plugins5Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(plugins5Id, PluginWithPojo.class, manifest, plugins5Parents).getStatusLine().getStatusCode());
// test plugin with endpoint having endpoint-context parameter
List<TestData> data = ImmutableList.of(new TestData(1, 10), new TestData(1, 20), new TestData(3, 15), new TestData(4, 5), new TestData(3, 15));
Map<Long, Long> expectedResult = new HashMap<>();
expectedResult.put(1L, 30L);
expectedResult.put(3L, 30L);
expectedResult.put(4L, 5L);
String response = callPluginMethod(plugins5Id, "interactive", "aggregator", "aggregate", GSON.toJson(data), ArtifactScope.USER, 200).getResponseBodyAsString();
Assert.assertEquals(expectedResult, GSON.fromJson(response, new TypeToken<Map<Long, Long>>() {
}.getType()));
// test calling a non-existent plugin method "bing"
callPluginMethod(plugins4Id, "interactive", "CallingPlugin", "bing", "user", ArtifactScope.USER, 404);
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, InvalidPlugin.class.getPackage().getName());
Id.Artifact invalidPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "invalid", "1.0.0");
Set<ArtifactRange> invalidPluginParents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), addPluginArtifact(invalidPluginId, InvalidPlugin.class, manifest, invalidPluginParents).getStatusLine().getStatusCode());
// test adding plugin artifact which has endpoint method containing 3 params (invalid)
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, InvalidPluginMethodParams.class.getPackage().getName());
invalidPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "invalidParams", "1.0.0");
invalidPluginParents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), addPluginArtifact(invalidPluginId, InvalidPluginMethodParams.class, manifest, invalidPluginParents).getStatusLine().getStatusCode());
// test adding plugin artifact which has endpoint method containing 2 params
// but 2nd param is not EndpointPluginContext (invalid)
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, InvalidPluginMethodParamType.class.getPackage().getName());
invalidPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "invalidParamType", "1.0.0");
invalidPluginParents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), addPluginArtifact(invalidPluginId, InvalidPluginMethodParamType.class, manifest, invalidPluginParents).getStatusLine().getStatusCode());
// test adding plugin artifact which has endpoint methods containing 2 params
// but 2nd param is implementation and extensions of EndpointPluginContext, should succeed
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, PluginEndpointContextTestPlugin.class.getPackage().getName());
Id.Artifact validPluginId = Id.Artifact.from(Id.Namespace.DEFAULT, "extender", "1.0.0");
Set<ArtifactRange> validPluginParents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), addPluginArtifact(validPluginId, PluginEndpointContextTestPlugin.class, manifest, validPluginParents).getStatusLine().getStatusCode());
}
Aggregations