Skip to content

Commit

Permalink
test: refactor http tests and migrated to a newer API
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Feb 8, 2024
1 parent 2396719 commit 871ca16
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 262 deletions.
9 changes: 5 additions & 4 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@
<version>1.77</version>
</dependency>


<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public void createDatabase() throws Exception {
.setUserName("root")
.setUserPassword("root")
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
200);

onAfterDatabaseCreated();
Expand All @@ -43,8 +42,7 @@ public void dropDatabase() throws Exception {
.setUserName("root")
.setUserPassword("root")
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
204);
super.stopServer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,32 @@
import com.orientechnologies.orient.server.OServer;
import java.io.File;
import java.io.IOException;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;

/**
* Base test class for HTTP protocol.
*
* @author Luca Garulli (l.garulli--(at)--orientdb.com) (l.garulli--at-orientdb.com)
*/
public abstract class BaseHttpTest {

protected String serverDirectory;

private static OServer server;
Expand All @@ -51,9 +46,11 @@ public abstract class BaseHttpTest {
private String databaseName;
private Boolean keepAlive = null;

private HttpRequestBase request;
private ClassicHttpRequest request;
private AbstractHttpEntity payload;
private HttpResponse response;
private ClassicHttpResponse response;

private CloseableHttpClient client;
private int retry = 1;

public enum CONTENT {
Expand All @@ -65,8 +62,7 @@ public BaseHttpTest payload(final String content, final CONTENT contentType) {
payload =
new StringEntity(
content,
ContentType.create(
contentType == CONTENT.JSON ? "application/json" : "plain/text", Consts.UTF_8));
ContentType.create(contentType == CONTENT.JSON ? "application/json" : "plain/text"));
return this;
}

Expand All @@ -82,6 +78,10 @@ protected void startServer() throws Exception {
}

protected void stopServer() throws Exception {
if (client != null) {
client.close();
}

if (server != null) {
server.shutdown();
server = null;
Expand All @@ -100,35 +100,31 @@ protected boolean isInDevelopmentMode() {
}

protected BaseHttpTest exec() throws IOException {
final HttpHost targetHost = new HttpHost(getHost(), getPort(), getProtocol());
final HttpHost targetHost = new HttpHost(getProtocol(), getHost(), getPort());

CredentialsProvider credsProvider = new BasicCredentialsProvider();
var credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(targetHost),
new UsernamePasswordCredentials(getUserName(), getUserPassword()));

// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
new UsernamePasswordCredentials(getUserName(), getUserPassword().toCharArray()));

// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
final BasicCredentialsProvider provider = new BasicCredentialsProvider();
AuthScope authScope = new AuthScope(targetHost);
provider.setCredentials(
authScope, new UsernamePasswordCredentials(getUserName(), getUserPassword().toCharArray()));

if (keepAlive != null) request.addHeader("Connection", keepAlive ? "Keep-Alive" : "Close");

if (payload != null && request instanceof HttpEntityEnclosingRequestBase)
((HttpEntityEnclosingRequestBase) request).setEntity(payload);
if (keepAlive != null) {
request.addHeader("Connection", keepAlive ? "Keep-Alive" : "Close");
}

final CloseableHttpClient httpClient = HttpClients.createDefault();
if (payload != null && request instanceof HttpUriRequestBase httpUriRequestBase) {
httpUriRequestBase.setEntity(payload);
}

// DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retry, false);
client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
// DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retry,
// false);
// context.setAttribute(HttpMethodParams.RETRY_HANDLER, retryhandler);

response = httpClient.execute(targetHost, request, context);
response = client.execute(request);

return this;
}
Expand Down Expand Up @@ -171,8 +167,10 @@ protected BaseHttpTest patch(final String url) throws IOException {
return this;
}

protected HttpResponse getResponse() throws IOException {
if (response == null) exec();
protected ClassicHttpResponse getResponse() throws IOException {
if (response == null) {
exec();
}
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.net.URLEncoder;
import org.junit.Assert;

/** @author Luca Garulli (l.garulli--(at)--orientdb.com) (l.garulli--at-orientdb.com) */
/**
* @author Luca Garulli (l.garulli--(at)--orientdb.com) (l.garulli--at-orientdb.com)
*/
public class HttpAuthenticationTest extends BaseHttpDatabaseTest {
public void testChangeOfUserOnSameConnectionIsAllowed() throws IOException {
Assert.assertEquals(
Expand All @@ -16,8 +18,7 @@ public void testChangeOfUserOnSameConnectionIsAllowed() throws IOException {
.setUserName("root")
.setUserPassword("root")
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
200);

Assert.assertEquals(
Expand All @@ -29,8 +30,7 @@ public void testChangeOfUserOnSameConnectionIsAllowed() throws IOException {
.setUserName("admin")
.setUserPassword("admin")
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
200);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.junit.Assert;
import org.junit.Before;

Expand All @@ -24,13 +23,12 @@ public void beforeMethod() {
getServer().getPlugin("script-interpreter").startup();
}

public void batchUpdate() throws IOException {
public void batchUpdate() throws Exception {
Assert.assertEquals(
post("command/" + getDatabaseName() + "/sql/")
.payload("create class User", CONTENT.TEXT)
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
200);

Assert.assertEquals(
Expand All @@ -39,8 +37,7 @@ public void batchUpdate() throws IOException {
.setUserName("admin")
.setUserPassword("admin")
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
200);

String response = EntityUtils.toString(getResponse().getEntity());
Expand Down Expand Up @@ -71,8 +68,7 @@ public void batchUpdate() throws IOException {
+ "}",
CONTENT.JSON)
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
200);

// TEST DOUBLE UPDATE
Expand All @@ -96,8 +92,7 @@ public void batchUpdate() throws IOException {
+ "}",
CONTENT.JSON)
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
200);

// TEST WRONG VERSION ON UPDATE
Expand All @@ -121,8 +116,7 @@ public void batchUpdate() throws IOException {
+ "}",
CONTENT.JSON)
.getResponse()
.getStatusLine()
.getStatusCode(),
.getCode(),
409);

batchWithEmpty();
Expand All @@ -139,10 +133,9 @@ private void batchWithEmpty() throws IOException {
+ "return [$a, $b]\""
+ "}]\n"
+ "}";
HttpResponse response =
post("batch/" + getDatabaseName()).payload(json, CONTENT.TEXT).getResponse();
var response = post("batch/" + getDatabaseName()).payload(json, CONTENT.TEXT).getResponse();

Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
Assert.assertEquals(response.getCode(), 200);
InputStream stream = response.getEntity().getContent();
String string = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,27 @@
public class HttpClassTest extends BaseHttpDatabaseTest {
@Test
public void testExistentClass() throws Exception {
Assert.assertEquals(
get("class/" + getDatabaseName() + "/OUser").getResponse().getStatusLine().getStatusCode(),
200);
Assert.assertEquals(get("class/" + getDatabaseName() + "/OUser").getResponse().getCode(), 200);
}

@Test
public void testNonExistentClass() throws Exception {
Assert.assertEquals(
get("class/" + getDatabaseName() + "/NonExistentCLass")
.getResponse()
.getStatusLine()
.getStatusCode(),
404);
get("class/" + getDatabaseName() + "/NonExistentCLass").getResponse().getCode(), 404);
}

@Test
public void testCreateClass() throws Exception {
Assert.assertEquals(
post("class/" + getDatabaseName() + "/NewClass")
.getResponse()
.getStatusLine()
.getStatusCode(),
201);
post("class/" + getDatabaseName() + "/NewClass").getResponse().getCode(), 201);
}

@Test
public void testDropClass() throws Exception {
Assert.assertEquals(
post("class/" + getDatabaseName() + "/NewClassToDrop")
.getResponse()
.getStatusLine()
.getStatusCode(),
201);
post("class/" + getDatabaseName() + "/NewClassToDrop").getResponse().getCode(), 201);
Assert.assertEquals(
delete("class/" + getDatabaseName() + "/NewClassToDrop")
.getResponse()
.getStatusLine()
.getStatusCode(),
204);
delete("class/" + getDatabaseName() + "/NewClassToDrop").getResponse().getCode(), 204);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,13 @@ public class HttpClusterTest extends BaseHttpDatabaseTest {
@Test
public void testExistentClass() throws Exception {
Assert.assertEquals(
get("cluster/" + getDatabaseName() + "/OUser")
.getResponse()
.getStatusLine()
.getStatusCode(),
200);
get("cluster/" + getDatabaseName() + "/OUser").getResponse().getCode(), 200);
}

@Test
public void testNonExistentClass() throws Exception {
Assert.assertEquals(
get("cluster/" + getDatabaseName() + "/NonExistentCLass")
.getResponse()
.getStatusLine()
.getStatusCode(),
404);
get("cluster/" + getDatabaseName() + "/NonExistentCLass").getResponse().getCode(), 404);
}

@Override
Expand Down
Loading

0 comments on commit 871ca16

Please sign in to comment.