Search in sources :

Example 91 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.

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.newInputStream(createAppJarFile(ConfigurableProgramsApp.class).toPath()), "1.0.0");
    artifactClient.add(NamespaceId.DEFAULT, artifactName, () -> Files.newInputStream(createAppJarFile(ConfigurableProgramsApp2.class).toPath()), "2.0.0");
    try {
        // deploy the app with just the worker
        ConfigurableProgramsApp.Programs conf = new ConfigurableProgramsApp.Programs("worker1", null, "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.SERVICE).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.WORKER).size());
        // update to use just the service
        conf = new ConfigurableProgramsApp.Programs(null, "service", "dataset1");
        request = new AppRequest<>(new ArtifactSummary(artifactIdV1.getArtifact(), artifactIdV1.getVersion()), conf);
        appClient.update(appId, request);
        // should only have the service
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.WORKER).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.SERVICE).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 workflow
        ConfigurableProgramsApp2.Programs conf2 = new ConfigurableProgramsApp2.Programs(null, null, "workflow1", "dataset1");
        AppRequest<ConfigurableProgramsApp2.Programs> request2 = new AppRequest<>(new ArtifactSummary(artifactIdV2.getArtifact(), artifactIdV2.getVersion()), conf2);
        appClient.update(appId, request2);
        // should only have a single workflow
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.WORKER).isEmpty());
        Assert.assertTrue(appClient.listPrograms(appId, ProgramType.SERVICE).isEmpty());
        Assert.assertEquals(1, appClient.listPrograms(appId, ProgramType.WORKFLOW).size());
    } finally {
        appClient.delete(appId);
        appClient.waitForDeleted(appId, 30, TimeUnit.SECONDS);
        artifactClient.delete(artifactIdV1);
        artifactClient.delete(artifactIdV2);
    }
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ConfigurableProgramsApp(io.cdap.cdap.client.app.ConfigurableProgramsApp) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) DatasetModuleNotFoundException(io.cdap.cdap.common.DatasetModuleNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) ConfigurableProgramsApp2(io.cdap.cdap.client.app.ConfigurableProgramsApp2) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) BadRequestException(io.cdap.cdap.common.BadRequestException) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 92 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.

the class MetadataHttpHandlerTestRun method testInvalidSearchParams.

@Test
public void testInvalidSearchParams() throws Exception {
    // TODO (CDAP-14946): Find a better way to determine allowed combinations of search parameters
    NamespaceId namespace = new NamespaceId("invalid");
    Set<String> targets = Collections.emptySet();
    try {
        searchMetadata(namespace, "*", targets, MetadataConstants.ENTITY_NAME_KEY + " ascending");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with bad sort field
    try {
        searchMetadata(namespace, "*", targets, "name asc");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with bad sort order
    try {
        searchMetadata(namespace, "*", targets, MetadataConstants.ENTITY_NAME_KEY + " unknown");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with numCursors for relevance sort
    try {
        searchMetadata(NamespaceId.DEFAULT, "search*", targets, null, 0, Integer.MAX_VALUE, 1, null);
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
    // search with cursor for relevance sort
    try {
        searchMetadata(NamespaceId.DEFAULT, "search*", targets, null, 0, Integer.MAX_VALUE, 0, "cursor");
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
}
Also used : BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 93 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.

the class ApplicationClient method enableSchedule.

/**
 * Enable a schedule in an application.
 *
 * @param scheduleId the id of the schedule to be enabled
 */
public void enableSchedule(ScheduleId scheduleId) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException, BadRequestException {
    String path = String.format("apps/%s/versions/%s/program-type/schedules/program-id/%s/action/enable", scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
    HttpResponse response = restClient.execute(HttpMethod.PUT, config.resolveNamespacedURLV3(scheduleId.getParent().getParent(), path), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(scheduleId.getParent());
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
    }
}
Also used : ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException)

Example 94 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.

the class ApplicationClient method update.

/**
 * Update an existing app to use a different artifact version or config.
 *
 * @param appId the id of the application to update
 * @param updateRequest the request to update the application with
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NotFoundException if the app or requested artifact could not be found
 * @throws BadRequestException if the request is invalid
 */
public void update(ApplicationId appId, AppRequest<?> updateRequest) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(appId.getParent(), String.format("apps/%s/update", appId.getApplication()));
    HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(updateRequest)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException("app or app artifact");
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) URL(java.net.URL)

Example 95 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.

the class ApplicationClient method addSchedule.

/**
 * Add a schedule to an application.
 *
 * @param app the application
 * @param scheduleDetail the schedule to be added
 */
public void addSchedule(ApplicationId app, ScheduleDetail scheduleDetail) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException, BadRequestException {
    String path = String.format("apps/%s/versions/%s/schedules/%s", app.getApplication(), app.getVersion(), scheduleDetail.getName());
    HttpResponse response = restClient.execute(HttpMethod.PUT, config.resolveNamespacedURLV3(app.getParent(), path), GSON.toJson(scheduleDetail), ImmutableMap.<String, String>of(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(app);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
    }
}
Also used : ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException)

Aggregations

BadRequestException (io.cdap.cdap.common.BadRequestException)188 Path (javax.ws.rs.Path)68 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)54 IOException (java.io.IOException)46 JsonSyntaxException (com.google.gson.JsonSyntaxException)44 NotFoundException (io.cdap.cdap.common.NotFoundException)42 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)42 POST (javax.ws.rs.POST)42 HttpResponse (io.cdap.common.http.HttpResponse)36 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)34 URL (java.net.URL)34 ProgramType (io.cdap.cdap.proto.ProgramType)30 InputStreamReader (java.io.InputStreamReader)28 Reader (java.io.Reader)28 ArrayList (java.util.ArrayList)28 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)26 ProgramId (io.cdap.cdap.proto.id.ProgramId)26 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)24 GET (javax.ws.rs.GET)24 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)22