Skip to content

Commit

Permalink
ISPN-12075 Remove alternative http clients from the testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Fernandes authored and pruivo committed Jul 3, 2020
1 parent ce77d03 commit a29917f
Show file tree
Hide file tree
Showing 31 changed files with 526 additions and 1,030 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.infinispan.marshaller.test;

import static org.infinispan.util.concurrent.CompletionStages.join;
import static org.testng.AssertJUnit.assertEquals;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.infinispan.client.rest.RestCacheClient;
import org.infinispan.client.rest.RestEntity;
import org.infinispan.client.rest.RestResponse;
import org.infinispan.commons.marshall.Marshaller;
import org.infinispan.it.endpoints.EmbeddedRestMemcachedHotRodTest;
import org.infinispan.it.endpoints.EndpointsCacheFactory;
Expand All @@ -33,11 +32,10 @@ public void testRestPutEmbeddedMemcachedHotRodGetTest() throws Exception {

// 1. Put with REST
byte[] bytes = marshaller.objectToByteBuffer(value);
EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key);
put.setRequestEntity(new ByteArrayRequestEntity(bytes, marshaller.mediaType().toString()));
HttpClient restClient = cacheFactory.getRestClient();
restClient.executeMethod(put);
assertEquals(HttpStatus.SC_NO_CONTENT, put.getStatusCode());

RestCacheClient restClient = cacheFactory.getRestCacheClient();
RestResponse response = join(restClient.put(key, RestEntity.create(marshaller.mediaType(), bytes)));
assertEquals(204, response.getStatus());

// 2. Get with Embedded (given a marshaller, it can unmarshall the result)
assertEquals(value, cacheFactory.getEmbeddedCache().get(key));
Expand Down
4 changes: 2 additions & 2 deletions client/marshaller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-rest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.infinispan.client.rest;

import java.io.File;
import java.io.InputStream;

import org.infinispan.client.rest.impl.okhttp.ByteArrayRestEntityOkHttp;
import org.infinispan.client.rest.impl.okhttp.FileRestEntityOkHttp;
import org.infinispan.client.rest.impl.okhttp.InputStreamEntityOkHttp;
import org.infinispan.client.rest.impl.okhttp.StringRestEntityOkHttp;
import org.infinispan.commons.dataconversion.MediaType;

