-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Kovetskiy
authored and
Alex Kovetskiy
committed
Feb 18, 2017
0 parents
commit 2c0b3ed
Showing
18 changed files
with
838 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.softgroup</groupId> | ||
<artifactId>basis</artifactId> | ||
|
||
<version>0.0.1</version> | ||
</parent> | ||
|
||
<artifactId>common-datamapper</artifactId> | ||
<packaging>jar</packaging> | ||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.softgroup</groupId> | ||
<artifactId>common-exceptions</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.7.4</version> | ||
<type>jar</type> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
33 changes: 33 additions & 0 deletions
33
...oup/basis/common-datamapper/src/main/java/com/softgroup/common/datamapper/DataMapper.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.softgroup.common.datamapper; | ||
|
||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
|
||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author vlischyshyn | ||
*/ | ||
public interface DataMapper { | ||
|
||
Map convertToMap(Object value); | ||
|
||
<T> T convert(Map map, Class<T> dataType); | ||
|
||
//<T> T convert(HashMap map, Class<T> dataType); | ||
|
||
<T> T mapData(String data, Class<T> dataType); | ||
|
||
<T> T mapData(String data, TypeReference dataType); | ||
|
||
<T> T mapData(byte[] message, Class<T> dataType); | ||
|
||
<T> T readValue(InputStream src, Class<T> valueType); | ||
|
||
<T> String dataToString(T data); | ||
|
||
String objectToString(Object data); | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
...is/common-datamapper/src/main/java/com/softgroup/common/datamapper/JacksonDataMapper.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.softgroup.common.datamapper; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.softgroup.common.exceptions.MapperException; | ||
|
||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Arthas | ||
*/ | ||
public class JacksonDataMapper implements DataMapper { | ||
private static final String CPJ = "Can't parse json"; | ||
|
||
private final ObjectMapper mapper; | ||
|
||
public JacksonDataMapper() { | ||
mapper = new ObjectMapper(); | ||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | ||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | ||
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); | ||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
} | ||
|
||
public ObjectMapper getMapper() { | ||
return mapper; | ||
} | ||
|
||
@Override | ||
public Map convertToMap(final Object value) { | ||
try { | ||
return (Map<String, Object>) mapper.convertValue(value, Map.class); | ||
} catch (Exception ex) { | ||
throw new MapperException("Can't convert to map ", ex); | ||
} | ||
} | ||
|
||
// @Override | ||
// public convert(map, dataType) { | ||
// try { | ||
// return mapper.convertValue(map, dataType); | ||
// } catch (Exception ex) { | ||
// throw new MapperException("Can't convert map ", ex); | ||
// } | ||
// } | ||
|
||
@Override | ||
public <T> T convert(Map map, Class<T> dataType) { | ||
try { | ||
return mapper.convertValue(map, dataType); | ||
} catch (Exception ex) { | ||
throw new MapperException("Can't convert map ", ex); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T mapData(String data, Class<T> dataType) { | ||
try { | ||
return mapper.readValue(data, dataType); | ||
} catch (Exception ex) { | ||
throw new MapperException(CPJ, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T mapData(String data, TypeReference dataType) { | ||
try { | ||
return mapper.readValue(data, dataType); | ||
} catch (Exception ex) { | ||
throw new MapperException(CPJ, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T mapData(byte[] message, Class<T> dataType) { | ||
try { | ||
return mapper.readValue(message, dataType); | ||
} catch (Exception ex) { | ||
throw new MapperException(CPJ, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T readValue(InputStream src, Class<T> valueType) { | ||
try { | ||
return mapper.readValue(src, valueType); | ||
} catch (Exception ex) { | ||
throw new MapperException("Can't read from stream ", ex); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> String dataToString(T data) { | ||
try { | ||
return new String(mapper.writeValueAsBytes(data), "UTF-8"); | ||
} catch (Exception ex) { | ||
throw new MapperException("Can't convert object to json string", ex); | ||
} | ||
} | ||
|
||
@Override | ||
public String objectToString(Object data) { | ||
try { | ||
return mapper.writeValueAsString(data); | ||
} catch (Exception e) { | ||
throw new MapperException("Can`t create string", e); | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...amapper/src/main/java/com/softgroup/common/datamapper/configuration/DataMapperAppCfg.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.softgroup.common.datamapper.configuration; | ||
|
||
import com.softgroup.common.datamapper.DataMapper; | ||
import com.softgroup.common.datamapper.JacksonDataMapper; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author DoctoRJurius | ||
*/ | ||
@Configuration | ||
public class DataMapperAppCfg { | ||
@Bean | ||
public DataMapper dataMapper() { | ||
return new JacksonDataMapper(); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...s/common-datamapper/src/test/java/com/softgroup/common/datamapper/DataMapperAppCfgIT.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.softgroup.common.datamapper; | ||
|
||
import com.softgroup.common.datamapper.configuration.DataMapperAppCfg; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* @author odin | ||
* @since 15.02.17. | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = {DataMapperAppCfg.class}) | ||
public class DataMapperAppCfgIT { | ||
|
||
@Autowired | ||
DataMapper dataMapper; | ||
|
||
@Test | ||
public void test(){ | ||
assertThat(dataMapper, CoreMatchers.notNullValue()); | ||
assertEquals(JacksonDataMapper.class, dataMapper.getClass()); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...oup/basis/common-datamapper/src/test/java/com/softgroup/common/datamapper/MapperTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.softgroup.common.datamapper; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.softgroup.common.datamapper.support.ModelA; | ||
import com.softgroup.common.datamapper.support.ModelB; | ||
import com.softgroup.common.datamapper.support.ModelWithEnum; | ||
import com.softgroup.common.exceptions.MapperException; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsInstanceOf.instanceOf; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
import static org.hamcrest.core.IsNull.nullValue; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author odin | ||
* @since 15.02.17. | ||
*/ | ||
public class MapperTest { | ||
|
||
@Test | ||
public void testNullArgs() { | ||
final JacksonDataMapper mapper = new JacksonDataMapper(); | ||
|
||
assertThat(mapper.dataToString(null), is("null")); | ||
assertThat(mapper.objectToString(null), is("null")); | ||
assertThat(mapper.convert(null, ModelA.class), nullValue()); | ||
|
||
} | ||
|
||
@Test | ||
public void testMap() { | ||
final JacksonDataMapper mapper = new JacksonDataMapper(); | ||
|
||
String str = "{\"name\":\"hello\"}"; | ||
ModelA ma = mapper.mapData(str, ModelA.class); | ||
assertThat(ma.getName(), is("hello")); | ||
assertThat(mapper.dataToString(ma), is(str)); | ||
|
||
Map<String, Object> map = new HashMap<>(); | ||
map.put("name", "hello world"); | ||
|
||
ma = mapper.convert(map, ModelA.class); | ||
assertThat(ma.getName(), is("hello world")); | ||
|
||
ma = mapper.mapData(str, new TypeReference<ModelA>() {}); | ||
assertThat(ma.getName(), is("hello")); | ||
assertThat(mapper.dataToString(ma), is(str)); | ||
|
||
str = "{\"id\":\"1q2w3e\",\"list\":[{\"name\":\"asd\"},{\"name\":\"zxc\"}]}"; | ||
ModelB<?> mb = mapper.mapData(str, ModelB.class); | ||
assertThat(mb.getId(), is("1q2w3e")); | ||
assertThat(mb.getList(), notNullValue()); | ||
assertThat(mb.getList().size(), is(2)); | ||
assertThat(mb.getList().get(0), is(instanceOf(Map.class))); | ||
ma = mapper.convert((Map<String, Object>) mb.getList().get(0), ModelA.class); | ||
assertThat(ma.getName(), is("asd")); | ||
|
||
Object oMap = mapper.convertToMap(mb); | ||
assertThat(oMap, notNullValue()); | ||
assertThat(oMap , is(instanceOf(Map.class))); | ||
map = (Map<String, Object>) oMap; | ||
assertThat(map.get("id"), is("1q2w3e")); | ||
|
||
assertThat(mapper.convertToMap(null), nullValue()); | ||
} | ||
|
||
@Test(expected = MapperException.class) | ||
public void testConvertEx() { | ||
new JacksonDataMapper().convert(new HashMap<>(), (Class)null); | ||
} | ||
|
||
@Test(expected = MapperException.class) | ||
public void testMapDataStringEx() { | ||
new JacksonDataMapper().mapData((String) null, ModelA.class); | ||
} | ||
|
||
@Test(expected = MapperException.class) | ||
public void testMapDataBytesEx() { | ||
new JacksonDataMapper().mapData((byte[]) null, ModelA.class); | ||
} | ||
|
||
@Test(expected = MapperException.class) | ||
public void testMapDataTypeRefEx() { | ||
new JacksonDataMapper().mapData(null, new TypeReference<ModelA>() {}); | ||
} | ||
|
||
@Test(expected = MapperException.class) | ||
public void testConvertToMapEx() { | ||
List<String> list = new ArrayList<>(); | ||
list.add("q"); | ||
list.add("w"); | ||
new JacksonDataMapper().convertToMap(list); | ||
} | ||
|
||
@Test | ||
public void testWithEnum() { | ||
final JacksonDataMapper mapper = new JacksonDataMapper(); | ||
|
||
String str = "{\"name\":\"hello\",\"type\":1}"; | ||
ModelWithEnum me = mapper.mapData(str, ModelWithEnum.class); | ||
assertThat(me.getName(), is("hello")); | ||
assertThat(me.getType(), is(ModelWithEnum.ModelType.MODEL_X)); | ||
assertThat(mapper.dataToString(me), is(str)); | ||
|
||
str = "{\"name\":\"hello\",\"type\":2}"; | ||
me = mapper.mapData(str, ModelWithEnum.class); | ||
assertThat(me.getName(), is("hello")); | ||
assertThat(me.getType(), is(ModelWithEnum.ModelType.MODEL_Y)); | ||
assertThat(mapper.dataToString(me), is(str)); | ||
|
||
} | ||
|
||
@Test(expected = MapperException.class) | ||
public void testWithWrongEnumValue() { | ||
final JacksonDataMapper mapper = new JacksonDataMapper(); | ||
String str = "{\"name\":\"hello\",\"type\":\"model_z\"}"; | ||
ModelWithEnum me = mapper.mapData(str, ModelWithEnum.class); | ||
} | ||
|
||
@Test | ||
public void testWithNoEnumValue() { | ||
final JacksonDataMapper mapper = new JacksonDataMapper(); | ||
String str = "{\"name\":\"hello\"}"; | ||
ModelWithEnum me = mapper.mapData(str, ModelWithEnum.class); | ||
assertThat(me.getName(), is("hello")); | ||
assertThat(me.getType(), nullValue()); | ||
assertThat(mapper.dataToString(me), is(str)); | ||
|
||
} | ||
|
||
} | ||
|
Oops, something went wrong.