Skip to content

Commit 62eeb2f

Browse files
authored
Merge pull request #2 from ungaratto93/dev
testes e funcionalidade de conversao
2 parents 3f09b41 + eb20033 commit 62eeb2f

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package br.com.ungaratto93.lib.facade;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParseException;
5+
6+
import java.util.Map;
7+
8+
public class GsonCheckStrategy implements JsomCheck {
9+
10+
@Override
11+
public void toCheck(String json) {
12+
if(json == null){
13+
throw new JsonParseException("string json is null for parsing");
14+
}
15+
if(json.isEmpty()){
16+
throw new JsonParseException("string json is empty for parsing");
17+
}
18+
}
19+
20+
@Override
21+
public void toCheck(JsonObject object) {
22+
if (object.isJsonNull()) {
23+
throw new JsonParseException("string json is null for parsing");
24+
}
25+
}
26+
27+
@Override
28+
public void toCheck(Map map) {
29+
if (map.isEmpty()) {
30+
throw new JsonParseException("map is empty for parsing");
31+
}
32+
}
33+
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package br.com.ungaratto93.lib.facade;
2+
3+
import com.google.gson.*;
4+
5+
import java.util.Map;
6+
7+
public class GsonConverter implements JsonConverter {
8+
9+
private final Gson gson;
10+
private final JsomCheck check;
11+
12+
public GsonConverter(JsomCheck check) {
13+
this.gson = new GsonBuilder().setPrettyPrinting().create();
14+
this.check = check;
15+
}
16+
17+
public JsonObject from(String json) {
18+
check.toCheck(json);
19+
return JsonParser.parseString(json).getAsJsonObject();
20+
}
21+
22+
public Map<String, JsonElement> from(JsonObject jsonObject) {
23+
check.toCheck(jsonObject);
24+
return jsonObject.asMap();
25+
}
26+
27+
public String fromComplexMap(Map<String, Map<String, Map<String, Object>>> map) {
28+
check.toCheck(map);
29+
return gson.toJson(map);
30+
}
31+
32+
public String fromSimpleMap(Map<String, String> map) {
33+
check.toCheck(map);
34+
return gson.toJson(map);
35+
}
36+
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package br.com.ungaratto93.lib.facade;
2+
3+
import com.google.gson.JsonObject;
4+
5+
import java.util.Map;
6+
7+
public interface JsomCheck {
8+
9+
void toCheck(String json);
10+
void toCheck(JsonObject object);
11+
void toCheck(Map map);
12+
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package br.com.ungaratto93.lib.facade;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
6+
import java.util.Map;
7+
8+
public interface JsonConverter {
9+
10+
JsonObject from(String json);
11+
Map<String, JsonElement> from(JsonObject jsonObject);
12+
String fromComplexMap(Map<String, Map<String, Map<String, Object>>> map);
13+
String fromSimpleMap(Map<String, String> map);
14+
15+
16+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package br.com.ungaratto93.lib.facade;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParseException;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.util.Map;
10+
11+
12+
public class ConverterTest {
13+
14+
JsonConverter converter = new GsonConverter(new GsonCheckStrategy());
15+
16+
@Test
17+
public void shouldTransformSimpleJsonStringToJsonObj() {
18+
String json = "{\n" +
19+
" \"name\": \"Jhon\",\n" +
20+
" \"age\": \"39\",\n" +
21+
" \"locality\": \"New York\",\n" +
22+
" \"region\": \"State\",\n" +
23+
" \"postalCode\": \"12345\",\n" +
24+
" \"countryName\": \"Country\"\n" +
25+
"}\n" +
26+
"\n";
27+
28+
JsonObject jsonObject = converter.from(json);
29+
Assert.assertTrue(jsonObject.getClass().equals(JsonObject.class));
30+
Assert.assertEquals("name", jsonObject.keySet().stream().findFirst().get());
31+
Assert.assertEquals("name=\"Jhon\"", jsonObject.entrySet().stream().findFirst().get().toString());
32+
}
33+
34+
@Test
35+
public void shouldTransformComplexeStringJsonToJsonObj() {
36+
String complexJson = "{\n" +
37+
" \"First\": \"[11, 12, 13, 14, 15]\",\n" +
38+
" \"Second\": \"[21, 22, 23]\",\n" +
39+
" \"Third\": \"[31, 32,33]\"\n" +
40+
"}\n";
41+
JsonObject jsonObject = converter.from(complexJson);
42+
Assert.assertTrue(jsonObject.getClass().equals(JsonObject.class));
43+
Assert.assertEquals("First", jsonObject.keySet().stream().findFirst().get());
44+
}
45+
46+
@Test
47+
public void shouldTransformJsonObjectToMap() {
48+
String json = "{\n" +
49+
" \"name\": \"Jhon\",\n" +
50+
" \"age\": \"39\",\n" +
51+
" \"locality\": \"New York\",\n" +
52+
" \"region\": \"State\",\n" +
53+
" \"postalCode\": \"12345\",\n" +
54+
" \"countryName\": \"Country\"\n" +
55+
"}\n" +
56+
"\n";
57+
58+
JsonObject jsonObject = converter.from(json);
59+
Map<String, JsonElement> map = converter.from(jsonObject);
60+
Assert.assertEquals("name", map.keySet().stream().findFirst().get());
61+
Assert.assertEquals("\"Jhon\"", map.values().stream().findFirst().get().toString());
62+
}
63+
64+
@Test
65+
public void shouldNotTransformNullJsonString() {
66+
String nullJson = null;
67+
Assert.assertThrows(JsonParseException.class, () -> { converter.from(nullJson);});
68+
}
69+
70+
@Test
71+
public void shouldNotTransformEmptyJsonString() {
72+
String nullJson = "";
73+
Assert.assertThrows(JsonParseException.class, () -> { converter.from(nullJson);});
74+
}
75+
76+
}

0 commit comments

Comments
 (0)