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());
}
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);
}
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);
}
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));
}
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());
}
}
Aggregations