Search in sources :

Example 36 with ArtifactRange

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

the class ArtifactHttpHandlerTest method testSystemArtifacts.

@Test
public void testSystemArtifacts() throws Exception {
    // add the app in the default namespace
    File systemArtifact = buildAppArtifact(WordCountApp.class, "wordcount-1.0.0.jar");
    Id.Artifact defaultId = Id.Artifact.from(Id.Namespace.DEFAULT, "wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), addArtifact(defaultId, Files.newInputStreamSupplier(systemArtifact), null).getStatusLine().getStatusCode());
    // 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");
    artifactRepository.addArtifact(systemId, systemArtifact, new HashSet<ArtifactRange>());
    // test get /artifacts
    Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER), new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    Set<ArtifactSummary> actualArtifacts = getArtifacts(Id.Namespace.DEFAULT);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts?scope=system
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    actualArtifacts = getArtifacts(Id.Namespace.DEFAULT, ArtifactScope.SYSTEM);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts?scope=user
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER));
    actualArtifacts = getArtifacts(Id.Namespace.DEFAULT, ArtifactScope.USER);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount?scope=user
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER));
    actualArtifacts = getArtifacts(Id.Namespace.DEFAULT, "wordcount", ArtifactScope.USER);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount?scope=system
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    actualArtifacts = getArtifacts(Id.Namespace.DEFAULT, "wordcount", ArtifactScope.SYSTEM);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount/versions/1.0.0?scope=user
    ArtifactInfo actualInfo = getArtifact(defaultId, ArtifactScope.USER);
    Assert.assertEquals("wordcount", actualInfo.getName());
    Assert.assertEquals("1.0.0", actualInfo.getVersion());
    // test get /artifacts/wordcount/versions/1.0.0?scope=system
    actualInfo = getArtifact(defaultId, ArtifactScope.SYSTEM);
    Assert.assertEquals("wordcount", actualInfo.getName());
    Assert.assertEquals("1.0.0", actualInfo.getVersion());
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) File(java.io.File) Test(org.junit.Test)

Example 37 with ArtifactRange

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

the class ArtifactHttpHandlerTest method invalidInputsToPluginEndpoint.

@Test
public void invalidInputsToPluginEndpoint() 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());
    // test plugin with endpoint that throws IllegalArgumentException
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, PluginWithPojo.class.getPackage().getName());
    Id.Artifact pluginsId = 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(pluginsId, PluginWithPojo.class, manifest, plugins5Parents).getStatusLine().getStatusCode());
    // this call should throw IllegalArgumentException
    String response = callPluginMethod(pluginsId, "interactive", "aggregator", "throwException", "testString", ArtifactScope.USER, 400).getResponseBodyAsString();
    Assert.assertEquals("Invalid user inputs: testString", response);
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) WordCountApp(co.cask.cdap.WordCountApp) PluginWithPojo(co.cask.cdap.internal.app.runtime.artifact.plugin.p5.PluginWithPojo) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Manifest(java.util.jar.Manifest) Test(org.junit.Test)

Example 38 with ArtifactRange

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

the class ArtifactConfigReaderTest method testInvalidParentNamespace.

@Test(expected = InvalidArtifactException.class)
public void testInvalidParentNamespace() throws IOException, InvalidArtifactException {
    ArtifactConfig badConfig = new ArtifactConfig(ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "b", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"))), ImmutableSet.<PluginClass>of(), ImmutableMap.<String, String>of());
    File configFile = new File(tmpFolder.newFolder(), "r1-1.0.0.json");
    try (BufferedWriter writer = Files.newWriter(configFile, Charsets.UTF_8)) {
        writer.write(badConfig.toString());
    }
    configReader.read(Id.Namespace.SYSTEM, configFile);
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 39 with ArtifactRange

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

the class ArtifactConfigReaderTest method testRead.

@Test
public void testRead() throws IOException, InvalidArtifactException {
    ArtifactConfig validConfig = new ArtifactConfig(ImmutableSet.of(new ArtifactRange(NamespaceId.SYSTEM.getNamespace(), "a", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")), new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "b", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"))), ImmutableSet.of(new PluginClass("type", "name", "desc", "classname", null, ImmutableMap.of("x", new PluginPropertyField("x", "some field", "int", true, false), "y", new PluginPropertyField("y", "some other field", "string", false, false)))), ImmutableMap.of("k1", "v1", "k2", "v2"));
    File configFile = new File(tmpFolder.newFolder(), "r1-1.0.0.json");
    try (BufferedWriter writer = Files.newWriter(configFile, Charsets.UTF_8)) {
        writer.write(validConfig.toString());
    }
    Assert.assertEquals(validConfig, configReader.read(Id.Namespace.DEFAULT, configFile));
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) PluginClass(co.cask.cdap.api.plugin.PluginClass) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 40 with ArtifactRange

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

the class ArtifactRepository method validateParentSet.

/**
   * Validates the parents of an artifact. Checks that each artifact only appears with a single version range.
   *
   * @param parents the set of parent ranges to validate
   * @throws InvalidArtifactException if there is more than one version range for an artifact
   */
@VisibleForTesting
static void validateParentSet(Id.Artifact artifactId, Set<ArtifactRange> parents) throws InvalidArtifactException {
    boolean isInvalid = false;
    StringBuilder errMsg = new StringBuilder("Invalid parents field.");
    // check for multiple version ranges for the same artifact.
    // ex: "parents": [ "etlbatch[1.0.0,2.0.0)", "etlbatch[3.0.0,4.0.0)" ]
    Set<String> parentNames = new HashSet<>();
    // keep track of dupes so that we don't have repeat error messages if there are more than 2 ranges for a name
    Set<String> dupes = new HashSet<>();
    for (ArtifactRange parent : parents) {
        String parentName = parent.getName();
        if (!parentNames.add(parentName) && !dupes.contains(parentName)) {
            errMsg.append(" Only one version range for parent '");
            errMsg.append(parentName);
            errMsg.append("' can be present.");
            dupes.add(parentName);
            isInvalid = true;
        }
        if (artifactId.getName().equals(parentName) && artifactId.getNamespace().toEntityId().getNamespace().equals(parent.getNamespace())) {
            throw new InvalidArtifactException(String.format("Invalid parent '%s' for artifact '%s'. An artifact cannot extend itself.", parent, artifactId));
        }
    }
    // "Invalid parents. Only one version range for parent 'etlbatch' can be present."
    if (isInvalid) {
        throw new InvalidArtifactException(errMsg.toString());
    }
}
Also used : ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)46 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)33 Test (org.junit.Test)31 NamespaceId (co.cask.cdap.proto.id.NamespaceId)28 Id (co.cask.cdap.proto.Id)24 PluginClass (co.cask.cdap.api.plugin.PluginClass)17 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)13 ArtifactId (co.cask.cdap.proto.id.ArtifactId)13 File (java.io.File)11 Manifest (java.util.jar.Manifest)11 HashSet (java.util.HashSet)9 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)8 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)7 ImmutableSet (com.google.common.collect.ImmutableSet)7 Set (java.util.Set)7 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)6 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)5 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)5 ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)4 IOException (java.io.IOException)4