From 9e71c4643fffb2ffc83d4f2eed9df5f251f50398 Mon Sep 17 00:00:00 2001 From: Zachary Bischoff <116595361+bischoffz@users.noreply.github.com> Date: Wed, 5 Jun 2024 18:08:56 -0400 Subject: [PATCH] update getObject methods (#53) * update getObject methods * fix errors --- .../ms/taskit/core/TranslationController.java | 33 ++++++++++++++++--- .../taskit/core/AT_TranslationController.java | 33 ++++++++++++++----- 2 files changed, 53 insertions(+), 13 deletions(-) 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 8b5d1df..46a5e72 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 @@ -196,7 +196,7 @@ public Builder addTranslationEngine(TranslationEngine translationEngine) { this.data.parentChildClassRelationshipMap.put(childClassRef, parentClassRef); } - + return this; } } @@ -412,10 +412,19 @@ void writeOutput(Path path, M object, Optional> superC * */ public T getFirstObject(Class classRef) { - for (Object object : this.objects) { + int index = -1; + for (int i = 0; i < this.objects.size(); i++) { + Object object = this.objects.get(i); + if (classRef.isAssignableFrom(object.getClass())) { - return classRef.cast(object); + index = i; + break; } + + } + + if (index > -1) { + return classRef.cast(this.objects.remove(index)); } throw new ContractException(CoreTranslationError.UNKNOWN_CLASSREF); @@ -429,12 +438,17 @@ public T getFirstObject(Class classRef) { */ public List getObjects(Class classRef) { List objects = new ArrayList<>(); - for (Object object : this.objects) { + for (int i = 0; i < this.objects.size(); i++) { + Object object = this.objects.get(i); + if (classRef.isAssignableFrom(object.getClass())) { objects.add(classRef.cast(object)); } + } + this.objects.removeAll(objects); + return objects; } @@ -442,7 +456,16 @@ public List getObjects(Class classRef) { * Returns the entire list of read in objects */ public List getObjects() { - return this.objects; + List objects = new ArrayList<>(this.objects); + + this.objects.clear(); + + return objects; + } + + // package access for testing + int getNumObjects() { + return this.objects.size(); } } 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 0b8ea46..45261e0 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 @@ -17,6 +17,7 @@ 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.testcomplexobject.input.TestComplexInputObject; 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; @@ -176,7 +177,7 @@ public void testReadInput() { translationController.readInput(); - assertTrue(translationController.getObjects().size() == 1); + assertEquals(1, translationController.getNumObjects()); TestAppObject actualTestAppObject = translationController.getFirstObject(TestAppObject.class); @@ -350,8 +351,10 @@ public void testWriteOutput_Base() { @UnitTestMethod(target = TranslationController.class, name = "getFirstObject", args = { Class.class }) public void testGetFirstObject() throws IOException { String fileName = "getFirstObject-testOutput.json"; + String fileName2 = "getFirstObject-testOutput2.json"; ResourceHelper.createFile(filePath, fileName); + ResourceHelper.createFile(filePath, fileName2); TestTranslationEngine testTranslationEngine = TestTranslationEngine.builder() .addTranslator(TestObjectTranslator.getTranslator()) @@ -360,18 +363,24 @@ public void testGetFirstObject() throws IOException { TranslationController translationController = TranslationController.builder() .addInputFilePath(filePath.resolve(fileName), TestInputObject.class, TranslationEngineType.CUSTOM) + .addInputFilePath(filePath.resolve(fileName2), TestComplexInputObject.class, + TranslationEngineType.CUSTOM) .addTranslationEngine(testTranslationEngine).build(); TestAppObject expectedAppObject = TestObjectUtil.generateTestAppObject(); + TestComplexAppObject expectedComplexAppObject = TestObjectUtil.generateTestComplexAppObject(); translationController.writeOutput(expectedAppObject, filePath.resolve(fileName), TranslationEngineType.CUSTOM); - translationController.readInput(); + translationController.writeOutput(expectedComplexAppObject, filePath.resolve(fileName2), + TranslationEngineType.CUSTOM); - assertTrue(translationController.getObjects().size() == 1); + translationController.readInput(); + assertEquals(2, translationController.getNumObjects()); TestAppObject actualTestAppObject = translationController.getFirstObject(TestAppObject.class); + assertEquals(1, translationController.getNumObjects()); assertNotNull(actualTestAppObject); @@ -389,9 +398,11 @@ public void testGetFirstObject() throws IOException { public void testGetObjects_OfClass() throws IOException { String fileName = "GetObjects_OfClass_1-testOutput.json"; String fileName2 = "GetObjects_OfClass_2-testOutput.json"; + String fileName3 = "GetObjects_OfClass_3-testOutput.json"; ResourceHelper.createFile(filePath, fileName); ResourceHelper.createFile(filePath, fileName2); + ResourceHelper.createFile(filePath, fileName3); TestTranslationEngine testTranslationEngine = TestTranslationEngine.builder() .addTranslator(TestObjectTranslator.getTranslator()) @@ -402,27 +413,33 @@ public void testGetObjects_OfClass() throws IOException { TranslationEngineType.CUSTOM) .addInputFilePath(filePath.resolve(fileName2), TestInputObject.class, TranslationEngineType.CUSTOM) + .addInputFilePath(filePath.resolve(fileName3), TestComplexInputObject.class, + TranslationEngineType.CUSTOM) .addTranslationEngine(testTranslationEngine).build(); List expectedObjects = TestObjectUtil.getListOfAppObjects(2); + TestComplexAppObject expectedComplexAppObject = TestObjectUtil.generateTestComplexAppObject(); translationController.writeOutput(expectedObjects.get(0), filePath.resolve(fileName), TranslationEngineType.CUSTOM); translationController.writeOutput(expectedObjects.get(1), filePath.resolve(fileName2), TranslationEngineType.CUSTOM); + translationController.writeOutput(expectedComplexAppObject, filePath.resolve(fileName3), + TranslationEngineType.CUSTOM); translationController.readInput(); - assertEquals(2, translationController.getObjects().size()); + assertEquals(3, translationController.getNumObjects()); List actualObjects = translationController.getObjects(TestAppObject.class); + assertEquals(1, translationController.getNumObjects()); assertEquals(2, actualObjects.size()); assertTrue(actualObjects.containsAll(expectedObjects)); - List actualObjects2 = translationController - .getObjects(TestComplexAppObject.class); + List actualObjects2 = translationController + .getObjects(TestAppObject.class); assertTrue(actualObjects2.isEmpty()); } @@ -455,10 +472,10 @@ public void testGetObjects() throws IOException { translationController.readInput(); - assertEquals(2, translationController.getObjects().size()); + assertEquals(2, translationController.getNumObjects()); List actualObjects = translationController.getObjects(); - + assertEquals(0, translationController.getNumObjects()); assertEquals(2, actualObjects.size()); assertTrue(actualObjects.containsAll(expectedObjects));