Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -136,19 +139,19 @@ public boolean equals(Object obj) {
if (this.getAddressComponents().size() != that.getAddressComponents().size()) {
return false;
}
for (int i=0; i<this.getAddressComponents().size(); i++) {
if (!this.getAddressComponents().get(i).equals(that.getAddressComponents().get(i))) {
return false;
}

if (this.getAddressComponents().size() == that.getAddressComponents().size()) {
return this.getAddressComponents().stream().allMatch(item -> that.getAddressComponents().contains(item));
}

if (this.getLineByLineFormat().size() != that.getLineByLineFormat().size()) {
return false;
}
for (int i=0; i<this.getLineByLineFormat().size(); i++) {
if (!this.getLineByLineFormat().get(i).equals(that.getLineByLineFormat().get(i))) {
return false;
}

if (this.getLineByLineFormat().size() == that.getLineByLineFormat().size()) {
return this.getLineByLineFormat().stream().allMatch(item -> that.getLineByLineFormat().contains(item));
}

if (!OpenmrsUtil.nullSafeEquals(this.getAddressHierarchyFile(), that.getAddressHierarchyFile())) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,14 +11,14 @@
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;

import java.io.File;
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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
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";

@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()));
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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":"^"
}
}

This file was deleted.

Loading
Loading