/
SyncClientTest.java
218 lines (186 loc) · 8.81 KB
/
SyncClientTest.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
package org.openmrs.module.sync2.api.sync;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.openmrs.*;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.FHIRClient;
import org.openmrs.module.sync2.client.ClientFactory;
import org.openmrs.module.sync2.client.rest.RestClient;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.openmrs.module.sync2.SyncConstants.PARENT_PASSWORD_PROPERTY;
import static org.openmrs.module.sync2.SyncConstants.PARENT_USERNAME_PROPERTY;
import static org.openmrs.module.sync2.api.model.enums.OpenMRSSyncInstance.PARENT;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.whenNew;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ SyncClient.class, Context.class })
public class SyncClientTest {
private static final String PATIENT_UUID = "patientUuid";
private static final String VISIT_UUID = "visitUuid";
private static final String ENCOUNTER_UUID = "encounterUuid";
private static final String OBS_UUID = "obsUuid";
private static final String FHIR_CLIENT_KEY = "fhir";
private static final String REST_CLIENT_KEY = "rest";
private static final String FHIR_RESOURCE_LINK = "openmrs/ws/fhir/Patient/";
private static final String REST_RESOURCE_LINK = "openmrs/ws/rest/v1/patient/";
private static final String PATIENT_CATEGORY = "patient";
private static final String VISIT_CATEGORY = "visit";
private static final String ENCOUNTER_CATEGORY = "encounter";
private static final String OB_CATEGORY = "ob";
private static final String PARENT_ADDRESS = "http://localhost:8080/";
private static final String PARENT_FEED_LOCATION = "http://localhost:8080/openmrs";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String REST_FULL_RESOURCE_URL = PARENT_ADDRESS + REST_RESOURCE_LINK + PATIENT_UUID;
private static final String FHIR_FULL_RESOURCE_URL = PARENT_ADDRESS + FHIR_RESOURCE_LINK + PATIENT_UUID;
private AdministrationService administrationServiceMock;
private Patient expectedPatient;
private Visit expectedVisit;
private Encounter expectedEncounter;
private Obs expectedOb;
private Map<String, String> links;
@Before
public void setUp() throws Exception {
expectedPatient = createPatient();
expectedVisit = createVisit();
administrationServiceMock = mock(AdministrationService.class);
mockStatic(Context.class);
BDDMockito.given(Context.getAdministrationService()).willReturn(administrationServiceMock);
BDDMockito.given(Context.getAdministrationService()).willReturn(administrationServiceMock);
doReturn(USERNAME).when(administrationServiceMock).getGlobalProperty(PARENT_USERNAME_PROPERTY);
doReturn(PASSWORD).when(administrationServiceMock).getGlobalProperty(PARENT_PASSWORD_PROPERTY);
doReturn(PARENT_FEED_LOCATION).when(administrationServiceMock).getGlobalProperty("sync2.general.parentFeedLocation");
links = new HashMap<>();
links.put(FHIR_CLIENT_KEY, FHIR_RESOURCE_LINK + PATIENT_UUID);
links.put(REST_CLIENT_KEY, REST_RESOURCE_LINK + PATIENT_UUID);
RestClient restClientMock = mock(RestClient.class);
doReturn(createPatient()).when(restClientMock).retrieveObject(PATIENT_CATEGORY, REST_FULL_RESOURCE_URL, USERNAME, PASSWORD);
doReturn(createVisit()).when(restClientMock).retrieveObject(VISIT_CATEGORY, REST_FULL_RESOURCE_URL, USERNAME, PASSWORD);
FHIRClient fhirClientMock = mock(FHIRClient.class);
doReturn(createPatient()).when(fhirClientMock).retrieveObject(PATIENT_CATEGORY, FHIR_FULL_RESOURCE_URL, USERNAME, PASSWORD);
doReturn(createVisit()).when(restClientMock).retrieveObject(VISIT_CATEGORY, FHIR_FULL_RESOURCE_URL, USERNAME, PASSWORD);
ClientFactory clientFactory = mock(ClientFactory.class);
doReturn(restClientMock).when(clientFactory).createClient(REST_CLIENT_KEY);
doReturn(fhirClientMock).when(clientFactory).createClient(FHIR_CLIENT_KEY);
whenNew(ClientFactory.class).withNoArguments().thenReturn(clientFactory);
}
@Test
public void pullDataFromParent_shouldCallRestClient() {
SyncClient resourceManager = new SyncClient();
Object pulledObject = resourceManager.pullData(PATIENT_CATEGORY, REST_CLIENT_KEY,
REST_FULL_RESOURCE_URL, PARENT);
assertThat(pulledObject, is(expectedPatient));
}
@Test
public void pullDataFromParent_shouldCallFHIRClient() {
SyncClient resourceManager = new SyncClient();
Object pulledObject = resourceManager.pullData(PATIENT_CATEGORY, FHIR_CLIENT_KEY,
FHIR_FULL_RESOURCE_URL, PARENT);
assertThat(pulledObject, is(expectedPatient));
}
private Patient createPatient() {
Patient patient = new Patient();
patient.setUuid(PATIENT_UUID);
PatientIdentifier patientIdentifier = new org.openmrs.PatientIdentifier();
patientIdentifier.setUuid("identifierId");
patientIdentifier.setIdentifier("identifier");
patientIdentifier.setPreferred(true);
patientIdentifier.setVoided(false);
Set<PatientIdentifier> patientIdentifierList = new TreeSet<>();
patientIdentifierList.add(patientIdentifier);
patient.setIdentifiers(patientIdentifierList);
patient.setGender("M");
patient.setBirthdate(new Date());
patient.setDead(false);
patient.setDeathDate(null);
patient.setCauseOfDeath(null);
PersonName personName = new PersonName();
personName.setUuid("nameUuid");
personName.setGivenName("John");
personName.setMiddleName(null);
personName.setFamilyName("Smith");
personName.setFamilyName2(null);
personName.setVoided(false);
Set<PersonName> personNameSet = new TreeSet<>();
personNameSet.add(personName);
patient.setNames(personNameSet);
patient.setVoided(false);
patient.setDeathdateEstimated(false);
patient.setBirthtime(new Date());
return patient;
}
private Visit createVisit() {
Visit visit = new Visit();
visit.setUuid(VISIT_UUID);
visit.setDateChanged(new Date());
visit.setDateCreated(new Date());
Encounter encounter = new Encounter();
encounter.setUuid(ENCOUNTER_UUID);
encounter.setDateChanged(new Date());
encounter.setDateCreated(new Date());
encounter.setEncounterDatetime(new Date());
visit.addEncounter(encounter);
visit.setIndication(null);
visit.setLocation(null);
visit.setStartDatetime(new Date());
visit.setStopDatetime(new Date());
visit.setVoided(false);
return visit;
}
private Encounter createEncounter() {
Encounter encounter = new Encounter();
encounter.setUuid(ENCOUNTER_UUID);
encounter.setDateChanged(new Date());
encounter.setDateCreated(new Date());
encounter.setEncounterDatetime(new Date());
encounter.setForm(null);
encounter.setLocation(null);
Obs obs = new org.openmrs.Obs();
obs.setUuid(OBS_UUID);
obs.setAccessionNumber("");
obs.setComment("");
obs.setDateCreated(new Date());
obs.setDateChanged(new Date());
Set<Obs> observations = new TreeSet<>();
observations.add(obs);
encounter.setObs(observations);
Order order = new org.openmrs.Order();
order.setUuid("orderId");
order.setDateCreated(new Date());
order.setDateActivated(new Date());
order.setDateChanged(new Date());
Set<Order> orders = new TreeSet<>();
orders.add(order);
encounter.setOrders(orders);
encounter.setVoided(true);
return encounter;
}
private Obs createOb() {
Obs obs = new org.openmrs.Obs();
obs.setUuid(OBS_UUID);
obs.setAccessionNumber("");
obs.setComment("");
obs.setDateCreated(new Date());
obs.setDateChanged(new Date());
obs.setObsGroup(null);
obs.setLocation(null);
obs.setObsDatetime(new Date());
obs.setValueText("");
obs.setValueTime(new Date());
obs.setVoided(false);
return obs;
}
}