Skip to content

Instantly share code, notes, and snippets.

@dabd
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save dabd/06db81a91ee266f09826 to your computer and use it in GitHub Desktop.

Select an option

Save dabd/06db81a91ee266f09826 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.me.modelmapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.HashMap;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.convention.MatchingStrategies;
import org.modelmapper.gson.JsonElementValueReader;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class JsonModelMapperTest {
private ModelMapper modelMapper;
public JsonModelMapperTest() {
}
public static class Foo {
public String a;
public String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
@BeforeMethod
public void setUpMethod() throws Exception {
modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldMatchingEnabled(true);
}
@Test
public void shouldMapHashMapToFoo() {
HashMap<String, String> map = new HashMap<>();
map.put("a", "aaaa");
map.put("b", "bbbb");
PropertyMap<HashMap<String, String>, Foo> fooMap = new PropertyMap<HashMap<String, String>, JsonModelMapperTest.Foo>() {
@Override
protected void configure() {
map(source.get("a"), destination.a);
map(source.get("b"), destination.b);
}
};
modelMapper.addMappings(fooMap);
Foo foo = modelMapper.map(map, Foo.class);
Assert.assertEquals(foo.getA(), map.get("a"));
Assert.assertEquals(foo.getB(), map.get("b"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment