diff --git a/.github/workflows/dev_build.yml b/.github/workflows/dev_build.yml
index 547308a..1ff8580 100644
--- a/.github/workflows/dev_build.yml
+++ b/.github/workflows/dev_build.yml
@@ -15,7 +15,7 @@ on:
branches-ignore: ["main"]
jobs:
- build:
+ dev-build:
runs-on: ubuntu-latest
steps:
- name: Checkout Taskit
diff --git a/.github/workflows/release_build.yml b/.github/workflows/release_build.yml
index 6203c86..44b8aa6 100644
--- a/.github/workflows/release_build.yml
+++ b/.github/workflows/release_build.yml
@@ -16,7 +16,7 @@ on:
branches: ["main"]
jobs:
- create-release:
+ release:
runs-on: ubuntu-latest
permissions:
contents: write
diff --git a/.github/workflows/release_pr_build.yml b/.github/workflows/release_pr_build.yml
index 4fb97f0..7637c30 100644
--- a/.github/workflows/release_pr_build.yml
+++ b/.github/workflows/release_pr_build.yml
@@ -13,7 +13,7 @@ on:
branches: ["main"]
jobs:
- main-build:
+ release-pr-build:
runs-on: ubuntu-latest
permissions:
contents: write
@@ -26,6 +26,16 @@ jobs:
with:
java-version: '17'
distribution: 'temurin'
+
+ - name: Get Version
+ run: |
+ echo "version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout --file pom.xml)" >> "$GITHUB_ENV"
+ - name: Version Is Snapshot
+ if: ${{ endsWith(env.version, 'SNAPSHOT') }}
+ run: |
+ echo "::error Version is a SNAPSHOT. Update version to proper version."
+ exit 1
+
- name: Build Taskit
run: mvn clean install -Pjavadoc,jacoco --file pom.xml
\ No newline at end of file
diff --git a/README.md b/README.md
index e14f8ee..7120523 100644
--- a/README.md
+++ b/README.md
@@ -71,9 +71,9 @@ See [TestObject](protobuf/src/main/proto/gov/hhs/aspr/ms/taskit/protobuf/testobj
- Maven 3.8.x
- Java 17
- Favorite IDE for Java development
-- Modeling Util located [here](https://github.com/HHS/ASPR-ms-util)
+- Modeling Utilities located [here](https://github.com/HHS/ASPR-ms-util)
-*Note that Modeling Util is in Maven Central, so there is no need to download and build it.
+*Note that Modeling Utilities is in Maven Central, so there is no need to clone and build it.
### Building
To build this project:
diff --git a/core/pom.xml b/core/pom.xml
index 4984da0..d94c3b2 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -17,7 +17,8 @@
Translation and Serialization Toolkit (Taskit): CoreThe core components of a toolkit that aids in the conversion between two Java objects, primiarly for use with coverting between an input Java object and a application Java object.
-
+ https://github.com/HHS/aspr-ms-taskit
+
scm:git:git://github.com/HHS/aspr-ms-taskit.git
diff --git a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/CoreTranslationError.java b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/CoreTranslationError.java
index a586bfc..965eb7c 100644
--- a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/CoreTranslationError.java
+++ b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/CoreTranslationError.java
@@ -4,54 +4,48 @@
public enum CoreTranslationError implements ContractError {
- CIRCULAR_TRANSLATOR_DEPENDENCIES("Circular translator dependencies: "),
- DUPLICATE_CLASSREF("Duplicate ClassRef"),
- DUPLICATE_OUTPUT_PATH_KEY("Duplicate ClassRef and Scenario Pair"),
- DUPLICATE_DEPENDENCY("Duplicate Dependency"),
- DUPLICATE_INPUT_PATH("Duplicate Input Path"),
- DUPLICATE_OUTPUT_PATH("Duplicate Output Path"),
- DUPLICATE_TRANSLATOR("Duplicate Translator"),
- DUPLICATE_TRANSLATION_SPEC("Duplicate TranslationSpec"),
- INVALID_TRANSLATION_ENGINE_CLASS_REF(
- "The given Translation Engine classRef does not match the class of the actual Translation Engine"),
- INVALID_TRANSLATION_ENGINE_BUILDER_CLASS_REF(
- "The given Translation Engine Builder classRef does not match the class of the actual Translation Engine Builder"),
- INVALID_OUTPUT_CLASSREF("The given class does not have a output file path associated with it."),
- INVALID_OUTPUT_PATH(
+ CIRCULAR_TRANSLATOR_DEPENDENCIES("Circular translator dependencies: "),
+ DUPLICATE_CLASSREF("Duplicate ClassRef"),
+ DUPLICATE_DEPENDENCY("Duplicate Dependency"),
+ DUPLICATE_INPUT_PATH("Duplicate Input Path"),
+ DUPLICATE_TRANSLATOR("Duplicate Translator"),
+ DUPLICATE_TRANSLATION_SPEC("Duplicate TranslationSpec"),
+ INVALID_INPUT_PATH("The given input file path does not exist"),
+ INVALID_OUTPUT_PATH(
"The given output file path does not exist. While the file will be created on write, the directory will not."),
- INVALID_INPUT_PATH("The given input file path does not exist"),
- MISSING_TRANSLATOR("Missing Translator: "),
- NO_TRANSLATION_ENGINES("There are no translation engines added to this controller."),
- NULL_TRANSLATOR_ID("Null TranslatorId"),
- NULL_TRANSLATOR("Null Translator"),
- NULL_TRANSLATION_ENGINE_BUILDER("Null Translation Engine Builder"),
- NULL_TRANSLATION_ENGINE("Null Translation Engine"),
- NULL_OBJECT_FOR_TRANSLATION("The object to be translated was null"),
- NULL_INIT_CONSUMER("Null Initilizer Consumer"),
- NULL_DEPENDENCY("Null dependency"),
- NULL_PATH("Null Path"),
- NULL_CLASS_REF("Null Class Ref"),
- NULL_TRANSLATION_SPEC("Null TranslationSpec"),
- NULL_TRANSLATION_SPEC_APP_CLASS("Null TranslationSpec App Class"),
- NULL_TRANSLATION_SPEC_INPUT_CLASS("Null TranslationSpec Input Class"),
- UNKNOWN_TRANSLATION_SPEC("No translation spec was provided for the given class"),
- UNITIALIZED_TRANSLATION_SPEC("TranslationSpec not initialized"),
- UNINITIALIZED_TRANSLATORS(
- "Translators were added to the builder but were not initialized. Make sure to call super.initTranslators() during your custom engine build method"),
- UNKNOWN_OBJECT("Object is not Translatable by this TranslationSpec"),
- UNKNWON_TRANSLATION_ENGINE_TYPE("Translation Engine Type was not set"),
- UNKNOWN_CLASSREF("No object has been read in with the specified classRef"),
- UNKNOWN_OUTPUT_PATH_KEY("The provided key does not exist"),
- MISSING_OUTPUT_PATH_KEYS("The number of keys provided does not match the number of objects to be written");
+ INVALID_TRANSLATION_ENGINE_BUILDER_CLASS_REF(
+ "The given Translation Engine Builder classRef does not match the class of the actual Translation Engine Builder"),
+ MISSING_TRANSLATOR("Missing Translator: "),
+ NO_TRANSLATION_ENGINES("There are no translation engines added to this controller."),
+ NULL_CLASS_REF("Null Class Ref"),
+ NULL_DEPENDENCY("Null dependency"),
+ NULL_INIT_CONSUMER("Null Initilizer Consumer"),
+ NULL_OBJECT_FOR_TRANSLATION("The object to be translated was null"),
+ NULL_PATH("Null Path"),
+ NULL_TRANSLATION_ENGINE("Null Translation Engine"),
+ NULL_TRANSLATION_ENGINE_BUILDER("Null Translation Engine Builder"),
+ NULL_TRANSLATION_SPEC("Null TranslationSpec"),
+ NULL_TRANSLATION_SPEC_APP_CLASS("Null TranslationSpec App Class"),
+ NULL_TRANSLATION_SPEC_INPUT_CLASS("Null TranslationSpec Input Class"),
+ NULL_TRANSLATOR("Null Translator"),
+ NULL_TRANSLATOR_ID("Null TranslatorId"),
+ UNINITIALIZED_TRANSLATION_SPEC("TranslationSpec not initialized"),
+ UNINITIALIZED_TRANSLATORS(
+ "Translators were added to the builder but were not initialized. Make sure to call super.initTranslators() during your custom engine build method"),
+ UNKNOWN_CLASSREF("No object has been read in with the specified classRef"),
+ UNKNOWN_OBJECT("Object is not Translatable by this TranslationSpec"),
+ UNKNWON_TRANSLATION_ENGINE_TYPE("Translation Engine Type was not set"),
+ UNKNOWN_TRANSLATION_SPEC("No translation spec was provided for the given class"),
+ UNSUPPORTED_VERSION("The given version is not supported");
- private final String description;
+ private final String description;
- private CoreTranslationError(final String description) {
- this.description = description;
- }
+ private CoreTranslationError(final String description) {
+ this.description = description;
+ }
- @Override
- public String getDescription() {
- return description;
- }
+ @Override
+ public String getDescription() {
+ return description;
+ }
}
diff --git a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationController.java b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationController.java
index b3a899b..1dc7737 100644
--- a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationController.java
+++ b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationController.java
@@ -12,8 +12,6 @@
import java.util.Optional;
import java.util.Set;
-import org.apache.commons.math3.util.Pair;
-
import gov.hhs.aspr.ms.util.errors.ContractException;
/**
@@ -36,8 +34,6 @@ final static class Data {
protected final List translators = new ArrayList<>();
protected final Map> inputFilePathMap = new LinkedHashMap<>();
protected final Map inputFilePathEngine = new LinkedHashMap<>();
- protected final Map outputFilePathMap = new LinkedHashMap<>();
- protected final Map outputFilePathEngine = new LinkedHashMap<>();
protected final Map, Class>> parentChildClassRelationshipMap = new LinkedHashMap<>();
Data() {
@@ -63,14 +59,10 @@ private void validateFilePathNotNull(Path filePath) {
}
}
- private void validatePathNotDuplicate(Path filePath, boolean in, boolean out) {
- if (in && this.data.inputFilePathMap.containsKey(filePath)) {
+ private void validatePathNotDuplicate(Path filePath) {
+ if (this.data.inputFilePathMap.containsKey(filePath)) {
throw new ContractException(CoreTranslationError.DUPLICATE_INPUT_PATH);
}
-
- if (out && this.data.outputFilePathMap.values().contains(filePath)) {
- throw new ContractException(CoreTranslationError.DUPLICATE_OUTPUT_PATH);
- }
}
private void validateTranslationEngineNotNull(TranslationEngine translationEngine) {
@@ -133,7 +125,7 @@ TranslationController buildWithoutInitAndChecks() {
public Builder addInputFilePath(Path filePath, Class> classRef, TranslationEngineType translationEngineType) {
validateFilePathNotNull(filePath);
validateClassRefNotNull(classRef);
- validatePathNotDuplicate(filePath, true, false);
+ validatePathNotDuplicate(filePath);
if (!filePath.toFile().exists()) {
throw new ContractException(CoreTranslationError.INVALID_INPUT_PATH);
@@ -144,98 +136,6 @@ public Builder addInputFilePath(Path filePath, Class> classRef, TranslationEng
return this;
}
- /**
- * Adds the path and class ref to be written to after building via
- * {@link TranslationController#writeOutput} with the given classRef and
- * scenario id of 0 as the key
- *
- * @throws ContractException
- *
- *
{@linkplain CoreTranslationError#NULL_PATH} if
- * filePath is null
- *
{@linkplain CoreTranslationError#NULL_CLASS_REF}
- * if classRef is null
- *
{@linkplain CoreTranslationError#DUPLICATE_OUTPUT_PATH}
- * if filePath has already been added
- *
{@linkplain CoreTranslationError#DUPLICATE_OUTPUT_PATH_KEY}
- * if the classRef and scenarioId pair has already
- * been added
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_PATH}
- * if filePath does not exist on the system. Note this
- * check is for the folder, not the file. The file
- * will be created if necessary by the writer
- *
- */
- public Builder addOutputFilePath(Path filePath, Class> classRef,
- TranslationEngineType translationEngineType) {
- return this.addOutputFilePath(filePath, classRef, 0, translationEngineType);
- }
-
- /**
- * Adds the path and class ref to be written to after building via
- * {@link TranslationController#writeOutput} with the given classRef and
- * scenarioId as the key
- *
- * @throws ContractException
- *
- *
{@linkplain CoreTranslationError#NULL_PATH} if
- * filePath is null
- *
{@linkplain CoreTranslationError#NULL_CLASS_REF}
- * if classRef is null
- *
{@linkplain CoreTranslationError#DUPLICATE_OUTPUT_PATH}
- * if filePath has already been added
- *
{@linkplain CoreTranslationError#DUPLICATE_OUTPUT_PATH_KEY}
- * if the classRef and scenarioId pair has already
- * been added
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_PATH}
- * if filePath does not exist on the system. Note this
- * check is for the folder, not the file. The file
- * will be created if necessary by the writer
- *
- */
- public Builder addOutputFilePath(Path filePath, Class> classRef, Integer scenarioId,
- TranslationEngineType translationEngineType) {
- validateClassRefNotNull(classRef);
-
- return this.addOutputFilePath(filePath, classRef.getName() + ":" + scenarioId, translationEngineType);
- }
-
- /**
- * Adds the path and class ref to be written to after building via
- * {@link TranslationController#writeOutput} with the given key
- *
- * @throws ContractException
- *
- *
{@linkplain CoreTranslationError#NULL_PATH} if
- * filePath is null
- *
{@linkplain CoreTranslationError#DUPLICATE_OUTPUT_PATH}
- * if filePath has already been added
- *
{@linkplain CoreTranslationError#DUPLICATE_OUTPUT_PATH_KEY}
- * if the classRef and scenarioId pair has already
- * been added
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_PATH}
- * if filePath does not exist on the system. Note this
- * check is for the folder, not the file. The file
- * will be created if necessary by the writer
- *
- */
- public Builder addOutputFilePath(Path filePath, String key, TranslationEngineType translationEngineType) {
- validateFilePathNotNull(filePath);
- validatePathNotDuplicate(filePath, false, true);
-
- if (this.data.outputFilePathMap.containsKey(key)) {
- throw new ContractException(CoreTranslationError.DUPLICATE_OUTPUT_PATH_KEY);
- }
-
- if (!filePath.getParent().toFile().exists()) {
- throw new ContractException(CoreTranslationError.INVALID_OUTPUT_PATH);
- }
-
- this.data.outputFilePathMap.put(key, filePath);
- this.data.outputFilePathEngine.put(filePath, translationEngineType);
- return this;
- }
-
/**
* Adds the given classRef markerInterace mapping.
*
@@ -296,10 +196,9 @@ public Builder addTranslationEngine(TranslationEngine translationEngine) {
this.data.parentChildClassRelationshipMap.put(childClassRef, parentClassRef);
}
-
+
return this;
}
-
}
/**
@@ -309,38 +208,6 @@ public static Builder builder() {
return new Builder(new Data());
}
- /**
- * Passes the given reader and inputClassRef to the built
- * {@link TranslationEngine} to read, parse and translate the inputData.
- *
- * @param the classType associated with the reader
- */
- void readInput(Path path, Class inputClassRef, TranslationEngine translationEngine) {
- Object appObject;
- try {
- appObject = translationEngine.readInput(path, inputClassRef);
- this.objects.add(appObject);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * Passes the given writer object and optional superClass to the built
- * {@link TranslationEngine} to translate and write to the outputFile
- *
- * @param the class of the object to write to the outputFile
- * @param the optional parent class of the object to write to the outputFile
- */
- void writeOutput(Path path, M object, Optional> superClass,
- TranslationEngine translationEngine) {
- try {
- translationEngine.writeOutput(path, object, superClass);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
void initTranslationEngines() {
for (TranslationEngine translationEngine : this.data.translationEngines) {
translationEngine.translationSpecsAreInitialized();
@@ -412,200 +279,127 @@ public TranslationController readInput() {
}
/**
- * Given the classRef and scenarioId, find the given outputFilePath. If the
- * classRef Scenario pair has been added, that is returned. Otherwise, checks to
- * see if the classRef exists in the parentChildClassRelationshipMap and if so,
- * returns
- * the resulting classRef scenarioId pair
- *
- * @param the childClass
- * @param the optional parentClass/MarkerInterfaceClass
- */
- Pair>> getOutputPathKey(Class classRef, Integer scenarioId) {
- String key = classRef.getName() + ":" + scenarioId;
-
- if (this.data.outputFilePathMap.containsKey(key)) {
- return new Pair<>(key, Optional.empty());
- }
-
- if (this.data.parentChildClassRelationshipMap.containsKey(classRef)) {
- // can safely cast because of type checking when adding to the
- // parentChildClassRelationshipMap
- @SuppressWarnings("unchecked")
- Class parentClass = (Class) this.data.parentChildClassRelationshipMap.get(classRef);
-
- key = parentClass.getName() + ":" + scenarioId;
-
- if (this.data.outputFilePathMap.containsKey(key)) {
- return new Pair<>(key, Optional.of(parentClass));
- }
- }
-
- throw new ContractException(CoreTranslationError.INVALID_OUTPUT_CLASSREF,
- "No path was provided for " + classRef.getName());
- }
-
- /**
- * Given the classRef and scenarioId, find the given outputFilePath. If the
- * classRef Scenario pair has been added, that is returned. Otherwise, checks to
- * see if the classRef exists in the parentChildClassRelationshipMap and if so,
- * returns
- * the resulting classRef scenarioId pair
+ * Passes the given reader and inputClassRef to the built
+ * {@link TranslationEngine} to read, parse and translate the inputData.
*
- * @param the childClass
- * @param the optional parentClass/MarkerInterfaceClass
+ * @param the classType associated with the reader
*/
- Path getOutputPath(String key) {
- if (this.data.outputFilePathMap.containsKey(key)) {
- return this.data.outputFilePathMap.get(key);
+ void readInput(Path path, Class inputClassRef, TranslationEngine translationEngine) {
+ Object appObject;
+ try {
+ appObject = translationEngine.readInput(path, inputClassRef);
+ this.objects.add(appObject);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
-
- throw new ContractException(CoreTranslationError.INVALID_OUTPUT_CLASSREF,
- "No path was provided for " + key);
}
/**
- * takes the list of objects and writes each object out to it's corresponding
- * outputFilePath, if it exists
- *
- * internally calls {@link TranslationController#writeOutput(Object)}
+ * writes the given object to the given path using the given translation engine
+ * type
*
- * @param the type of the list of obects to write to output
+ * @param the classType of the object
* @throws ContractException
*
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_CLASSREF}
- * if the class of the object paired with the
- * scenarioId does not have a associated
- * outputFilePath
+ *
{@linkplain CoreTranslationError#NULL_OBJECT_FOR_TRANSLATION}
+ * if the object is null
+ *
{@linkplain CoreTranslationError#NULL_PATH}
+ * if the path is null
+ *
{@linkplain CoreTranslationError#INVALID_OUTPUT_PATH}
+ * if the path does not exist (specifically the parent
+ * directory of the path ie. a/b/c/foo.txt throws this
+ * if a/b/c doesn't exist
*
{@linkplain CoreTranslationError#NULL_TRANSLATION_ENGINE}
* if translationEngine is null
*
*/
- public void writeOutput(List objects) {
- for (T object : objects) {
- this.writeOutput(object);
+ public void writeOutput(M object, Path path,
+ TranslationEngineType translationEngineType) {
+
+ Optional> parentClassRef = Optional.empty();
+
+ if (this.data.parentChildClassRelationshipMap.containsKey(object.getClass())) {
+ // can safely cast because of type checking when adding to the
+ // parentChildClassRelationshipMap
+ @SuppressWarnings("unchecked")
+ Class parentClass = (Class) this.data.parentChildClassRelationshipMap.get(object.getClass());
+
+ parentClassRef = Optional.of(parentClass);
}
+ this.writeOutput(object, parentClassRef, path, translationEngineType);
}
/**
- * takes the list of objects and writes each object out to it's corresponding
- * outputFilePath using the supplied key, if it exists
- *
- * internally calls {@link TranslationController#writeOutput(Object)}
+ * writes the given object to the given path using the given translation engine
+ * type using the parent class as the actual output type
*
- * @param the type of the list of obects to write to output
+ * @param the classType of the object
* @throws ContractException
*
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_CLASSREF}
- * if the supplied key has no valid outputFilePath
- * mapping
+ *
{@linkplain CoreTranslationError#NULL_CLASS_REF}
+ * if the parent classref is null
+ *
{@linkplain CoreTranslationError#NULL_OBJECT_FOR_TRANSLATION}
+ * if the object is null
+ *
{@linkplain CoreTranslationError#NULL_PATH}
+ * if the path is null
+ *
{@linkplain CoreTranslationError#INVALID_OUTPUT_PATH}
+ * if the path does not exist (specifically the parent
+ * directory of the path ie. a/b/c/foo.txt throws this
+ * if a/b/c doesn't exist
*
{@linkplain CoreTranslationError#NULL_TRANSLATION_ENGINE}
* if translationEngine is null
*
*/
- public void writeOutput(Map objects) {
- for (String key : objects.keySet()) {
- this.writeOutput(objects.get(key), key);
+ public void writeOutput(M object, Class parentClassRef, Path path,
+ TranslationEngineType translationEngineType) {
+
+ if (parentClassRef == null) {
+ throw new ContractException(CoreTranslationError.NULL_CLASS_REF);
}
+
+ this.writeOutput(object, Optional.of(parentClassRef), path, translationEngineType);
}
- /**
- * takes the list of objects with the specified scenarioId and writes each
- * object out to it's corresponding outputFilePath, if it exists
- *
- * internally calls {@link TranslationController#writeOutput(Object, Integer)}
- *
- * @param the type of the list of obects to write to output
- * @throws ContractException
- *
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_CLASSREF}
- * if the class of the object paired with the
- * scenarioId does not have a associated
- * outputFilePath
- *
{@linkplain CoreTranslationError#NULL_TRANSLATION_ENGINE}
- * if translationEngine is null
- *
- */
- public void writeOutput(List objects, Integer scenarioId) {
- for (T object : objects) {
- this.writeOutput(object, scenarioId);
+ void writeOutput(M object, Optional> parentClassRef, Path path,
+ TranslationEngineType translationEngineType) {
+
+ if (object == null) {
+ throw new ContractException(CoreTranslationError.NULL_OBJECT_FOR_TRANSLATION);
}
- }
- /**
- * takes the given object and writes it out to it's corresponding
- * outputFilePath, if it exists
- *
- * internally calls {@link TranslationController#writeOutput(Object, Integer)}
- * with a scenarioId of 0
- *
- * @param the type of the list of obects to write to output
- * @throws ContractException
- *
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_CLASSREF}
- * if the class of the object paired with the
- * scenarioId does not have a associated
- * outputFilePath
- *
{@linkplain CoreTranslationError#NULL_TRANSLATION_ENGINE}
- * if translationEngine is null
- *
- */
- public void writeOutput(T object) {
- this.writeOutput(object, 0);
- }
+ if (path == null) {
+ throw new ContractException(CoreTranslationError.NULL_PATH);
+ }
- /**
- * takes the given object and scenarioId pair and writes it out to it's
- * corresponding outputFilePath, if it exists
- *
- * @param the classType of the object
- * @param the optional type of the parent class of the object
- * @throws ContractException
- *
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_CLASSREF}
- * if the class of the object paired with the
- * scenarioId does not have a associated
- * outputFilePath
- *
{@linkplain CoreTranslationError#NULL_TRANSLATION_ENGINE}
- * if translationEngine is null
- *
- */
- @SuppressWarnings("unchecked")
- public void writeOutput(M object, Integer scenarioId) {
- // this gives an unchecked warning, surprisingly
- Class classRef = (Class) object.getClass();
+ if (!path.getParent().toFile().exists()) {
+ throw new ContractException(CoreTranslationError.INVALID_OUTPUT_PATH);
+ }
+
+ TranslationEngine translationEngine = this.translationEngines.get(translationEngineType);
- Pair>> keyPair = getOutputPathKey(classRef, scenarioId);
+ if (translationEngine == null) {
+ throw new ContractException(CoreTranslationError.NULL_TRANSLATION_ENGINE);
+ }
- this.writeOutput(object, keyPair.getFirst(), keyPair.getSecond());
+ this.writeOutput(path, object, parentClassRef, translationEngine);
}
/**
- * takes the given object and scenarioId pair and writes it out to it's
- * corresponding outputFilePath, if it exists
+ * Passes the given writer object and optional superClass to the built
+ * {@link TranslationEngine} to translate and write to the outputFile
*
- * @param the classType of the object
- * @throws ContractException
- *
- *
{@linkplain CoreTranslationError#INVALID_OUTPUT_CLASSREF}
- * if the supplied key has no valid outputFilePath
- * mapping
- *
{@linkplain CoreTranslationError#NULL_TRANSLATION_ENGINE}
- * if translationEngine is null
- *
+ * @param the class of the object to write to the outputFile
+ * @param the optional parent class of the object to write to the outputFile
*/
- public void writeOutput(M object, String key) {
- this.writeOutput(object, key, Optional.empty());
+ void writeOutput(Path path, M object, Optional> superClass,
+ TranslationEngine translationEngine) {
+ try {
+ translationEngine.writeOutput(path, object, superClass);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
- void writeOutput(M object, String key, Optional> parentClassRef) {
- Path path = getOutputPath(key);
-
- TranslationEngineType type = this.data.outputFilePathEngine.get(path);
- TranslationEngine translationEngine = this.translationEngines.get(type);
-
- this.writeOutput(path, object, parentClassRef, translationEngine);
- }
/**
* Searches the list of read in objects and returns the first Object found of
* the given classRef
diff --git a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationEngine.java b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationEngine.java
index 327a1c5..4849a89 100644
--- a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationEngine.java
+++ b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationEngine.java
@@ -78,7 +78,8 @@ public boolean equals(Object obj) {
/**
* This class contains protected final methods for all of its abstract methods.
- * All descendant classes of this class MUST call these if you want it to function properly.
+ * All descendant classes of this class MUST call these if you want it to
+ * function properly.
*/
public abstract static class Builder {
protected Data data;
@@ -121,6 +122,7 @@ private void validateClassRefNotNull(Class> classRef) {
void clearBuilder() {
this.data = new Data();
}
+
/**
* Builder for the TranslationEngine
*/
@@ -212,17 +214,19 @@ protected final void _addTranslator(Translator translator) {
* added
*
*/
- public abstract Builder addParentChildClassRelationship(Class classRef, Class markerInterface);
+ public abstract Builder addParentChildClassRelationship(Class classRef,
+ Class parentClassRef);
- protected final void _addParentChildClassRelationship(Class classRef, Class markerInterface) {
+ protected final void _addParentChildClassRelationship(Class classRef,
+ Class parentClassRef) {
validateClassRefNotNull(classRef);
- validateClassRefNotNull(markerInterface);
+ validateClassRefNotNull(parentClassRef);
if (this.data.childToParentClassMap.containsKey(classRef)) {
throw new ContractException(CoreTranslationError.DUPLICATE_CLASSREF);
}
- this.data.childToParentClassMap.put(classRef, markerInterface);
+ this.data.childToParentClassMap.put(classRef, parentClassRef);
}
/*
@@ -432,6 +436,7 @@ protected void validateInit() {
validateTranslationEngineType();
validateTranslatorsInitialized();
}
+
/**
* returns whether this translationEngine is initialized or not
*/
@@ -469,7 +474,8 @@ public Set getTranslationSpecs() {
* abstract method that must be implemented by child TranslatorCores that
* defines how to write to output files
*/
- protected abstract void writeOutput(Path path, M appObject, Optional> superClass) throws IOException;
+ protected abstract void writeOutput(Path path, M appObject, Optional> superClass)
+ throws IOException;
/**
* abstract method that must be implemented by child TranslatorCores that
diff --git a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationSpec.java b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationSpec.java
index e20951c..40c0f46 100644
--- a/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationSpec.java
+++ b/core/src/main/java/gov/hhs/aspr/ms/taskit/core/TranslationSpec.java
@@ -133,7 +133,7 @@ public boolean equals(Object obj) {
void checkInit() {
if (!this.initialized) {
- throw new ContractException(CoreTranslationError.UNITIALIZED_TRANSLATION_SPEC);
+ throw new ContractException(CoreTranslationError.UNINITIALIZED_TRANSLATION_SPEC);
}
}
}
diff --git a/core/src/test/java/gov/hhs/aspr/ms/taskit/core/AT_TranslationController.java b/core/src/test/java/gov/hhs/aspr/ms/taskit/core/AT_TranslationController.java
index 8bae610..554f1db 100644
--- a/core/src/test/java/gov/hhs/aspr/ms/taskit/core/AT_TranslationController.java
+++ b/core/src/test/java/gov/hhs/aspr/ms/taskit/core/AT_TranslationController.java
@@ -8,19 +8,16 @@
import java.io.IOException;
import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
-import org.apache.commons.math3.util.Pair;
import org.junit.jupiter.api.Test;
import gov.hhs.aspr.ms.taskit.core.testsupport.TestObjectUtil;
import gov.hhs.aspr.ms.taskit.core.testsupport.TestTranslationEngine;
import gov.hhs.aspr.ms.taskit.core.testsupport.testcomplexobject.TestComplexAppObject;
import gov.hhs.aspr.ms.taskit.core.testsupport.testcomplexobject.TestComplexObjectTranslator;
+import gov.hhs.aspr.ms.taskit.core.testsupport.testobject.TestAppChildObject;
import gov.hhs.aspr.ms.taskit.core.testsupport.testobject.TestAppObject;
import gov.hhs.aspr.ms.taskit.core.testsupport.testobject.TestObjectTranslator;
import gov.hhs.aspr.ms.taskit.core.testsupport.testobject.input.TestInputObject;
@@ -170,13 +167,12 @@ public void testReadInput() {
TranslationController translationController = TranslationController.builder()
.addInputFilePath(filePath.resolve(fileName), TestInputObject.class,
TranslationEngineType.CUSTOM)
- .addOutputFilePath(filePath.resolve(fileName), TestAppObject.class,
- TranslationEngineType.CUSTOM)
.addTranslationEngine(testTranslationEngine).build();
TestAppObject expectedAppObject = TestObjectUtil.generateTestAppObject();
- translationController.writeOutput(expectedAppObject);
+ translationController.writeOutput(expectedAppObject, filePath.resolve(fileName),
+ TranslationEngineType.CUSTOM);
translationController.readInput();
@@ -223,189 +219,84 @@ public void testWriteOutput_Engine() {
}
@Test
- @UnitTestForCoverage
- public void testGetOutputPathKey() {
- String fileName = "GetOutputPath_1-testOutput.json";
- String fileName2 = "GetOutputPath_2-testOutput.json";
-
- ResourceHelper.createOutputFile(filePath, fileName);
- ResourceHelper.createOutputFile(filePath, fileName2);
-
- TranslationController translationController = TranslationController.builder()
- .addTranslationEngine(TestTranslationEngine.builder().build())
- .addOutputFilePath(filePath.resolve(fileName), TestAppObject.class,
- TranslationEngineType.CUSTOM)
- .addOutputFilePath(filePath.resolve(fileName2), Object.class, 1,
- TranslationEngineType.CUSTOM)
- .addParentChildClassRelationship(TestAppObject.class, Object.class).build();
-
- Pair>> expectedPair1 = new Pair<>(
- TestAppObject.class.getName() + ":" + 0,
- Optional.empty());
- Pair>> expectedPair2 = new Pair<>(Object.class.getName() + ":" + 1,
- Optional.of(Object.class));
-
- Pair>> actualPair1 = translationController
- .getOutputPathKey(TestAppObject.class, 0);
- Pair>> actualPair2 = translationController
- .getOutputPathKey(TestAppObject.class, 1);
-
- assertEquals(expectedPair1, actualPair1);
- assertEquals(expectedPair2, actualPair2);
- // preconditions
-
- // if the class scenarioId pair does not exist and there is no parent child
- // class relationship
- ContractException contractException = assertThrows(ContractException.class, () -> {
- translationController.getOutputPathKey(TestInputObject.class, 1);
- });
-
- assertEquals(CoreTranslationError.INVALID_OUTPUT_CLASSREF, contractException.getErrorType());
-
- // if the class and scenarioID pair does not exist AND there is a parent child
- // class relationship AND the parentClass scenarioId pair does not exists
- contractException = assertThrows(ContractException.class, () -> {
- translationController.getOutputPathKey(TestAppObject.class, 4);
- });
-
- assertEquals(CoreTranslationError.INVALID_OUTPUT_CLASSREF, contractException.getErrorType());
- }
-
- @Test
- @UnitTestMethod(target = TranslationController.class, name = "writeOutput", args = { List.class })
- public void testWriteOutput_List() {
- String fileName = "WriteOutput_List_1-testOutput.json";
- String fileName2 = "WriteOutput_List_2-testOutput.json";
+ @UnitTestMethod(target = TranslationController.class, name = "writeOutput", args = { Object.class,
+ Path.class, TranslationEngineType.class })
+ public void testWriteOutput() {
+ String fileName = "writeOutput-testOutput.json";
ResourceHelper.createOutputFile(filePath, fileName);
- ResourceHelper.createOutputFile(filePath, fileName2);
TestTranslationEngine testTranslationEngine = TestTranslationEngine.builder()
.addTranslator(TestObjectTranslator.getTranslator())
- .addTranslator(TestComplexObjectTranslator.getTranslator()).build();
+ .addTranslator(TestComplexObjectTranslator.getTranslator())
+ .addParentChildClassRelationship(TestAppChildObject.class, TestAppObject.class)
+ .build();
TranslationController translationController = TranslationController.builder()
- .addOutputFilePath(filePath.resolve(fileName), TestAppObject.class,
- TranslationEngineType.CUSTOM)
- .addOutputFilePath(filePath.resolve(fileName2), TestComplexAppObject.class,
- TranslationEngineType.CUSTOM)
.addTranslationEngine(testTranslationEngine).build();
- List