This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private RestDocumentationResultHandler document; | |
ConstrainedFields fields = new ConstrainedFields(Offer.class); | |
@Rule | |
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ConstrainedFields fields = new ConstrainedFields(Offer.class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static class ConstrainedFields { | |
private final ConstraintDescriptions constraintDescriptions; | |
ConstrainedFields(Class<?> input) { | |
this.constraintDescriptions = new ConstraintDescriptions(input); | |
} | |
private FieldDescriptor withPath(String path) { | |
return fieldWithPath(path).attributes(key("constraints").value(StringUtils |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void putOffer() throws Exception { | |
ArrayList<Offer> offers = (ArrayList<Offer>) offersService.findAll(); | |
Offer offerToChange = offers.get(1); | |
offerToChange.setStartingPoint("München"); | |
offerToChange.setDestination("Frankfurt"); | |
offerToChange.setPrice(100); | |
offerToChange.setUsername("admin"); | |
offerToChange.setDate("2017-11-11T09:25"); | |
String json = mapper.writeValueAsString(offerToChange); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void getOffersByUsername() throws Exception { | |
mockMvc.perform(get("/api/offers/username/{username}", user1.getUsername())) | |
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk()) | |
.andDo(this.document.document(pathParameters( | |
parameterWithName("username").description("The user whos offers should be retrieved")))) | |
.andDo(this.document.document(responseFields(fieldWithPath("[]").description("An array of offers")) | |
.andWithPrefix("[].", offer))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void getOffers() throws Exception { | |
mockMvc.perform(get("/api/offers")).andExpect(status().isOk()) | |
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) | |
.andDo(this.document.document(responseFields(fieldWithPath("[]").description("An array of offers")) | |
.andWithPrefix("[].", offer))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void postOffer() throws Exception { | |
Offer newOffer = new Offer("Innsbruck", "Lindau", "admin", "2016-10-10T09:50", 40); | |
String json = mapper.writeValueAsString(newOffer); | |
mockMvc.perform(post("/api/offers").contentType(MediaType.APPLICATION_JSON_UTF8).content(json)) | |
.andExpect(status().isCreated()) | |
.andDo(this.document.document(responseFields(offer), requestFields(offer))); | |
} |