Java源码示例:org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders
示例1
@Test
void getAllWorkbasketAccessItemsDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID_ACCESSITEMS,
"WBI:100000000000000000000000000000000001"))
.accept("application/hal+json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetAllWorkbasketAccessItemsDocTest",
responseFields(allWorkbasketAccessItemsFieldDescriptors)));
}
示例2
@Test
public void testDeleteStoryOnBoard() throws Exception {
UUID boardUuid = UUID.randomUUID();
UUID storyUuid = UUID.randomUUID();
when( this.service.deleteStory( any( UUID.class ), any( UUID.class ) ) ).thenReturn( ResponseEntity.accepted().build() );
this.mockMvc.perform( RestDocumentationRequestBuilders.delete( "/boards/{boardUuid}/stories/{storyUuid}", boardUuid, storyUuid ) )
.andDo( print() )
.andExpect( status().isAccepted() )
.andDo( document("delete-story",
pathParameters(
parameterWithName( "boardUuid" ).description( "The unique id of the board" ),
parameterWithName( "storyUuid" ).description( "The unique id of the story on the board to be deleted" )
)
));
verify( this.service, times( 1 ) ).deleteStory( any( UUID.class ), any( UUID.class ) );
}
示例3
@Test
void createWorkbasketDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.post(restHelper.toUrl(Mapping.URL_WORKBASKET))
.contentType("application/json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS)
.content(
"{\"key\" : \"asdasdasd\", \"name\" : \"Gruppenpostkorb KSC\", "
+ "\"domain\" : \"DOMAIN_A\", \"type\" : \"GROUP\", "
+ "\"created\" : \"2018-02-01T11:00:00Z\",\r\n"
+ " \"modified\" : \"2018-02-01T11:00:00Z\"}"))
.andExpect(MockMvcResultMatchers.status().isCreated())
.andDo(
MockMvcRestDocumentation.document(
"CreateWorkbasketDocTest",
requestFields(createWorkbasketFieldDescriptors),
responseFields(workbasketFieldDescriptors)))
.andReturn();
}
示例4
@Test
void getAllTaskCommentsForSpecificTaskDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_TASK_GET_POST_COMMENTS,
"TKI:000000000000000000000000000000000000"))
.accept(MediaTypes.HAL_JSON)
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetAllTaskCommentsForSpecificTaskDocTest",
responseFields(allTaskCommentsFieldDescriptors)));
}
示例5
@Test
public void appDefault() throws Exception {
registerApp(ApplicationType.source, "http", "1.2.0.RELEASE");
registerApp(ApplicationType.source, "http", "1.3.0.RELEASE");
this.mockMvc.perform(RestDocumentationRequestBuilders
.put("/apps/{type}/{name}/{version:.+}", ApplicationType.source, "http", "1.2.0.RELEASE").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isAccepted())
.andDo(
this.documentationHandler.document(
pathParameters(
parameterWithName("type").description("The type of application. One of " + Arrays.asList(ApplicationType.values())),
parameterWithName("name").description("The name of the application"),
parameterWithName("version").description("The version of the application")
)
)
);
unregisterApp(ApplicationType.source, "http", "1.2.0.RELEASE");
unregisterApp(ApplicationType.source, "http", "1.3.0.RELEASE");
}
示例6
/**
* This test demonstrates that Spring REST Docs can be used in a Spring Auto REST Docs setup.
* <p>
* All Spring Auto REST Docs snippets are created as well because of the setup in {@link MockMvcBase}.
* This is not a requirement and one could use a pure Spring REST Docs setup here.
* <p>
* The result of the manual documentation below ends up with section snippet using
* classic Spring REST Docs' path-parameters.adoc and response-fields.adoc.
* So there is no conflict and one is free to decide which snippets are included in the documentation.
* <p>
* RestDocumentationRequestBuilders.get is required for Spring REST Docs' pathParameters to work.
* Spring AUTO Rest Docs works with both MockMvcRequestBuilders and RestDocumentationRequestBuilders.
*/
@Test
public void getItemWithRestDocs() throws Exception {
mockMvc.perform(RestDocumentationRequestBuilders.get("/items/{id}", 1))
.andExpect(status().isOk())
.andDo(commonDocumentation(
pathParameters(
parameterWithName("id").description("ID of the item.")),
relaxedResponseFields(fieldWithPath("id")
.description("There are more fields but only the ID field is documented.")),
sectionBuilder().snippetNames(
AUTO_AUTHORIZATION,
PATH_PARAMETERS, // classic snippet
AUTO_REQUEST_PARAMETERS,
AUTO_REQUEST_FIELDS,
RESPONSE_FIELDS, // classic snippet
CURL_REQUEST, // classic snippet
HTTP_RESPONSE // classic snippet
).build()));
}
示例7
@Test
void commonSummaryResourceFieldsDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_CLASSIFICATIONS) + "?page=2&page-size=5")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"CommonSummaryResourceFields", responseFields(pagingFieldDescriptors)));
}
示例8
@Test
void getAllTasksDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_TASKS) + "?por.type=VNR&por.value=22334455")
.accept("application/hal+json")
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetAllTasksDocTest", responseFields(allTasksFieldDescriptors)));
}
示例9
@Test
void getSpecificTaskDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_TASKS_ID, "TKI:100000000000000000000000000000000000"))
.accept("application/hal+json")
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetSpecificTaskDocTest", responseFields(taskFieldDescriptors)));
}
示例10
@Test
void taskSubSetDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_TASKS_ID, "TKI:100000000000000000000000000000000000"))
.accept("application/hal+json")
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"TaskSubset", responseFields(taskSubsetFieldDescriptors)));
}
示例11
@Test
void updateTaskDocTest() throws Exception {
URL url =
new URL(restHelper.toUrl(Mapping.URL_TASKS_ID, "TKI:100000000000000000000000000000000000"));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", ADMIN_CREDENTIALS);
assertEquals(200, con.getResponseCode());
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), UTF_8));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
String originalTask = content.toString();
this.mockMvc
.perform(
RestDocumentationRequestBuilders.put(
restHelper.toUrl(
Mapping.URL_TASKS_ID, "TKI:100000000000000000000000000000000000"))
.header("Authorization", ADMIN_CREDENTIALS)
.contentType("application/json")
.content(originalTask))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"UpdateTaskDocTest",
requestFields(taskFieldDescriptors),
responseFields(taskFieldDescriptors)));
}
示例12
@Test
void selectAndClaimTaskDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.post(
restHelper.toUrl(Mapping.URL_TASKS_ID_SELECT_AND_CLAIM) + "?custom14=abc")
.accept("application/hal+json")
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"SelectAndClaimTaskDocTest", responseFields(taskFieldDescriptors)));
}
示例13
@Test
void commonFieldsDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_CLASSIFICATIONS_ID, "CLI:100000000000000000000000000000000009"))
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"CommonFields", responseFields(selfLinkFieldDescriptors)));
}
示例14
@Test
void getWorkbasketAccessItemsDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_WORKBASKET_ACCESS_ITEMS)
+ "?sort-by=workbasket-key&order=asc&access-ids=user-2-2")
.accept("application/hal+json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetWorkbasketAccessItemsDocTest", responseFields(accessItemFieldDescriptors)));
}
示例15
@Test
@DirtiesContext
void removeWorkbasketAccessItemsDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.delete(
restHelper.toUrl(Mapping.URL_WORKBASKET_ACCESS_ITEMS) + "?access-id=user-2-2")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isNoContent())
.andDo(MockMvcRestDocumentation.document("RemoveWorkbasketAccessItemsDocTest"));
}
示例16
@Test
void getAllWorkbasketsDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_WORKBASKET) + "?type=PERSONAL")
.accept("application/hal+json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetAllWorkbasketsDocTest", responseFields(allWorkbasketsFieldDescriptors)));
}
示例17
@Test
void getSpecificWorkbasketDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID, "WBI:100000000000000000000000000000000001"))
.accept("application/hal+json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetSpecificWorkbasketDocTest", responseFields(workbasketFieldDescriptors)));
}
示例18
@Test
void workbasketSubsetDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID, "WBI:100000000000000000000000000000000001"))
.accept("application/hal+json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"WorkbasketSubset", responseFields(workbasketSubsetFieldDescriptors)));
}
示例19
@Test
void removeWorkbasketAsDistributionTargetDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.delete(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID_DISTRIBUTION,
"WBI:100000000000000000000000000000000007"))
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isNoContent())
.andDo(MockMvcRestDocumentation.document("RemoveWorkbasketAsDistributionTargetDocTest"));
}
示例20
@Test
void getAllWorkbasketDistributionTargets() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID_DISTRIBUTION,
"WBI:100000000000000000000000000000000002"))
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetAllWorkbasketDistributionTargets",
responseFields(allDistributionTargetsFieldDescriptors)));
}
示例21
@Test
void updateWorkbasketDocTest() throws Exception {
URL url =
new URL(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID, "WBI:100000000000000000000000000000000002"));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", ADMIN_CREDENTIALS);
assertEquals(200, con.getResponseCode());
String modifiedWorkbasket;
try (BufferedReader in =
new BufferedReader(new InputStreamReader(con.getInputStream(), UTF_8))) {
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
con.disconnect();
modifiedWorkbasket = content.toString();
}
this.mockMvc
.perform(
RestDocumentationRequestBuilders.put(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID, "WBI:100000000000000000000000000000000002"))
.header("Authorization", TEAMLEAD_1_CREDENTIALS)
.contentType("application/json")
.content(modifiedWorkbasket))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"UpdateWorkbasketDocTest",
requestFields(workbasketFieldDescriptors),
responseFields(workbasketFieldDescriptors)));
}
示例22
@Test
void deleteWorkbasketDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.delete(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID, "WBI:100000000000000000000000000000000008"))
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isNoContent())
.andDo(MockMvcRestDocumentation.document("DeleteWorkbasketDocTest"));
}
示例23
@Test
void accessItemDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(
Mapping.URL_WORKBASKET_ID_ACCESSITEMS,
"WBI:100000000000000000000000000000000001"))
.accept("application/hal+json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"AccessItemsDocTest", responseFields(accessItemFieldDescriptors)));
}
示例24
@Test
void exportAllWorkbasketDefinitions() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_WORKBASKET_DEFINITIONS))
.accept("application/json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"ExportWorkbasketdefinitionsDocTest",
responseFields(workbasketDefinitionsFieldDescriptors)));
}
示例25
@Test
void getTaskStatusReport() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(restHelper.toUrl(Mapping.URL_MONITOR_TASKS_STATUS))
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetTaskStatusReportDocTest", responseFields(taskReportFieldDescriptors)));
}
示例26
@Test
void tasksWorkbasketReport() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_MONITOR_TASKS_WORKBASKET)
+ "?daysInPast=4&states=READY,CLAIMED,COMPLETED")
.accept("application/hal+json")
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetTaskWorkbasketReportDocTest", responseFields(taskReportFieldDescriptors)));
}
示例27
@Test
void tasksClassificationReport() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_MONITOR_TASKS_CLASSIFICATION))
.accept("application/hal+json")
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetTaskClassificationReportDocTest", responseFields(taskReportFieldDescriptors)));
}
示例28
@Test
void getTimestampReport() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(restHelper.toUrl(Mapping.URL_MONITOR_TIMESTAMP))
.accept("application/hal+json")
.header("Authorization", ADMIN_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetTimestampReportDocTest", responseFields(taskReportFieldDescriptors)));
}
示例29
@Test
void getAllDomainsDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(restHelper.toUrl(Mapping.URL_DOMAIN))
.accept("application/json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetAllDomainsDocTest", responseFields(allDomainsFieldDescriptors)));
}
示例30
@Test
void getAllClassificationCategoriesDocTest() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get(
restHelper.toUrl(Mapping.URL_CLASSIFICATION_CATEGORIES))
.accept("application/json")
.header("Authorization", TEAMLEAD_1_CREDENTIALS))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
MockMvcRestDocumentation.document(
"GetAllClassificationCategoriesDocTest",
responseFields(allClassificationCategoriesFieldDescriptors)));
}