use of co.cask.cdap.proto.artifact.PluginInfo 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"));
}
use of co.cask.cdap.proto.artifact.PluginInfo in project cdap by caskdata.
the class ClientUpgradeContext method getPluginArtifact.
@Nullable
@Override
public ArtifactSelectorConfig getPluginArtifact(String pluginType, String pluginName) {
try {
List<PluginInfo> plugins = artifactClient.getPluginInfo(artifactId, pluginType, pluginName, ArtifactScope.SYSTEM);
if (plugins.isEmpty()) {
return null;
}
// doesn't really matter which one we choose, as all of them should be valid.
// choosing the last one because that tends to be the one with the highest version.
// order is not guaranteed though.
ArtifactSummary chosenArtifact = plugins.get(plugins.size() - 1).getArtifact();
return new ArtifactSelectorConfig(chosenArtifact.getScope().name(), chosenArtifact.getName(), chosenArtifact.getVersion());
} catch (Exception e) {
return null;
}
}
use of co.cask.cdap.proto.artifact.PluginInfo 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");
}
}
use of co.cask.cdap.proto.artifact.PluginInfo 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());
}
use of co.cask.cdap.proto.artifact.PluginInfo in project cdap by caskdata.
the class ArtifactClient method getPluginInfo.
/**
* Gets all the plugins of the given type and name available to the given artifact.
*
* @param artifactId the id of the artifact to get
* @param pluginType the type of plugins to get
* @param pluginName the name of the plugins to get
* @param scope the scope of the artifact
* @return list of {@link PluginInfo}
* @throws NotFoundException if the given artifact does not exist or plugins for that artifact do not exist
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public List<PluginInfo> getPluginInfo(ArtifactId artifactId, String pluginType, String pluginName, ArtifactScope scope) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
String path = String.format("artifacts/%s/versions/%s/extensions/%s/plugins/%s?scope=%s", artifactId.getArtifact(), artifactId.getVersion(), pluginType, pluginName, scope.name());
URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(response.getResponseBodyAsString());
}
return ObjectResponse.<List<PluginInfo>>fromJsonBody(response, PLUGIN_INFOS_TYPE).getResponseObject();
}
Aggregations