Expand All @@ -27,4 +29,8 @@ static RestEntity create(MediaType contentType, byte[] body) {
static RestEntity create(MediaType contentType, File file) {
return new FileRestEntityOkHttp(contentType, file);
}

static RestEntity create(MediaType contentType, InputStream inputStream) {
return new InputStreamEntityOkHttp(contentType, inputStream);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.infinispan.client.rest.impl.okhttp;

import java.io.InputStream;

import org.infinispan.client.rest.RestEntity;
import org.infinispan.commons.dataconversion.MediaType;

import okhttp3.RequestBody;

/**
* @since 12.0
*/
public class InputStreamEntityOkHttp implements RestEntity, RestEntityAdaptorOkHttp {
private final MediaType contentType;
private final InputStream inputStream;

public InputStreamEntityOkHttp(MediaType contentType, InputStream inputStream) {
this.contentType = contentType;
this.inputStream = inputStream;
}

@Override
public String getBody() {
throw new UnsupportedOperationException();
}

@Override
public MediaType contentType() {
return contentType;
}

@Override
public RequestBody toRequestBody() {
return new StreamRequestBody(okhttp3.MediaType.get(contentType.toString()), inputStream);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.infinispan.client.rest.impl.okhttp;

import java.io.IOException;
import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

/**
* Support for {@link java.io.InputStream} to be used in POST and PUT {@link okhttp3.RequestBody}.
* @since 12.0
*/
public class StreamRequestBody extends RequestBody {
private final MediaType contentType;
private final InputStream inputStream;

public StreamRequestBody(MediaType contentType, InputStream inputStream) {
this.contentType = contentType;
this.inputStream = inputStream;
}

@Override
public MediaType contentType() {
return contentType;
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
try (Source source = Okio.source(inputStream)) {
sink.writeAll(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class ThreadLeakChecker {
"|testcontainers" +
"|Okio Watchdog" +
"|OkHttp ConnectionPool" +
// OkHttp uses daemon threads for HTTP/2
"|OkHttp Http2Connection" +
").*");
private static final String ARQUILLIAN_CONSOLE_CONSUMER =
"org.jboss.as.arquillian.container.managed.ManagedDeployableContainer$ConsoleConsumer";
Expand Down
9 changes: 2 additions & 7 deletions integrationtests/endpoints-interop-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-rest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
import static org.infinispan.rest.JSONConstants.TYPE;
import static org.infinispan.server.core.test.ServerTestingUtil.findFreePort;
import static org.infinispan.test.TestingUtil.killCacheManagers;
import static org.infinispan.util.concurrent.CompletionStages.join;
import static org.testng.Assert.assertEquals;

import java.io.IOException;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.infinispan.client.hotrod.DataFormat;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.Search;
import org.infinispan.client.rest.RestCacheClient;
import org.infinispan.client.rest.RestClient;
import org.infinispan.client.rest.RestEntity;
import org.infinispan.client.rest.RestResponse;
import org.infinispan.client.rest.configuration.RestClientConfigurationBuilder;
import org.infinispan.commons.dataconversion.MediaType;
import org.infinispan.commons.util.Util;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.query.dsl.Query;
Expand All @@ -48,7 +48,7 @@
public abstract class BaseJsonTest extends AbstractInfinispanTest {

RestServer restServer;
HttpClient restClient;
RestClient restClient;
private EmbeddedCacheManager cacheManager;
private RemoteCacheManager remoteCacheManager;
private RemoteCache<String, CryptoCurrency> remoteCache;
Expand All @@ -58,7 +58,7 @@ public abstract class BaseJsonTest extends AbstractInfinispanTest {

HotRodServer hotRodServer;

private String restEndpoint;
private RestCacheClient restCacheClient;

abstract ConfigurationBuilder getIndexCacheConfiguration();

Expand All @@ -77,12 +77,11 @@ protected void setup() throws Exception {

restServer = new RestServer();
restServer.start(builder.build(), cacheManager);
restClient = new HttpClient();

restClient = RestClient.forConfiguration(new RestClientConfigurationBuilder().addServer().host(restServer.getHost()).port(restServer.getPort()).build());
restCacheClient = restClient.cache(CACHE_NAME);
hotRodServer = startHotRodServer(cacheManager);
remoteCacheManager = createRemoteCacheManager();
remoteCache = remoteCacheManager.getCache(CACHE_NAME);
restEndpoint = String.format("http://localhost:%s/rest/v2/caches/%s", restServer.getPort(), CACHE_NAME);
}

protected String getEntityName() {
Expand All @@ -93,24 +92,20 @@ protected String getJsonType() {
return getEntityName();
}

private void writeCurrencyViaJson(String key, String description, int rank) throws IOException {
EntityEnclosingMethod put = new PutMethod(restEndpoint + "/" + key);
private void writeCurrencyViaJson(String key, String description, int rank) {
ObjectNode currency = MAPPER.createObjectNode();
currency.put(TYPE, getJsonType());
currency.put("description", description);
currency.put("rank", rank);
put.setRequestEntity(new StringRequestEntity(currency.toString(), "application/json", "UTF-8"));
RestEntity value = RestEntity.create(MediaType.APPLICATION_JSON, currency.toString());
RestResponse response = join(restCacheClient.put(key, value));

restClient.executeMethod(put);
System.out.println(put.getResponseBodyAsString());
assertEquals(put.getStatusCode(), HttpStatus.SC_NO_CONTENT);
assertEquals(response.getStatus(), 204);
}

private CryptoCurrency readCurrencyViaJson(String key) throws IOException {
HttpMethod get = new GetMethod(restEndpoint + "/" + key);
get.setRequestHeader("Accept", "application/json");
restClient.executeMethod(get);
String json = get.getResponseBodyAsString();
RestResponse response = join(restCacheClient.get(key, MediaType.APPLICATION_JSON_TYPE));
String json = response.getBody();
JsonNode jsonNode = new ObjectMapper().readTree(json);
JsonNode description = jsonNode.get("description");
JsonNode rank = jsonNode.get("rank");
Expand Down Expand Up @@ -169,6 +164,7 @@ public void testHotRodInteroperability() throws Exception {

@AfterClass
protected void teardown() {
Util.close(restClient);
remoteCacheManager.stop();
if (restServer != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package org.infinispan.it.endpoints;

import static org.infinispan.util.concurrent.CompletionStages.join;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.infinispan.Cache;
import org.infinispan.client.rest.RestCacheClient;
import org.infinispan.client.rest.RestEntity;
import org.infinispan.commons.dataconversion.MediaType;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.test.AbstractInfinispanTest;
Expand Down Expand Up @@ -39,10 +34,9 @@ protected void teardown() {
EndpointsCacheFactory.killCacheFactories(cacheFactory);
}

public void testLoadingAndStoringEventsRest() throws IOException {
public void testLoadingAndStoringEventsRest() {
Cache<String, String> embedded = cacheFactory.getEmbeddedCache();
HttpClient remote = cacheFactory.getRestClient();
String restUrl = cacheFactory.getRestUrl();
RestCacheClient remote = cacheFactory.getRestCacheClient();

TestCacheListener l = new TestCacheListener();
embedded.addListener(l);
Expand All @@ -52,10 +46,8 @@ public void testLoadingAndStoringEventsRest() throws IOException {
assertTrue(l.modified.isEmpty());
assertTrue(l.visited.isEmpty());

EntityEnclosingMethod put = new PutMethod(restUrl + "/k");
put.setRequestEntity(new ByteArrayRequestEntity(
"v".getBytes(), MediaType.APPLICATION_OCTET_STREAM_TYPE));
remote.executeMethod(put);
RestEntity v = RestEntity.create(MediaType.APPLICATION_OCTET_STREAM, "v".getBytes());
join(remote.put("k", v));

assertEquals(1, l.createdCounter);
assertEquals("v".getBytes(), (byte[]) l.created.get("k"));
Expand All @@ -64,31 +56,25 @@ public void testLoadingAndStoringEventsRest() throws IOException {
assertTrue(l.visited.isEmpty());


EntityEnclosingMethod put2 = new PutMethod(restUrl + "/key");
put2.setRequestEntity(new ByteArrayRequestEntity(
"value".getBytes(), MediaType.APPLICATION_OCTET_STREAM_TYPE));
remote.executeMethod(put2);
RestEntity value = RestEntity.create(MediaType.APPLICATION_OCTET_STREAM, "value".getBytes());
join(remote.put("key", value));

assertEquals(2, l.createdCounter);
assertTrue(l.removed.isEmpty());
assertEquals(0, l.modifiedCounter);
assertTrue(l.visited.isEmpty());

EntityEnclosingMethod put3 = new PutMethod(restUrl + "/key");
put3.setRequestEntity(new ByteArrayRequestEntity(
"modifiedValue".getBytes(), MediaType.APPLICATION_OCTET_STREAM_TYPE));
remote.executeMethod(put3);
RestEntity modifiedValue = RestEntity.create(MediaType.APPLICATION_OCTET_STREAM, "modifiedValue".getBytes());
join(remote.put("key", modifiedValue));

assertEquals(2, l.createdCounter);
assertTrue(l.removed.isEmpty());
assertEquals(1, l.modifiedCounter);
assertEquals("modifiedValue".getBytes(), (byte[]) l.modified.get("key"));
assertTrue(l.visited.isEmpty());

EntityEnclosingMethod post = new PutMethod(restUrl + "/k");
post.setRequestEntity(new ByteArrayRequestEntity(
"replacedValue".getBytes(), MediaType.APPLICATION_OCTET_STREAM_TYPE));
remote.executeMethod(post);
RestEntity replacedValue = RestEntity.create(MediaType.APPLICATION_OCTET_STREAM, "replacedValue".getBytes());
join(remote.put("k", replacedValue));

assertEquals(2, l.createdCounter);
assertTrue(l.removed.isEmpty());
Expand All @@ -99,8 +85,7 @@ public void testLoadingAndStoringEventsRest() throws IOException {
//resetting so don't have to type "== 2" etc. all over again
l.reset();

DeleteMethod delete = new DeleteMethod(restUrl + "/key");
remote.executeMethod(delete);
join(remote.remove("key"));

assertTrue(l.created.isEmpty());
assertEquals(1, l.removedCounter);
Expand All @@ -109,8 +94,7 @@ public void testLoadingAndStoringEventsRest() throws IOException {

l.reset();

GetMethod get = new GetMethod(restUrl + "/k");
remote.executeMethod(get);
join(remote.get("k"));

assertTrue(l.created.isEmpty());
assertTrue(l.removed.isEmpty());
Expand Down

0 comments on commit a29917f

Please sign in to comment.