Search in sources :

Example 1 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method testUpdateSchedule.

private void testUpdateSchedule(ApplicationId appV2Id) throws Exception {
    ScheduleDetail updateDetail = new ScheduleDetail(AppWithSchedule.SCHEDULE, "updatedDescription", null, ImmutableMap.of("twoKey", "twoValue", "someKey", "newValue"), new TimeTrigger("0 4 * * *"), ImmutableList.of(new ConcurrencyConstraint(5)), null);
    // trying to update schedule for a non-existing app should fail
    HttpResponse response = updateSchedule(TEST_NAMESPACE1, "nonExistingApp", null, AppWithSchedule.SCHEDULE, updateDetail);
    Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), response.getResponseCode());
    // trying to update a non-existing schedule should fail
    ScheduleDetail nonExistingSchedule = new ScheduleDetail("NonExistingSchedule", "updatedDescription", null, ImmutableMap.of("twoKey", "twoValue"), new TimeTrigger("0 4 * * *"), ImmutableList.of(new ConcurrencyConstraint(5)), null);
    response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, "NonExistingSchedule", nonExistingSchedule);
    Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), response.getResponseCode());
    // should be able to update an existing schedule with a valid new time schedule
    response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, updateDetail);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    // verify that the schedule information for updated
    ScheduleDetail schedule = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE);
    Assert.assertEquals("updatedDescription", schedule.getDescription());
    Assert.assertEquals("0 4 * * *", ((TimeTrigger) schedule.getTrigger()).getCronExpression());
    Assert.assertEquals(new ProtoConstraint.ConcurrencyConstraint(5), schedule.getConstraints().get(0));
    // the properties should have been replaced
    Assert.assertEquals(2, schedule.getProperties().size());
    Assert.assertEquals("newValue", schedule.getProperties().get("someKey"));
    Assert.assertEquals("twoValue", schedule.getProperties().get("twoKey"));
    // the old property should not exist
    Assert.assertNull(schedule.getProperties().get("oneKey"));
    // the above update should not have affected the schedule for the other version of the app
    schedule = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, appV2Id.getVersion(), AppWithSchedule.SCHEDULE);
    Assert.assertNotEquals("updatedDescription", schedule.getDescription());
    Assert.assertEquals("0/15 * * * * ?", ((TimeTrigger) schedule.getTrigger()).getCronExpression());
    // try to update the schedule again but this time with property as null. It should retain the old properties
    ScheduleDetail scheduleDetail = new ScheduleDetail(AppWithSchedule.SCHEDULE, "updatedDescription", null, null, new ProtoTrigger.TimeTrigger("0 4 * * *"), null, null);
    response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, scheduleDetail);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    schedule = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE);
    Assert.assertEquals(2, schedule.getProperties().size());
    Assert.assertEquals("newValue", schedule.getProperties().get("someKey"));
    Assert.assertEquals("twoValue", schedule.getProperties().get("twoKey"));
    Assert.assertEquals(new ProtoConstraint.ConcurrencyConstraint(5), schedule.getConstraints().get(0));
}
Also used : ConcurrencyConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint) ProtoConstraint(io.cdap.cdap.proto.ProtoConstraint) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) HttpResponse(io.cdap.common.http.HttpResponse) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail)

Example 2 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method testNonExistentNamespace.

/**
 * Tests getting a non-existent namespace
 */
@Test
public void testNonExistentNamespace() throws Exception {
    String[] endpoints = { "spark", "services", "workers", "mapreduce", "workflows" };
    for (String endpoint : endpoints) {
        HttpResponse response = doGet("/v3/namespaces/default/" + endpoint);
        Assert.assertEquals(200, response.getResponseCode());
        response = doGet("/v3/namespaces/garbage/" + endpoint);
        Assert.assertEquals(404, response.getResponseCode());
    }
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) Test(org.junit.Test)

Example 3 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method getProgramSpecification.

private JsonObject getProgramSpecification(String namespace, String appId, String programType, String programId) throws Exception {
    HttpResponse response = requestProgramSpecification(namespace, appId, programType, programId);
    Assert.assertEquals(200, response.getResponseCode());
    return GSON.fromJson(response.getResponseBodyAsString(), JsonObject.class);
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse)

Example 4 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method historyStatusWithRetry.

