Permalink
Browse files

criando deserialização

  • Loading branch information...
1 parent 14f9d26 commit 056005d8a7e4cc35df285be82aa7f0a74c2d2b9e @guilhermegregio committed May 4, 2012
@@ -21,67 +21,68 @@
public class GsonDeserialization implements Deserializer {
private static final Logger logger = LoggerFactory.getLogger(GsonDeserialization.class);
-
- private final ParameterNameProvider paramNameProvider;
-
- public GsonDeserialization(ParameterNameProvider paramNameProvider) {
- this.paramNameProvider = paramNameProvider;
- }
-
- @Override
- public Object[] deserialize(InputStream inputStream, ResourceMethod method) {
- Method jMethod = method.getMethod();
- Class<?>[] types = jMethod.getParameterTypes();
- if (types.length == 0) {
- throw new IllegalArgumentException("Methods that consumes representations must receive just one argument");
- }
-
- ObjectMapper mapper = getObjectMapper();
- Object[] params = new Object[types.length];
- String[] parameterNames = paramNameProvider.parameterNamesFor(jMethod);
-
- try {
- String content = getContentOfStream(inputStream);
- logger.debug("json retrieved: " + content);
- JsonNode root = mapper.readTree(content);
-
- for (int i=0; i < types.length; i++) {
- String name = parameterNames[i];
- JsonNode node = root.get(name);
- if (node != null) {
- params[i] = mapper.readValue(node, types[i]);
- }
- }
- } catch (Exception e) {
- throw new ResultException("Unable to deserialize data", e);
- }
-
- return params;
- }
-
- protected ObjectMapper getObjectMapper() {
- ObjectMapper mapper = new ObjectMapper();
- mapper = new ObjectMapper();
- mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
- mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
- mapper.configure(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING, true);
-
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- mapper.setDateFormat(sdf);
-
- return mapper;
- }
-
- private String getContentOfStream(InputStream input) throws IOException {
- StringBuilder content = new StringBuilder();
-
- byte[] buffer = new byte[1024];
- int readed = 0;
- while ((readed=input.read(buffer)) != -1) {
- content.append(new String(buffer, 0, readed));
- }
-
- return content.toString();
- }
+
+ private final ParameterNameProvider paramNameProvider;
+
+ public GsonDeserialization(ParameterNameProvider paramNameProvider) {
+ this.paramNameProvider = paramNameProvider;
+ }
+
+ @Override
+ public Object[] deserialize(InputStream inputStream, ResourceMethod method) {
+ Method jMethod = method.getMethod();
+ Class<?>[] types = jMethod.getParameterTypes();
+ if (types.length == 0) {
+ throw new IllegalArgumentException(
+ "Methods that consumes representations must receive just one argument");
+ }
+
+ ObjectMapper mapper = getObjectMapper();
+ Object[] params = new Object[types.length];
+ String[] parameterNames = paramNameProvider.parameterNamesFor(jMethod);
+
+ try {
+ String content = getContentOfStream(inputStream);
+ logger.debug("json retrieved: " + content);
+ JsonNode root = mapper.readTree(content);
+
+ for (int i = 0; i < types.length; i++) {
+ String name = parameterNames[i];
+ JsonNode node = root.get(name);
+ if (node != null) {
+ params[i] = mapper.readValue(node, types[i]);
+ }
+ }
+ } catch (Exception e) {
+ throw new ResultException("Unable to deserialize data", e);
+ }
+
+ return params;
+ }
+
+ protected ObjectMapper getObjectMapper() {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper = new ObjectMapper();
+ mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
+ mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
+ mapper.configure(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING, true);
+
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+ mapper.setDateFormat(sdf);
+
+ return mapper;
+ }
+
+ private String getContentOfStream(InputStream input) throws IOException {
+ StringBuilder content = new StringBuilder();
+
+ byte[] buffer = new byte[1024];
+ int readed = 0;
+ while ((readed = input.read(buffer)) != -1) {
+ content.append(new String(buffer, 0, readed));
+ }
+
+ return content.toString();
+ }
}
@@ -23,129 +23,140 @@
public class GsonDeserializationTest {
- private GsonDeserialization deserializer;
- private ParameterNameProvider provider;
- private DefaultResourceMethod list;
- private DefaultResourceMethod listLimit;
- private DefaultResourceMethod add;
- private DefaultResourceMethod addProduct;
-
- @Before
- public void setup() throws Exception {
- provider = mock(ParameterNameProvider.class);
- deserializer = new GsonDeserialization(provider);
-
- DefaultResourceClass resourceClass = new DefaultResourceClass(OrderController.class);
-
- list = new DefaultResourceMethod(resourceClass, OrderController.class.getDeclaredMethod("list"));
- listLimit = new DefaultResourceMethod(resourceClass, OrderController.class.getDeclaredMethod("list", Integer.class));
- add = new DefaultResourceMethod(resourceClass, OrderController.class.getDeclaredMethod("add", Order.class));
- addProduct = new DefaultResourceMethod(resourceClass, OrderController.class.getDeclaredMethod("addProduct", Order.class, Product.class));
- }
-
- static class OrderController {
-
- public void list() {}
-
- public void list(Integer limit) {}
-
- public void add(Order order) {}
-
- public void addProduct(Order order, Product product) {}
-
- }
-
- @Test(expected=IllegalArgumentException.class)
- public void shouldMethodNotAcceptedWithoutArguments() {
- deserializer.deserialize(new ByteArrayInputStream(new byte[0]), list);
- }
-
- @Test
- public void shouldSkipArgumentsNotDefined() {
- InputStream input = new ByteArrayInputStream("{\"product\":{\"id\":1,\"name\":\"Product 1\"}}".getBytes());
-
- when(provider.parameterNamesFor(listLimit.getMethod())).thenReturn(new String[] {"limit"});
-
- Object[] deserialized = deserializer.deserialize(input, listLimit);
-
-
- assertThat(deserialized.length, is(1));
- assertThat(deserialized[0], is(nullValue()));
- }
-
- @Test
- public void shouldDeserializeAnOrderWhenMethodHasOneArgument() {
- String json = "{\"order\":{\"id\":1,\"products\":[{\"id\":1,\"name\":\"Product 1\","
- + "\"group\":{\"id\":1,\"name\":\"Group 1\"}},{\"id\":2,\"name\":\"Product 2\","
- + "\"group\":{\"id\":2,\"name\":\"Group 2\"}}]}}";
- InputStream input = new ByteArrayInputStream(json.getBytes());
-
- when(provider.parameterNamesFor(add.getMethod())).thenReturn(new String[] {"order"});
-
- Object[] deserialized = deserializer.deserialize(input, add);
-
- assertThat(deserialized.length, is(1));
- assertThat(deserialized[0], is(instanceOf(Order.class)));
- Order order = (Order) deserialized[0];
- assertThat(order.getId(), is(1L));
- assertThat(order.getProducts().size(), is(2));
- assertThat(order.getProducts().get(0).getId(), is(1L));
- assertThat(order.getProducts().get(0).getGroup().getId(), is(1L));
- assertThat(order.getProducts().get(1).getId(), is(2L));
- assertThat(order.getProducts().get(1).getGroup().getId(), is(2L));
- }
-
- @Test
- public void shouldDeserializeAnOrderAndProductWhenMethodHasTwoArguments() {
- String json = "{\"order\":{\"id\":1,\"products\":[{\"id\":1,\"name\":\"Product 1\","
- + "\"group\":{\"id\":1,\"name\":\"Group 1\"}},{\"id\":2,\"name\":\"Product 2\","
- + "\"group\":{\"id\":2,\"name\":\"Group 2\"}}]},"
- + "\"product\":{\"id\":1,\"name\":\"Product 1\"}}";
- InputStream input = new ByteArrayInputStream(json.getBytes());
-
- when(provider.parameterNamesFor(addProduct.getMethod())).thenReturn(new String[] {"order", "product"});
-
- Object[] deserialized = deserializer.deserialize(input, addProduct);
-
- assertThat(deserialized.length, is(2));
- assertThat(deserialized[0], is(instanceOf(Order.class)));
- assertThat(deserialized[1], is(instanceOf(Product.class)));
-
- Order order = (Order) deserialized[0];
- assertThat(order.getId(), is(1L));
- assertThat(order.getProducts().size(), is(2));
- assertThat(order.getProducts().get(0).getId(), is(1L));
- assertThat(order.getProducts().get(0).getGroup().getId(), is(1L));
- assertThat(order.getProducts().get(1).getId(), is(2L));
- assertThat(order.getProducts().get(1).getGroup().getId(), is(2L));
-
- Product product = (Product) deserialized[1];
- assertThat(product.getId(), is(1L));
- assertThat(product.getName(), is("Product 1"));
- }
-
- @Test
- public void shouldDeserializeOneOrderWhenMethodHasTwoArguments() {
- String json = "{\"order\":{\"id\":1,\"products\":[{\"id\":1,\"name\":\"Product 1\","
- + "\"group\":{\"id\":1,\"name\":\"Group 1\"}},{\"id\":2,\"name\":\"Product 2\","
- + "\"group\":{\"id\":2,\"name\":\"Group 2\"}}]}}";
- InputStream input = new ByteArrayInputStream(json.getBytes());
-
- when(provider.parameterNamesFor(addProduct.getMethod())).thenReturn(new String[] {"order", "product"});
-
- Object[] deserialized = deserializer.deserialize(input, addProduct);
-
- assertThat(deserialized.length, is(2));
- assertThat(deserialized[0], is(instanceOf(Order.class)));
- assertThat(deserialized[1], is(nullValue()));
-
- Order order = (Order) deserialized[0];
- assertThat(order.getId(), is(1L));
- assertThat(order.getProducts().size(), is(2));
- assertThat(order.getProducts().get(0).getId(), is(1L));
- assertThat(order.getProducts().get(0).getGroup().getId(), is(1L));
- assertThat(order.getProducts().get(1).getId(), is(2L));
- assertThat(order.getProducts().get(1).getGroup().getId(), is(2L));
- }
-
+ private GsonDeserialization deserializer;
+ private ParameterNameProvider provider;
+ private DefaultResourceMethod list;
+ private DefaultResourceMethod listLimit;
+ private DefaultResourceMethod add;
+ private DefaultResourceMethod addProduct;
+
+ @Before
+ public void setup() throws Exception {
+ provider = mock(ParameterNameProvider.class);
+ deserializer = new GsonDeserialization(provider);
+
+ DefaultResourceClass resourceClass = new DefaultResourceClass(OrderController.class);
+
+ list = new DefaultResourceMethod(resourceClass,
+ OrderController.class.getDeclaredMethod("list"));
+ listLimit = new DefaultResourceMethod(resourceClass,
+ OrderController.class.getDeclaredMethod("list", Integer.class));
+ add = new DefaultResourceMethod(resourceClass, OrderController.class.getDeclaredMethod(
+ "add", Order.class));
+ addProduct = new DefaultResourceMethod(resourceClass,
+ OrderController.class.getDeclaredMethod("addProduct", Order.class, Product.class));
+ }
+
+ static class OrderController {
+
+ public void list() {
+ }
+
+ public void list(Integer limit) {
+ }
+
+ public void add(Order order) {
+ }
+
+ public void addProduct(Order order, Product product) {
+ }
+
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldMethodNotAcceptedWithoutArguments() {
+ deserializer.deserialize(new ByteArrayInputStream(new byte[0]), list);
+ }
+
+ @Test
+ public void shouldSkipArgumentsNotDefined() {
+ InputStream input = new ByteArrayInputStream(
+ "{\"product\":{\"id\":1,\"name\":\"Product 1\"}}".getBytes());
+
+ when(provider.parameterNamesFor(listLimit.getMethod()))
+ .thenReturn(new String[] { "limit" });
+
+ Object[] deserialized = deserializer.deserialize(input, listLimit);
+
+ assertThat(deserialized.length, is(1));
+ assertThat(deserialized[0], is(nullValue()));
+ }
+
+ @Test
+ public void shouldDeserializeAnOrderWhenMethodHasOneArgument() {
+ String json = "{\"order\":{\"id\":1,\"products\":[{\"id\":1,\"name\":\"Product 1\","
+ + "\"group\":{\"id\":1,\"name\":\"Group 1\"}},{\"id\":2,\"name\":\"Product 2\","
+ + "\"group\":{\"id\":2,\"name\":\"Group 2\"}}]}}";
+ InputStream input = new ByteArrayInputStream(json.getBytes());
+
+ when(provider.parameterNamesFor(add.getMethod())).thenReturn(new String[] { "order" });
+
+ Object[] deserialized = deserializer.deserialize(input, add);
+
+ assertThat(deserialized.length, is(1));
+ assertThat(deserialized[0], is(instanceOf(Order.class)));
+ Order order = (Order) deserialized[0];
+ assertThat(order.getId(), is(1L));
+ assertThat(order.getProducts().size(), is(2));
+ assertThat(order.getProducts().get(0).getId(), is(1L));
+ assertThat(order.getProducts().get(0).getGroup().getId(), is(1L));
+ assertThat(order.getProducts().get(1).getId(), is(2L));
+ assertThat(order.getProducts().get(1).getGroup().getId(), is(2L));
+ }
+
+ @Test
+ public void shouldDeserializeAnOrderAndProductWhenMethodHasTwoArguments() {
+ String json = "{\"order\":{\"id\":1,\"products\":[{\"id\":1,\"name\":\"Product 1\","
+ + "\"group\":{\"id\":1,\"name\":\"Group 1\"}},{\"id\":2,\"name\":\"Product 2\","
+ + "\"group\":{\"id\":2,\"name\":\"Group 2\"}}]},"
+ + "\"product\":{\"id\":1,\"name\":\"Product 1\"}}";
+ InputStream input = new ByteArrayInputStream(json.getBytes());
+
+ when(provider.parameterNamesFor(addProduct.getMethod())).thenReturn(
+ new String[] { "order", "product" });
+
+ Object[] deserialized = deserializer.deserialize(input, addProduct);
+
+ assertThat(deserialized.length, is(2));
+ assertThat(deserialized[0], is(instanceOf(Order.class)));
+ assertThat(deserialized[1], is(instanceOf(Product.class)));
+
+ Order order = (Order) deserialized[0];
+ assertThat(order.getId(), is(1L));
+ assertThat(order.getProducts().size(), is(2));
+ assertThat(order.getProducts().get(0).getId(), is(1L));
+ assertThat(order.getProducts().get(0).getGroup().getId(), is(1L));
+ assertThat(order.getProducts().get(1).getId(), is(2L));
+ assertThat(order.getProducts().get(1).getGroup().getId(), is(2L));
+
+ Product product = (Product) deserialized[1];
+ assertThat(product.getId(), is(1L));
+ assertThat(product.getName(), is("Product 1"));
+ }
+
+ @Test
+ public void shouldDeserializeOneOrderWhenMethodHasTwoArguments() {
+ String json = "{\"order\":{\"id\":1,\"products\":[{\"id\":1,\"name\":\"Product 1\","
+ + "\"group\":{\"id\":1,\"name\":\"Group 1\"}},{\"id\":2,\"name\":\"Product 2\","
+ + "\"group\":{\"id\":2,\"name\":\"Group 2\"}}]}}";
+ InputStream input = new ByteArrayInputStream(json.getBytes());
+
+ when(provider.parameterNamesFor(addProduct.getMethod())).thenReturn(
+ new String[] { "order", "product" });
+
+ Object[] deserialized = deserializer.deserialize(input, addProduct);
+
+ assertThat(deserialized.length, is(2));
+ assertThat(deserialized[0], is(instanceOf(Order.class)));
+ assertThat(deserialized[1], is(nullValue()));
+
+ Order order = (Order) deserialized[0];
+ assertThat(order.getId(), is(1L));
+ assertThat(order.getProducts().size(), is(2));
+ assertThat(order.getProducts().get(0).getId(), is(1L));
+ assertThat(order.getProducts().get(0).getGroup().getId(), is(1L));
+ assertThat(order.getProducts().get(1).getId(), is(2L));
+ assertThat(order.getProducts().get(1).getGroup().getId(), is(2L));
+ }
+
}

0 comments on commit 056005d

Please sign in to comment.