Skip to content

Commit 19b92c1

Browse files
authored
Merge pull request #217 from sir-gon/develop
[FEATURE] New utility to encapsulate JSON loading.
2 parents 306a079 + 71d2af4 commit 19b92c1

File tree

4 files changed

+52
-56
lines changed

4 files changed

+52
-56
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package util;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.util.List;
7+
8+
/**
9+
* JsonLoader.
10+
*/
11+
public final class JsonLoader {
12+
13+
private JsonLoader() {}
14+
15+
/**
16+
* loadJson.
17+
*/
18+
public static <T> List<T> loadJson(String path, Class<T> type) throws IOException {
19+
ObjectMapper objectMapper = new ObjectMapper();
20+
21+
File file = new File(
22+
JsonLoader.class.getClassLoader()
23+
.getResource(path)
24+
.getFile()
25+
);
26+
27+
ObjectMapper mapper = new ObjectMapper();
28+
return mapper.readerForListOf(type)
29+
.readValue(objectMapper.readTree(file));
30+
}
31+
32+
}

algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,46 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import com.fasterxml.jackson.core.JsonProcessingException;
6-
import com.fasterxml.jackson.databind.JsonNode;
7-
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import java.io.File;
95
import java.io.IOException;
106
import java.util.Arrays;
117
import java.util.List;
128
import org.junit.jupiter.api.BeforeAll;
139
import org.junit.jupiter.api.Test;
1410
import org.junit.jupiter.api.TestInstance;
1511
import org.junit.jupiter.api.TestInstance.Lifecycle;
16-
12+
import util.JsonLoader;
1713

1814
@TestInstance(Lifecycle.PER_CLASS)
1915
class ArraysLeftRotationTest {
2016

21-
public JsonNode testCases;
17+
public static class ArraysLeftRotationTestCase {
18+
public List<Integer> input;
19+
public List<Integer> expected;
20+
}
21+
22+
List<ArraysLeftRotationTestCase> testCases;
2223

2324
@BeforeAll
2425
public void setup() throws IOException {
25-
ObjectMapper objectMapper = new ObjectMapper();
2626

2727
String path = String.join("/", "hackerrank",
2828
"interview_preparation_kit",
2929
"arrays",
3030
"ctci_array_left_rotation.testcases.json");
31-
File file = new File(
32-
this.getClass()
33-
.getClassLoader()
34-
.getResource(path)
35-
.getFile()
36-
);
37-
this.testCases = objectMapper.readTree(file);
38-
}
39-
40-
@Test void testRotLeftOne() throws JsonProcessingException {
4131

42-
ObjectMapper mapper = new ObjectMapper();
43-
44-
for (JsonNode testCase : this.testCases) {
45-
int[] input = mapper.readValue(testCase.get("input").toString(), int[].class);
46-
List<Integer> tlist = Arrays.stream(input).boxed().toList();
47-
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(tlist);
32+
this.testCases = JsonLoader.loadJson(path, ArraysLeftRotationTestCase.class);
33+
}
4834

49-
int[] expected = mapper.readValue(testCase.get("expected").toString(), int[].class);
50-
List<Integer> texpected = Arrays.stream(expected).boxed().toList();
35+
@Test void testRotLeftOne() {
5136

37+
for (ArraysLeftRotationTestCase test : this.testCases) {
38+
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(test.input);
5239

53-
assertEquals(texpected, solutionFound,
40+
assertEquals(test.expected, solutionFound,
5441
String.format("%s(%s) answer must be: %s",
5542
"CompareTriplets.compareTriplets",
56-
testCase.get("input"),
57-
testCase.get("expected"))
43+
test.input,
44+
test.expected)
5845
);
5946
}
6047
}

algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/CrushBruteForceTest.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import java.io.File;
75
import java.io.IOException;
86
import java.util.List;
97
import org.junit.jupiter.api.BeforeAll;
108
import org.junit.jupiter.api.Test;
119
import org.junit.jupiter.api.TestInstance;
1210
import org.junit.jupiter.api.TestInstance.Lifecycle;
13-
11+
import util.JsonLoader;
1412

1513
@TestInstance(Lifecycle.PER_CLASS)
1614
class CrushBruteForceTest {
@@ -26,22 +24,12 @@ public static class CrushBruteForceTestCase {
2624

2725
@BeforeAll
2826
public void setup() throws IOException {
29-
ObjectMapper objectMapper = new ObjectMapper();
3027

3128
String path = String.join("/", "hackerrank",
3229
"interview_preparation_kit",
3330
"arrays",
3431
"crush.testcases.json");
35-
File file = new File(
36-
this.getClass()
37-
.getClassLoader()
38-
.getResource(path)
39-
.getFile()
40-
);
41-
42-
ObjectMapper mapper = new ObjectMapper();
43-
this.testCases = mapper.readerForListOf(CrushBruteForceTestCase.class)
44-
.readValue(objectMapper.readTree(file));
32+
this.testCases = JsonLoader.loadJson(path, CrushBruteForceTestCase.class);
4533
}
4634

4735
@Test void testArrayManipulation() {

algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/CrushTest.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import java.io.File;
75
import java.io.IOException;
86
import java.util.List;
97
import org.junit.jupiter.api.BeforeAll;
108
import org.junit.jupiter.api.Test;
119
import org.junit.jupiter.api.TestInstance;
1210
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
1312

1413

1514
@TestInstance(Lifecycle.PER_CLASS)
@@ -26,22 +25,12 @@ public static class CrushTestCase {
2625

2726
@BeforeAll
2827
public void setup() throws IOException {
29-
ObjectMapper objectMapper = new ObjectMapper();
30-
3128
String path = String.join("/", "hackerrank",
3229
"interview_preparation_kit",
3330
"arrays",
3431
"crush.testcases.json");
35-
File file = new File(
36-
this.getClass()
37-
.getClassLoader()
38-
.getResource(path)
39-
.getFile()
40-
);
41-
42-
ObjectMapper mapper = new ObjectMapper();
43-
this.testCases = mapper.readerForListOf(CrushTestCase.class)
44-
.readValue(objectMapper.readTree(file));
32+
33+
this.testCases = JsonLoader.loadJson(path, CrushTestCase.class);
4534
}
4635

4736
@Test void testArrayManipulation() {

0 commit comments

Comments
 (0)