private void historyStatusWithRetry(ProgramId program, ProgramRunStatus status, int size) throws Exception {
    String urlAppVersionPart = ApplicationId.DEFAULT_VERSION.equals(program.getVersion()) ? "" : "/versions/" + program.getVersion();
    String basePath = String.format("apps/%s%s/%s/%s/runs", program.getApplication(), urlAppVersionPart, program.getType().getCategoryName(), program.getProgram());
    String runsUrl = getVersionedAPIPath(basePath + "?status=" + status.name(), Constants.Gateway.API_VERSION_3_TOKEN, program.getNamespace());
    int trials = 0;
    while (trials++ < 5) {
        HttpResponse response = doGet(runsUrl);
        List<RunRecord> result = GSON.fromJson(response.getResponseBodyAsString(), LIST_OF_RUN_RECORD);
        if (result != null && result.size() >= size) {
            for (RunRecord m : result) {
                String runUrl = getVersionedAPIPath(basePath + "/" + m.getPid(), Constants.Gateway.API_VERSION_3_TOKEN, program.getNamespace());
                response = doGet(runUrl);
                RunRecord actualRunRecord = GSON.fromJson(response.getResponseBodyAsString(), RunRecord.class);
                Assert.assertEquals(m.getStatus(), actualRunRecord.getStatus());
            }
            break;
        }
        TimeUnit.SECONDS.sleep(1);
    }
    Assert.assertTrue(trials < 5);
}
Also used : RunRecord(io.cdap.cdap.proto.RunRecord) HttpResponse(io.cdap.common.http.HttpResponse) ConcurrencyConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint) ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) ProtoConstraint(io.cdap.cdap.proto.ProtoConstraint)

Example 5 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method testAddSchedule.

