-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup and test cases for the failure modes with improved errors
- Loading branch information
1 parent
f7edb07
commit b3a5004
Showing
8 changed files
with
202 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/unitvectory/jsonparamunit/JsonParamError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v2.0 which accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
package com.unitvectory.jsonparamunit; | ||
|
||
/** | ||
* The JsonParamError exception used to indicate an assertion failed. | ||
* | ||
* @author Jared Hatfield (UnitVectorY Labs) | ||
*/ | ||
public class JsonParamError extends AssertionError { | ||
|
||
|
||
/** | ||
* Creates a new instance of the JsonParamError class. | ||
* | ||
* @param message the message | ||
*/ | ||
public JsonParamError(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Creates a new instance of the JsonParamError class. | ||
* | ||
* @param message the message | ||
* @param cause the cause | ||
*/ | ||
public JsonParamError(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Creates a new instance of the JsonParamError class. | ||
* | ||
* @param cause the cause | ||
*/ | ||
public JsonParamError(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/test/java/com/unitvectory/jsonparamunit/FailuresTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v2.0 which accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
package com.unitvectory.jsonparamunit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import java.io.File; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.unitvectory.fileparamunit.ListFileSource; | ||
|
||
/** | ||
* Test cases for testing the failures by intentionally misconfiguring the test case environment and | ||
* asserting the correct exception is thrown to describe the failure. | ||
* | ||
* @author Jared Hatfield (UnitVectorY Labs) | ||
*/ | ||
public class FailuresTest extends JsonNodeParamUnit { | ||
|
||
@Test | ||
public void filePathNullTest() { | ||
JsonParamError exception = assertThrows(JsonParamError.class, () -> { | ||
run(null); | ||
}); | ||
|
||
assertEquals("The provided filePath is null.", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void filePathEmptyTest() { | ||
JsonParamError exception = assertThrows(JsonParamError.class, () -> { | ||
run(""); | ||
}); | ||
|
||
assertEquals("The provided filePath is empty.", exception.getMessage()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ListFileSource(resources = "/failures/badjson", fileExtension = ".json") | ||
public void fileNotExistTest(String file) { | ||
JsonParamError exception = assertThrows(JsonParamError.class, () -> { | ||
// Changing the file path it won't exist | ||
run(file + "x"); | ||
}); | ||
|
||
assertEquals("The provided filePath does not exist.", exception.getMessage()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ListFileSource(resources = "/failures/badjson", fileExtension = ".json") | ||
public void fileIsDirectoryTest(String file) { | ||
// For this test instead of the file we are getting the directory it is in which will fail | ||
String directory = new File(file).getParent(); | ||
JsonParamError exception = assertThrows(JsonParamError.class, () -> { | ||
// Changing the file path it won't exist | ||
run(directory); | ||
}); | ||
|
||
assertEquals("The provided filePath is not a regular file.", exception.getMessage()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ListFileSource(resources = "/failures/badjson", fileExtension = ".json") | ||
public void badJsonTest(String file) { | ||
JsonParamError exception = assertThrows(JsonParamError.class, () -> { | ||
// Changing the file path it won't exist | ||
run(file); | ||
}); | ||
|
||
assertEquals("Failed to parse the JSON from the test file.", exception.getMessage()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ListFileSource(resources = "/failures/noinput", fileExtension = ".json") | ||
public void noInputTest(String file) { | ||
JsonParamError exception = assertThrows(JsonParamError.class, () -> { | ||
run(file); | ||
}); | ||
|
||
assertEquals("The 'input' JSON Object is missing from the test file.", | ||
exception.getMessage()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ListFileSource(resources = "/failures/nooutput", fileExtension = ".json") | ||
public void noOutputTest(String file) { | ||
JsonParamError exception = assertThrows(JsonParamError.class, () -> { | ||
run(file); | ||
}); | ||
|
||
assertEquals("The 'output' JSON Object is missing from the test file.", | ||
exception.getMessage()); | ||
} | ||
|
||
@Override | ||
public JsonNode process(JsonNode input, String context) { | ||
return this.getConfig().getMapper().createObjectNode(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"context": "context", | ||
"output": { | ||
"foo": "bar" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"input": { | ||
"foo": "bar" | ||
}, | ||
"context": "myContext" | ||
} |