List of usage examples for org.springframework.restdocs.operation.preprocess Preprocessors prettyPrint
public static OperationPreprocessor prettyPrint()
From source file:com.netflix.genie.web.controllers.ClusterRestControllerIntegrationTests.java
/** * Make sure can successfully delete all clusters. * * @throws Exception on a configuration error *//*from w w w.java 2s .c om*/ @Test public void canDeleteAllClusters() throws Exception { Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(0L)); this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.UP).build(), null); this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.OUT_OF_SERVICE).build(), null); this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.TERMINATED).build(), null); Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(3L)); final RestDocumentationResultHandler deleteResultHandler = MockMvcRestDocumentation.document( "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint())); this.mvc.perform(MockMvcRequestBuilders.delete(CLUSTERS_API)) .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(deleteResultHandler); Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(0L)); }
From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTests.java
private void checkJob(final int documentationId, final String id, final String commandArgs) throws Exception { final RestDocumentationResultHandler getResultHandler = MockMvcRestDocumentation.document( "{class-name}/" + documentationId + "/getJob/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM, // Path parameters Snippets.HAL_CONTENT_TYPE_HEADER, // Response Headers Snippets.getJobResponsePayload(), // Response fields Snippets.JOB_LINKS // Links );//from w w w . j av a 2 s. co m this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}", id)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)) .andExpect(MockMvcResultMatchers.jsonPath(ID_PATH, Matchers.is(id))) .andExpect(MockMvcResultMatchers.jsonPath(CREATED_PATH, Matchers.notNullValue())) .andExpect(MockMvcResultMatchers.jsonPath(UPDATED_PATH, Matchers.notNullValue())) .andExpect(MockMvcResultMatchers.jsonPath(VERSION_PATH, Matchers.is(JOB_VERSION))) .andExpect(MockMvcResultMatchers.jsonPath(USER_PATH, Matchers.is(JOB_USER))) .andExpect(MockMvcResultMatchers.jsonPath(NAME_PATH, Matchers.is(JOB_NAME))) .andExpect(MockMvcResultMatchers.jsonPath(DESCRIPTION_PATH, Matchers.is(JOB_DESCRIPTION))) .andExpect(MockMvcResultMatchers.jsonPath(COMMAND_ARGS_PATH, Matchers.is(commandArgs))) .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH, Matchers.is(JobStatus.SUCCEEDED.toString()))) .andExpect(MockMvcResultMatchers.jsonPath(STATUS_MESSAGE_PATH, Matchers.is(JOB_STATUS_MSG))) .andExpect(MockMvcResultMatchers.jsonPath(STARTED_PATH, Matchers.not(new Date(0)))) .andExpect(MockMvcResultMatchers.jsonPath(FINISHED_PATH, Matchers.notNullValue())) .andExpect(MockMvcResultMatchers.jsonPath(ARCHIVE_LOCATION_PATH, Matchers.isEmptyOrNullString())) .andExpect(MockMvcResultMatchers.jsonPath(CLUSTER_NAME_PATH, Matchers.is(CLUSTER1_NAME))) .andExpect(MockMvcResultMatchers.jsonPath(COMMAND_NAME_PATH, Matchers.is(CMD1_NAME))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH + ".*", Matchers.hasSize(8))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey(SELF_LINK_KEY))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey("request"))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey("execution"))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey("output"))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey("status"))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey("cluster"))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey("command"))) .andExpect(MockMvcResultMatchers.jsonPath(LINKS_PATH, Matchers.hasKey("applications"))) .andDo(getResultHandler); }
From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTests.java
private void checkJobStatus(final int documentationId, final String id) throws Exception { final RestDocumentationResultHandler getResultHandler = MockMvcRestDocumentation.document( "{class-name}/" + documentationId + "/getJobStatus/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM, // Path parameters Snippets.JSON_CONTENT_TYPE_HEADER, // Response Headers PayloadDocumentation.responseFields(PayloadDocumentation.fieldWithPath("status") .description("The job status. One of: " + Arrays.toString(JobStatus.values())) .attributes(Snippets.EMPTY_CONSTRAINTS)) // Response fields );//from ww w . jav a2 s . c o m this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}/status", id)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH, Matchers.is(JobStatus.SUCCEEDED.toString()))) .andDo(getResultHandler); }
From source file:com.netflix.genie.web.controllers.CommandRestControllerIntegrationTests.java
/** * Test to make sure that a command can be updated. * * @throws Exception on configuration errors *///ww w .j a va2s . com @Test public void canUpdateCommand() throws Exception { Assert.assertThat(this.jpaCommandRepository.count(), Matchers.is(0L)); this.createConfigResource( new Command.Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE, CHECK_DELAY).withId(ID) .build(), null); final String commandResource = COMMANDS_API + "/{id}"; final Command createdCommand = objectMapper .readValue(this.mvc.perform(MockMvcRequestBuilders.get(commandResource, ID)).andReturn() .getResponse().getContentAsByteArray(), CommandResource.class) .getContent(); Assert.assertThat(createdCommand.getStatus(), Matchers.is(CommandStatus.ACTIVE)); final Command.Builder updateCommand = new Command.Builder(createdCommand.getName(), createdCommand.getUser(), createdCommand.getVersion(), CommandStatus.INACTIVE, createdCommand.getExecutable(), createdCommand.getCheckDelay()) .withId(createdCommand.getId().orElseThrow(IllegalArgumentException::new)) .withCreated(createdCommand.getCreated().orElseThrow(IllegalArgumentException::new)) .withUpdated(createdCommand.getUpdated().orElseThrow(IllegalArgumentException::new)) .withTags(createdCommand.getTags()).withConfigs(createdCommand.getConfigs()) .withDependencies(createdCommand.getDependencies()); createdCommand.getDescription().ifPresent(updateCommand::withDescription); createdCommand.getSetupFile().ifPresent(updateCommand::withSetupFile); final RestDocumentationResultHandler updateResultHandler = MockMvcRestDocumentation.document( "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request header Snippets.ID_PATH_PARAM, // path parameters Snippets.getCommandRequestPayload() // payload fields ); this.mvc.perform( RestDocumentationRequestBuilders.put(commandResource, ID).contentType(MediaType.APPLICATION_JSON) .content(this.objectMapper.writeValueAsBytes(updateCommand.build()))) .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(updateResultHandler); this.mvc.perform(MockMvcRequestBuilders.get(commandResource, ID)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)) .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH, Matchers.is(CommandStatus.INACTIVE.toString()))); Assert.assertThat(this.jpaCommandRepository.count(), Matchers.is(1L)); }
From source file:com.netflix.genie.web.controllers.CommandRestControllerIntegrationTests.java
/** * Test to make sure that a command can be patched. * * @throws Exception on configuration errors *///from w ww . j a va2 s.c om @Test public void canPatchCommand() throws Exception { Assert.assertThat(this.jpaCommandRepository.count(), Matchers.is(0L)); final String id = this.createConfigResource( new Command.Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE, CHECK_DELAY).withId(ID) .build(), null); final String commandResource = COMMANDS_API + "/{id}"; this.mvc.perform(MockMvcRequestBuilders.get(commandResource, id)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath(NAME_PATH, Matchers.is(NAME))); final String newName = UUID.randomUUID().toString(); final String patchString = "[{ \"op\": \"replace\", \"path\": \"/name\", \"value\": \"" + newName + "\" }]"; final JsonPatch patch = JsonPatch.fromJson(this.objectMapper.readTree(patchString)); final RestDocumentationResultHandler patchResultHandler = MockMvcRestDocumentation.document( "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request headers Snippets.ID_PATH_PARAM, // path params Snippets.PATCH_FIELDS // request payload ); this.mvc.perform(RestDocumentationRequestBuilders.patch(commandResource, id) .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(patch))) .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(patchResultHandler); this.mvc.perform(MockMvcRequestBuilders.get(commandResource, id)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)) .andExpect(MockMvcResultMatchers.jsonPath(NAME_PATH, Matchers.is(newName))); Assert.assertThat(this.jpaCommandRepository.count(), Matchers.is(1L)); }
From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTests.java
private void checkJobOutput(final int documentationId, final String id) throws Exception { // Check getting a directory as json final RestDocumentationResultHandler jsonResultHandler = MockMvcRestDocumentation.document( "{class-name}/" + documentationId + "/getJobOutput/json/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath") .description("The path to the directory to get").optional()), // Path parameters HeaderDocumentation.requestHeaders(HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT) .description(MediaType.APPLICATION_JSON_VALUE).optional()), // Request header HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE) .description(MediaType.APPLICATION_JSON_VALUE)), // Response Headers Snippets.OUTPUT_DIRECTORY_FIELDS); this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}/output/{filePath}", id, "")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.jsonPath("parent", Matchers.isEmptyOrNullString())) .andExpect(MockMvcResultMatchers.jsonPath("$.directories[0].name", Matchers.is("genie/"))) .andExpect(MockMvcResultMatchers.jsonPath("$.files[0].name", Matchers.is("dep1"))) .andExpect(MockMvcResultMatchers.jsonPath("$.files[1].name", Matchers.is("jobsetupfile"))) .andExpect(MockMvcResultMatchers.jsonPath("$.files[2].name", Matchers.is("run"))) .andExpect(MockMvcResultMatchers.jsonPath("$.files[3].name", Matchers.is("stderr"))) .andExpect(MockMvcResultMatchers.jsonPath("$.files[4].name", Matchers.is("stdout"))) .andDo(jsonResultHandler);/*from w w w .j av a 2 s . com*/ // Check getting a directory as HTML final RestDocumentationResultHandler htmlResultHandler = MockMvcRestDocumentation.document( "{class-name}/" + documentationId + "/getJobOutput/html/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath") .description("The path to the directory to get").optional()), // Path parameters HeaderDocumentation.requestHeaders( HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT).description(MediaType.TEXT_HTML)), // Request header HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE) .description(MediaType.TEXT_HTML)) // Response Headers ); this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}/output/{filePath}", id, "") .accept(MediaType.TEXT_HTML)).andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.TEXT_HTML)) .andDo(htmlResultHandler); // Check getting a file // Check getting a directory as HTML final RestDocumentationResultHandler fileResultHandler = MockMvcRestDocumentation.document( "{class-name}/" + documentationId + "/getJobOutput/file/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath") .description("The path to the file to get").optional()), // Path parameters HeaderDocumentation.requestHeaders(HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT) .description(MediaType.ALL_VALUE).optional()), // Request header HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE) .description("The content type of the file being returned").optional()) // Response Headers ); // check that the generated run file is correct final String runShFileName = SystemUtils.IS_OS_LINUX ? "linux-runsh.txt" : "non-linux-runsh.txt"; final String runShFile = this.resourceLoader.getResource(BASE_DIR + runShFileName).getFile() .getAbsolutePath(); final String runFileContents = new String(Files.readAllBytes(Paths.get(runShFile)), "UTF-8"); final String jobWorkingDir = this.jobDirResource.getFile().getCanonicalPath() + FILE_DELIMITER + id; final String expectedRunScriptContent = this.getExpectedRunContents(runFileContents, jobWorkingDir, id); this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}/output/{filePath}", id, "run")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().string(expectedRunScriptContent)) .andDo(fileResultHandler); }
From source file:com.netflix.genie.web.controllers.ClusterRestControllerIntegrationTests.java
/** * Test to make sure that you can delete a cluster. * * @throws Exception on configuration error *///from w w w. j a v a 2 s . com @Test public void canDeleteACluster() throws Exception { Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(0L)); final String id1 = UUID.randomUUID().toString(); final String id2 = UUID.randomUUID().toString(); final String id3 = UUID.randomUUID().toString(); final String name1 = UUID.randomUUID().toString(); final String name2 = UUID.randomUUID().toString(); final String name3 = UUID.randomUUID().toString(); final String user1 = UUID.randomUUID().toString(); final String user2 = UUID.randomUUID().toString(); final String user3 = UUID.randomUUID().toString(); final String version1 = UUID.randomUUID().toString(); final String version2 = UUID.randomUUID().toString(); final String version3 = UUID.randomUUID().toString(); this.createConfigResource(new Cluster.Builder(name1, user1, version1, ClusterStatus.UP).withId(id1).build(), null); this.createConfigResource( new Cluster.Builder(name2, user2, version2, ClusterStatus.OUT_OF_SERVICE).withId(id2).build(), null); this.createConfigResource( new Cluster.Builder(name3, user3, version3, ClusterStatus.TERMINATED).withId(id3).build(), null); Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(3L)); final RestDocumentationResultHandler deleteResultHandler = MockMvcRestDocumentation.document( "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM // path parameters ); this.mvc.perform(RestDocumentationRequestBuilders.delete(CLUSTERS_API + "/{id}", id2)) .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(deleteResultHandler); this.mvc.perform(MockMvcRequestBuilders.get(CLUSTERS_API + "/{id}", id2)) .andExpect(MockMvcResultMatchers.status().isNotFound()); Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(2L)); }
From source file:com.netflix.genie.web.controllers.ApplicationRestControllerIntegrationTests.java
/** * Test to make sure that an application can be updated. * * @throws Exception on configuration errors *///w w w . j a v a 2 s . co m @Test public void canUpdateApplication() throws Exception { Assert.assertThat(this.jpaApplicationRepository.count(), Matchers.is(0L)); final String id = this.createConfigResource( new Application.Builder(NAME, USER, VERSION, ApplicationStatus.ACTIVE).withId(ID).build(), null); final String applicationResource = APPLICATIONS_API + "/{id}"; final Application createdApp = this.objectMapper .readValue(this.mvc.perform(MockMvcRequestBuilders.get(applicationResource, id)).andReturn() .getResponse().getContentAsByteArray(), ApplicationResource.class) .getContent(); Assert.assertThat(createdApp.getStatus(), Matchers.is(ApplicationStatus.ACTIVE)); final Application.Builder newStatusApp = new Application.Builder(createdApp.getName(), createdApp.getUser(), createdApp.getVersion(), ApplicationStatus.INACTIVE) .withId(createdApp.getId().orElseThrow(IllegalArgumentException::new)) .withCreated(createdApp.getCreated().orElseThrow(IllegalArgumentException::new)) .withUpdated(createdApp.getUpdated().orElseThrow(IllegalArgumentException::new)) .withTags(createdApp.getTags()).withConfigs(createdApp.getConfigs()) .withDependencies(createdApp.getDependencies()); createdApp.getDescription().ifPresent(newStatusApp::withDescription); createdApp.getSetupFile().ifPresent(newStatusApp::withSetupFile); final RestDocumentationResultHandler updateResultHandler = MockMvcRestDocumentation.document( "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request header Snippets.ID_PATH_PARAM, // path parameters Snippets.getApplicationRequestPayload() // payload fields ); this.mvc.perform(RestDocumentationRequestBuilders.put(applicationResource, id) .contentType(MediaType.APPLICATION_JSON) .content(this.objectMapper.writeValueAsBytes(newStatusApp.build()))) .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(updateResultHandler); this.mvc.perform(MockMvcRequestBuilders.get(applicationResource, id)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)) .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH, Matchers.is(ApplicationStatus.INACTIVE.toString()))); Assert.assertThat(this.jpaApplicationRepository.count(), Matchers.is(1L)); }
From source file:com.netflix.genie.web.controllers.ClusterRestControllerIntegrationTests.java
/** * Test to make sure we can add configurations to the cluster after it is created. * * @throws Exception on configuration problems *//* w w w . j av a 2 s .co m*/ @Test public void canAddConfigsToCluster() throws Exception { Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(0L)); this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.UP).withId(ID).build(), null); final RestDocumentationResultHandler addResultHandler = MockMvcRestDocumentation.document( "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM, // path params Snippets.CONTENT_TYPE_HEADER, // request header PayloadDocumentation.requestFields(Snippets.CONFIG_FIELDS) // response fields ); final RestDocumentationResultHandler getResultHandler = MockMvcRestDocumentation.document( "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM, // path params Snippets.JSON_CONTENT_TYPE_HEADER, // response headers PayloadDocumentation.responseFields(Snippets.CONFIG_FIELDS) // response fields ); this.canAddElementsToResource(CLUSTERS_API + "/{id}/configs", ID, addResultHandler, getResultHandler); }