-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
272 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
160 changes: 160 additions & 0 deletions
160
src/test/java/org/smartregister/domain/AddressTest.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,160 @@ | ||
package org.smartregister.domain; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.joda.time.DateTime; | ||
import org.junit.Test; | ||
import org.smartregister.common.AddressField; | ||
|
||
import com.openpojo.reflection.impl.PojoClassFactory; | ||
import com.openpojo.validation.Validator; | ||
import com.openpojo.validation.ValidatorBuilder; | ||
import com.openpojo.validation.test.impl.GetterTester; | ||
import com.openpojo.validation.test.impl.SetterTester; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import nl.jqno.equalsverifier.Warning; | ||
import org.smartregister.domain.Address; | ||
|
||
public class AddressTest { | ||
|
||
@Test | ||
public void testEqualAndHashCode() { | ||
EqualsVerifier.forClass(Address.class).suppress(Warning.NONFINAL_FIELDS).verify(); | ||
} | ||
|
||
@Test | ||
public void testGetterAndSetter() { | ||
Validator validator = ValidatorBuilder.create().with(new SetterTester()).with(new GetterTester()).build(); | ||
|
||
validator.validate(PojoClassFactory.getPojoClass(Address.class)); | ||
} | ||
|
||
@Test | ||
public void testSetAndGetAddressField() { | ||
Address address = new Address(); | ||
address.withAddressFields(null); | ||
assertNull(address.getAddressFields()); | ||
|
||
address.withAddressField("field", "val"); | ||
assertEquals("val", address.getAddressField("field")); | ||
assertNull(address.getAddressField("ddd")); | ||
|
||
address.withAddressFields(null); | ||
assertNull(address.getAddressFields()); | ||
|
||
address.withAddressField(AddressField.AREA, "val"); | ||
assertEquals("val", address.getAddressField(AddressField.AREA)); | ||
assertNull(address.getAddressField(AddressField.HOUSE_NUMBER)); | ||
|
||
address.withAddressFields(null); | ||
assertNull(address.getAddressFields()); | ||
|
||
} | ||
|
||
@Test | ||
public void testAddAndRemoveAddressField() { | ||
Address address = new Address(); | ||
address.withAddressFields(null); | ||
assertNull(address.getAddressFields()); | ||
|
||
address.addAddressField("field", "val"); | ||
address.addAddressField(AddressField.AREA, "val"); | ||
assertEquals(2, address.getAddressFields().size()); | ||
assertEquals("val", address.getAddressField("field")); | ||
assertEquals("val", address.getAddressField(AddressField.AREA)); | ||
|
||
address.removeAddressField("ddd"); | ||
address.removeAddressField(AddressField.HOUSE_NUMBER); | ||
assertEquals(2, address.getAddressFields().size()); | ||
assertEquals("val", address.getAddressField("field")); | ||
assertEquals("val", address.getAddressField(AddressField.AREA)); | ||
|
||
address.removeAddressField("field"); | ||
address.removeAddressField(AddressField.AREA); | ||
assertEquals(0, address.getAddressFields().size()); | ||
assertNull(address.getAddressField("field")); | ||
assertNull(address.getAddressField(AddressField.AREA)); | ||
} | ||
|
||
@Test | ||
public void testGetAddressFieldUsingRegex() { | ||
Address address = new Address(); | ||
address.withAddressFields(null); | ||
assertNull(address.getAddressFields()); | ||
|
||
address.addAddressField(AddressField.AREA, "area"); | ||
address.addAddressField("field", "val"); | ||
|
||
assertNull(address.getAddressFieldMatchingRegex("")); | ||
assertNull(address.getAddressFieldMatchingRegex("^[0-9]+")); | ||
assertEquals("val", address.getAddressFieldMatchingRegex("^field")); | ||
assertNull(address.getAddressFieldMatchingRegex("^TYPE")); | ||
assertEquals("area", address.getAddressFieldMatchingRegex("^AREA")); | ||
assertNull(address.getAddressFieldMatchingRegex("^area")); | ||
assertNull(address.getAddressFieldMatchingRegex("^id")); | ||
|
||
} | ||
|
||
@Test | ||
public void testIsActive() { | ||
Address address = new Address(); | ||
assertTrue(address.isActive()); | ||
|
||
address.withEndDate(new DateTime().plusDays(5)); | ||
assertTrue(address.isActive()); | ||
|
||
address.withEndDate(new DateTime().minusDays(5)); | ||
assertFalse(address.isActive()); | ||
} | ||
|
||
@Test | ||
public void testDurationCalculation() { | ||
Address address = new Address(); | ||
assertEquals(-1, address.durationInDays()); | ||
|
||
address.withStartDate(new DateTime().minusDays(5)); | ||
assertEquals(5, address.durationInDays()); | ||
|
||
address.withStartDate(new DateTime()); | ||
address.withEndDate(new DateTime().plusMonths(2)); | ||
assertEquals(2, address.durationInMonths()); | ||
|
||
address.withEndDate(new DateTime().plusWeeks(2)); | ||
assertEquals(2, address.durationInWeeks()); | ||
|
||
address.withEndDate(new DateTime().plusYears(2)); | ||
assertEquals(2, address.durationInYears()); | ||
} | ||
|
||
@Test | ||
public void testCreatingObject() { | ||
Address address = new Address(); | ||
String latitude = "222"; | ||
String longitude = "22"; | ||
String geopoint = "geopoint"; | ||
String postalCode = "postalCode"; | ||
String town = "town"; | ||
String subDistrict = "sub"; | ||
String countyDistrict = "count"; | ||
String cityVillage = "village"; | ||
String stateProvince = "province"; | ||
String country = "country"; | ||
address.withLatitude(latitude).withLongitude(longitude).withGeopoint(geopoint).withPostalCode(postalCode) | ||
.withTown(town).withSubDistrict(subDistrict).withCountyDistrict(countyDistrict).withCityVillage(cityVillage) | ||
.withStateProvince(stateProvince).withCountry(country); | ||
|
||
assertEquals(latitude, address.getLatitude()); | ||
assertEquals(longitude, address.getLongitude()); | ||
assertEquals(geopoint, address.getGeopoint()); | ||
assertEquals(postalCode, address.getPostalCode()); | ||
assertEquals(town, address.getTown()); | ||
assertEquals(subDistrict, address.getSubDistrict()); | ||
assertEquals(countyDistrict, address.getCountyDistrict()); | ||
assertEquals(cityVillage, address.getCityVillage()); | ||
assertEquals(country, address.getCountry()); | ||
} | ||
} |
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,110 @@ | ||
package org.smartregister.domain; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.joda.time.DateTime; | ||
import org.junit.Test; | ||
|
||
import com.openpojo.reflection.impl.PojoClassFactory; | ||
import com.openpojo.validation.Validator; | ||
import com.openpojo.validation.ValidatorBuilder; | ||
import com.openpojo.validation.test.impl.GetterTester; | ||
import com.openpojo.validation.test.impl.SetterTester; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import nl.jqno.equalsverifier.Warning; | ||
import org.smartregister.domain.Obs; | ||
|
||
public class ObsTest { | ||
|
||
@Test | ||
public void testEqualAndHashcodeContract() { | ||
EqualsVerifier.forClass(Obs.class).suppress(Warning.NONFINAL_FIELDS).withIgnoredFields("set").verify(); | ||
} | ||
|
||
@Test | ||
public void testGetterAndSetter() { | ||
Validator validator = ValidatorBuilder.create().with(new SetterTester()).with(new GetterTester()).build(); | ||
|
||
validator.validate(PojoClassFactory.getPojoClass(Obs.class)); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void shouldThrowExceptionIfMultipleValuesFound() { | ||
List<Object> values = new ArrayList<>(); | ||
values.add("dd"); | ||
values.add("d"); | ||
Obs obs = new Obs().withValues(values); | ||
|
||
obs.getValue(); | ||
} | ||
|
||
@Test | ||
public void testGettingSingleValue() { | ||
Obs obs = new Obs().withValues(null); | ||
assertNull(obs.getValue()); | ||
|
||
obs.setValue("d"); | ||
assertEquals("d", obs.getValue()); | ||
|
||
} | ||
|
||
@Test | ||
public void testAddToValueList() { | ||
Obs obs = new Obs().withValues(null); | ||
obs = obs.addToValueList(null); | ||
|
||
assertNull(obs.getValues()); | ||
|
||
List<Object> values = new ArrayList<>(); | ||
values.add("dd"); | ||
obs = obs.addToValueList(values); | ||
|
||
assertEquals(1, obs.getValues().size()); | ||
assertEquals("dd", obs.getValue()); | ||
} | ||
|
||
@Test | ||
public void shouldDeleteExistingValueWileAddingNewOne() { | ||
List<Object> values = new ArrayList<>(); | ||
values.add("dd"); | ||
values.add("d"); | ||
Obs obs = new Obs().withValues(values); | ||
|
||
obs.addToValueList("dd"); | ||
|
||
assertEquals(1, obs.getValues().size()); | ||
assertEquals("dd", obs.getValue()); | ||
} | ||
|
||
@Test | ||
public void testCreatingObject() { | ||
List<Object> values = new ArrayList<>(); | ||
values.add("dd"); | ||
String fieldType = "fieldType"; | ||
String dataType = "dataType"; | ||
String fieldCode = "fieldCode"; | ||
String parentCode = "parentCode"; | ||
String value = "sd"; | ||
String comment = "comment"; | ||
String formSubmission = "formSubmission"; | ||
|
||
Obs obs = new Obs().withFieldType(fieldType).withFieldDataType(dataType).withFieldCode(fieldCode) | ||
.withParentCode(parentCode).withValue(value).withComments(comment).withFormSubmissionField(formSubmission) | ||
.withEffectiveDatetime(new DateTime(0l)).withHumanReadableValues(values); | ||
|
||
assertEquals(fieldType, obs.getFieldType()); | ||
assertEquals(fieldCode, obs.getFieldCode()); | ||
assertEquals(dataType, obs.getFieldDataType()); | ||
assertEquals(parentCode, obs.getParentCode()); | ||
assertEquals(value, obs.getValue()); | ||
assertEquals(comment, obs.getComments()); | ||
assertEquals(formSubmission, obs.getFormSubmissionField()); | ||
assertEquals(new DateTime(0l), obs.getEffectiveDatetime()); | ||
assertEquals(values, obs.getHumanReadableValues()); | ||
} | ||
} |