-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from opensrp/initial-structure
Add unit tests of model classes
- Loading branch information
Showing
6 changed files
with
577 additions
and
0 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
251 changes: 251 additions & 0 deletions
251
src/test/java/org/smartregister/model/location/LocationHierarchyTreeTest.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,251 @@ | ||
/* | ||
* Copyright 2021 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.smartregister.model.location; | ||
|
||
import org.hl7.fhir.r4.model.Location; | ||
import org.hl7.fhir.r4.model.Reference; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertNull; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class LocationHierarchyTreeTest { | ||
|
||
@Test | ||
public void testAddLocationWithoutChildLocations() { | ||
Location location = new Location(); | ||
location.setId("Location/1"); | ||
location.setName("Test Location"); | ||
Reference partOfReference = new Reference(); | ||
partOfReference.setReference(""); | ||
location.setPartOf(partOfReference); | ||
LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); | ||
locationHierarchyTree.addLocation(location); | ||
|
||
Tree tree = locationHierarchyTree.getLocationsHierarchy(); | ||
assertNotNull(tree); | ||
assertNotNull(tree.getTree()); | ||
assertEquals("Location/1", tree.getTree().getTreeNodeId().getValue()); | ||
assertEquals("Location/1", tree.getTree().getTreeNode().getNodeId().getValue()); | ||
assertEquals("Test Location", tree.getTree().getTreeNode().getLabel().getValue()); | ||
assertNull(tree.getTree().getTreeNode().getParent().getValue()); | ||
assertEquals(0, tree.getTree().getTreeNode().getChildren().size()); | ||
} | ||
|
||
@Test | ||
public void testBuildTreeFromList() { | ||
Location location1 = new Location(); | ||
location1.setId("Location/1"); | ||
location1.setName("Test Location"); | ||
Reference partOfReference = new Reference(); | ||
partOfReference.setReference(""); | ||
location1.setPartOf(partOfReference); | ||
|
||
Location location2 = new Location(); | ||
location2.setId("Location/2"); | ||
location2.setName("Test Location 2"); | ||
partOfReference = new Reference(); | ||
partOfReference.setReference("Location/1"); | ||
location2.setPartOf(partOfReference); | ||
|
||
Location location3 = new Location(); | ||
location3.setId("Location/3"); | ||
location3.setName("Test Location 3"); | ||
partOfReference = new Reference(); | ||
partOfReference.setReference("Location/2"); | ||
location3.setPartOf(partOfReference); | ||
|
||
LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); | ||
|
||
List<Location> locationList = new ArrayList<>(); | ||
locationList.add(location1); | ||
locationList.add(location2); | ||
locationList.add(location3); | ||
|
||
locationHierarchyTree.buildTreeFromList(locationList); | ||
Tree tree = locationHierarchyTree.getLocationsHierarchy(); | ||
assertNotNull(tree); | ||
assertNotNull(tree.getTree()); | ||
assertEquals("Location/1", tree.getTree().getTreeNodeId().getValue()); | ||
assertEquals("Location/1", tree.getTree().getTreeNode().getNodeId().getValue()); | ||
assertEquals("Test Location", tree.getTree().getTreeNode().getLabel().getValue()); | ||
assertNull(tree.getTree().getTreeNode().getParent().getValue()); | ||
assertEquals(1, tree.getTree().getTreeNode().getChildren().size()); | ||
|
||
assertEquals( | ||
"Location/2", | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getNodeId() | ||
.getValue()); | ||
assertEquals( | ||
"Test Location 2", | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getLabel() | ||
.getValue()); | ||
assertNotNull( | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getParent() | ||
.getValue()); | ||
assertEquals( | ||
"Location/1", | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getParent() | ||
.getValue()); | ||
assertEquals( | ||
1, | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getChildren() | ||
.size()); | ||
|
||
assertEquals( | ||
"Location/3", | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getNodeId() | ||
.getValue()); | ||
assertEquals( | ||
"Test Location 3", | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getLabel() | ||
.getValue()); | ||
assertNotNull( | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getParent() | ||
.getValue()); | ||
assertEquals( | ||
"Location/2", | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getParent() | ||
.getValue()); | ||
assertEquals( | ||
0, | ||
tree.getTree() | ||
.getTreeNode() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getChildren() | ||
.get(0) | ||
.getChildren() | ||
.getChildren() | ||
.size()); | ||
|
||
assertNotNull(locationHierarchyTree.getLocationsHierarchy().getParentChildren()); | ||
assertEquals(2, locationHierarchyTree.getLocationsHierarchy().getParentChildren().size()); | ||
assertEquals( | ||
"Location/1", | ||
locationHierarchyTree | ||
.getLocationsHierarchy() | ||
.getParentChildren() | ||
.get(0) | ||
.getIdentifier() | ||
.getValue()); | ||
assertEquals( | ||
1, | ||
locationHierarchyTree | ||
.getLocationsHierarchy() | ||
.getParentChildren() | ||
.get(0) | ||
.getChildIdentifiers() | ||
.size()); | ||
assertEquals( | ||
"Location/2", | ||
locationHierarchyTree | ||
.getLocationsHierarchy() | ||
.getParentChildren() | ||
.get(0) | ||
.getChildIdentifiers() | ||
.get(0) | ||
.getValue()); | ||
|
||
assertEquals( | ||
"Location/2", | ||
locationHierarchyTree | ||
.getLocationsHierarchy() | ||
.getParentChildren() | ||
.get(1) | ||
.getIdentifier() | ||
.getValue()); | ||
assertEquals( | ||
1, | ||
locationHierarchyTree | ||
.getLocationsHierarchy() | ||
.getParentChildren() | ||
.get(1) | ||
.getChildIdentifiers() | ||
.size()); | ||
assertEquals( | ||
"Location/3", | ||
locationHierarchyTree | ||
.getLocationsHierarchy() | ||
.getParentChildren() | ||
.get(1) | ||
.getChildIdentifiers() | ||
.get(0) | ||
.getValue()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/test/java/org/smartregister/model/location/TreeNodeTest.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,82 @@ | ||
/* | ||
* Copyright 2021 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.smartregister.model.location; | ||
|
||
import org.hl7.fhir.r4.model.Location; | ||
import org.hl7.fhir.r4.model.StringType; | ||
import org.junit.Test; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertNull; | ||
|
||
public class TreeNodeTest { | ||
|
||
@Test | ||
public void testAddingChild() { | ||
|
||
StringType rootNodeName = new StringType(); | ||
rootNodeName.setValue("Root Node"); | ||
|
||
StringType rootNodeId = new StringType(); | ||
rootNodeId.setValue("Location/1"); | ||
|
||
StringType rootNodeLabel = new StringType(); | ||
rootNodeLabel.setValue("Root Location Node"); | ||
|
||
Location location = new Location(); | ||
location.setId("Location/1"); | ||
|
||
StringType childNodeName = new StringType(); | ||
childNodeName.setValue("Child Node"); | ||
|
||
StringType childNodeId = new StringType(); | ||
childNodeId.setValue("Location/2"); | ||
|
||
StringType childNodeLabel = new StringType(); | ||
childNodeLabel.setValue("Child Location Node"); | ||
|
||
TreeNode rootNode = new TreeNode(rootNodeName, rootNodeId, rootNodeLabel, location, null); | ||
TreeNode childNode = | ||
new TreeNode(childNodeName, childNodeId, childNodeLabel, location, rootNodeId); | ||
rootNode.addChild(childNode); | ||
|
||
assertEquals( | ||
childNodeId.getValue(), | ||
rootNode.findChild(childNodeId.getValue()).getNodeId().getValue()); | ||
assertEquals( | ||
rootNodeId.getValue(), | ||
rootNode.findChild(childNodeId.getValue()).getParent().getValue()); | ||
} | ||
|
||
@Test | ||
public void findInvalidChildren() { | ||
StringType rootNodeName = new StringType(); | ||
rootNodeName.setValue("Root Node"); | ||
|
||
StringType rootNodeId = new StringType(); | ||
rootNodeId.setValue("Location/1"); | ||
|
||
StringType rootNodeLabel = new StringType(); | ||
rootNodeLabel.setValue("Root Location Node"); | ||
|
||
Location location = new Location(); | ||
location.setId("Location/1"); | ||
|
||
TreeNode rootNode = new TreeNode(rootNodeName, rootNodeId, rootNodeLabel, location, null); | ||
assertEquals(0, rootNode.getChildren().size()); | ||
assertNull(rootNode.findChild("Location/2")); | ||
} | ||
} |
Oops, something went wrong.