Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
08/09/08 16:18:42 (5 months ago)
Author:
mcunderlik
Message:

data_synchronization_bidirectional branch: fixed unit tests to work with Junit4 changes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/branches/data_synchronization_bidirectional/test/api/org/openmrs/test/synchronization/engine/SyncPatientTest.java

    r5163 r5215  
    1414package org.openmrs.test.synchronization.engine; 
    1515 
     16import static org.junit.Assert.assertEquals; 
     17import static org.junit.Assert.assertNotSame; 
     18import static org.junit.Assert.assertTrue; 
     19import static org.junit.Assert.assertFalse; 
     20import static org.junit.Assert.assertNull; 
     21import static org.junit.Assert.assertNotNull; 
     22 
    1623import java.text.SimpleDateFormat; 
    1724import java.util.ArrayList; 
     
    2229import java.util.Set; 
    2330 
    24 import org.hibernate.Session
     31import org.junit.Test
    2532import org.openmrs.Concept; 
    2633import org.openmrs.Encounter; 
     
    3037import org.openmrs.PatientIdentifier; 
    3138import org.openmrs.PatientIdentifierType; 
    32 import org.openmrs.PatientState; 
    3339import org.openmrs.PatientProgram; 
    3440import org.openmrs.PersonName; 
     
    4147import org.openmrs.util.OpenmrsUtil; 
    4248 
     49import org.springframework.test.annotation.NotTransactional; 
     50import org.springframework.transaction.annotation.Transactional; 
     51import org.springframework.test.annotation.Rollback; 
     52 
    4353/** 
    4454 * Tests creating various pieces of data via synchronization 
     
    5161    } 
    5262         
    53          
    54         public void testEnrollInProgram() throws Exception { 
     63        @Test 
     64        @NotTransactional 
     65        public void shouldEnrollInProgram() throws Exception { 
    5566                runSyncTest(new SyncTestHelper() { 
    5667                        int numberEnrolledBefore = 0; 
     
    8394        } 
    8495         
    85         public void testEnrollInProgramAndState() throws Exception { 
     96        @Test 
     97        @NotTransactional 
     98        public void shouldEnrollInProgramAndState() throws Exception { 
    8699                runSyncTest(new SyncTestHelper() { 
    87100                        int numberEnrolledBefore = 0; 
     
    130143        } 
    131144         
    132         public void testChangeState() throws Exception { 
     145        @Test 
     146        @NotTransactional 
     147        public void shouldChangeState() throws Exception { 
    133148                runSyncTest(new SyncTestHelper() { 
    134149                        Program hivProgram; 
     
    155170        } 
    156171         
     172        @Test 
     173        @NotTransactional 
     174        public void shouldCreateEncounterAndObs() throws Exception { 
     175                runSyncTest(new CreateEncounterAndObsTest()); 
     176        } 
    157177        private class CreateEncounterAndObsTest implements SyncTestHelper { 
    158178                int numEncountersSoFar = 0; 
     
    166186                        Concept reason = cs.getConceptByName("REASON ORDER STOPPED"); 
    167187                        Concept other = cs.getConceptByName("OTHER NON-CODED"); 
    168                         Location loc = Context.getEncounterService().getLocationByName("Someplace"); 
     188                        Location loc = Context.getLocationService().getLocation("Someplace"); 
    169189 
    170190                        User u = Context.getUserService().getUser(1); 
    171191                        Patient p = Context.getPatientService().getPatient(2); 
    172                         numEncountersSoFar = Context.getEncounterService().getEncounters(p).size(); 
     192                        numEncountersSoFar = Context.getEncounterService().getEncountersByPatient(p).size(); 
    173193                         
    174194                        Encounter enc = new Encounter(); 
     
    187207                        enc.addObs(o1); 
    188208                        enc.addObs(o2); 
    189                         Context.getEncounterService().createEncounter(enc); 
     209                        Context.getEncounterService().saveEncounter(enc); 
    190210 
    191211                        Obs noEnc = new Obs(); 
     
    195215                        noEnc.setPerson(p); 
    196216                        noEnc.setLocation(loc); 
    197                         Context.getObsService().createObs(noEnc); 
     217                        Context.getObsService().saveObs(noEnc,null); 
    198218                } 
    199                  
    200219                public void runOnParent() { 
    201220                         
     
    204223                        Concept reason = cs.getConceptByName("REASON ORDER STOPPED"); 
    205224                        Concept other = cs.getConceptByName("OTHER NON-CODED"); 
    206                         Location loc = Context.getEncounterService().getLocationByName("Someplace"); 
     225                        Location loc = Context.getLocationService().getLocation("Someplace"); 
    207226                        Patient p = Context.getPatientService().getPatient(2); 
    208227                         
    209228                         
    210                         List<Encounter> encs = Context.getEncounterService().getEncounters(p); 
     229                        List<Encounter> encs = Context.getEncounterService().getEncountersByPatient(p); 
    211230                        assertEquals("Should now have one more encounter than before", 
    212231                                     numEncountersSoFar + 1, 
     
    222241                        assertEquals(lookAt.getLocation(), loc); 
    223242                         
    224                         assertEquals("Should have two obs", lookAt.getObs().size(), 2); 
     243                        //reload lookAt 
     244                        int lookAtId = lookAt.getEncounterId(); 
     245                        Context.evictFromSession(lookAt); 
     246                        lookAt = Context.getEncounterService().getEncounter(lookAtId); 
     247                        assertEquals("Should have two obs", 2,lookAt.getObs().size()); 
    225248                        for (Obs o : lookAt.getObs()) { 
    226249                                if (o.getConcept().equals(weight)) { 
    227                                         assertEquals("Weight should be 74.0", o.getValueNumeric(), 74.0); 
     250                                        assertEquals("Weight should be 74.0", o.getValueNumeric(), (Double)74.0); 
    228251                                } else { 
    229252                                        assertEquals("Reason should be OTHER NON-CODED", o.getValueCoded(), other); 
     
    232255                         
    233256                        boolean found = false; 
    234                         for (Obs o : Context.getObsService().getObservations(p, false)) { 
     257                        for (Obs o : Context.getObsService().getObservationsByPerson(p)) { 
    235258                                if ( (OpenmrsUtil.compare(o.getObsDatetime(), anotherDate) == 0) && o.getConcept().equals(weight) && o.getValueNumeric().equals(12.3)) 
    236259                                        found = true; 
     
    238261                        assertTrue("Cannot find newly created encounter-less obs", found); 
    239262                } 
    240         } 
    241          
    242         public void testCreateEncounterAndObs() throws Exception { 
    243                 runSyncTest(new CreateEncounterAndObsTest()); 
    244263        } 
    245264         
     
    249268         * @throws Exception 
    250269         */ 
    251         public void testEditEncounter() throws Exception { 
     270        @Test 
     271        @NotTransactional 
     272        public void shouldEditEncounter() throws Exception { 
    252273                runSyncTest(new SyncTestHelper() { 
    253274                        Date d1 = ymd.parse("1978-01-01"); 
     
    274295        } 
    275296 
    276         public void testEditObs() throws Exception { 
     297        @Test 
     298        @NotTransactional 
     299        public void shouldEditObs() throws Exception { 
    277300                runSyncTest(new SyncTestHelper() { 
    278301                        Date d = ymd.parse("1978-04-11"); 
    279302                        Concept weight = null; 
     303                        int obsCount = 0; 
    280304                        public void runOnChild(){ 
    281305                                weight = Context.getConceptService().getConceptByName("WEIGHT"); 
    282306                                Patient p = Context.getPatientService().getPatient(2); 
    283307                                Obs obs = null; 
    284                                 for (Obs o : Context.getObsService().getObservations(p, weight, false)) { 
    285                                         if (OpenmrsUtil.compare(o.getObsDatetime(), d) == 0) 
     308                                for (Obs o : Context.getObsService().getObservationsByPersonAndConcept(p, weight)) { 
     309                                        if (OpenmrsUtil.compare(o.getObsDatetime(), d) == 0) { 
    286310                                                obs = o; 
     311                                        } 
    287312                                } 
     313                                 
     314                                obsCount = obs.getEncounter().getObs().size(); 
    288315                                assertNotNull("Before test, could not find expected obs", obs); 
    289316                                Context.getObsService().voidObs(obs, "Data entry error"); 
     317                                 
     318                                assertEquals(obsCount - 1,obs.getEncounter().getObs().size() ); 
     319                                obsCount = obs.getEncounter().getObs().size(); 
    290320                                 
    291321                                Obs newObs = new Obs(); 
     
    298328                                newObs.setValueNumeric(99.9); 
    299329                                newObs.setEncounter(obs.getEncounter()); 
    300                                 Context.getObsService().createObs(newObs); 
    301                         } 
    302                         public void runOnParent() { 
    303                                 Patient p = Context.getPatientService().getPatient(2); 
    304                                 boolean found = false; 
    305                                 for (Obs o : Context.getObsService().getObservations(p, weight, false)) 
     330                                newObs = Context.getObsService().saveObs(newObs, null); 
     331                                obsCount++; 
     332                                 
     333                                int encId = newObs.getEncounter().getEncounterId(); 
     334                                Context.evictFromSession(newObs.getEncounter()); 
     335                                assertEquals(obsCount,Context.getEncounterService().getEncounter(encId).getObs().size()); 
     336                        } 
     337                        public void runOnParent() { 
     338                                int encId = 0; 
     339                                Patient p = Context.getPatientService().getPatient(2); 
     340                                Obs obs = null; 
     341                                for (Obs o : Context.getObsService().getObservationsByPersonAndConcept(p, weight)) 
    306342                                        if (OpenmrsUtil.compare(o.getObsDatetime(),d)==0) { 
    307                                                 assertEquals(o.getEncounter().getObs().size(), 3); 
    308                                                 assertEquals(o.getValueNumeric(), 99.9); 
    309                                                 found = true; 
     343                                                obs = o; 
     344                                                break; 
    310345                                        } 
    311                                 assertTrue(found); 
    312                         } 
    313                 }); 
    314         } 
    315  
    316         public void testCreatePatient() throws Exception { 
     346                                assertNotNull(obs); 
     347                                assertEquals( (Double)99.9, obs.getValueNumeric()); 
     348                                encId = obs.getEncounter().getEncounterId();                             
     349                                Context.evictFromSession(obs.getEncounter()); 
     350                                assertEquals(obsCount,Context.getEncounterService().getEncounter(encId).getObs().size()); 
     351                        } 
     352                }); 
     353        } 
     354                 
     355        @Test 
     356        @NotTransactional 
     357        public void shouldCreatePatient() throws Exception { 
     358                 
    317359                runSyncTest(new SyncTestHelper() { 
    318360                        public void runOnChild() { 
     
    353395        } 
    354396         
    355         public void testEditPatient() throws Exception { 
     397        @Test 
     398        @NotTransactional 
     399        public void shouldEditPatient() throws Exception { 
    356400                runSyncTest(new SyncTestHelper() { 
    357401                        PatientIdentifierType pit; 
     
    380424                }); 
    381425        } 
    382  
    383426}