in genie-web/src/integTest/java/com/netflix/genie/web/apis/rest/v3/controllers/CommandRestControllerIntegrationTest.java [1086:1237]
void canSetApplicationsForACommand() throws Exception {
this.createConfigResource(
new Command
.Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE_AND_ARGS)
.withId(ID)
.build(),
null
);
final String commandApplicationsAPI = COMMANDS_API + "/{id}/applications";
RestAssured
.given(this.getRequestSpecification())
.when()
.port(this.port)
.get(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.OK.value()))
.contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
.body("$", Matchers.empty());
final String placeholder = UUID.randomUUID().toString();
final String applicationId1 = UUID.randomUUID().toString();
final String applicationId2 = UUID.randomUUID().toString();
final String applicationId3 = UUID.randomUUID().toString();
this.createConfigResource(
new Application
.Builder(placeholder, placeholder, placeholder, ApplicationStatus.ACTIVE)
.withId(applicationId1)
.build(),
null
);
this.createConfigResource(
new Application
.Builder(placeholder, placeholder, placeholder, ApplicationStatus.ACTIVE)
.withId(applicationId2)
.build(),
null
);
this.createConfigResource(
new Application
.Builder(placeholder, placeholder, placeholder, ApplicationStatus.ACTIVE)
.withId(applicationId3)
.build(),
null
);
final RestDocumentationFilter setFilter = RestAssuredRestDocumentation.document(
"{class-name}/{method-name}/{step}/",
Snippets.CONTENT_TYPE_HEADER, // Request Headers
Snippets.ID_PATH_PARAM, // Path parameters
PayloadDocumentation.requestFields(
PayloadDocumentation
.fieldWithPath("[]")
.description("Array of application ids to replace the existing set of applications with")
.attributes(Snippets.EMPTY_CONSTRAINTS)
) // Request payload
);
RestAssured
.given(this.getRequestSpecification())
.filter(setFilter)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(GenieObjectMapper.getMapper().writeValueAsBytes(Lists.newArrayList(applicationId1, applicationId2)))
.when()
.port(this.port)
.put(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.NO_CONTENT.value()));
RestAssured
.given(this.getRequestSpecification())
.when()
.port(this.port)
.get(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.OK.value()))
.contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
.body("$", Matchers.hasSize(2))
.body("[0].id", Matchers.is(applicationId1))
.body("[1].id", Matchers.is(applicationId2));
// Should flip the order
RestAssured
.given(this.getRequestSpecification())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(GenieObjectMapper.getMapper().writeValueAsBytes(Lists.newArrayList(applicationId2, applicationId1)))
.when()
.port(this.port)
.put(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.NO_CONTENT.value()));
RestAssured
.given(this.getRequestSpecification())
.when()
.port(this.port)
.get(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.OK.value()))
.contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
.body("$", Matchers.hasSize(2))
.body("[0].id", Matchers.is(applicationId2))
.body("[1].id", Matchers.is(applicationId1));
// Should reorder and add a new one
RestAssured
.given(this.getRequestSpecification())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(
GenieObjectMapper
.getMapper()
.writeValueAsBytes(Lists.newArrayList(applicationId1, applicationId2, applicationId3))
)
.when()
.port(this.port)
.put(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.NO_CONTENT.value()));
RestAssured
.given(this.getRequestSpecification())
.when()
.port(this.port)
.get(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.OK.value()))
.contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
.body("$", Matchers.hasSize(3))
.body("[0].id", Matchers.is(applicationId1))
.body("[1].id", Matchers.is(applicationId2))
.body("[2].id", Matchers.is(applicationId3));
//Should clear applications
RestAssured
.given(this.getRequestSpecification())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(GenieObjectMapper.getMapper().writeValueAsBytes(Lists.newArrayList()))
.when()
.port(this.port)
.put(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.NO_CONTENT.value()));
RestAssured
.given(this.getRequestSpecification())
.when()
.port(this.port)
.get(commandApplicationsAPI, ID)
.then()
.statusCode(Matchers.is(HttpStatus.OK.value()))
.contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
.body("$", Matchers.empty());
}