Created
September 22, 2014 08:17
-
-
Save dabd/e912418bd8105f3bd7ec to your computer and use it in GitHub Desktop.
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
| package org.me.modelmapper; | |
| import com.fasterxml.jackson.databind.JsonNode; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import java.util.ArrayList; | |
| import org.modelmapper.ModelMapper; | |
| import org.modelmapper.config.Configuration; | |
| import org.modelmapper.jackson.JsonNodeValueReader; | |
| import org.testng.Assert; | |
| import org.testng.annotations.BeforeMethod; | |
| import org.testng.annotations.Test; | |
| public class JsonModelMapperTest { | |
| private final JsonNodeValueReader valueReader = new JsonNodeValueReader(); | |
| private ModelMapper modelMapper; | |
| public JsonModelMapperTest() { | |
| } | |
| public static class Foo { | |
| private String a; | |
| private ArrayList<String> b; | |
| public String getA() { | |
| return a; | |
| } | |
| public void setA(String a) { | |
| this.a = a; | |
| } | |
| public ArrayList<String> getB() { | |
| return b; | |
| } | |
| public void setB(ArrayList<String> b) { | |
| this.b = b; | |
| } | |
| } | |
| @BeforeMethod | |
| public void setUpMethod() throws Exception { | |
| modelMapper = new ModelMapper(); | |
| modelMapper.getConfiguration() | |
| .setFieldMatchingEnabled(true) | |
| .setFieldAccessLevel(Configuration.AccessLevel.PRIVATE) | |
| .addValueReader(valueReader); | |
| } | |
| @Test | |
| public void shouldMapFromJsonNode() throws Exception { | |
| String json = "{\"a\": \"aaa\", \"b\": [\"ccc\"]} "; | |
| JsonNode node = new ObjectMapper().readTree(json); | |
| Foo foo = modelMapper.map(node, Foo.class); | |
| ArrayList<String> list = new ArrayList<String>() {{ | |
| add("ccc"); | |
| }}; | |
| Assert.assertEquals(foo.getA(), "aaa"); | |
| Assert.assertEquals(foo.getB(), list); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment