Skip to content

Commit

Permalink
Rad 657 test http client (#401)
Browse files Browse the repository at this point in the history
* Updated shell script

* Updated readme.md file

* Make improvment in the README.md file

* Update README.md

* Update README.md

* Integrating code artifact suggestions

* RAD-657 test http client setting

---------

Co-authored-by: Vipul Deshkar <[email protected]>
Co-authored-by: Vipul Deshkar <[email protected]>
  • Loading branch information
3 people authored Nov 23, 2023
1 parent 72e3851 commit 23cd335
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion fhir-helpers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.elimu.a2d2</groupId>
<artifactId>fhir-helpers</artifactId>
<version>0.0.17</version>
<version>0.0.18</version>
<packaging>jar</packaging>
<name>fhir-helpers</name>
<url>http://maven.apache.org</url>
Expand Down
2 changes: 1 addition & 1 deletion fhir-query-helper-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</parent>

<artifactId>fhir-query-helper-base</artifactId>
<version>0.0.17</version>
<version>0.0.18</version>
<description>CDS Helper base</description>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.regex.Pattern;

import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.Logger;
Expand Down Expand Up @@ -116,6 +117,12 @@ public QueryingServerHelperBase(String url, FhirVersionAbs fhirVersion, FhirVers

log.info("Cache config: {}", (cacheService == null) ? "NO_CACHE" : cacheService.getCacheType());
}

public void setHttpClient(HttpClient client) {
for (int count = 0; count < clients.size(); count++) {
clients.next().getFhirContext().getRestfulClientFactory().setHttpClient(client);
}
}

private synchronized static Rotator<FhirClientWrapper> getClientInstance(FhirContext ctxt, String fhirUrl) {
String key = genClientKey(ctxt, fhirUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public synchronized T next() {
iter = rotation.iterator();
}
return iter.next();
}

public int size() {
return rotation.size();
}
}
2 changes: 1 addition & 1 deletion fhir-query-helper-dstu2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</parent>

<artifactId>fhir-query-helper-dstu2</artifactId>
<version>0.0.17</version>
<version>0.0.18</version>
<packaging>jar</packaging>
<description>CDS Helper</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
Expand All @@ -25,12 +24,20 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
Expand Down Expand Up @@ -95,6 +102,8 @@ public void testQSH() throws Exception {
client.setDontValidateConformance(true);
client.setEncoding(EncodingEnum.JSON);*/
QueryingServerHelper qsh = new QueryingServerHelper(url);
HttpClient client = expectGet("https://hapi.fhir.org/baseDstu2/Patient/131?_format=json", 200, "{\"resourceType\":\"Patient\", \"id\":\"131\"}");
qsh.setHttpClient(client);
FhirResponse<?> resp = qsh.getResourceByIdInPathResponse("Patient", patientId);
Assert.assertEquals(200, resp.getResponseStatusCode());
System.out.println(resp.getResult());
Expand All @@ -111,6 +120,24 @@ public void testQSH() throws Exception {
Assert.assertEquals(200, resp.getStatusLine().getStatusCode());*/
}

private HttpClient expectGet(String url, int httpStatus, String responseBody) throws Exception {
HttpClient client = Mockito.mock(HttpClient.class);
Mockito.when(client.execute(Mockito.any(HttpGet.class))).thenAnswer(new Answer<HttpResponse>() {
@Override
public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
HttpGet get = (HttpGet) invocation.getArgument(0);
Assert.assertEquals(url, get.getURI().toASCIIString());
HttpResponse response = Mockito.mock(HttpResponse.class);
StatusLine statusLine = Mockito.mock(StatusLine.class);
Mockito.when(statusLine.getStatusCode()).thenReturn(httpStatus);
Mockito.when(response.getStatusLine()).thenReturn(statusLine);
Mockito.when(response.getEntity()).thenReturn(new StringEntity(responseBody, ContentType.APPLICATION_JSON));
return response;
}
});
return client;
}

@Test
public void testQueryBuilder() {
doReturn(fhirResponse).when(queryingServerHelper).queryServer(eq(FHIR2_URL + "/Patient?_count=1&_sort=-_lastUpdated"),
Expand Down
2 changes: 1 addition & 1 deletion fhir-query-helper-dstu3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</parent>

<artifactId>fhir-query-helper-dstu3</artifactId>
<version>0.0.17</version>
<version>0.0.18</version>
<packaging>jar</packaging>
<description>CDS Helper DSTU3</description>

Expand Down
2 changes: 1 addition & 1 deletion fhir-query-helper-r4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</parent>

<artifactId>fhir-query-helper-r4</artifactId>
<version>0.0.17</version>
<version>0.0.18</version>
<packaging>jar</packaging>
<description>CDS Helper R4</description>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<protobuf-java.version>3.21.8</protobuf-java.version>
<mockito.version>2.23.0</mockito.version>
<jedis.version>4.0.1</jedis.version>
<cds.helper.version>0.0.17</cds.helper.version>
<cds.helper.version>0.0.18</cds.helper.version>
<freemarker.version>2.3.30</freemarker.version>
<jacoco.version>0.8.7</jacoco.version>
<jaxb.api.version>2.3.1</jaxb.api.version>
Expand Down

0 comments on commit 23cd335

Please sign in to comment.