Search in sources :

Example 41 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ApplicationClientTestRun method testAppUpdate.

@Test
public void testAppUpdate() throws Exception {
    String artifactName = "cfg-programs";
    ArtifactId artifactIdV1 = NamespaceId.DEFAULT.artifact(artifactName, "1.0.0");
    ArtifactId artifactIdV2 = NamespaceId.DEFAULT.artifact(artifactName, "2.0.0");
    ApplicationId appId = NamespaceId.DEFAULT.app("ProgramsApp");
    artifactClient.add(NamespaceId.DEFAULT, artifactName, Files.newInputStreamSupplier(createAppJarFile(ConfigurableProgramsApp.class)), "1.0.0");
    artifactClient.add(NamespaceId.DEFAULT, artifactName, Files.newInputStreamSupplier(createAppJarFile(ConfigurableProgramsApp2.class)), "2.0.0");
    try {
        // deploy the app with just the worker
        ConfigurableProgramsApp.Programs conf = new ConfigurableProgramsApp.Programs(null, "worker1", "stream1", "dataset1");
        AppRequest<ConfigurableProgramsApp.Programs> request = new AppRequest<>(new ArtifactSummary(artifactIdV1.getArtifact(), artifactIdV1.getVersion()), conf);
        appClient.deploy(appId, request);
        // should only have the worker
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.FLOW).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.WORKER).size());
        // update to use just the flow
        conf = new ConfigurableProgramsApp.Programs("flow1", null, "stream1", "dataset1");
        request = new AppRequest<>(new ArtifactSummary(artifactIdV1.getArtifact(), artifactIdV1.getVersion()), conf);
        appClient.update(appId, request);
        // should only have the flow
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.WORKER).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.FLOW).size());
        // check nonexistent app is not found
        try {
            appClient.update(NamespaceId.DEFAULT.app("ghost"), request);
            Assert.fail();
        } catch (NotFoundException e) {
        // expected
        }
        // check different artifact name is invalid
        request = new AppRequest<>(new ArtifactSummary("ghost", artifactIdV1.getVersion()), conf);
        try {
            appClient.update(appId, request);
            Assert.fail();
        } catch (BadRequestException e) {
        // expected
        }
        // check nonexistent artifact is not found
        request = new AppRequest<>(new ArtifactSummary(artifactIdV1.getArtifact(), "0.0.1"), conf);
        try {
            appClient.update(appId, request);
            Assert.fail();
        } catch (NotFoundException e) {
        // expected
        }
        // update artifact version. This version uses a different app class with that can add a service
        ConfigurableProgramsApp2.Programs conf2 = new ConfigurableProgramsApp2.Programs(null, null, "stream1", "dataset1", "service2");
        AppRequest<ConfigurableProgramsApp2.Programs> request2 = new AppRequest<>(new ArtifactSummary(artifactIdV2.getArtifact(), artifactIdV2.getVersion()), conf2);
        appClient.update(appId, request2);
        // should only have a single service
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.WORKER).isEmpty());
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.FLOW).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.SERVICE).size());
    } finally {
        appClient.delete(appId);
        appClient.waitForDeleted(appId, 30, TimeUnit.SECONDS);
        artifactClient.delete(artifactIdV1);
        artifactClient.delete(artifactIdV2);
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ConfigurableProgramsApp(co.cask.cdap.client.app.ConfigurableProgramsApp) DatasetModuleNotFoundException(co.cask.cdap.common.DatasetModuleNotFoundException) DatasetNotFoundException(co.cask.cdap.common.DatasetNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) AppRequest(co.cask.cdap.proto.artifact.AppRequest) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ConfigurableProgramsApp2(co.cask.cdap.client.app.ConfigurableProgramsApp2) BadRequestException(co.cask.cdap.common.BadRequestException) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 42 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ArtifactClientTestRun method testAddSelfExtendingThrowsBadRequest.

@Test
public void testAddSelfExtendingThrowsBadRequest() throws Exception {
    try {
        artifactClient.add(NamespaceId.DEFAULT, "abc", DUMMY_SUPPLIER, "1.0.0", Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "abc", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"))));
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) BadRequestException(co.cask.cdap.common.BadRequestException) Test(org.junit.Test)

Example 43 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class MetadataHttpHandlerTestRun method testInvalidParams.

@Test
public void testInvalidParams() throws Exception {
    NamespaceId namespace = new NamespaceId("testInvalidParams");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
    try {
        EnumSet<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
        searchMetadata(namespace, "text", targets, AbstractSystemMetadataWriter.CREATION_TIME_KEY + " desc");
        Assert.fail("Expected not to be able to specify 'query' and 'sort' parameters.");
    } catch (BadRequestException expected) {
    // expected
    }
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 44 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class DefaultNamespaceAdmin method updateProperties.

@Override
public synchronized void updateProperties(NamespaceId namespaceId, NamespaceMeta namespaceMeta) throws Exception {
    if (!exists(namespaceId)) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    authorizationEnforcer.enforce(namespaceId, authenticationContext.getPrincipal(), Action.ADMIN);
    NamespaceMeta existingMeta = nsStore.get(namespaceId);
    // Already ensured that namespace exists, so namespace meta should not be null
    Preconditions.checkNotNull(existingMeta);
    NamespaceMeta.Builder builder = new NamespaceMeta.Builder(existingMeta);
    if (namespaceMeta.getDescription() != null) {
        builder.setDescription(namespaceMeta.getDescription());
    }
    NamespaceConfig config = namespaceMeta.getConfig();
    if (config != null && !Strings.isNullOrEmpty(config.getSchedulerQueueName())) {
        builder.setSchedulerQueueName(config.getSchedulerQueueName());
    }
    if (config != null) {
        builder.setExploreAsPrincipal(config.isExploreAsPrincipal());
    }
    Set<String> difference = existingMeta.getConfig().getDifference(config);
    if (!difference.isEmpty()) {
        throw new BadRequestException(String.format("Mappings %s for namespace %s cannot be updated once the namespace " + "is created.", difference, namespaceId));
    }
    NamespaceMeta updatedMeta = builder.build();
    nsStore.update(updatedMeta);
    // refresh the cache with new meta
    namespaceMetaCache.refresh(namespaceId);
    LOG.info("Namespace {} updated with meta {}", namespaceId, updatedMeta);
}
Also used : NamespaceConfig(co.cask.cdap.proto.NamespaceConfig) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) CacheBuilder(com.google.common.cache.CacheBuilder) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 45 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class DefaultPreviewManager method getProgramIdFromRequest.

private ProgramId getProgramIdFromRequest(ApplicationId preview, AppRequest request) throws BadRequestException {
    if (request.getPreview() == null) {
        throw new BadRequestException("Preview config cannot be null");
    }
    String programName = request.getPreview().getProgramName();
    ProgramType programType = request.getPreview().getProgramType();
    if (programName == null || programType == null) {
        throw new IllegalArgumentException("ProgramName or ProgramType cannot be null.");
    }
    return preview.program(programType, programName);
}
Also used : BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)82 Path (javax.ws.rs.Path)29 NotFoundException (co.cask.cdap.common.NotFoundException)25 HttpResponse (co.cask.common.http.HttpResponse)17 JsonSyntaxException (com.google.gson.JsonSyntaxException)17 URL (java.net.URL)17 POST (javax.ws.rs.POST)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)16 IOException (java.io.IOException)15 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)13 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)12 HttpRequest (co.cask.common.http.HttpRequest)12 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ProgramType (co.cask.cdap.proto.ProgramType)7 ProgramId (co.cask.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6