Last active
June 8, 2019 18:12
-
-
Save isears/45e4d3ff54f34855afadc08d63aa101d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.nio.charset.StandardCharsets; | |
import java.util.ArrayList; | |
import java.util.Base64; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
import com.google.gson.*; | |
/** | |
* | |
* @author levine | |
*/ | |
public class RestOpenMrsClientCreatePatient { | |
/* | |
CREATE PERSON & PATIENT TOGETHER | |
https://github.com/openmrs/openmrs-module-webservices.rest/blob/master/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientController1_9Test.java#L102-L116 | |
@Test | |
public void shouldCreatePersonAndPatient() throws Exception { | |
long originalCount = service.getAllPatients().size(); | |
String json = "{ \"identifiers\": [{ \"identifier\":\"abc123ez\", " | |
+ "\"identifierType\":\"2f470aa8-1d73-43b7-81b5-01f0c0dfa53c\", " | |
+ "\"location\":\"9356400c-a5a2-4532-8f2b-2361b3446eb8\", " + "\"preferred\": true }], " + "\"person\": { " | |
+ "\"gender\": \"M\", " + "\"age\": 47, " + "\"birthdate\": \"1970-01-01T00:00:00.000+0100\", " | |
+ "\"birthdateEstimated\": false, " + "\"dead\": false, " + "\"deathDate\": null, " | |
+ "\"causeOfDeath\": null, " + "\"names\": [{\"givenName\": \"Thomas\", \"familyName\": \"Smith\"}] " + "}}"; | |
SimpleObject newPatient = deserialize(handle(newPostRequest(getURI(), json))); | |
assertNotNull(PropertyUtils.getProperty(newPatient, "uuid")); | |
assertEquals(originalCount + 1, service.getAllPatients().size()); | |
*/ | |
String baseURL = "http://127.0.0.1:8081/openmrs-standalone/ws/rest/v1/"; | |
public static void main(String[] args) { | |
RestOpenMrsClientCreatePatient test = new RestOpenMrsClientCreatePatient(); | |
String personUUID = test.createPerson(); | |
String newId = test.getPatientId(); | |
System.out.println("PERSON UUID: " + personUUID + " NEW PATIENT ID: " + newId); | |
test.createPatient(personUUID, newId); | |
test.createPersonPatientOnePost(test.getPatientId()); | |
} | |
private String createPerson() { | |
String personUUID = ""; | |
String output, action, response = null; | |
action | |
= "{ \"names\": [ { \"givenName\": \"bbrrererxxss\", \"familyName\": \"javcvcvgbb\"} ], \"gender\": \"M\", \"birthdate\": \"2014-04-29\", \"birthdateEstimated\": true, \"addresses\": [ { \"address1\": \"add1\", \"cityVillage\": \"city\" } ]}"; | |
System.out.println("ACTION: " + action); | |
try { | |
URL urlForPost = new URL(baseURL + "person"); | |
HttpURLConnection postConnJSON = (HttpURLConnection) urlForPost.openConnection(); | |
String encoded = Base64.getEncoder().encodeToString(("admin" + ":" + "Admin123").getBytes(StandardCharsets.UTF_8)); //Java 8 | |
postConnJSON.setRequestProperty("Authorization", "Basic " + encoded); | |
postConnJSON.setDoOutput(true); | |
postConnJSON.setRequestMethod("POST"); | |
postConnJSON.setRequestProperty("Content-Type", "application/json"); | |
postConnJSON.setRequestProperty("Accept", "application/json"); | |
OutputStream os = postConnJSON.getOutputStream(); | |
byte[] input = action.getBytes("utf-8"); | |
os.write(input, 0, input.length); | |
System.out.println("POST Response Message : " + postConnJSON.getResponseMessage()); | |
personUUID = processResponsePerson(postConnJSON); | |
} catch (Exception ex) { | |
System.out.println("****************** POST ISSUE: " + ex); | |
} | |
return personUUID; | |
} | |
private String processResponsePerson(HttpURLConnection connectionToURL) throws IOException, RuntimeException { | |
String personUUID; | |
int code = connectionToURL.getResponseCode(); | |
if (connectionToURL.getResponseCode() >= 300) { | |
throw new RuntimeException("Failed : HTTP error code : " | |
+ connectionToURL.getResponseCode()); | |
} | |
BufferedReader br = new BufferedReader(new InputStreamReader( | |
(connectionToURL.getInputStream()))); | |
String output, msg = ""; | |
System.out.println("Output from Server .... \n"); | |
System.out.println("Response Code: " + connectionToURL.getResponseCode()); | |
while ((output = br.readLine()) != null) { | |
msg += output; | |
System.out.println(output); | |
} | |
personUUID = processJSONPerson(msg); | |
connectionToURL.disconnect(); | |
System.out.println("--------------------------\n\n"); | |
return personUUID; | |
} | |
private String processJSONPerson(String msg) { | |
// GET PATIENTS | |
JsonParser prsr = new JsonParser(); | |
JsonElement jsEl = prsr.parse(msg); | |
System.out.println("PERSON"); | |
JsonObject obj = jsEl.getAsJsonObject(); | |
String personUUID = obj.get("uuid").getAsString(); | |
return personUUID; | |
} | |
private String getPatientId() { | |
try { | |
String baseURL = "http://127.0.0.1:8081/openmrs-standalone/module/idgen/generateIdentifier.form?source=1&username=admin&password=Admin123"; | |
URL url; | |
System.out.println("GET: " + baseURL); | |
url = new URL(baseURL); | |
HttpURLConnection connectionToURL = (HttpURLConnection) url.openConnection(); | |
String encoded = Base64.getEncoder().encodeToString(("admin" + ":" + "Admin123").getBytes(StandardCharsets.UTF_8)); //Java 8 | |
connectionToURL.setRequestProperty("Authorization", "Basic " + encoded); | |
connectionToURL.setRequestMethod("GET"); | |
connectionToURL.setRequestProperty("Accept", "application/json"); | |
return processResponsePatientId(connectionToURL); | |
} catch (Exception ex) { | |
System.out.println("****************** GET ISSUE: " + ex); | |
} | |
return ""; | |
} | |
private String processResponsePatientId(HttpURLConnection connectionToURL) throws IOException, RuntimeException { | |
String patientId = ""; | |
int code = connectionToURL.getResponseCode(); | |
if (connectionToURL.getResponseCode() >= 300) { | |
throw new RuntimeException("Failed : HTTP error code : " | |
+ connectionToURL.getResponseCode()); | |
} | |
BufferedReader br = new BufferedReader(new InputStreamReader( | |
(connectionToURL.getInputStream()))); | |
String output, msg = ""; | |
System.out.println("Output from Server .... \n"); | |
System.out.println("Response Code: " + connectionToURL.getResponseCode()); | |
while ((output = br.readLine()) != null) { | |
msg += output; | |
System.out.println(output); | |
} | |
patientId = processJSONPatientId(msg); | |
connectionToURL.disconnect(); | |
System.out.println("--------------------------\n\n"); | |
return patientId; | |
} | |
private String processJSONPatientId(String msg) { | |
String newId = ""; | |
JsonParser prsr = new JsonParser(); | |
JsonElement jsEl = prsr.parse(msg); | |
System.out.println("IDENTIFIERS"); | |
JsonObject obj = jsEl.getAsJsonObject(); | |
JsonArray resultArray = obj.getAsJsonArray("identifiers"); | |
for (JsonElement resultElement : resultArray) { | |
newId = resultElement.getAsString(); | |
System.out.println("IDENTIFIER: " + newId); | |
} | |
return newId; | |
} | |
private void createPatient(String personUUID, String newId) { | |
/* | |
curl -u admin:Admin123 -X GET "http://127.0.0.1:8081/openmrs-standalone/ws/rest/v1/location?v=default" -H "accept: application/json" | |
*/ | |
String output, action; | |
action | |
= "{ \"person\": \"" + personUUID + "\"," | |
+ "\"identifiers\": [" | |
+ "{" | |
+ "\"identifier\": \"" + newId + "\"," | |
+ "\"identifierType\": \"05a29f94-c0ed-11e2-94be-8c13b969e334\"," | |
+ "\"location\": \"aff27d58-a15c-49a6-9beb-d30dcfc0c66e\"," | |
+ "\"preferred\": true" | |
+ "} ]}"; | |
//= "{ \"names\": [ { \"givenName\": \"bbvv\", \"familyName\": \"java\"} ], \"gender\": \"M\", \"birthdate\": \"2014-04-29\", \"birthdateEstimated\": true, \"addresses\": [ { \"address1\": \"add1\", \"cityVillage\": \"city\" } ]}"; | |
System.out.println("ACTION: " + action); | |
try { | |
URL urlForPost = new URL(baseURL + "patient"); | |
HttpURLConnection postConnJSON = (HttpURLConnection) urlForPost.openConnection(); | |
String encoded = Base64.getEncoder().encodeToString(("admin" + ":" + "Admin123").getBytes(StandardCharsets.UTF_8)); //Java 8 | |
postConnJSON.setRequestProperty("Authorization", "Basic " + encoded); | |
postConnJSON.setDoOutput(true); | |
postConnJSON.setRequestMethod("POST"); | |
postConnJSON.setRequestProperty("Content-Type", "application/json"); | |
postConnJSON.setRequestProperty("Accept", "application/json"); | |
OutputStream os = postConnJSON.getOutputStream(); | |
byte[] input = action.getBytes("utf-8"); | |
os.write(input, 0, input.length); | |
System.out.println("POST Response Message : " + postConnJSON.getResponseMessage()); | |
processResponsePatient(postConnJSON); | |
} catch (Exception ex) { | |
System.out.println("****************** POST ISSUE: " + ex); | |
} | |
} | |
private void processResponsePatient(HttpURLConnection connectionToURL) throws IOException, RuntimeException { | |
int code = connectionToURL.getResponseCode(); | |
if (connectionToURL.getResponseCode() >= 300) { | |
throw new RuntimeException("Failed : HTTP error code : " | |
+ connectionToURL.getResponseCode()); | |
} | |
BufferedReader br = new BufferedReader(new InputStreamReader( | |
(connectionToURL.getInputStream()))); | |
String output, msg = ""; | |
System.out.println("Output from Server .... \n"); | |
System.out.println("Response Code: " + connectionToURL.getResponseCode()); | |
while ((output = br.readLine()) != null) { | |
msg += output; | |
} | |
System.out.println(output); | |
connectionToURL.disconnect(); | |
} | |
private void createPersonPatientOnePost(String patientId) { | |
String action = "{ \"identifiers\": [{ \"identifier\":\"" + patientId + "\", " | |
+ "\"identifierType\":\"05a29f94-c0ed-11e2-94be-8c13b969e334\", " | |
+ "\"location\":\"aff27d58-a15c-49a6-9beb-d30dcfc0c66e\", " + "\"preferred\": true }], " + "\"person\": { " | |
+ "\"gender\": \"M\", " + "\"age\": 49, " + "\"birthdate\": \"1970-01-01T00:00:00.000+0100\", " | |
+ "\"birthdateEstimated\": false, " + "\"dead\": false, " + "\"deathDate\": null, " | |
+ "\"causeOfDeath\": null, " + "\"names\": [{\"givenName\": \"Thomas\", \"familyName\": \"Smith12\"}] " + "}}"; | |
System.out.println("ACTION: " + action); | |
try { | |
URL urlForPost = new URL(baseURL + "patient"); | |
HttpURLConnection postConnJSON = (HttpURLConnection) urlForPost.openConnection(); | |
String encoded = Base64.getEncoder().encodeToString(("admin" + ":" + "Admin123").getBytes(StandardCharsets.UTF_8)); //Java 8 | |
postConnJSON.setRequestProperty("Authorization", "Basic " + encoded); | |
postConnJSON.setDoOutput(true); | |
postConnJSON.setRequestMethod("POST"); | |
postConnJSON.setRequestProperty("Content-Type", "application/json"); | |
postConnJSON.setRequestProperty("Accept", "application/json"); | |
OutputStream os = postConnJSON.getOutputStream(); | |
byte[] input = action.getBytes("utf-8"); | |
os.write(input, 0, input.length); | |
System.out.println("POST Response Message : " + postConnJSON.getResponseMessage()); | |
processResponsePatient(postConnJSON); | |
} catch (Exception ex) { | |
System.out.println("****************** POST ISSUE: " + ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment