Skip to content

Instantly share code, notes, and snippets.

@espendennis
espendennis / OfferApiDocumentation.class
Created October 17, 2016 13:44
Instantiating the inner class
private RestDocumentationResultHandler document;
ConstrainedFields fields = new ConstrainedFields(Offer.class);
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");
@espendennis
espendennis / OfferApiDocumentation.class
Created October 17, 2016 13:43
Instantiating the inner class
ConstrainedFields fields = new ConstrainedFields(Offer.class);
@espendennis
espendennis / OfferApiDocumentation.class
Created October 17, 2016 13:41
Inner Class ConstrainedFields
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
@espendennis
espendennis / OfferDocumentation.adoc
Created October 17, 2016 13:38
Final OfferDocumentation.adoc

/api/offers Getting Started Guide

@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);
@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)));
}
@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)));
}
@espendennis
espendennis / OfferDocumentation.adoc
Created October 17, 2016 13:13
OfferDocumentation

/api/offers Getting Started Guide

@espendennis
espendennis / OfferApiDocumentation.class
Created October 17, 2016 13:03
Updated test method
@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)));
}