diff --git a/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfiguration.java b/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfiguration.java index 33e38a47..f237e9bf 100644 --- a/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfiguration.java +++ b/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfiguration.java @@ -5,6 +5,8 @@ import org.openmrs.api.context.Context; import org.openmrs.util.OpenmrsUtil; +import com.fasterxml.jackson.annotation.JsonIgnore; + import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -76,6 +78,7 @@ public void setAddressHierarchyFile(AddressHierarchyFile addressHierarchyFile) { /** * @return a new AddressTemplate instance for the given configuration */ + @JsonIgnore public Object getAddressTemplate() { Object addressTemplate = null; try { @@ -136,19 +139,19 @@ public boolean equals(Object obj) { if (this.getAddressComponents().size() != that.getAddressComponents().size()) { return false; } - for (int i=0; i that.getAddressComponents().contains(item)); } + if (this.getLineByLineFormat().size() != that.getLineByLineFormat().size()) { return false; } - for (int i=0; i that.getLineByLineFormat().contains(item)); } + if (!OpenmrsUtil.nullSafeEquals(this.getAddressHierarchyFile(), that.getAddressHierarchyFile())) { return false; } diff --git a/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoader.java b/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoader.java index 9c454ba5..67091465 100644 --- a/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoader.java +++ b/api/src/main/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoader.java @@ -1,7 +1,5 @@ package org.openmrs.module.addresshierarchy.config; -import com.thoughtworks.xstream.XStream; -import com.thoughtworks.xstream.io.xml.DomDriver; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; @@ -13,6 +11,7 @@ import org.openmrs.module.addresshierarchy.AddressHierarchyLevel; import org.openmrs.module.addresshierarchy.service.AddressHierarchyService; import org.openmrs.module.addresshierarchy.util.AddressHierarchyImportUtil; +import org.openmrs.serialization.JacksonSerializer; import org.openmrs.util.OpenmrsConstants; import org.openmrs.util.OpenmrsUtil; @@ -20,7 +19,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Method; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; @@ -30,7 +28,7 @@ */ public class AddressConfigurationLoader { - protected static final String ADDR_CONFIG_FILE_NAME = "addressConfiguration.xml"; + protected static final String ADDR_CONFIG_FILE_NAME = "addressConfiguration.json"; public static final String NOT_COMPUTABLE_CHECKSUM = "not_computable_checksum"; public static final String NOT_READABLE_CHECKSUM = "not_readadble_checksum"; @@ -151,8 +149,8 @@ public static void wipeAddressHierarchy() { public static void installAddressTemplate(Object addressTemplate) { try { log.info("Installing address template"); - String xml = Context.getSerializationService().getDefaultSerializer().serialize(addressTemplate); - setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_ADDRESS_TEMPLATE, xml); + String json = getSerializer().serialize(addressTemplate); + setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_ADDRESS_TEMPLATE, json); } catch (Exception e) { throw new IllegalArgumentException("Unable to serialize and save address template", e); @@ -245,7 +243,7 @@ public static AddressConfiguration readFromFile(File file) { */ public static AddressConfiguration readFromString(String configuration) { try { - return (AddressConfiguration) getSerializer().fromXML(configuration); + return (AddressConfiguration) getSerializer().deserialize(configuration, AddressConfiguration.class); } catch (Exception e) { throw new IllegalArgumentException("Unable to load address configuration from configuration file. Please check the format of this file", e); @@ -256,28 +254,15 @@ public static AddressConfiguration readFromString(String configuration) { * Writes a serialized String representing the address configuration from an AddressConfiguration object */ public static String writeToString(AddressConfiguration configuration) { - return getSerializer().toXML(configuration); + return getSerializer().serialize(configuration); } /** * @return the serializer instance used to load configuration from file */ - public static XStream getSerializer() { - XStream xs = new XStream(new DomDriver()); - try { - Method allowTypeHierarchy = XStream.class.getMethod("allowTypeHierarchy", Class.class); - allowTypeHierarchy.invoke(xs, AddressConfiguration.class); - allowTypeHierarchy.invoke(xs, AddressComponent.class); - allowTypeHierarchy.invoke(xs, AddressHierarchyFile.class); - log.debug("Successfully configured address configuration serializer with allowed types"); - } - catch (Exception e) { - log.debug("Error configuring address configuration serializer with allowed types", e); - } - xs.alias("addressConfiguration", AddressConfiguration.class); - xs.alias("addressComponent", AddressComponent.class); - xs.alias("addressHierarchyFile", AddressHierarchyFile.class); - return xs; + public static JacksonSerializer getSerializer() { + JacksonSerializer serializer = Context.getRegisteredComponent("jacksonSerializer", JacksonSerializer.class); + return serializer; } /** diff --git a/api/src/test/java/org/openmrs/module/addresshierarchy/AddressHierarchyActivatorTest.java b/api/src/test/java/org/openmrs/module/addresshierarchy/AddressHierarchyActivatorTest.java index 419ce381..d25b11d2 100644 --- a/api/src/test/java/org/openmrs/module/addresshierarchy/AddressHierarchyActivatorTest.java +++ b/api/src/test/java/org/openmrs/module/addresshierarchy/AddressHierarchyActivatorTest.java @@ -1,28 +1,26 @@ package org.openmrs.module.addresshierarchy; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.openmrs.GlobalProperty; import org.openmrs.api.context.Context; import org.openmrs.module.addresshierarchy.config.AddressConfigurationLoader; import org.openmrs.module.addresshierarchy.config.ConfigDirUtil; import org.openmrs.module.addresshierarchy.service.AddressHierarchyService; -import org.openmrs.test.BaseModuleContextSensitiveTest; -import org.openmrs.test.Verifies; +import org.openmrs.test.jupiter.BaseModuleContextSensitiveTest; import org.openmrs.util.OpenmrsConstants; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.annotation.DirtiesContext; import org.springframework.util.CollectionUtils; import java.io.File; import java.util.HashSet; import java.util.List; -import java.util.Properties; import java.util.Set; -@DirtiesContext public class AddressHierarchyActivatorTest extends BaseModuleContextSensitiveTest { private static String APP_DATA_TEST_DIRECTORY = "testAppDataDir"; @@ -30,13 +28,17 @@ public class AddressHierarchyActivatorTest extends BaseModuleContextSensitiveTes @Autowired private AddressHierarchyActivator activator; - @Before + @BeforeEach public void setup() { Context.getAdministrationService().saveGlobalProperty(new GlobalProperty(AddressHierarchyConstants.GLOBAL_PROP_INITIALIZE_ADDRESS_HIERARCHY_CACHE_ON_STARTUP, "true")); setAppDataDirPath(APP_DATA_TEST_DIRECTORY); + Context.getAdministrationService().saveGlobalProperty( + new GlobalProperty("addressHierarchy.configuration.serializer.whitelist.types", + "org.openmrs.module.addresshierarchy.**")); + Assert.assertTrue(CollectionUtils.isEmpty(Context.getService(AddressHierarchyService.class).getAddressHierarchyLevels())); } @@ -46,7 +48,6 @@ private void setAppDataDirPath(String strPath) { } @Test - @Verifies(value = "should load new address hierarchy configuration from configuration/addresshierarchy", method = "started()") public void started_shouldLoadAddressHierachyConfig() { // Setup @@ -91,7 +92,6 @@ private void assertConfigurationAsExpected() { } @Test - @Verifies(value = "should not load again an address hierarchy configuration from configuration/addresshierarchy", method = "started()") public void started_shouldNotReLoadAddressHierachyConfig() { // Setup @@ -119,7 +119,6 @@ public void started_shouldNotReLoadAddressHierachyConfig() { } @Test - @Verifies(value = "should keep existing entries when wipe is set to false", method = "started()") public void started_shouldNotWipeExistingEntries() { // Setup @@ -162,9 +161,8 @@ public void started_shouldNotWipeExistingEntries() { Assert.assertEquals("Point Shirley", pointShirley.getName()); } - @Ignore + @Disabled @Test - @Verifies(value = "should wipe existing entries when wipe is set to true", method = "started()") public void started_shouldWipeExistingEntries() { // Setup @@ -202,9 +200,9 @@ public void started_shouldWipeExistingEntries() { for (AddressHierarchyEntry entry : entries) { entryNames.add(entry.getName()); } - Assert.assertFalse(entryNames.contains("Beacon Hill")); - Assert.assertFalse(entryNames.contains("Jamaica Plain")); - Assert.assertTrue(entryNames.contains("Auburndale")); - Assert.assertTrue(entryNames.contains("Chestnut Hill")); + assertFalse(entryNames.contains("Beacon Hill")); + assertFalse(entryNames.contains("Jamaica Plain")); + assertTrue(entryNames.contains("Auburndale")); + assertTrue(entryNames.contains("Chestnut Hill")); } } \ No newline at end of file diff --git a/api/src/test/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoaderTest.java b/api/src/test/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoaderTest.java index 30271d9e..4db0b9a2 100644 --- a/api/src/test/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoaderTest.java +++ b/api/src/test/java/org/openmrs/module/addresshierarchy/config/AddressConfigurationLoaderTest.java @@ -1,35 +1,45 @@ package org.openmrs.module.addresshierarchy.config; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; import java.nio.file.Files; - import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openmrs.GlobalProperty; +import org.openmrs.api.AdministrationService; import org.openmrs.module.addresshierarchy.AddressField; +import org.openmrs.test.jupiter.BaseModuleContextSensitiveTest; import org.openmrs.util.OpenmrsClassLoader; +import org.springframework.beans.factory.annotation.Autowired; -public class AddressConfigurationLoaderTest { +public class AddressConfigurationLoaderTest extends BaseModuleContextSensitiveTest { protected final Log log = LogFactory.getLog(getClass()); - public static final String CONFIG_RESOURCE = "org/openmrs/module/addresshierarchy/include/addressConfiguration.xml"; + public static final String CONFIG_RESOURCE = "org/openmrs/module/addresshierarchy/include/addressConfiguration.json"; + + @Autowired + private AdministrationService adminService; - @Before - public void setup() throws IOException { + @BeforeEach + public void setup() throws IOException, Exception { System.setProperty("user.home", Files.createTempDirectory(null).toString()); // see OpenmrsUtil.getApplicationDataDirectory() + adminService.saveGlobalProperty( + new GlobalProperty("addressHierarchy.configuration.serializer.whitelist.types", + "org.openmrs.module.addresshierarchy.**")); } @Test public void should_writeToString() throws Exception { AddressConfiguration config = getAddressConfiguration(); - String actualXml = IOUtils.toString(OpenmrsClassLoader.getInstance().getResourceAsStream(CONFIG_RESOURCE), "UTF-8"); - String expectedXml = AddressConfigurationLoader.writeToString(config); - Assert.assertEquals(StringUtils.deleteWhitespace(expectedXml), StringUtils.deleteWhitespace(actualXml)); + String expectedJson = IOUtils.toString(OpenmrsClassLoader.getInstance().getResourceAsStream(CONFIG_RESOURCE), "UTF-8"); + String actualJson = AddressConfigurationLoader.writeToString(config); + assertEquals(StringUtils.deleteWhitespace(expectedJson), StringUtils.deleteWhitespace(actualJson)); } @Test @@ -38,7 +48,7 @@ public void should_readFromString() throws Exception { String serialized = IOUtils.toString(OpenmrsClassLoader.getInstance().getResourceAsStream(CONFIG_RESOURCE), "UTF-8"); AddressConfiguration actualConfig = AddressConfigurationLoader.readFromString(serialized); - Assert.assertEquals(expectedConfig, actualConfig); + assertTrue(expectedConfig.equals(actualConfig)); } protected AddressConfiguration getAddressConfiguration() { diff --git a/api/src/test/resources/org/openmrs/module/addresshierarchy/include/addressConfiguration.json b/api/src/test/resources/org/openmrs/module/addresshierarchy/include/addressConfiguration.json new file mode 100644 index 00000000..7fae53b9 --- /dev/null +++ b/api/src/test/resources/org/openmrs/module/addresshierarchy/include/addressConfiguration.json @@ -0,0 +1,47 @@ +{ + "wipe":false, + "addressComponents":[ + { + "field":"COUNTRY", + "nameMapping":"Country", + "sizeMapping":40, + "elementDefault":"Sierra Leone", + "requiredInHierarchy":true + }, + { + "field":"STATE_PROVINCE", + "nameMapping":"Province/Area", + "sizeMapping":40, + "requiredInHierarchy":true + }, + { + "field":"COUNTY_DISTRICT", + "nameMapping":"District", + "sizeMapping":40, + "requiredInHierarchy":false + }, + { + "field":"CITY_VILLAGE", + "nameMapping":"Chiefdom", + "sizeMapping":40, + "requiredInHierarchy":false + }, + { + "field":"ADDRESS_1", + "nameMapping":"Address", + "sizeMapping":80, + "requiredInHierarchy":false + } + ], + "lineByLineFormat":[ + "address1", + "cityVillage", + "countyDistrict,stateProvince", + "country" + ], + "addressHierarchyFile":{ + "filename":"address-hierarchy-entries.csv", + "entryDelimiter":"|", + "identifierDelimiter":"^" + } +} \ No newline at end of file diff --git a/api/src/test/resources/org/openmrs/module/addresshierarchy/include/addressConfiguration.xml b/api/src/test/resources/org/openmrs/module/addresshierarchy/include/addressConfiguration.xml deleted file mode 100644 index ddc9a3ee..00000000 --- a/api/src/test/resources/org/openmrs/module/addresshierarchy/include/addressConfiguration.xml +++ /dev/null @@ -1,47 +0,0 @@ - - false - - - COUNTRY - Country - 40 - Sierra Leone - true - - - STATE_PROVINCE - Province/Area - 40 - true - - - COUNTY_DISTRICT - District - 40 - false - - - CITY_VILLAGE - Chiefdom - 40 - false - - - ADDRESS_1 - Address - 80 - false - - - - address1 - cityVillage - countyDistrict, stateProvince - country - - - address-hierarchy-entries.csv - | - ^ - - \ No newline at end of file diff --git a/api/src/test/resources/testAppDataDir/configuration/addresshierarchy/addressConfiguration.json b/api/src/test/resources/testAppDataDir/configuration/addresshierarchy/addressConfiguration.json new file mode 100644 index 00000000..fad14a26 --- /dev/null +++ b/api/src/test/resources/testAppDataDir/configuration/addresshierarchy/addressConfiguration.json @@ -0,0 +1,46 @@ +{ + "addressComponents": [ + { + "field": "COUNTRY", + "nameMapping": "Country", + "sizeMapping": "40", + "elementDefault": "United States", + "requiredInHierarchy": true + }, + { + "field": "STATE_PROVINCE", + "nameMapping": "State", + "sizeMapping": "40", + "requiredInHierarchy": true + }, + { + "field": "COUNTY_DISTRICT", + "nameMapping": "County", + "sizeMapping": "40", + "requiredInHierarchy": true + }, + { + "field": "CITY_VILLAGE", + "nameMapping": "Citi", + "sizeMapping": "40", + "requiredInHierarchy": true + }, + { + "field": "NEIGHBORHOOD_CELL", + "nameMapping": "Neighborhood", + "sizeMapping": "80", + "requiredInHierarchy": false + } + ], + "lineByLineFormat": [ + "neighborhoodCell", + "cityVillage", + "countyDistrict, stateProvince", + "country" + ], + "addressHierarchyFile": { + "filename": "address-hierarchy-entries.csv", + "entryDelimiter": ",", + "identifierDelimiter": "^" + } +} \ No newline at end of file diff --git a/api/src/test/resources/testAppDataDir/configuration/addresshierarchy/addressConfiguration.xml b/api/src/test/resources/testAppDataDir/configuration/addresshierarchy/addressConfiguration.xml deleted file mode 100644 index e721c100..00000000 --- a/api/src/test/resources/testAppDataDir/configuration/addresshierarchy/addressConfiguration.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - COUNTRY - Country - 40 - United States - true - - - STATE_PROVINCE - State - 40 - true - - - COUNTY_DISTRICT - County - 40 - true - - - CITY_VILLAGE - Citi - 40 - true - - - NEIGHBORHOOD_CELL - Neighborhood - 80 - false - - - - neighborhoodCell - cityVillage - countyDistrict, stateProvince - country - - - address-hierarchy-entries.csv - , - ^ - - \ No newline at end of file diff --git a/omod/src/test/java/org/openmrs/module/addresshierarchy/web/controller/ajax/AddressHierarchyAjaxControllerTest.java b/omod/src/test/java/org/openmrs/module/addresshierarchy/web/controller/ajax/AddressHierarchyAjaxControllerTest.java index 64161ddc..f3e4f9b5 100644 --- a/omod/src/test/java/org/openmrs/module/addresshierarchy/web/controller/ajax/AddressHierarchyAjaxControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/addresshierarchy/web/controller/ajax/AddressHierarchyAjaxControllerTest.java @@ -3,9 +3,9 @@ import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; -import org.junit.Before; -import org.junit.Test; -import org.openmrs.test.BaseModuleContextSensitiveTest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openmrs.test.jupiter.BaseModuleContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.ModelMap; @@ -24,7 +24,7 @@ public class AddressHierarchyAjaxControllerTest extends BaseModuleContextSensiti @Autowired private AddressHierarchyAjaxController controller; - @Before + @BeforeEach public void setupDatabase() throws Exception { initializeInMemoryDatabase(); authenticate(); diff --git a/pom.xml b/pom.xml index 70cf4c6f..55f3baf3 100644 --- a/pom.xml +++ b/pom.xml @@ -79,7 +79,7 @@ - 2.7.0 + 3.0.0-SNAPSHOT UTF-8 1.1.0-SNAPSHOT 1.22.0