Search in sources :

Example 1 with ArtifactVersionRange

use of co.cask.cdap.api.artifact.ArtifactVersionRange 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 2 with ArtifactVersionRange

use of co.cask.cdap.api.artifact.ArtifactVersionRange in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactPlugin.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/extensions/{plugin-type}/plugins/{plugin-name}")
public void getArtifactPlugin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @PathParam("plugin-name") String pluginName, @QueryParam("scope") @DefaultValue("user") final String scope, @QueryParam("artifactName") final String pluginArtifactName, @QueryParam("artifactVersion") String pluginVersion, @QueryParam("artifactScope") final String pluginScope, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException, InvalidArtifactRangeException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    final NamespaceId pluginArtifactNamespace = validateAndGetScopedNamespace(namespace, pluginScope);
    ArtifactId parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
    int limitNumber = Integer.valueOf(limit);
    limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    Predicate<ArtifactId> predicate = new Predicate<ArtifactId>() {

        @Override
        public boolean apply(ArtifactId input) {
            // by default, the scoped namespace is for USER scope
            return (((pluginScope == null && NamespaceId.SYSTEM.equals(input.getParent())) || pluginArtifactNamespace.equals(input.getParent())) && (pluginArtifactName == null || pluginArtifactName.equals(input.getArtifact())) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
        }
    };
    try {
        SortedMap<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(namespace, Id.Artifact.fromEntityId(parentArtifactId), pluginType, pluginName, predicate, limitNumber, sortOrder);
        List<PluginInfo> pluginInfos = Lists.newArrayList();
        // flatten the map
        for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
            ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
            ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
            PluginClass pluginClass = pluginsEntry.getValue();
            pluginInfos.add(new PluginInfo(pluginClass, pluginArtifactSummary));
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(pluginInfos));
    } catch (PluginNotExistsException e) {
        responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", parentArtifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactVersionRange(co.cask.cdap.api.artifact.ArtifactVersionRange) IOException(java.io.IOException) PluginEndpoint(co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint) ArtifactSortOrder(co.cask.cdap.proto.artifact.ArtifactSortOrder) Predicate(com.google.common.base.Predicate) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with ArtifactVersionRange

use of co.cask.cdap.api.artifact.ArtifactVersionRange in project cdap by caskdata.

the class ArtifactSelectorProvider method getArtifactSelector.

/**
 * Gets the corresponding {@link ArtifactSelector} for this config.
 * Validates that any given scope, name, and version are all valid or null. The scope must be an
 * {@link ArtifactScope}, the version must be an {@link ArtifactVersion}, and the name only contains
 * alphanumeric, '-', or '_'. Also checks that at least one field is non-null.
 *
 * @return an {@link ArtifactSelector} using these config settings
 * @throws IllegalArgumentException if any one of the fields are invalid
 */
private ArtifactSelector getArtifactSelector(ArtifactSelectorConfig config) {
    String name = config.getName();
    if (name != null && !nameMatcher.matchesAllOf(name)) {
        throw new IllegalArgumentException(String.format("'%s' is an invalid artifact name. " + "Must contain only alphanumeric, '-', '.', or '_' characters.", name));
    }
    String version = config.getVersion();
    ArtifactVersionRange range;
    try {
        range = version == null ? null : ArtifactVersionRange.parse(version);
    } catch (InvalidArtifactRangeException e) {
        throw new IllegalArgumentException(String.format("%s is an invalid artifact version." + "Must be an exact version or a version range " + "with a lower and upper bound.", version));
    }
    String scope = config.getScope();
    ArtifactScope artifactScope = scope == null ? null : ArtifactScope.valueOf(scope.toUpperCase());
    return new ArtifactSelector(pluginType, pluginName, artifactScope, name, range);
}
Also used : ArtifactScope(co.cask.cdap.api.artifact.ArtifactScope) ArtifactVersionRange(co.cask.cdap.api.artifact.ArtifactVersionRange) InvalidArtifactRangeException(co.cask.cdap.api.artifact.InvalidArtifactRangeException)

Aggregations

ArtifactVersionRange (co.cask.cdap.api.artifact.ArtifactVersionRange)3 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)2 PluginClass (co.cask.cdap.api.plugin.PluginClass)2 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)1 ArtifactScope (co.cask.cdap.api.artifact.ArtifactScope)1 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)1 InvalidArtifactRangeException (co.cask.cdap.api.artifact.InvalidArtifactRangeException)1 ArtifactDescriptor (co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor)1 PluginEndpoint (co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint)1 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)1 ArtifactSortOrder (co.cask.cdap.proto.artifact.ArtifactSortOrder)1 PluginInfo (co.cask.cdap.proto.artifact.PluginInfo)1 ArtifactId (co.cask.cdap.proto.id.ArtifactId)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 Predicate (com.google.common.base.Predicate)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1