Skip to content

Commit

Permalink
code refactor for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
tminglei committed Apr 23, 2017
1 parent 58bda3a commit ea99f9f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ You define the api meta data in classes' static code blocks, then it was collect
static Mapping<?> petStatus = $(text(oneOf(Arrays.asList("available", "pending", "sold"))))
.desc("pet status in the store").example("available").$$;
static Mapping<?> pet = $(mapping(
field("id", $(vLong()).desc("pet id").example(gen("petId").or(gen(() -> new Faker().number().randomNumber()))).$$),
field("id", $(vLong()).desc("pet id").example(gen("petId").or(gen(() -> faker.number().randomNumber()))).$$),
field("name", $(text(required())).desc("pet name").$$),
field("category", attach(required()).to($(mapping(
field("id", vLong(required())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@
@Path("/pet")
@Produces({"application/json", "application/xml"})
public class PetResource {
static PetData petData = new PetData();
private static PetData petData = new PetData();
private static Faker faker = new Faker();

private ResourceBundle bundle = ResourceBundle.getBundle("bind-messages");
private Messages messages = (key) -> bundle.getString(key);

static Mapping<?> petStatus = $(text(oneOf(Arrays.asList("available", "pending", "sold"))))
.desc("pet status in the store").example("available").$$;
static Mapping<?> pet = $(mapping(
field("id", $(longv()).desc("pet id").example(gen("petId").or(gen(() -> new Faker().number().randomNumber()))).$$),
field("id", $(longv()).desc("pet id").example(gen("petId").or(gen(() -> faker.number().randomNumber()))).$$),
field("name", $(text(required())).desc("pet name").$$),
field("category", $(mapping(
field("id", longv(required())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.io.Writer;
Expand All @@ -17,14 +18,15 @@ public class DataWriterImpl implements DataWriter {
private static final String FORMAT_JSON = "application/json";
private static final String FORMAT_XML = "application/xml";

private static final ObjectMapper objectMapper = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(SerializationFeature.INDENT_OUTPUT, true);

@Override
public void write(Writer writer, String format, DataProvider provider) throws IOException {
switch (format.toLowerCase()) {
case FORMAT_JSON:
String dataJson = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.writer().withDefaultPrettyPrinter()
.writeValueAsString(provider.get());
String dataJson = objectMapper.writeValueAsString(provider.get());
writer.write(dataJson);
break;
case FORMAT_XML:
Expand Down

0 comments on commit ea99f9f

Please sign in to comment.