private void testAddSchedule(String scheduleName) throws Exception {
    String partitionScheduleName = scheduleName + "Partition";
    String orScheduleName = scheduleName + "Or";
    ProtoTrigger.TimeTrigger protoTime = new ProtoTrigger.TimeTrigger("0 * * * ?");
    ProtoTrigger.PartitionTrigger protoPartition = new ProtoTrigger.PartitionTrigger(NamespaceId.DEFAULT.dataset("data"), 5);
    ProtoTrigger.OrTrigger protoOr = ProtoTrigger.or(protoTime, protoPartition);
    String description = "Something";
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, AppWithSchedule.WORKFLOW_NAME);
    ImmutableMap<String, String> properties = ImmutableMap.of("a", "b", "c", "d");
    TimeTrigger timeTrigger = new TimeTrigger("0 * * * ?");
    ScheduleDetail timeDetail = new ScheduleDetail(TEST_NAMESPACE1, AppWithSchedule.NAME, ApplicationId.DEFAULT_VERSION, scheduleName, description, programInfo, properties, timeTrigger, Collections.emptyList(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS, null, null);
    PartitionTrigger partitionTrigger = new PartitionTrigger(protoPartition.getDataset(), protoPartition.getNumPartitions());
    ScheduleDetail expectedPartitionDetail = new ScheduleDetail(TEST_NAMESPACE1, AppWithSchedule.NAME, ApplicationId.DEFAULT_VERSION, partitionScheduleName, description, programInfo, properties, partitionTrigger, Collections.emptyList(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS, null, null);
    ScheduleDetail requestPartitionDetail = new ScheduleDetail(TEST_NAMESPACE1, AppWithSchedule.NAME, ApplicationId.DEFAULT_VERSION, partitionScheduleName, description, programInfo, properties, protoPartition, Collections.emptyList(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS, null, null);
    ScheduleDetail expectedOrDetail = new ScheduleDetail(TEST_NAMESPACE1, AppWithSchedule.NAME, ApplicationId.DEFAULT_VERSION, orScheduleName, description, programInfo, properties, new OrTrigger(timeTrigger, partitionTrigger), Collections.emptyList(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS, null, null);
    ScheduleDetail requestOrDetail = new ScheduleDetail(TEST_NAMESPACE1, AppWithSchedule.NAME, ApplicationId.DEFAULT_VERSION, orScheduleName, description, programInfo, properties, protoOr, Collections.emptyList(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS, null, null);
    // trying to add the schedule with different name in path param than schedule spec should fail
    HttpResponse response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, "differentName", timeDetail);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.code(), response.getResponseCode());
    // adding a schedule to a non-existing app should fail
    response = addSchedule(TEST_NAMESPACE1, "nonExistingApp", null, scheduleName, timeDetail);
    Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), response.getResponseCode());
    // adding a schedule to invalid type of program type should fail
    ScheduleDetail invalidScheduleDetail = new ScheduleDetail(scheduleName, "Something", new ScheduleProgramInfo(SchedulableProgramType.MAPREDUCE, AppWithSchedule.MAPREDUCE), properties, protoTime, Collections.emptyList(), TimeUnit.MINUTES.toMillis(1));
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, invalidScheduleDetail);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.code(), response.getResponseCode());
    // adding a schedule for a program that does not exist
    ScheduleDetail nonExistingDetail = new ScheduleDetail(TEST_NAMESPACE1, AppWithSchedule.NAME, ApplicationId.DEFAULT_VERSION, scheduleName, description, new ScheduleProgramInfo(SchedulableProgramType.MAPREDUCE, "nope"), properties, timeTrigger, Collections.emptyList(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS, null, null);
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, nonExistingDetail);
    Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), response.getResponseCode());
    // test adding a schedule
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, timeDetail);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, partitionScheduleName, requestPartitionDetail);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, orScheduleName, requestOrDetail);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    List<ScheduleDetail> schedules = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, AppWithSchedule.WORKFLOW_NAME);
    Assert.assertEquals(4, schedules.size());
    Assert.assertEquals(timeDetail, schedules.get(1));
    Assert.assertEquals(expectedOrDetail, schedules.get(2));
    Assert.assertEquals(expectedPartitionDetail, schedules.get(3));
    List<ScheduleDetail> schedulesForApp = listSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, null);
    Assert.assertEquals(schedules, schedulesForApp);
    // trying to add ScheduleDetail of the same schedule again should fail with AlreadyExistsException
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, timeDetail);
    Assert.assertEquals(HttpResponseStatus.CONFLICT.code(), response.getResponseCode());
    // although we should be able to add schedule to a different version of the app
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, scheduleName, timeDetail);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    // this should not have affected the schedules of the default version
    List<ScheduleDetail> scheds = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, AppWithSchedule.WORKFLOW_NAME);
    Assert.assertEquals(schedules, scheds);
    // there should be two schedules now for version 2
    List<ScheduleDetail> schedules2 = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, AppWithSchedule.WORKFLOW_NAME);
    Assert.assertEquals(2, schedules2.size());
    Assert.assertEquals(timeDetail, schedules2.get(1));
    List<ScheduleDetail> schedulesForApp2 = listSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2);
    Assert.assertEquals(schedules2, schedulesForApp2);
    // Add a schedule with no schedule name in spec
    ScheduleDetail detail2 = new ScheduleDetail(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, null, "Something 2", programInfo, properties, new TimeTrigger("0 * * * ?"), Collections.emptyList(), TimeUnit.HOURS.toMillis(6), null, null);
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, "schedule-100", detail2);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    ScheduleDetail detail100 = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, "schedule-100");
    Assert.assertEquals("schedule-100", detail100.getName());
    Assert.assertEquals(detail2.getTimeoutMillis(), detail100.getTimeoutMillis());
}
Also used : TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) OrTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.OrTrigger) HttpResponse(io.cdap.common.http.HttpResponse) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) PartitionTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger)

Aggregations

HttpResponse (io.cdap.common.http.HttpResponse)445 URL (java.net.URL)174 HttpRequest (io.cdap.common.http.HttpRequest)134 Test (org.junit.Test)129 NotFoundException (io.cdap.cdap.common.NotFoundException)47 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)37 IOException (java.io.IOException)36 ProgramId (io.cdap.cdap.proto.id.ProgramId)34 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)33 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)27 TypeToken (com.google.common.reflect.TypeToken)26 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)24 JsonObject (com.google.gson.JsonObject)21 RunRecord (io.cdap.cdap.proto.RunRecord)21 TypeToken (com.google.gson.reflect.TypeToken)20 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)20 HashMap (java.util.HashMap)20 Id (io.cdap.cdap.common.id.Id)19 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)19 BadRequestException (io.cdap.cdap.common.BadRequestException)18