[507] | 1 | /*
|
---|
| 2 | * To change this template, choose Tools | Templates
|
---|
| 3 | * and open the template in the editor.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | package gov.hhs.fha.nhinc.cpp;
|
---|
| 7 |
|
---|
| 8 | import gov.hhs.fha.nhinc.mpilib.*;
|
---|
| 9 | import org.junit.After;
|
---|
| 10 | import org.junit.AfterClass;
|
---|
| 11 | import org.junit.Before;
|
---|
| 12 | import org.junit.BeforeClass;
|
---|
| 13 | import org.junit.Test;
|
---|
| 14 | import org.w3c.dom.*;
|
---|
| 15 | import javax.xml.xpath.*;
|
---|
| 16 | import javax.xml.parsers.*;
|
---|
| 17 | import static org.junit.Assert.*;
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | *
|
---|
| 21 | * @author svalluripalli
|
---|
| 22 | */
|
---|
| 23 | public class CPPUtilsTest {
|
---|
| 24 |
|
---|
| 25 | public CPPUtilsTest() {
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | private static final String orgId = "123";
|
---|
| 29 | private static final String patId = "123-123";
|
---|
| 30 |
|
---|
| 31 | @BeforeClass
|
---|
| 32 | public static void setUpClass() throws Exception {
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | @AfterClass
|
---|
| 36 | public static void tearDownClass() throws Exception {
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | @Before
|
---|
| 40 | public void setUp() {
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @After
|
---|
| 44 | public void tearDown() {
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | @Test
|
---|
| 48 | public void testextractId()
|
---|
| 49 | {
|
---|
| 50 | Document doc = null;
|
---|
| 51 | Identifier id = null;
|
---|
| 52 | try
|
---|
| 53 | {
|
---|
| 54 | DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance( );
|
---|
| 55 | DocumentBuilder builder = builderFactory.newDocumentBuilder( );
|
---|
| 56 |
|
---|
| 57 | doc = builder.parse (new org.xml.sax.InputSource ("test/gov/hhs/fha/nhinc/cpp/OptIn.xml"));
|
---|
| 58 |
|
---|
| 59 | id = CPPUtils.extractPatientId(doc);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | catch (Exception ex)
|
---|
| 63 | {
|
---|
| 64 | assertEquals(1,0);
|
---|
| 65 | }
|
---|
| 66 | assertEquals(patId, id.getId());
|
---|
| 67 | assertEquals(orgId, id.getOrganizationId());
|
---|
| 68 | assertFalse(CPPUtils.isOptedIn(doc));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Test
|
---|
| 72 | public void testCreateXAML_OptIn()
|
---|
| 73 | {
|
---|
| 74 | Patient p = new Patient();
|
---|
| 75 | PersonName name = new PersonName("Test", "Dummy");
|
---|
| 76 | p.setName(name);
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | Identifiers ids = new Identifiers();
|
---|
| 80 | Identifier id = new Identifier(patId,orgId);
|
---|
| 81 |
|
---|
| 82 | ids.add(id);
|
---|
| 83 | p.setIdentifiers(ids);
|
---|
| 84 | p.setOptedIn(true);
|
---|
| 85 |
|
---|
| 86 | Document policy = CPPUtils.createXAML(p);
|
---|
| 87 |
|
---|
| 88 | assertPatInfo(policy);
|
---|
| 89 |
|
---|
| 90 | //should only have 1 Rule
|
---|
| 91 | assertEquals(policy.getElementsByTagName("Rule").getLength(), 1);
|
---|
| 92 |
|
---|
| 93 | Node Rule = policy.getElementsByTagName("Rule").item(0);
|
---|
| 94 |
|
---|
| 95 | assertEquals(CPPUtils.OPT_IN_RULE_ID,Rule.getAttributes().getNamedItem("RuleId").getNodeValue());
|
---|
| 96 |
|
---|
| 97 | assertEquals(2,Rule.getChildNodes().getLength());
|
---|
| 98 |
|
---|
| 99 | XPathFactory factory=XPathFactory.newInstance();
|
---|
| 100 | XPath xPath=factory.newXPath();
|
---|
| 101 |
|
---|
| 102 | NodeList descrs = null;
|
---|
| 103 | NodeList target = null;
|
---|
| 104 | try
|
---|
| 105 | {
|
---|
| 106 | descrs =(NodeList) (xPath.evaluate("Policy/Rule/Description", policy,XPathConstants.NODESET));
|
---|
| 107 | target =(NodeList) (xPath.evaluate("Policy/Rule/Target", policy,XPathConstants.NODESET));
|
---|
| 108 | }
|
---|
| 109 | catch(Exception ex)
|
---|
| 110 | {
|
---|
| 111 |
|
---|
| 112 | }
|
---|
| 113 | assertEquals(1, descrs.getLength());
|
---|
| 114 | assertEquals(1, target.getLength());
|
---|
| 115 | assertEquals(CPPUtils.PERMIT_DESCR,descrs.item(0).getTextContent());
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | @Test
|
---|
| 119 | public void testCreateXAML_OptOut()
|
---|
| 120 | {
|
---|
| 121 | Patient p = new Patient();
|
---|
| 122 | PersonName name = new PersonName("Test", "Dummy");
|
---|
| 123 | p.setName(name);
|
---|
| 124 |
|
---|
| 125 | Identifiers ids = new Identifiers();
|
---|
| 126 | Identifier id = new Identifier(patId,orgId);
|
---|
| 127 |
|
---|
| 128 | ids.add(id);
|
---|
| 129 | p.setIdentifiers(ids);
|
---|
| 130 | p.setOptedIn(false);
|
---|
| 131 |
|
---|
| 132 | Document policy = CPPUtils.createXAML(p);
|
---|
| 133 |
|
---|
| 134 | assertPatInfo(policy);
|
---|
| 135 |
|
---|
| 136 | //should only have 1 Rule
|
---|
| 137 | assertEquals(policy.getElementsByTagName("Rule").getLength(), 1);
|
---|
| 138 |
|
---|
| 139 | Node Rule = policy.getElementsByTagName("Rule").item(0);
|
---|
| 140 |
|
---|
| 141 | assertEquals(Rule.getAttributes().getNamedItem("RuleId").getNodeValue(),CPPUtils.OPT_OUT_RULE_ID);
|
---|
| 142 |
|
---|
| 143 | assertEquals(Rule.getChildNodes().getLength(), 2);
|
---|
| 144 |
|
---|
| 145 | XPathFactory factory=XPathFactory.newInstance();
|
---|
| 146 | XPath xPath=factory.newXPath();
|
---|
| 147 |
|
---|
| 148 | NodeList descrs = null;
|
---|
| 149 | NodeList target = null;
|
---|
| 150 | try
|
---|
| 151 | {
|
---|
| 152 | descrs =(NodeList) (xPath.evaluate("Policy/Rule/Description", policy,XPathConstants.NODESET));
|
---|
| 153 | target =(NodeList) (xPath.evaluate("Policy/Rule/Target", policy,XPathConstants.NODESET));
|
---|
| 154 | }
|
---|
| 155 | catch(Exception ex)
|
---|
| 156 | {
|
---|
| 157 | }
|
---|
| 158 | assertEquals(1, descrs.getLength());
|
---|
| 159 | assertEquals(1, target.getLength());
|
---|
| 160 | assertEquals(CPPUtils.DENY_DESCR,descrs.item(0).getTextContent());
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private void assertPatInfo(Document policy)
|
---|
| 164 | {
|
---|
| 165 | //should only have 1 patient
|
---|
| 166 | assertEquals(policy.getElementsByTagName("PatientId").getLength(), 1);
|
---|
| 167 |
|
---|
| 168 | assertEquals(policy.getElementsByTagName("PatientId").item(0).getAttributes().getNamedItem("Root").getNodeValue(),orgId);
|
---|
| 169 | assertEquals(policy.getElementsByTagName("PatientId").item(0).getAttributes().getNamedItem("Extension").getNodeValue(),patId);
|
---|
| 170 |
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | }
|
---|