Skip to content

Commit

Permalink
Merge pull request #197 from Olog/integration-test-http-client
Browse files Browse the repository at this point in the history
Integration test http client
  • Loading branch information
shroffk authored Sep 24, 2024
2 parents 1588bf2 + e96b849 commit f5022c4
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 872 deletions.
471 changes: 212 additions & 259 deletions src/test/java/org/phoebus/olog/docker/ITUtil.java

Large diffs are not rendered by default.

75 changes: 21 additions & 54 deletions src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.phoebus.olog.docker.ITUtil.AuthorizationChoice;
Expand All @@ -31,7 +30,6 @@
import org.phoebus.olog.entity.Logbook;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Utility class to help (Docker) integration tests for Olog and Elasticsearch with focus on support test of behavior for logbook endpoints.
Expand All @@ -42,10 +40,8 @@
*/
public class ITUtilLogbooks {

static final ObjectMapper mapper = new ObjectMapper();

static final Logbook[] LOGBOOKS_NULL = null;
static final Logbook LOGBOOK_NULL = null;
private static final Logbook[] LOGBOOKS_NULL = null;
private static final Logbook LOGBOOK_NULL = null;

/**
* This class is not to be instantiated.
Expand All @@ -64,7 +60,7 @@ private ITUtilLogbooks() {
*/
static String object2Json(Logbook value) {
try {
return mapper.writeValueAsString(value);
return ITUtil.MAPPER.writeValueAsString(value);
} catch (JsonProcessingException e) {
fail();
}
Expand All @@ -78,7 +74,7 @@ static String object2Json(Logbook value) {
*/
static String object2Json(Logbook[] value) {
try {
return mapper.writeValueAsString(value);
return ITUtil.MAPPER.writeValueAsString(value);
} catch (JsonProcessingException e) {
fail();
}
Expand Down Expand Up @@ -107,27 +103,21 @@ public static Logbook assertRetrieveLogbook(String path, Logbook expected) {
* @param expected expected response logbook
*/
public static Logbook assertRetrieveLogbook(String path, int expectedResponseCode, Logbook expected) {
Logbook actual = null;
try {
String[] response = null;
Logbook actual = null;
String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS + path);

response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS + path);
ITUtil.assertResponseLength2Code(response, expectedResponseCode);
if (HttpURLConnection.HTTP_OK == expectedResponseCode) {
actual = mapper.readValue(response[1], Logbook.class);
actual = ITUtil.MAPPER.readValue(response[1], Logbook.class);
}

if (expected != null) {
assertEquals(expected, actual);
}

return actual;
} catch (IOException e) {
fail();
} catch (Exception e) {
fail();
}
return null;
return actual;
}

// ----------------------------------------------------------------------------------------------------
Expand All @@ -148,16 +138,14 @@ public static Logbook[] assertListLogbooks(int expectedEqual, Logbook... expecte
* @return number of logbooks
*/
public static Logbook[] assertListLogbooks(int expectedResponseCode, int expectedGreaterThanOrEqual, int expectedLessThanOrEqual, Logbook... expected) {
Logbook[] actual = null;
try {
String[] response = null;
Logbook[] actual = null;
String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS);

response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS);
ITUtil.assertResponseLength2Code(response, expectedResponseCode);
if (HttpURLConnection.HTTP_OK == expectedResponseCode) {
actual = mapper.readValue(response[1], Logbook[].class);
actual = ITUtil.MAPPER.readValue(response[1], Logbook[].class);
}

// expected number of items in list
// (if non-negative number)
// expectedGreaterThanOrEqual <= nbr of items <= expectedLessThanOrEqual
Expand All @@ -167,19 +155,13 @@ public static Logbook[] assertListLogbooks(int expectedResponseCode, int expecte
if (expectedLessThanOrEqual >= 0) {
assertTrue(actual.length <= expectedLessThanOrEqual);
}

// expected content
if (expected != null && expected.length > 0) {
ITUtil.assertEqualsLogbooks(actual, expected);
}

return actual;
} catch (IOException e) {
fail();
} catch (Exception e) {
fail();
}
return null;
return actual;
}

// ----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -218,27 +200,21 @@ public static Logbook assertCreateLogbook(AuthorizationChoice authorizationChoic
* @param expected expected response logbook
*/
public static Logbook assertCreateLogbook(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Logbook expected) {
Logbook actual = null;
try {
String[] response = null;
Logbook actual = null;
String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.PUT, authorizationChoice, EndpointChoice.LOGBOOKS, path, json));

response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.PUT, authorizationChoice, EndpointChoice.LOGBOOKS, path, json));
ITUtil.assertResponseLength2Code(response, expectedResponseCode);
if (HttpURLConnection.HTTP_OK == expectedResponseCode) {
actual = mapper.readValue(response[1], Logbook.class);
actual = ITUtil.MAPPER.readValue(response[1], Logbook.class);
}

if (expected != null) {
assertEquals(expected, actual);
}

return actual;
} catch (IOException e) {
fail();
} catch (Exception e) {
fail();
}
return null;
return actual;
}

// ----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -271,27 +247,21 @@ public static Logbook[] assertCreateLogbooks(AuthorizationChoice authorizationCh
* @param expected expected response logbooks
*/
public static Logbook[] assertCreateLogbooks(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Logbook[] expected) {
Logbook[] actual = null;
try {
String[] response = null;
Logbook[] actual = null;
String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.PUT, authorizationChoice, EndpointChoice.LOGBOOKS, path, json));

response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.PUT, authorizationChoice, EndpointChoice.LOGBOOKS, path, json));
ITUtil.assertResponseLength2Code(response, expectedResponseCode);
if (HttpURLConnection.HTTP_OK == expectedResponseCode) {
actual = mapper.readValue(response[1], Logbook[].class);
actual = ITUtil.MAPPER.readValue(response[1], Logbook[].class);
}

if (expected != null) {
ITUtil.assertEqualsLogbooks(expected, actual);
}

return actual;
} catch (IOException e) {
fail();
} catch (Exception e) {
fail();
}
return null;
return actual;
}

// ----------------------------------------------------------------------------------------------------
Expand All @@ -311,12 +281,9 @@ public static void assertRemoveLogbook(String path) {
*/
public static void assertRemoveLogbook(AuthorizationChoice authorizationChoice, String path, int expectedResponseCode) {
try {
String[] response = null;
String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.DELETE, authorizationChoice, EndpointChoice.LOGBOOKS, path, null));

response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.DELETE, authorizationChoice, EndpointChoice.LOGBOOKS, path, null));
ITUtil.assertResponseLength2Code(response, expectedResponseCode);
} catch (IOException e) {
fail();
} catch (Exception e) {
fail();
}
Expand Down
Loading

0 comments on commit f5022c4

Please sign in to comment.