|
| 1 | +package com.baeldung.gson.jsonarraytohashmap; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import com.google.gson.JsonArray; |
| 12 | +import com.google.gson.JsonObject; |
| 13 | + |
| 14 | +public class JSONArrayToHashMapConverterUnitTest { |
| 15 | + |
| 16 | + private JsonArray jsonArray; |
| 17 | + private JsonArray jsonArrayWithDuplicates; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + void setUp() { |
| 21 | + jsonArray = new JsonArray(); |
| 22 | + |
| 23 | + JsonObject jsonObject1 = new JsonObject(); |
| 24 | + jsonObject1.addProperty("name", "John Doe"); |
| 25 | + jsonObject1.addProperty("age", 35); |
| 26 | + jsonArray.add(jsonObject1); |
| 27 | + |
| 28 | + JsonObject jsonObject2 = new JsonObject(); |
| 29 | + jsonObject2.addProperty("name", "Mary Jenn"); |
| 30 | + jsonObject2.addProperty("age", 41); |
| 31 | + jsonArray.add(jsonObject2); |
| 32 | + |
| 33 | + jsonArrayWithDuplicates = new JsonArray(); |
| 34 | + |
| 35 | + JsonObject jsonObject3 = new JsonObject(); |
| 36 | + jsonObject3.addProperty("name", "John Doe"); |
| 37 | + jsonObject3.addProperty("age", 35); |
| 38 | + jsonArrayWithDuplicates.add(jsonObject3); |
| 39 | + |
| 40 | + JsonObject jsonObject4 = new JsonObject(); |
| 41 | + jsonObject4.addProperty("name", "John Doe"); |
| 42 | + jsonObject4.addProperty("age", 41); |
| 43 | + jsonArrayWithDuplicates.add(jsonObject4); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + @Test |
| 48 | + void givenJsonArrayWithObjects_whenConvertUsingIterative_thenCorrectHashMap() { |
| 49 | + Map<String, Integer> hashMap = JSONArrayToHashMapConverter.convertUsingIterative(jsonArray); |
| 50 | + |
| 51 | + assertEquals(35, hashMap.get("John Doe")); |
| 52 | + assertEquals(41, hashMap.get("Mary Jenn")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void givenJsonArrayWithObjects_whenConvertUsingStreams_thenCorrectHashMap() { |
| 57 | + Map<String, Integer> hashMap = JSONArrayToHashMapConverter.convertUsingStreams(jsonArray); |
| 58 | + |
| 59 | + assertEquals(35, hashMap.get("John Doe")); |
| 60 | + assertEquals(41, hashMap.get("Mary Jenn")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void givenJsonArrayWithObjects_whenConvertUsingGson_thenCorrectHashMap() { |
| 65 | + Map<String, Integer> hashMap = JSONArrayToHashMapConverter.convertUsingGson(jsonArray); |
| 66 | + |
| 67 | + assertEquals(35, hashMap.get("John Doe")); |
| 68 | + assertEquals(41, hashMap.get("Mary Jenn")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void givenJsonArrayWithDuplicateKeys_whenConvertUsingIterative_thenLastValueWins() { |
| 73 | + Map<String, Integer> hashMap = JSONArrayToHashMapConverter.convertUsingIterative(jsonArrayWithDuplicates); |
| 74 | + assertEquals(41, hashMap.get("John Doe")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void givenJsonArrayWithDuplicateKeys_whenConvertUsingStreams_thenLastValueWins() { |
| 79 | + Map<String, Integer> hashMap = JSONArrayToHashMapConverter.convertUsingIterative(jsonArrayWithDuplicates); |
| 80 | + assertEquals(41, hashMap.get("John Doe")); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void givenJsonArrayWithDuplicateKeys_whenConvertUsingGson_thenLastValueWins() { |
| 85 | + Map<String, Integer> hashMap = JSONArrayToHashMapConverter.convertUsingGson(jsonArrayWithDuplicates); |
| 86 | + assertEquals(41, hashMap.get("John Doe")); |
| 87 | + } |
| 88 | +} |
0 commit comments