Skip to content

Commit

Permalink
Increase JsonUtil coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 2, 2023
1 parent adeccf4 commit 4a3deb6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/adventofcode/flashk/common/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public final class JsonUtil {

private JsonUtil() {}

/**
* Builds a json tree.
* <p>Input accepts basic json and even simple arrays such as:</p>
Expand All @@ -28,12 +28,10 @@ public static JsonNode buildTree(String input) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readTree(input);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new IllegalArgumentException("The file "+input+" has wrong format.");
}

return null;
}

/**
* Builds a json tree using Gson library.
* <p>Input accepts basic json and even simple arrays such as:</p>
Expand All @@ -59,5 +57,5 @@ public static JsonElement buildGsonTree(String input) {
public static boolean isInt(JsonElement element) {
return element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber();
}

}
65 changes: 65 additions & 0 deletions src/test/java/com/adventofcode/flashk/common/JsonUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.adventofcode.flashk.common;

import com.adventofcode.flashk.common.test.constants.TestFilename;
import com.adventofcode.flashk.common.test.constants.TestFolder;
import com.adventofcode.flashk.common.test.utils.Util;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.JsonElement;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class JsonUtilTest {

@BeforeEach
void setUp() {
}

@AfterEach
void tearDown() {
}

@Test
void buildTreeFromArrayJsonTest() {
String content = Util.readStringLines(TestFolder.COMMON, TestFilename.ARRAY_JSON).get(0);
JsonNode jsonNode = JsonUtil.buildTree(content);

assertNotNull(jsonNode);
assertTrue(jsonNode.isArray());

assertEquals(2,jsonNode.get(0).asInt());
assertEquals(3,jsonNode.get(1).asInt());
assertEquals(4,jsonNode.get(2).asInt());
}

@Test
void buildTreeFromInvalidJsonTest() {
String content = Util.readStringLines(TestFolder.COMMON, TestFilename.INVALID_JSON).get(0);
assertThrows(IllegalArgumentException.class, () -> JsonUtil.buildTree(content));
}

@Test
void buildGsonTreeTest() {

String content = Util.readStringLines(TestFolder.COMMON, TestFilename.ARRAY_JSON).get(0);
JsonElement jsonElement = JsonUtil.buildGsonTree(content);

assertNotNull(jsonElement);
assertTrue(jsonElement.isJsonArray());

assertEquals(2, jsonElement.getAsJsonArray().get(0).getAsInt());
assertEquals(3, jsonElement.getAsJsonArray().get(1).getAsInt());
assertEquals(4, jsonElement.getAsJsonArray().get(2).getAsInt());
}

@Test
void isIntTest() {

String content = Util.readStringLines(TestFolder.COMMON, TestFilename.ARRAY_JSON).get(0);
JsonElement jsonElement = JsonUtil.buildGsonTree(content).getAsJsonArray().get(0);

assertTrue(JsonUtil.isInt(jsonElement));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ private TestFilename() {}
public final static String INPUT_FILE_SAMPLE_2 = "sample_2.input";
public final static String INPUT_FILE_SINGLE_SAMPLE = "single_sample.input";

// Other tests
public final static String ARRAY_JSON = "array.json";
public final static String INVALID_JSON = "invalid.json";

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public final class TestFolder {
private TestFolder() {}

// Folders
public final static String COMMON = "common";
public final static String DAY_XX = "day_xx";
public final static String DAY_01 = "day_01";
public final static String DAY_02 = "day_02";
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/common/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2,3,4]
1 change: 1 addition & 0 deletions src/test/resources/common/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2,3

0 comments on commit 4a3deb6

Please sign in to comment.