Skip to content

Commit

Permalink
SYNCT-295: Replaced RequestEntity class to the ClientHttpEntity class
Browse files Browse the repository at this point in the history
  • Loading branch information
areklalo committed Dec 14, 2018
1 parent b121c04 commit b0b973d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
@@ -1,7 +1,7 @@
package org.openmrs.module.sync2.api.model;

import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;

import java.io.Serializable;
import java.net.URI;
Expand All @@ -18,7 +18,7 @@ public class InnerRequest implements Serializable {
public InnerRequest() {
}

public InnerRequest(RequestEntity<?> entity) {
public InnerRequest(ClientHttpEntity<?> entity) {
this.method = entity.getMethod();
this.url = entity.getUrl();
this.body = (entity.getBody() != null) ? entity.getBody().toString() : null;
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.openmrs.User;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.fhir.api.client.ClientHttpRequestInterceptor;
import org.openmrs.module.fhir.api.helper.ClientHelper;
import org.openmrs.module.sync2.api.model.RequestWrapper;
Expand All @@ -13,7 +14,6 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
Expand All @@ -33,7 +33,7 @@ public ResponseEntity<String> getObject(RequestWrapper wrapper) {
RestTemplate restTemplate = prepareRestTemplate(wrapper.getClientName());
ClientHelper helper = ClientHelperFactory.createClient(wrapper.getClientName());
try {
RequestEntity<String> req = new RequestEntity<>(wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
ClientHttpEntity<String> req = new ClientHttpEntity<>(wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
return copyResponseWithContentType(exchange(restTemplate, helper, req, String.class));
}
catch (HttpClientErrorException e) {
Expand All @@ -48,7 +48,7 @@ public ResponseEntity<String> sendObject(RequestWrapper wrapper) {
ClientHelper helper = ClientHelperFactory.createClient(wrapper.getClientName());
Object object = helper.convertToObject(wrapper.getRequest().getBody(), wrapper.getClazz());

RequestEntity req = new RequestEntity<>(object, wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
ClientHttpEntity req = new ClientHttpEntity<>(object, wrapper.getRequest().getMethod(), wrapper.getRequest().getUrl());
ResponseEntity<String> res = exchange(restTemplate, helper, req, String.class);

return new ResponseEntity<>(res.getBody(), res.getStatusCode());
Expand Down Expand Up @@ -119,10 +119,10 @@ private HttpHeaders setRequestHeaders(ClientHelper clientHelper, HttpHeaders hea
return headers;
}

private ResponseEntity exchange(RestTemplate restTemplate, ClientHelper helper, RequestEntity request, Class clazz) {
private ResponseEntity exchange(RestTemplate restTemplate, ClientHelper helper, ClientHttpEntity request, Class clazz) {
HttpHeaders headers = new HttpHeaders();
setRequestHeaders(helper, headers);
HttpEntity entity = new HttpEntity(request.getBody(), headers);
HttpEntity entity = new HttpEntity<>(request.getBody(), headers);
return restTemplate.exchange(request.getUrl(), request.getMethod(), entity, clazz);
}

Expand Down
@@ -1,5 +1,6 @@
package org.openmrs.module.sync2.api.sync;

import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.fhir.api.client.ClientHttpRequestInterceptor;
import org.openmrs.module.fhir.api.helper.ClientHelper;
import org.openmrs.module.sync2.api.exceptions.SyncException;
Expand All @@ -16,7 +17,6 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.HttpClientErrorException;
Expand Down Expand Up @@ -127,7 +127,7 @@ private Object retrieveObject(CategoryEnum category, String resourceUrl, String
ClientHelper helper = ClientHelperFactory.createClient(clientName);
Class<?> clazz = helper.resolveClassByCategory(category.getCategory());

RequestEntity request = helper.retrieveRequest(resourceUrl);
ClientHttpEntity request = helper.retrieveRequest(resourceUrl);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
Expand All @@ -139,7 +139,7 @@ private ResponseEntity<String> createObject(CategoryEnum category, String resour
String clientName, OpenMRSSyncInstance instance) throws RestClientException, URISyntaxException {
ClientHelper helper = ClientHelperFactory.createClient(clientName);

RequestEntity request = helper.createRequest(resourceUrl, object);
ClientHttpEntity request = helper.createRequest(resourceUrl, object);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
Expand All @@ -151,7 +151,7 @@ private ResponseEntity<String> deleteObject(CategoryEnum category, String resour
String clientName, OpenMRSSyncInstance instance) throws URISyntaxException {
ClientHelper helper = ClientHelperFactory.createClient(clientName);

RequestEntity request = helper.deleteRequest(resourceUrl, uuid);
ClientHttpEntity request = helper.deleteRequest(resourceUrl, uuid);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
Expand All @@ -163,7 +163,7 @@ private ResponseEntity<String> updateObject(CategoryEnum category, String resour
String clientName, OpenMRSSyncInstance instance) throws URISyntaxException {
ClientHelper helper = ClientHelperFactory.createClient(clientName);

RequestEntity request = helper.updateRequest(resourceUrl, object);
ClientHttpEntity request = helper.updateRequest(resourceUrl, object);
if (shouldWrappMessage(clientName, instance)) {
request = sendRequest(category, destinationUrl, clientName, new InnerRequest(request));
}
Expand All @@ -178,14 +178,14 @@ private HttpHeaders setRequestHeaders(ClientHelper clientHelper, HttpHeaders hea
return headers;
}

private ResponseEntity exchange(ClientHelper helper, RequestEntity request, Class clazz) {
private ResponseEntity exchange(ClientHelper helper, ClientHttpEntity request, Class clazz) {
HttpHeaders headers = new HttpHeaders();
setRequestHeaders(helper, headers);
HttpEntity entity = new HttpEntity(request.getBody(), headers);
return restTemplate.exchange(request.getUrl(), request.getMethod(), entity, clazz);
}

private RequestEntity<RequestWrapper> sendRequest(CategoryEnum category, String destinationUrl, String clientName,
private ClientHttpEntity<RequestWrapper> sendRequest(CategoryEnum category, String destinationUrl, String clientName,
InnerRequest request) throws URISyntaxException {
ClientHelper clientHelper = ClientHelperFactory.createClient(clientName);
Class<?> clazz = clientHelper.resolveClassByCategory(category.getCategory());
Expand All @@ -197,7 +197,7 @@ private RequestEntity<RequestWrapper> sendRequest(CategoryEnum category, String
wrapper.setClientName(clientName);
wrapper.setRequest(request);

return new RequestEntity<>(wrapper, HttpMethod.POST, new URI(destinationUrl));
return new ClientHttpEntity<>(wrapper, HttpMethod.POST, new URI(destinationUrl));
}

private boolean shouldWrappMessage(String clientName, OpenMRSSyncInstance instance) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.BasicAuthInterceptor;
import org.openmrs.module.fhir.api.client.BasicHttpRequestInterceptor;
import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.fhir.api.client.ClientHttpRequestInterceptor;
import org.openmrs.module.fhir.api.helper.ClientHelper;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
Expand All @@ -18,7 +19,6 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;

Expand All @@ -40,35 +40,35 @@ public class RESTClientHelper implements ClientHelper {
private final SimpleObjectMessageConverter simpleConverter = new SimpleObjectMessageConverter();

@Override
public RequestEntity retrieveRequest(String url) throws URISyntaxException {
return new RequestEntity(HttpMethod.GET, new URI(url));
public ClientHttpEntity retrieveRequest(String url) throws URISyntaxException {
return new ClientHttpEntity(HttpMethod.GET, new URI(url));
}

@Override
public RequestEntity createRequest(String url, Object object) throws URISyntaxException {
public ClientHttpEntity createRequest(String url, Object object) throws URISyntaxException {
if (object instanceof SimpleObject) {
getRestResourceConverter().convertObject(url, object);
}

return new RequestEntity(convertToFormattedData(object), HttpMethod.POST, new URI(url));
return new ClientHttpEntity<String>(convertToFormattedData(object), HttpMethod.POST, new URI(url));
}

@Override
public RequestEntity deleteRequest(String url, String uuid) throws URISyntaxException {
public ClientHttpEntity deleteRequest(String url, String uuid) throws URISyntaxException {
url += "/" + uuid;
return new RequestEntity(uuid, HttpMethod.DELETE, new URI(url));
return new ClientHttpEntity<String>(uuid, HttpMethod.DELETE, new URI(url));
}

@Override
public RequestEntity updateRequest(String url, Object object) throws URISyntaxException {
public ClientHttpEntity updateRequest(String url, Object object) throws URISyntaxException {
if (object instanceof AuditMessage) {
url += "/" + ((AuditMessage) object).getUuid();
return new RequestEntity(convertToFormattedData(object), HttpMethod.POST, new URI(url));
return new ClientHttpEntity<String>(convertToFormattedData(object), HttpMethod.POST, new URI(url));
} else {
getRestResourceConverter().convertObject(url, object);
url += "/" + ((SimpleObject) object).get("uuid");
}
return new RequestEntity(convertToFormattedData(object), HttpMethod.POST, new URI(url));
return new ClientHttpEntity<String>(convertToFormattedData(object), HttpMethod.POST, new URI(url));
}

@Override
Expand Down
Expand Up @@ -4,9 +4,9 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.client.ClientHttpEntity;
import org.openmrs.module.sync2.api.model.audit.AuditMessage;
import org.openmrs.module.sync2.api.utils.ContextUtils;
import org.openmrs.module.sync2.client.SimpleObjectMessageConverter;
Expand All @@ -17,7 +17,7 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;

import java.io.IOException;
import java.net.URI;

Expand Down Expand Up @@ -48,7 +48,7 @@ public void setUp() throws Exception {

@Test
public void retrieveRequest() throws Exception {
RequestEntity expected = new RequestEntity(HttpMethod.GET, URI.create(TEST_URI));
ClientHttpEntity expected = new ClientHttpEntity(HttpMethod.GET, URI.create(TEST_URI));

RESTClientHelper restClientHelper = new RESTClientHelper();
assertEquals(expected, restClientHelper.retrieveRequest(TEST_URI));
Expand All @@ -59,14 +59,14 @@ public void createRequest() throws Exception {
Patient patient = new Patient();
SimpleObject simpleObject = getSimpleObject(patient);
String json = (new SimpleObjectMessageConverter()).convertToJson(simpleObject);
RequestEntity expected = new RequestEntity(json, HttpMethod.POST, URI.create(TEST_URI));
ClientHttpEntity expected = new ClientHttpEntity<String>(json, HttpMethod.POST, URI.create(TEST_URI));

assertEquals(expected, restClientHelper.createRequest(TEST_URI, simpleObject));
}

@Test
public void deleteRequest() throws Exception {
RequestEntity expected = new RequestEntity(TEST_PATIENT_UUID, HttpMethod.DELETE,
ClientHttpEntity expected = new ClientHttpEntity<String>(TEST_PATIENT_UUID, HttpMethod.DELETE,
URI.create(TEST_URI + "/" + TEST_PATIENT_UUID));
RESTClientHelper restClientHelper = new RESTClientHelper();
assertEquals(expected, restClientHelper.deleteRequest(TEST_URI, TEST_PATIENT_UUID));
Expand All @@ -78,7 +78,7 @@ public void updateRequest() throws Exception {
patient.setUuid(TEST_PATIENT_UUID);
SimpleObject simpleObject = getSimpleObject(patient);
String json = (new SimpleObjectMessageConverter()).convertToJson(simpleObject);
RequestEntity expected = new RequestEntity(json, HttpMethod.POST,
ClientHttpEntity expected = new ClientHttpEntity<String>(json, HttpMethod.POST,
URI.create(TEST_URI + "/" + TEST_PATIENT_UUID));

assertEquals(expected, restClientHelper.updateRequest(TEST_URI, simpleObject));
Expand Down

0 comments on commit b0b973d

Please sign in to comment.