Skip to content
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

Readd Parent Child Class Relationship #39

Merged
merged 3 commits into from
Mar 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final static class Data {
protected final List<Translator> translators = new ArrayList<>();
protected final Map<Path, Class<?>> inputFilePathMap = new LinkedHashMap<>();
protected final Map<Path, TranslationEngineType> inputFilePathEngine = new LinkedHashMap<>();
protected final Map<Class<?>, Class<?>> parentChildClassRelationshipMap = new LinkedHashMap<>();

Data() {
}
Expand Down Expand Up @@ -135,6 +136,37 @@ public Builder addInputFilePath(Path filePath, Class<?> classRef, TranslationEng
return this;
}

/**
* Adds the given classRef markerInterace mapping.
* <p>
* explicitly used when calling {@link TranslationController#writeOutput} with a
* class for which a classRef ScenarioId pair does not exist and/or the need to
* output the given class as the markerInterface instead of the concrete class
*
* @param <M> the childClass
* @param <U> the parentClass/MarkerInterfaceClass
* @throws ContractException
* <ul>
* <li>{@linkplain CoreTranslationError#NULL_CLASS_REF}
* if classRef is null or if markerInterface is
* null</li>
* <li>{@linkplain CoreTranslationError#DUPLICATE_CLASSREF}
* if child parent relationship has already been
* added</li>
* </ul>
*/
public <M extends U, U> Builder addParentChildClassRelationship(Class<M> classRef, Class<U> parentClassRef) {
validateClassRefNotNull(classRef);
validateClassRefNotNull(parentClassRef);

if (this.data.parentChildClassRelationshipMap.containsKey(classRef)) {
throw new ContractException(CoreTranslationError.DUPLICATE_CLASSREF);
}

this.data.parentChildClassRelationshipMap.put(classRef, parentClassRef);
return this;
}

/**
* Adds a {@link TranslationEngine.Builder}
*
Expand All @@ -151,7 +183,6 @@ public Builder addTranslationEngine(TranslationEngine translationEngine) {

return this;
}

}

/**
Expand Down Expand Up @@ -269,7 +300,17 @@ <U> void readInput(Path path, Class<U> inputClassRef, TranslationEngine translat
public <M extends U, U> void writeOutput(M object, Path path,
TranslationEngineType translationEngineType) {

this.writeOutput(object, Optional.empty(), path, translationEngineType);
Optional<Class<U>> 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<U> parentClass = (Class<U>) this.data.parentChildClassRelationshipMap.get(object.getClass());

parentClassRef = Optional.of(parentClass);
}
this.writeOutput(object, parentClassRef, path, translationEngineType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ public void testWriteOutput() {

TestAppObject expectedAppObject = TestObjectUtil.generateTestAppObject();

translationController.writeOutput(expectedAppObject, filePath.resolve(fileName), TranslationEngineType.CUSTOM);
translationController.writeOutput(expectedAppObject, filePath.resolve(fileName),
TranslationEngineType.CUSTOM);

// preconditions
// the runtime exception is covered by the test - testMakeFileWriter()
Expand Down Expand Up @@ -319,15 +320,17 @@ public void testWriteOutput_Base() {

// if the path is invalid
contractException = assertThrows(ContractException.class, () -> {
translationController.writeOutput(expectedAppObject, Optional.empty(), filePath.resolve("badPath").resolve(fileName),
translationController.writeOutput(expectedAppObject, Optional.empty(),
filePath.resolve("badPath").resolve(fileName),
TranslationEngineType.CUSTOM);
});

assertEquals(CoreTranslationError.INVALID_OUTPUT_PATH, contractException.getErrorType());

// if the translation engine is null
contractException = assertThrows(ContractException.class, () -> {
translationController.writeOutput(expectedAppObject, Optional.empty(), filePath.resolve(fileName),
translationController.writeOutput(expectedAppObject, Optional.empty(),
filePath.resolve(fileName),
TranslationEngineType.UNKNOWN);
});

Expand All @@ -352,7 +355,8 @@ public void testGetFirstObject() throws IOException {

TestAppObject expectedAppObject = TestObjectUtil.generateTestAppObject();

translationController.writeOutput(expectedAppObject, filePath.resolve(fileName), TranslationEngineType.CUSTOM);
translationController.writeOutput(expectedAppObject, filePath.resolve(fileName),
TranslationEngineType.CUSTOM);

translationController.readInput();

Expand Down Expand Up @@ -521,6 +525,34 @@ public void testAddInputFilePath() {

}

@Test
@UnitTestMethod(target = TranslationController.Builder.class, name = "addParentChildClassRelationship", args = {
Class.class, Class.class })
public void testAddParentChildClassRelationship() {
TranslationController.builder().addParentChildClassRelationship(TestAppObject.class, Object.class);

// preconditions
ContractException contractException = assertThrows(ContractException.class, () -> {
TranslationController.builder().addParentChildClassRelationship(null, Object.class);
});

assertEquals(CoreTranslationError.NULL_CLASS_REF, contractException.getErrorType());

contractException = assertThrows(ContractException.class, () -> {
TranslationController.builder().addParentChildClassRelationship(TestAppObject.class, null);
});

assertEquals(CoreTranslationError.NULL_CLASS_REF, contractException.getErrorType());

contractException = assertThrows(ContractException.class, () -> {
TranslationController.builder()
.addParentChildClassRelationship(TestAppObject.class, Object.class)
.addParentChildClassRelationship(TestAppObject.class, Object.class);
});

assertEquals(CoreTranslationError.DUPLICATE_CLASSREF, contractException.getErrorType());
}

@Test
@UnitTestMethod(target = TranslationController.Builder.class, name = "addTranslationEngine", args = {
TranslationEngine.class })
Expand Down