/
SyncClientTest.java
145 lines (115 loc) · 5.9 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
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.Patient;
import org.openmrs.PatientIdentifier;
import org.openmrs.PersonName;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.FHIRClient;
import org.openmrs.module.sync2.api.impl.SyncPullServiceImpl;
import org.openmrs.module.sync2.api.utils.SyncUtils;
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.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
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 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 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 Map<String, String> links;
@Before
public void setUp() throws Exception {
expectedPatient = createPatient();
administrationServiceMock = mock(AdministrationService.class);
mockStatic(Context.class);
BDDMockito.given(Context.getAdministrationService()).willReturn(administrationServiceMock);
BDDMockito.given(Context.getAdministrationService()).willReturn(administrationServiceMock);
doReturn(USERNAME).when(administrationServiceMock).getGlobalProperty("sync2.user.login");
doReturn(PASSWORD).when(administrationServiceMock).getGlobalProperty("sync2.user.password");
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);
FHIRClient fhirClientMock = mock(FHIRClient.class);
doReturn(createPatient()).when(fhirClientMock).retrieveObject(PATIENT_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.pullDataFromParent(PATIENT_CATEGORY, REST_CLIENT_KEY,
REST_FULL_RESOURCE_URL);
assertThat(pulledObject, is(expectedPatient));
}
@Test
public void pullDataFromParent_shouldCallFHIRClient() {
SyncClient resourceManager = new SyncClient();
Object pulledObject = resourceManager.pullDataFromParent(PATIENT_CATEGORY, FHIR_CLIENT_KEY,
FHIR_FULL_RESOURCE_URL);
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;
}
}