|
| 1 | +package org.opencds.cqf.tooling.utilities; |
| 2 | + |
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | +import static org.testng.Assert.assertNull; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.hl7.fhir.r5.model.Extension; |
| 10 | +import org.hl7.fhir.r5.model.IntegerType; |
| 11 | +import org.hl7.fhir.r5.model.StringType; |
| 12 | +import org.opencds.cqf.tooling.utilities.constants.CqfConstants; |
| 13 | +import org.opencds.cqf.tooling.utilities.constants.CqfmConstants; |
| 14 | +import org.testng.annotations.Test; |
| 15 | + |
| 16 | +public class LogicDefinitionUtilsTests { |
| 17 | + |
| 18 | + private static Extension logicDefinition(String url, String libraryName, String name, int displaySequence) { |
| 19 | + Extension ext = new Extension().setUrl(url); |
| 20 | + ext.addExtension(new Extension().setUrl("libraryName").setValue(new StringType(libraryName))); |
| 21 | + ext.addExtension(new Extension().setUrl("name").setValue(new StringType(name))); |
| 22 | + ext.addExtension(new Extension().setUrl("displaySequence").setValue(new IntegerType(displaySequence))); |
| 23 | + return ext; |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void TestKeyUsesLibraryNameAndName() { |
| 28 | + Extension ext = logicDefinition(CqfConstants.LOGIC_DEFINITION_EXT_URL, "HRDMeasure", "Inpatient Beds Initial Population", 38); |
| 29 | + assertEquals(LogicDefinitionUtils.getLogicDefinitionKey(ext), "HRDMeasure|Inpatient Beds Initial Population"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void TestKeyIsNullWhenLibraryNameMissing() { |
| 34 | + Extension ext = new Extension().setUrl(CqfConstants.LOGIC_DEFINITION_EXT_URL); |
| 35 | + ext.addExtension(new Extension().setUrl("name").setValue(new StringType("X"))); |
| 36 | + assertNull(LogicDefinitionUtils.getLogicDefinitionKey(ext)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void TestIsLogicDefinitionRecognizesBothUrls() { |
| 41 | + Extension cqf = new Extension().setUrl(CqfConstants.LOGIC_DEFINITION_EXT_URL); |
| 42 | + Extension cqfm = new Extension().setUrl(CqfmConstants.LOGIC_DEFINITION_EXT_URL); |
| 43 | + Extension other = new Extension().setUrl("http://example.org/other"); |
| 44 | + assertEquals(LogicDefinitionUtils.isLogicDefinition(cqf), true); |
| 45 | + assertEquals(LogicDefinitionUtils.isLogicDefinition(cqfm), true); |
| 46 | + assertEquals(LogicDefinitionUtils.isLogicDefinition(other), false); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void TestDeduplicateRemovesDuplicatesKeepingFirst() { |
| 51 | + List<Extension> extensions = new ArrayList<>(); |
| 52 | + extensions.add(logicDefinition(CqfConstants.LOGIC_DEFINITION_EXT_URL, "HRDMeasure", "Inpatient Beds Initial Population", 38)); |
| 53 | + extensions.add(logicDefinition(CqfConstants.LOGIC_DEFINITION_EXT_URL, "HRDMeasure", "Adult Inpatient Beds Initial Population", 40)); |
| 54 | + extensions.add(logicDefinition(CqfConstants.LOGIC_DEFINITION_EXT_URL, "HRDMeasure", "Inpatient Beds Initial Population", 82)); |
| 55 | + |
| 56 | + LogicDefinitionUtils.deduplicate(extensions); |
| 57 | + |
| 58 | + assertEquals(extensions.size(), 2); |
| 59 | + assertEquals(LogicDefinitionUtils.getLogicDefinitionKey(extensions.get(0)), "HRDMeasure|Inpatient Beds Initial Population"); |
| 60 | + // The first-encountered entry is kept, so the displaySequence of the survivor is 38, not 82. |
| 61 | + Extension kept = extensions.get(0); |
| 62 | + int displaySequence = ((IntegerType) kept.getExtensionByUrl("displaySequence").getValue()).getValue(); |
| 63 | + assertEquals(displaySequence, 38); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void TestDeduplicatePreservesNonLogicDefinitionExtensions() { |
| 68 | + List<Extension> extensions = new ArrayList<>(); |
| 69 | + extensions.add(new Extension().setUrl("http://example.org/other").setValue(new StringType("a"))); |
| 70 | + extensions.add(logicDefinition(CqfConstants.LOGIC_DEFINITION_EXT_URL, "Lib", "Def", 1)); |
| 71 | + extensions.add(logicDefinition(CqfConstants.LOGIC_DEFINITION_EXT_URL, "Lib", "Def", 2)); |
| 72 | + extensions.add(new Extension().setUrl("http://example.org/other").setValue(new StringType("b"))); |
| 73 | + |
| 74 | + LogicDefinitionUtils.deduplicate(extensions); |
| 75 | + |
| 76 | + // One logicDefinition removed; both unrelated extensions retained. |
| 77 | + assertEquals(extensions.size(), 3); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void TestDeduplicateDedupesAcrossCqfmAndCqfUrls() { |
| 82 | + List<Extension> extensions = new ArrayList<>(); |
| 83 | + extensions.add(logicDefinition(CqfmConstants.LOGIC_DEFINITION_EXT_URL, "Lib", "Def", 1)); |
| 84 | + extensions.add(logicDefinition(CqfConstants.LOGIC_DEFINITION_EXT_URL, "Lib", "Def", 2)); |
| 85 | + |
| 86 | + LogicDefinitionUtils.deduplicate(extensions); |
| 87 | + |
| 88 | + assertEquals(extensions.size(), 1); |
| 89 | + } |
| 90 | +} |
0 commit comments