Skip to content

[FEATURE] New utility to encapsulate JSON loading. #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions algorithm-exercises-java/src/main/java/util/JsonLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package util;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
* JsonLoader.
*/
public final class JsonLoader {

private JsonLoader() {}

/**
* loadJson.
*/
public static <T> List<T> loadJson(String path, Class<T> type) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

File file = new File(
JsonLoader.class.getClassLoader()
.getResource(path)
.getFile()
);

ObjectMapper mapper = new ObjectMapper();
return mapper.readerForListOf(type)
.readValue(objectMapper.readTree(file));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,46 @@

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import util.JsonLoader;

@TestInstance(Lifecycle.PER_CLASS)
class ArraysLeftRotationTest {

public JsonNode testCases;
public static class ArraysLeftRotationTestCase {
public List<Integer> input;
public List<Integer> expected;
}

List<ArraysLeftRotationTestCase> testCases;

@BeforeAll
public void setup() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

String path = String.join("/", "hackerrank",
"interview_preparation_kit",
"arrays",
"ctci_array_left_rotation.testcases.json");
File file = new File(
this.getClass()
.getClassLoader()
.getResource(path)
.getFile()
);
this.testCases = objectMapper.readTree(file);
}

@Test void testRotLeftOne() throws JsonProcessingException {

ObjectMapper mapper = new ObjectMapper();

for (JsonNode testCase : this.testCases) {
int[] input = mapper.readValue(testCase.get("input").toString(), int[].class);
List<Integer> tlist = Arrays.stream(input).boxed().toList();
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(tlist);
this.testCases = JsonLoader.loadJson(path, ArraysLeftRotationTestCase.class);
}

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

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

assertEquals(texpected, solutionFound,
assertEquals(test.expected, solutionFound,
String.format("%s(%s) answer must be: %s",
"CompareTriplets.compareTriplets",
testCase.get("input"),
testCase.get("expected"))
test.input,
test.expected)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

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

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import util.JsonLoader;

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

@BeforeAll
public void setup() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

String path = String.join("/", "hackerrank",
"interview_preparation_kit",
"arrays",
"crush.testcases.json");
File file = new File(
this.getClass()
.getClassLoader()
.getResource(path)
.getFile()
);

ObjectMapper mapper = new ObjectMapper();
this.testCases = mapper.readerForListOf(CrushBruteForceTestCase.class)
.readValue(objectMapper.readTree(file));
this.testCases = JsonLoader.loadJson(path, CrushBruteForceTestCase.class);
}

@Test void testArrayManipulation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

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

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import util.JsonLoader;


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

@BeforeAll
public void setup() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

String path = String.join("/", "hackerrank",
"interview_preparation_kit",
"arrays",
"crush.testcases.json");
File file = new File(
this.getClass()
.getClassLoader()
.getResource(path)
.getFile()
);

ObjectMapper mapper = new ObjectMapper();
this.testCases = mapper.readerForListOf(CrushTestCase.class)
.readValue(objectMapper.readTree(file));

this.testCases = JsonLoader.loadJson(path, CrushTestCase.class);
}

@Test void testArrayManipulation() {
Expand Down
Loading