Skip to content

Commit

Permalink
Merge pull request #2 from opensrp/initial-structure
Browse files Browse the repository at this point in the history
Add unit tests of model classes
  • Loading branch information
rehammuzzamil authored Jan 24, 2022
2 parents 8de37f5 + 79191cb commit 7c9106c
Show file tree
Hide file tree
Showing 6 changed files with 577 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@
<artifactId>fhir-common-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>

<distributionManagement>
<repository>
<id>nexus-releases</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshots Repository</name>
<uniqueVersion>false</uniqueVersion>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<hapi.fhir.base.version>5.5.0</hapi.fhir.base.version>
<junit.version>4.13.1</junit.version>
</properties>

<dependencies>
Expand All @@ -25,5 +39,14 @@
<artifactId>org.hl7.fhir.r4</artifactId>
<version>5.4.10</version>
</dependency>

<!-- Test Dependencies-->
<dependency>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
<scope>test</scope>
<version>${junit.version}</version>
</dependency>

</dependencies>
</project>
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 src/test/java/org/smartregister/model/location/TreeNodeTest.java
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"));
}
}
Loading

0 comments on commit 7c9106c

Please sign in to comment.