-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChangeRelationshipsServiceTest.java
239 lines (195 loc) · 8.59 KB
/
ChangeRelationshipsServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.changerelationships.api;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.openmrs.Person;
import org.openmrs.PersonName;
import org.openmrs.Relationship;
import org.openmrs.RelationshipType;
import org.openmrs.api.PersonService;
import org.openmrs.api.context.Context;
import org.openmrs.log.myLogger;
import org.openmrs.test.BaseModuleContextSensitiveTest;
/**
* Tests {@link ${ChangeRelationshipsService}}.
*/
public class ChangeRelationshipsServiceTest extends BaseModuleContextSensitiveTest {
private PersonService personService;
private ChangeRelationshipsService testCangeRelationshipService;
private ArrayList<Person> testPeople;
private ArrayList<Relationship> testRelations;
private ArrayList<RelationshipType> testRelationshipTypes;
private void setup()
{
personService = Context.getPersonService();
this.testCangeRelationshipService = Context.getService(ChangeRelationshipsService.class);
this.testPeople = new ArrayList();
this.testRelations = new ArrayList();
this.testRelationshipTypes = new ArrayList();
}
private void createTestPeopleAndRelations()
{
setup();
testPeople.add(createAndSavePerson("Person1", "For1", "Testing1", "Male", 1967, 8,3 ));
testPeople.add(createAndSavePerson("Person2", "For2", "Testing2", "Male", 1967, 8,3 ));
testPeople.add(createAndSavePerson("Person3", "For3", "Testing3", "Male", 1967, 8,3 ));
testPeople.add(createAndSavePerson("Person4", "For4", "Testing4", "Male", 1967, 8,3 ));
testPeople.add(createAndSavePerson("Person5", "For5", "Testing5", "Male", 1967, 8,3 ));
testRelationshipTypes.add(createNewRelationType("TestRType1", "TestRType2"));
testRelationshipTypes.add(createNewRelationType("TestRType3", "TestRType4"));
testRelationshipTypes.add(createNewRelationType("TestRType5", "TestRType6"));
testRelationshipTypes.add(createNewRelationType("TestRType7", "TestRType8"));
testRelations.add(new Relationship(testPeople.get(0), testPeople.get(1), testRelationshipTypes.get(0) ));
testRelations.add(new Relationship(testPeople.get(0), testPeople.get(2), testRelationshipTypes.get(0) ));
testRelations.add(new Relationship(testPeople.get(0), testPeople.get(3), testRelationshipTypes.get(0) ));
testRelations.add(new Relationship(testPeople.get(0), testPeople.get(4), testRelationshipTypes.get(1) ));
saveAllRelations();
}
private void saveAllRelations() {
for(Relationship r : testRelations)
personService.saveRelationship(r);
}
private RelationshipType createNewRelationType(String string, String string2) {
RelationshipType tempType = new RelationshipType();
String desc = new String(string + string2);
tempType.setDescription(desc);
tempType.setaIsToB(string);
tempType.setbIsToA(string2);
personService.saveRelationshipType(tempType);
return tempType;
}
private Person createAndSavePerson(String givenName, String middleName, String familyName, String gender,
int birthDateyy, int birthDatemm, int birthDatedd)
{
Person tempPersonObj = new Person();
PersonName tempPersonNameObj = new PersonName(givenName, middleName, familyName);
tempPersonObj.setGender(gender);
tempPersonObj.addName(tempPersonNameObj);
tempPersonObj.setBirthdate(new Date(birthDateyy, birthDatemm, birthDatedd));
try{
personService.savePerson(tempPersonObj);
}catch(Exception ae)
{
myLogger.print("Unable to save person record to DB in Test Function");
}
return tempPersonObj;
}
private void deleteDataCreatedForTests() {
/*Purge all relations*/
for(Relationship rs : testRelations)
{
personService.purgeRelationship(rs);
}
/*Purge all relationshipTypes*/
for(RelationshipType rt : testRelationshipTypes)
{
personService.purgeRelationshipType(rt);
}
/*Purge all persons*/
for(Person p : testPeople)
{
personService.purgePerson(p);
}
}
@Test
public void shouldSetupContext() {
assertNotNull(Context.getService(ChangeRelationshipsService.class));
}
@Test
public void shouldSetupPersonService(){
setup();
assertNotNull(personService);
}
@Test
public void getPersonObjectFromInputnameTest()
{
setup();
Person testPerson = createAndSavePerson("Person4", "For4", "Testing4", "Male", 1909, 12, 12);
Person testPerson2 = this.testCangeRelationshipService.getPersonObjectFromInputname(testPerson.getFamilyName());
assertNotNull(testPerson2);
assertArrayEquals(testPerson2.getFamilyName().toCharArray(), testPerson.getFamilyName().toCharArray());
testPerson2 = this.testCangeRelationshipService.getPersonObjectFromInputname(testPerson.getGivenName());
assertNotNull(testPerson2);
assertArrayEquals(testPerson2.getGivenName().toCharArray(), testPerson.getGivenName().toCharArray());
testPerson2 = this.testCangeRelationshipService.getPersonObjectFromInputname(testPerson.getMiddleName());
assertNotNull(testPerson2);
assertArrayEquals(testPerson2.getMiddleName().toCharArray(), testPerson.getMiddleName().toCharArray());
assertNull(this.testCangeRelationshipService.getPersonObjectFromInputname("Person who does not exist"));
personService.purgePerson(testPerson); //delete person created for testing
}
@Test
public void findRelationshipTypeFromInputTest()
{
setup();
String relationName;
List<RelationshipType> testTypes = personService.getAllRelationshipTypes();
for(RelationshipType trstype : testTypes)
{
relationName = trstype.getaIsToB() + "/" + trstype.getbIsToA();
assertNotNull(this.testCangeRelationshipService.findRelationshipTypeFromInput(relationName));
}
}
/*Test assertions depends on type of input provided in createTestPeopleAndRelations()*/
//@Test
/*public void numberOfRelationshipGivenPersonAndRelationshipTypeTest()
{
setup();
createTestPeopleAndRelations();
System.out.println("Test person : " + testPeople.get(0).getFamilyName() +
"Test RType : " + testRelationshipTypes.get(0).getaIsToB());
assertEquals(3, this.testCangeRelationshipService.numberOfRelationships(testPeople.get(0),
testRelationshipTypes.get(0)));
assertEquals(1, this.testCangeRelationshipService.numberOfRelationships(testPeople.get(0),
testRelationshipTypes.get(1)));
assertEquals(0, this.testCangeRelationshipService.numberOfRelationships(testPeople.get(1),
testRelationshipTypes.get(0)));
deleteDataCreatedForTests();
}*/
/*Test assertions depends on type of input provided in createTestPeopleAndRelations()*/
/*@Test
public void numberOfRelationshipsWhenAllSelected()
{
setup();
createTestPeopleAndRelations();
assertEquals(4, this.testCangeRelationshipService.numberOfRelationships(testPeople.get(0)));
assertEquals(0, this.testCangeRelationshipService.numberOfRelationships(testPeople.get(3)));
deleteDataCreatedForTests();
}*/
//@Test
//public void updateRelativesToNewPersonTest()
//{
//setup();
//createTestPeopleAndRelations();
/*First person has 1 relative for relationshipType 1, the following function also sets the list of
* people who are related to Person at testPeople[0]*/
//this.testCangeRelationshipService.numberOfRelationships(testPeople.get(0), testRelationshipTypes.get(1));
/*Must be able to update */
//boolean updateSuccessful = this.testCangeRelationshipService.updateRelativesToNewPerson
//(testPeople.get(2), testRelationshipTypes.get(0));
//assertTrue(updateSuccessful);
//this.testCangeRelationshipService.numberOfRelationships(testPeople.get(0));
//updateSuccessful = this.testCangeRelationshipService.updateRelativesToNewPerson(testPeople.get(2),
//testRelationshipTypes.get(0));
/*This should fail because testPeople[2](who is the new person/toPerson) is one of the people already
* related to testPeople[0]. While all the records will be updated, the record for person testPeople[2]
* will not*/
//assertFalse(updateSuccessful);
//deleteDataCreatedForTests();
//}
}