|
| 1 | +/** |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 3 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 4 | + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under |
| 5 | + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. |
| 6 | + * |
| 7 | + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS |
| 8 | + * graphic logo is a trademark of OpenMRS Inc. |
| 9 | + */ |
| 10 | +package org.openmrs.module.initializer.api.loaders; |
| 11 | + |
| 12 | +import org.junit.Assert; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.openmrs.Location; |
| 16 | +import org.openmrs.LocationAttribute; |
| 17 | +import org.openmrs.LocationTag; |
| 18 | +import org.openmrs.api.LocationService; |
| 19 | +import org.openmrs.api.context.Context; |
| 20 | +import org.openmrs.customdatatype.datatype.DateDatatype; |
| 21 | +import org.openmrs.module.initializer.DomainBaseModuleContextSensitive_2_7_Test; |
| 22 | +import org.openmrs.module.initializer.api.loc.LocationsLoader; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 25 | + |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Date; |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +import static org.hamcrest.CoreMatchers.is; |
| 32 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 33 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 34 | + |
| 35 | +/** |
| 36 | + * This is a copy of the LocationsLoaderIntegration test in order to test the same test against |
| 37 | + * OpenMRS 2.7 |
| 38 | + */ |
| 39 | +public class LocationsLoader_2_7_IntegrationTest extends DomainBaseModuleContextSensitive_2_7_Test { |
| 40 | + |
| 41 | + @Autowired |
| 42 | + @Qualifier("locationService") |
| 43 | + private LocationService ls; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private LocationsLoader loader; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private DateDatatype dateDatatype; |
| 50 | + |
| 51 | + private Locale localeEn = Locale.ENGLISH; |
| 52 | + |
| 53 | + private Locale localeKm = new Locale("km", "KH"); |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setup() throws Exception { |
| 57 | + executeDataSet("testdata/test-metadata.xml"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void load_shouldLoadAccordingToCsvFiles() { |
| 62 | + // Pre-load verif |
| 63 | + { |
| 64 | + Location loc = ls.getLocation(4089); |
| 65 | + Assert.assertEquals("a03e395c-b881-49b7-b6fc-983f6bddc7fc", loc.getUuid()); |
| 66 | + Assert.assertEquals("Acme Clinic", loc.getName()); |
| 67 | + Assert.assertThat(loc.getDescription(), nullValue()); |
| 68 | + Assert.assertThat(loc.getParentLocation(), nullValue()); |
| 69 | + Assert.assertThat(loc.getTags().size(), is(0)); |
| 70 | + |
| 71 | + Collection<LocationAttribute> attributes = loc.getActiveAttributes(); |
| 72 | + Assert.assertThat(attributes.size(), is(1)); |
| 73 | + Assert.assertEquals("2016-04-14", |
| 74 | + dateDatatype.serialize((Date) ((LocationAttribute) attributes.toArray()[0]).getValue())); |
| 75 | + } |
| 76 | + { |
| 77 | + Location loc = ls.getLocation(4090); |
| 78 | + Assert.assertEquals("cbaaaab4-d960-4ae9-9b6a-8983fbd947b6", loc.getUuid()); |
| 79 | + Assert.assertEquals("Legacy Location", loc.getName()); |
| 80 | + Assert.assertEquals("Legacy location that must be retired", loc.getDescription()); |
| 81 | + Assert.assertThat(loc.getRetired(), is(false)); |
| 82 | + } |
| 83 | + |
| 84 | + // Replay |
| 85 | + loader.load(); |
| 86 | + |
| 87 | + // Verify fetch by name |
| 88 | + { |
| 89 | + Location loc = ls.getLocation("LOCATION_NO_UUID"); |
| 90 | + Assert.assertEquals("Main Street", loc.getAddress1()); |
| 91 | + Assert.assertEquals("fdddc31a-3930-11ea-9712-a73c3c19744f", loc.getUuid()); |
| 92 | + } |
| 93 | + // Verify creation with dynamic tag creation |
| 94 | + { |
| 95 | + Location loc = ls.getLocation("The Lake Clinic-Cambodia"); |
| 96 | + Assert.assertEquals("Paradise Street", loc.getAddress1()); |
| 97 | + Assert.assertEquals("Siem Reap", loc.getCountyDistrict()); |
| 98 | + Assert.assertEquals("Siem Reap", loc.getStateProvince()); |
| 99 | + Assert.assertEquals("Cambodia", loc.getCountry()); |
| 100 | + |
| 101 | + Set<LocationTag> tags = loc.getTags(); |
| 102 | + Assert.assertThat(tags, notNullValue()); |
| 103 | + Assert.assertThat(tags.size(), is(2)); |
| 104 | + Assert.assertThat(tags.contains(ls.getLocationTagByName("Login Location")), is(true)); |
| 105 | + Assert.assertThat(tags.contains(ls.getLocationTagByName("Another Location Tag")), is(true)); |
| 106 | + |
| 107 | + Collection<LocationAttribute> attributes = loc.getActiveAttributes(); |
| 108 | + Assert.assertThat(attributes.size(), is(2)); |
| 109 | + Assert.assertEquals("CODE-TLC-123", ((LocationAttribute) attributes.toArray()[0]).getValue()); |
| 110 | + Assert.assertEquals("2017-05-15", |
| 111 | + dateDatatype.serialize((Date) ((LocationAttribute) attributes.toArray()[1]).getValue())); |
| 112 | + } |
| 113 | + // Verify creation with Tag| headers |
| 114 | + { |
| 115 | + Location loc = ls.getLocation("OPD Room"); |
| 116 | + Assert.assertEquals(ls.getLocation("The Lake Clinic-Cambodia"), loc.getParentLocation()); |
| 117 | + |
| 118 | + Set<LocationTag> tags = loc.getTags(); |
| 119 | + Assert.assertThat(tags, notNullValue()); |
| 120 | + Assert.assertThat(tags.size(), is(1)); |
| 121 | + Assert.assertThat(tags.contains(ls.getLocationTagByName("Facility Location")), is(true)); |
| 122 | + } |
| 123 | + // Verify that the provided UUID is correctly assigned |
| 124 | + { |
| 125 | + Location loc = ls.getLocationByUuid("1cb58794-3c49-11ea-b3eb-f7801304f314"); |
| 126 | + Assert.assertNotNull(loc); |
| 127 | + Assert.assertEquals("New Location", loc.getName()); |
| 128 | + } |
| 129 | + // Verify edit |
| 130 | + { |
| 131 | + Location loc = ls.getLocationByUuid("a03e395c-b881-49b7-b6fc-983f6bddc7fc"); |
| 132 | + Assert.assertEquals("Acme Clinic", loc.getName()); |
| 133 | + Assert.assertEquals("This now becomes a child of TLC", loc.getDescription()); |
| 134 | + Assert.assertEquals(ls.getLocation("The Lake Clinic-Cambodia"), loc.getParentLocation()); |
| 135 | + |
| 136 | + Set<LocationTag> tags = loc.getTags(); |
| 137 | + Assert.assertThat(tags, notNullValue()); |
| 138 | + Assert.assertThat(tags.size(), is(1)); |
| 139 | + Assert.assertThat(tags.contains(ls.getLocationTagByName("Login Location")), is(true)); |
| 140 | + |
| 141 | + Collection<LocationAttribute> attributes = loc.getActiveAttributes(); |
| 142 | + Assert.assertThat(attributes.size(), is(1)); |
| 143 | + Assert.assertEquals("2019-03-13", |
| 144 | + dateDatatype.serialize((Date) ((LocationAttribute) attributes.toArray()[0]).getValue())); |
| 145 | + |
| 146 | + } |
| 147 | + // Verify retire |
| 148 | + { |
| 149 | + Location loc = ls.getLocationByUuid("cbaaaab4-d960-4ae9-9b6a-8983fbd947b6"); |
| 150 | + Assert.assertThat(loc.getRetired(), is(true)); |
| 151 | + } |
| 152 | + // Verify that location with an invalid parent isn't created |
| 153 | + { |
| 154 | + Location loc = ls.getLocationByUuid("2b9824a3-92f0-4966-8f34-1b105624b267"); |
| 155 | + Assert.assertNull(loc); |
| 156 | + } |
| 157 | + |
| 158 | + // verify Locations domain i18n on entries with display:xy fields |
| 159 | + { |
| 160 | + Assert.assertEquals("Acme Clinic (translated)", Context.getMessageSourceService() |
| 161 | + .getMessage("ui.i18n.Location.name.a03e395c-b881-49b7-b6fc-983f6bddc7fc", null, localeEn)); |
| 162 | + Assert.assertEquals("គ្លីនិកអាមី", Context.getMessageSourceService() |
| 163 | + .getMessage("ui.i18n.Location.name.a03e395c-b881-49b7-b6fc-983f6bddc7fc", null, localeKm)); |
| 164 | + Assert.assertEquals("Acme Clinic (translated)", Context.getMessageSourceService() |
| 165 | + .getMessage("org.openmrs.Location.a03e395c-b881-49b7-b6fc-983f6bddc7fc", null, localeEn)); |
| 166 | + Assert.assertEquals("គ្លីនិកអាមី", Context.getMessageSourceService() |
| 167 | + .getMessage("org.openmrs.Location.a03e395c-b881-49b7-b6fc-983f6bddc7fc", null, localeKm)); |
| 168 | + } |
| 169 | + // verify no Locations domain i18n on entries without display:xy fields and entries without pre-filled uuids |
| 170 | + { |
| 171 | + Assert.assertNotNull(ls.getLocationByUuid("1cb58794-3c49-11ea-b3eb-f7801304f314")); |
| 172 | + Assert.assertEquals("ui.i18n.Location.name.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 173 | + Context.getMessageSourceService().getMessage("ui.i18n.Location.name.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 174 | + null, localeEn)); |
| 175 | + Assert.assertEquals("ui.i18n.Location.name.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 176 | + Context.getMessageSourceService().getMessage("ui.i18n.Location.name.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 177 | + null, localeKm)); |
| 178 | + |
| 179 | + Assert.assertEquals("org.openmrs.Location.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 180 | + Context.getMessageSourceService().getMessage("org.openmrs.Location.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 181 | + null, localeEn)); |
| 182 | + Assert.assertEquals("org.openmrs.Location.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 183 | + Context.getMessageSourceService().getMessage("org.openmrs.Location.1cb58794-3c49-11ea-b3eb-f7801304f314", |
| 184 | + null, localeKm)); |
| 185 | + |
| 186 | + Location loc = ls.getLocation("The Lake Clinic-Cambodia"); |
| 187 | + Assert.assertNotNull(loc); |
| 188 | + |
| 189 | + String uuid = loc.getUuid(); |
| 190 | + Assert.assertEquals("ui.i18n.Location.name." + uuid, |
| 191 | + Context.getMessageSourceService().getMessage("ui.i18n.Location.name." + uuid, null, localeEn)); |
| 192 | + Assert.assertEquals("ui.i18n.Location.name." + uuid, |
| 193 | + Context.getMessageSourceService().getMessage("ui.i18n.Location.name." + uuid, null, localeKm)); |
| 194 | + Assert.assertEquals("org.openmrs.Location." + uuid, |
| 195 | + Context.getMessageSourceService().getMessage("org.openmrs.Location." + uuid, null, localeEn)); |
| 196 | + Assert.assertEquals("org.openmrs.Location." + uuid, |
| 197 | + Context.getMessageSourceService().getMessage("org.openmrs.Location." + uuid, null, localeKm)); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments