|
9 | 9 | */ |
10 | 10 | package org.openmrs.module.initializer.api; |
11 | 11 |
|
| 12 | +import java.util.Collection; |
12 | 13 | import java.util.Locale; |
13 | 14 |
|
14 | 15 | import org.junit.Assert; |
|
17 | 18 | import org.openmrs.Concept; |
18 | 19 | import org.openmrs.ConceptName; |
19 | 20 | import org.openmrs.Drug; |
| 21 | +import org.openmrs.DrugIngredient; |
20 | 22 | import org.openmrs.api.ConceptService; |
21 | 23 | import org.openmrs.module.initializer.DomainBaseModuleContextSensitiveTest; |
22 | 24 | import org.openmrs.module.initializer.api.drugs.DrugsLoader; |
@@ -93,6 +95,20 @@ public void setup() { |
93 | 95 | d.setStrength("100mg"); |
94 | 96 | d = cs.saveDrug(d); |
95 | 97 | } |
| 98 | + { |
| 99 | + Drug d = new Drug(); |
| 100 | + d.setUuid("8abf401a-7f65-11f0-9e36-be568b1ab237"); |
| 101 | + d.setName("Drug with Erythromycine"); |
| 102 | + d.setConcept(cs.getConceptByName("d4T")); |
| 103 | + d.setStrength("20mg"); |
| 104 | + DrugIngredient ingredient = new DrugIngredient(); |
| 105 | + ingredient.setDrug(d); |
| 106 | + ingredient.setIngredient(cs.getConceptByName("Erythromycine")); |
| 107 | + ingredient.setStrength(20.0); |
| 108 | + ingredient.setUnits(cs.getConceptByName("mg")); |
| 109 | + d.getIngredients().add(ingredient); |
| 110 | + cs.saveDrug(d); |
| 111 | + } |
96 | 112 | } |
97 | 113 |
|
98 | 114 | @Test |
@@ -141,4 +157,89 @@ public void load_shouldLoadDrugsAccordingToCsvFiles() { |
141 | 157 | Assert.assertTrue(d.getRetired()); |
142 | 158 | } |
143 | 159 | } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void load_shouldAddIngredientsToDrugs() { |
| 163 | + loader.load(); |
| 164 | + { |
| 165 | + Drug d = cs.getDrug("Combo Drug"); |
| 166 | + Assert.assertNotNull(d); |
| 167 | + Assert.assertEquals(cs.getConceptByName("d4T"), d.getConcept()); |
| 168 | + Assert.assertEquals(cs.getConceptByName("Tablet"), d.getDosageForm()); |
| 169 | + Assert.assertEquals("10mg", d.getStrength()); |
| 170 | + Collection<DrugIngredient> ingredients = d.getIngredients(); |
| 171 | + Concept erythromycine = cs.getConceptByName("Erythromycine"); |
| 172 | + Concept cetirizine = cs.getConceptByName("Cetirizine"); |
| 173 | + Concept mg = cs.getConceptByName("mg"); |
| 174 | + Assert.assertEquals(2, ingredients.size()); |
| 175 | + for (DrugIngredient ingredient : ingredients) { |
| 176 | + if (ingredient.getIngredient().equals(erythromycine)) { |
| 177 | + Assert.assertEquals((Double) 4.0, ingredient.getStrength()); |
| 178 | + Assert.assertEquals(mg, ingredient.getUnits()); |
| 179 | + } else if (ingredient.getIngredient().equals(cetirizine)) { |
| 180 | + Assert.assertEquals((Double) 6.0, ingredient.getStrength()); |
| 181 | + Assert.assertEquals(mg, ingredient.getUnits()); |
| 182 | + } else { |
| 183 | + Assert.fail("Unexpected ingredient " + ingredient); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void load_shouldRemoveIngredientsFromDrugs() { |
| 191 | + |
| 192 | + Drug drug = cs.getDrugByUuid("8abf401a-7f65-11f0-9e36-be568b1ab237"); |
| 193 | + Assert.assertNotNull(drug); |
| 194 | + Assert.assertEquals("Drug with Erythromycine", drug.getName()); |
| 195 | + Assert.assertEquals(1, drug.getIngredients().size()); |
| 196 | + DrugIngredient erythromycine = drug.getIngredients().iterator().next(); |
| 197 | + Assert.assertEquals(cs.getConceptByName("Erythromycine"), erythromycine.getIngredient()); |
| 198 | + Assert.assertEquals((Double) 20.0, erythromycine.getStrength()); |
| 199 | + Assert.assertEquals(cs.getConceptByName("mg"), erythromycine.getUnits()); |
| 200 | + |
| 201 | + loader.load(); |
| 202 | + |
| 203 | + drug = cs.getDrugByUuid("8abf401a-7f65-11f0-9e36-be568b1ab237"); |
| 204 | + Assert.assertNotNull(drug); |
| 205 | + Assert.assertEquals("Drug without Erythromycine", drug.getName()); |
| 206 | + Assert.assertEquals(1, drug.getIngredients().size()); |
| 207 | + DrugIngredient cetirizine = drug.getIngredients().iterator().next(); |
| 208 | + Assert.assertNotEquals(erythromycine.getUuid(), cetirizine.getUuid()); |
| 209 | + Assert.assertEquals(cs.getConceptByName("Cetirizine"), cetirizine.getIngredient()); |
| 210 | + Assert.assertEquals((Double) 15.0, cetirizine.getStrength()); |
| 211 | + Assert.assertEquals(cs.getConceptByName("mg"), cetirizine.getUnits()); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void load_shouldModifyIngredientsInDrugs() { |
| 216 | + |
| 217 | + loader.load(); |
| 218 | + |
| 219 | + Drug drug = cs.getDrugByUuid("8abf401a-7f65-11f0-9e36-be568b1ab237"); |
| 220 | + Assert.assertNotNull(drug); |
| 221 | + Assert.assertEquals("Drug without Erythromycine", drug.getName()); |
| 222 | + Assert.assertEquals(1, drug.getIngredients().size()); |
| 223 | + DrugIngredient cetirizine = drug.getIngredients().iterator().next(); |
| 224 | + Assert.assertEquals(cs.getConceptByName("Cetirizine"), cetirizine.getIngredient()); |
| 225 | + Assert.assertEquals((Double) 15.0, cetirizine.getStrength()); |
| 226 | + |
| 227 | + cetirizine.setStrength(20.0); |
| 228 | + cs.saveDrug(drug); |
| 229 | + |
| 230 | + drug = cs.getDrugByUuid("8abf401a-7f65-11f0-9e36-be568b1ab237"); |
| 231 | + Assert.assertEquals(1, drug.getIngredients().size()); |
| 232 | + DrugIngredient originalIngredient = drug.getIngredients().iterator().next(); |
| 233 | + Assert.assertEquals((Double) 20.0, originalIngredient.getStrength()); |
| 234 | + |
| 235 | + loader.getDirUtil().deleteChecksums(); |
| 236 | + loader.load(); |
| 237 | + |
| 238 | + drug = cs.getDrugByUuid("8abf401a-7f65-11f0-9e36-be568b1ab237"); |
| 239 | + Assert.assertEquals(1, drug.getIngredients().size()); |
| 240 | + DrugIngredient modifiedIngredient = drug.getIngredients().iterator().next(); |
| 241 | + // NOTE: Ideally we'd test this, but due to a bug in core with saving drug ingredients, it doesn't work unless you are on very specific versions of core |
| 242 | + //Assert.assertEquals(originalIngredient.getUuid(), modifiedIngredient.getUuid()); |
| 243 | + Assert.assertEquals((Double) 15.0, modifiedIngredient.getStrength()); |
| 244 | + } |
144 | 245 | } |
0 commit comments