From b5a94191722c47e985ce94d4f5d7be4363fe9269 Mon Sep 17 00:00:00 2001 From: Lars Johansson Date: Tue, 9 Jul 2024 15:55:21 +0200 Subject: [PATCH 1/5] Update to use Java HttpClient instead of HttpURLConnection for integration tests with Docker --- .../java/org/phoebus/olog/docker/ITUtil.java | 79 +++++++++++-------- .../phoebus/olog/docker/ITUtilLogbooks.java | 4 +- .../org/phoebus/olog/docker/ITUtilLogs.java | 6 +- .../phoebus/olog/docker/ITUtilProperties.java | 4 +- .../org/phoebus/olog/docker/ITUtilTags.java | 4 +- .../java/org/phoebus/olog/docker/OlogIT.java | 36 ++++----- .../phoebus/olog/docker/OlogLogbooksIT.java | 5 +- .../org/phoebus/olog/docker/OlogLogsIT.java | 6 +- .../phoebus/olog/docker/OlogLogsQueryIT.java | 28 +------ .../phoebus/olog/docker/OlogPropertiesIT.java | 5 +- .../org/phoebus/olog/docker/OlogTagsIT.java | 5 +- 11 files changed, 78 insertions(+), 104 deletions(-) diff --git a/src/test/java/org/phoebus/olog/docker/ITUtil.java b/src/test/java/org/phoebus/olog/docker/ITUtil.java index 91f5bc4..7f8603d 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtil.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtil.java @@ -18,7 +18,6 @@ package org.phoebus.olog.docker; -import org.junit.jupiter.api.AfterAll; import org.phoebus.olog.entity.Log; import org.phoebus.olog.entity.Logbook; import org.phoebus.olog.entity.Property; @@ -35,7 +34,12 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; -import java.net.URL; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -96,6 +100,8 @@ public class ITUtil { // integration test - docker + private static final HttpClient CLIENT = HttpClient.newHttpClient(); + public static final String INTEGRATIONTEST_DOCKER_COMPOSE = "docker-compose-integrationtest.yml"; public static final String INTEGRATIONTEST_LOG_MESSAGE = ".*Started Application.*"; @@ -160,60 +166,65 @@ public static void extractJacocoReport(ComposeContainer environment, String dest * Refresh Elastic indices and return response code and string. * * @return response code and string + * @throws InterruptedException * @throws IOException + * @throws URISyntaxException */ - static String[] refreshElasticIndices() throws IOException { - return doGetJson(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); + static String[] refreshElasticIndices() throws URISyntaxException, IOException, InterruptedException { + return sendRequest(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); } /** * Refresh Elastic indices and assert response is of length 2 and has response code HttpURLConnection.HTTP_OK. - * + * @throws InterruptedException * @throws IOException + * @throws URISyntaxException + * */ - static void assertRefreshElasticIndices() throws IOException { - String[] response = doGetJson(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); + static void assertRefreshElasticIndices() throws URISyntaxException, IOException, InterruptedException { + String[] response = sendRequest(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); + ITUtil.assertResponseLength2CodeOK(response); } /** - * Do GET request with given string as URL and return response code. - * - * @param spec string to parse as URL - * @return response code + * Send GET request with given string as URI and return response status code. * + * @param str string to parse as URI + * @return response status code + * @throws URISyntaxException If request is created with non-legal URI characters + * @throws InterruptedException * @throws IOException */ - static int doGet(String spec) throws IOException { - URL url = new URL(spec); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - return con.getResponseCode(); + static int sendRequestStatusCode(String str) throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(str)) + .GET() + .build(); + + HttpResponse response = CLIENT.send(request, BodyHandlers.ofString()); + + return response.statusCode(); } /** - * Do GET request with given string as URL and return response with string array with response code and response string. - * - * @param spec string to parse as URL - * @return string array with response code and response string + * Send GET request with given string as URI and return string array with response status code and response body. * + * @param str string to parse as URI + * @return string array with response status code and response body + * @throws URISyntaxException If request is created with non-legal URI characters + * @throws InterruptedException * @throws IOException */ - static String[] doGetJson(String spec) throws IOException { - URL url = new URL(spec); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - int responseCode = con.getResponseCode(); - - String line; - StringBuilder sb = new StringBuilder(); - try (BufferedReader br = responseCode == HttpURLConnection.HTTP_OK - ? new BufferedReader(new InputStreamReader(con.getInputStream())) - : new BufferedReader(new InputStreamReader(con.getErrorStream()))) { - while((line = br.readLine()) != null) { - sb.append(line); - } - } + static String[] sendRequest(String str) throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(str)) + .GET() + .build(); + + HttpResponse response = CLIENT.send(request, BodyHandlers.ofString()); - return new String[] {String.valueOf(responseCode), sb.toString().trim()}; + return new String[] {String.valueOf(response.statusCode()), response.body()}; } /** diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java b/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java index c9d2d26..d3b9af4 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java @@ -111,7 +111,7 @@ public static Logbook assertRetrieveLogbook(String path, int expectedResponseCod String[] response = null; Logbook actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS + path); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Logbook.class); @@ -152,7 +152,7 @@ public static Logbook[] assertListLogbooks(int expectedResponseCode, int expecte String[] response = null; Logbook[] actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Logbook[].class); diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java b/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java index ff20e6d..1415cc9 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java @@ -107,7 +107,7 @@ public static Log assertRetrieveLog(String path, int expectedResponseCode, Log e String[] response = null; Log actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGS + path); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Log.class); @@ -155,7 +155,7 @@ public static Log[] assertListLogs(String queryString, int expectedResponseCode, String[] response = null; Log[] actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGS + queryString); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + queryString); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Log[].class); @@ -214,7 +214,7 @@ public static SearchResult assertSearchLogs(String queryString, int expectedResp String[] response = null; SearchResult actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGS + "/search" + queryString); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + "/search" + queryString); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], SearchResult.class); diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java b/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java index 0e497ff..e40bf8d 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java @@ -111,7 +111,7 @@ public static Property assertRetrieveProperty(String path, int expectedResponseC String[] response = null; Property actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + path); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Property.class); @@ -159,7 +159,7 @@ public static Property[] assertListProperties(String queryString, int expectedRe String[] response = null; Property[] actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + queryString); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + queryString); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Property[].class); diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilTags.java b/src/test/java/org/phoebus/olog/docker/ITUtilTags.java index 9bbfe39..d0f16f4 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilTags.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilTags.java @@ -111,7 +111,7 @@ public static Tag assertRetrieveTag(String path, int expectedResponseCode, Tag e String[] response = null; Tag actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_TAGS + path); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_TAGS + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Tag.class); @@ -152,7 +152,7 @@ public static Tag[] assertListTags(int expectedResponseCode, int expectedGreater String[] response = null; Tag[] actual = null; - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_TAGS); + response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_TAGS); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Tag[].class); diff --git a/src/test/java/org/phoebus/olog/docker/OlogIT.java b/src/test/java/org/phoebus/olog/docker/OlogIT.java index 92c7177..1c72d52 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogIT.java @@ -24,7 +24,6 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.IOException; import java.net.HttpURLConnection; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -71,77 +70,70 @@ public static void extractJacocoReport() { @Test void ologUp() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @Test void ologUpTags() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG + "/tags"; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG + "/tags"); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @Test void ologUpLogbooks() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG + "/logbooks"; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG + "/logbooks"); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @Test void ologUpProperties() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG + "/properties"; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG + "/properties"); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @Test void ologUpLogs() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG + "/logs"; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG + "/logs"); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @Test void ologUpConfiguration() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG + "/configuration"; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG + "/configuration"); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @Test void ologUpAttachment() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG + "/attachment"; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG + "/attachment"); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } diff --git a/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java b/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java index c9e3524..8d60bf2 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java @@ -150,11 +150,10 @@ public static void extractJacocoReport() { @Test void ologUp() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } diff --git a/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java b/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java index 46eedbc..f075efb 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java @@ -29,7 +29,6 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.IOException; import java.net.HttpURLConnection; import java.util.Arrays; import java.util.Set; @@ -97,11 +96,10 @@ public static void extractJacocoReport() { @Test void ologUp() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } diff --git a/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java b/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java index 846baf9..7bef232 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java @@ -25,7 +25,6 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.IOException; import java.net.HttpURLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -94,11 +93,10 @@ public static void extractJacocoReport() { @Test void ologUp() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @@ -184,8 +182,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertListLogs("?desc=complete", 0); ITUtilLogs.assertListLogs("?desc=Complete", 0); ITUtilLogs.assertListLogs("?desc=check&desc=complete", 0); - ITUtilLogs.assertListLogs("?desc=check complete", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertListLogs("?desc='check complete'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertListLogs("?desc=" + URLEncoder.encode("check complete", StandardCharsets.UTF_8), 0); ITUtilLogs.assertListLogs("?desc='" + URLEncoder.encode("check complete", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertListLogs("?desc=" + URLEncoder.encode("CHECK COMPLETE", StandardCharsets.UTF_8), 0); @@ -202,7 +198,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertListLogs("?desc=after&desc=Maintenance", 3); ITUtilLogs.assertListLogs("?desc=" + URLEncoder.encode("after maintenance", StandardCharsets.UTF_8), 3); ITUtilLogs.assertListLogs("?desc='" + URLEncoder.encode("after maintenance", StandardCharsets.UTF_8) + "'", 0); - ITUtilLogs.assertListLogs("?desc=\"" + URLEncoder.encode("after maintenance", StandardCharsets.UTF_8) + "\"", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertListLogs("?desc=" + URLEncoder.encode("\"after maintenance\"", StandardCharsets.UTF_8), 3); ITUtilLogs.assertListLogs("?desc=" + URLEncoder.encode("\"after mainTENance\"", StandardCharsets.UTF_8), 3); ITUtilLogs.assertListLogs("?desc=" + URLEncoder.encode("\"after mainTENance", StandardCharsets.UTF_8), HttpURLConnection.HTTP_INTERNAL_ERROR, -1, -1); @@ -217,8 +212,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertSearchLogs("?desc=complete", 0); ITUtilLogs.assertSearchLogs("?desc=Complete", 0); ITUtilLogs.assertSearchLogs("?desc=check&desc=complete", 0); - ITUtilLogs.assertSearchLogs("?desc=check complete", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertSearchLogs("?desc='check complete'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertSearchLogs("?desc=" + URLEncoder.encode("check complete", StandardCharsets.UTF_8), 0); ITUtilLogs.assertSearchLogs("?desc='" + URLEncoder.encode("check complete", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertSearchLogs("?desc=" + URLEncoder.encode("CHECK COMPLETE", StandardCharsets.UTF_8), 0); @@ -235,7 +228,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertSearchLogs("?desc=after&desc=Maintenance", 3); ITUtilLogs.assertSearchLogs("?desc=" + URLEncoder.encode("after maintenance", StandardCharsets.UTF_8), 3); ITUtilLogs.assertSearchLogs("?desc='" + URLEncoder.encode("after maintenance", StandardCharsets.UTF_8) + "'", 0); - ITUtilLogs.assertSearchLogs("?desc=\"" + URLEncoder.encode("after maintenance", StandardCharsets.UTF_8) + "\"", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertSearchLogs("?desc=" + URLEncoder.encode("\"after maintenance\"", StandardCharsets.UTF_8), 3); ITUtilLogs.assertSearchLogs("?desc=" + URLEncoder.encode("\"after mainTENance\"", StandardCharsets.UTF_8), 3); ITUtilLogs.assertSearchLogs("?desc=" + URLEncoder.encode("\"after mainTENance", StandardCharsets.UTF_8), HttpURLConnection.HTTP_INTERNAL_ERROR, -1, -1); @@ -253,8 +245,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertListLogs("?title=update", 37); ITUtilLogs.assertListLogs("?title=Update", 37); ITUtilLogs.assertListLogs("?title=shift&title=update", 37); - ITUtilLogs.assertListLogs("?title=shift update", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertListLogs("?title='shift update'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertListLogs("?title=" + URLEncoder.encode("\"Shift Update\"", StandardCharsets.UTF_8), 37); ITUtilLogs.assertListLogs("?title=" + URLEncoder.encode("\"shiFT updATE\"", StandardCharsets.UTF_8), 37); ITUtilLogs.assertListLogs("?title=" + URLEncoder.encode("shiFT updATE\"", StandardCharsets.UTF_8), HttpURLConnection.HTTP_INTERNAL_ERROR, -1, -1); @@ -275,8 +265,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertSearchLogs("?title=update", 37); ITUtilLogs.assertSearchLogs("?title=Update", 37); ITUtilLogs.assertSearchLogs("?title=shift&title=update", 37); - ITUtilLogs.assertSearchLogs("?title=shift update", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertSearchLogs("?title='shift update'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertSearchLogs("?title=" + URLEncoder.encode("\"Shift Update\"", StandardCharsets.UTF_8), 37); ITUtilLogs.assertSearchLogs("?title=" + URLEncoder.encode("\"shiFT updATE\"", StandardCharsets.UTF_8), 37); ITUtilLogs.assertSearchLogs("?title=" + URLEncoder.encode("shiFT updATE\"", StandardCharsets.UTF_8), HttpURLConnection.HTTP_INTERNAL_ERROR, -1, -1); @@ -337,8 +325,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertListLogs("?phrase=complete", 0); ITUtilLogs.assertListLogs("?phrase=Complete", 0); ITUtilLogs.assertListLogs("?phrase=check&phrase=complete", 0); - ITUtilLogs.assertListLogs("?phrase=check complete", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertListLogs("?phrase='check complete'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertListLogs("?phrase=" + URLEncoder.encode("check complete", StandardCharsets.UTF_8), 0); ITUtilLogs.assertListLogs("?phrase='" + URLEncoder.encode("check complete", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertListLogs("?phrase=" + URLEncoder.encode("CHECK COMPLETE", StandardCharsets.UTF_8), 0); @@ -353,8 +339,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertSearchLogs("?phrase=complete", 0); ITUtilLogs.assertSearchLogs("?phrase=Complete", 0); ITUtilLogs.assertSearchLogs("?phrase=check&phrase=complete", 0); - ITUtilLogs.assertSearchLogs("?phrase=check complete", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertSearchLogs("?phrase='check complete'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertSearchLogs("?phrase=" + URLEncoder.encode("check complete", StandardCharsets.UTF_8), 0); ITUtilLogs.assertSearchLogs("?phrase='" + URLEncoder.encode("check complete", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertSearchLogs("?phrase=" + URLEncoder.encode("CHECK COMPLETE", StandardCharsets.UTF_8), 0); @@ -546,8 +530,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertListLogs("?properties=A", 20); ITUtilLogs.assertListLogs("?properties=B", 0); ITUtilLogs.assertListLogs("?properties=C", 0); - ITUtilLogs.assertListLogs("?properties=Shift Info C", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertListLogs("?properties='Shift Info C'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertListLogs("?properties=" + URLEncoder.encode("Shift Info C", StandardCharsets.UTF_8), 20); ITUtilLogs.assertListLogs("?properties='" + URLEncoder.encode("Shift Info C", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertListLogs("?properties=.operator", 60); @@ -580,8 +562,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertSearchLogs("?properties=A", 20); ITUtilLogs.assertSearchLogs("?properties=B", 0); ITUtilLogs.assertSearchLogs("?properties=C", 0); - ITUtilLogs.assertSearchLogs("?properties=Shift Info C", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertSearchLogs("?properties='Shift Info C'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertSearchLogs("?properties=" + URLEncoder.encode("Shift Info C", StandardCharsets.UTF_8), 20); ITUtilLogs.assertSearchLogs("?properties='" + URLEncoder.encode("Shift Info C", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertSearchLogs("?properties=.operator", 60); @@ -618,8 +598,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertListLogs("?level=update", 54); ITUtilLogs.assertListLogs("?level=Update", 54); ITUtilLogs.assertListLogs("?level=shift&level=update", 60); - ITUtilLogs.assertListLogs("?level=shift update", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertListLogs("?level='shift update'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertListLogs("?level=" + URLEncoder.encode("shift update", StandardCharsets.UTF_8), 60); ITUtilLogs.assertListLogs("?level='" + URLEncoder.encode("shift update", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertListLogs("?level=" + URLEncoder.encode("SHIFT UPDATE", StandardCharsets.UTF_8), 60); @@ -636,8 +614,6 @@ void handleLogsQueryByPattern() { ITUtilLogs.assertSearchLogs("?level=update", 54); ITUtilLogs.assertSearchLogs("?level=Update", 54); ITUtilLogs.assertSearchLogs("?level=shift&level=update", 60); - ITUtilLogs.assertSearchLogs("?level=shift update", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); - ITUtilLogs.assertSearchLogs("?level='shift update'", HttpURLConnection.HTTP_BAD_REQUEST, -1, -1); ITUtilLogs.assertSearchLogs("?level=" + URLEncoder.encode("shift update", StandardCharsets.UTF_8), 60); ITUtilLogs.assertSearchLogs("?level='" + URLEncoder.encode("shift update", StandardCharsets.UTF_8) + "'", 0); ITUtilLogs.assertSearchLogs("?level=" + URLEncoder.encode("SHIFT UPDATE", StandardCharsets.UTF_8), 60); diff --git a/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java b/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java index 0e6bc7b..ff949bc 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java @@ -224,11 +224,10 @@ public static void extractJacocoReport() { @Test void ologUp() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } diff --git a/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java b/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java index aabafc6..5f41d8e 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java @@ -148,11 +148,10 @@ public static void extractJacocoReport() { @Test void ologUp() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } From a0320a0263b71b447a4f29946bc72a06e18c42d8 Mon Sep 17 00:00:00 2001 From: Lars Johansson Date: Tue, 9 Jul 2024 16:38:52 +0200 Subject: [PATCH 2/5] Update to use Java HttpClient instead of curl command for integration tests with Docker --- .../java/org/phoebus/olog/docker/ITUtil.java | 164 ++++++------------ .../phoebus/olog/docker/ITUtilLogbooks.java | 56 ++---- .../org/phoebus/olog/docker/ITUtilLogs.java | 92 ++-------- .../phoebus/olog/docker/ITUtilProperties.java | 56 ++---- .../org/phoebus/olog/docker/ITUtilTags.java | 56 ++---- .../phoebus/olog/docker/OlogLogbooksIT.java | 11 -- .../phoebus/olog/docker/OlogPropertiesIT.java | 9 - .../org/phoebus/olog/docker/OlogTagsIT.java | 9 - 8 files changed, 110 insertions(+), 343 deletions(-) diff --git a/src/test/java/org/phoebus/olog/docker/ITUtil.java b/src/test/java/org/phoebus/olog/docker/ITUtil.java index 7f8603d..dbf017d 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtil.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtil.java @@ -22,6 +22,7 @@ import org.phoebus.olog.entity.Logbook; import org.phoebus.olog.entity.Property; import org.phoebus.olog.entity.Tag; +import org.springframework.http.HttpHeaders; import org.testcontainers.containers.ComposeContainer; import org.testcontainers.containers.ContainerState; import org.testcontainers.containers.wait.strategy.Wait; @@ -29,10 +30,8 @@ import com.github.dockerjava.api.DockerClient; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; @@ -40,9 +39,8 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; +import java.util.Base64; import java.util.Optional; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -59,11 +57,12 @@ public class ITUtil { static final String UTF_8 = "UTF-8"; - static final String AUTH_USER = "user:userPass"; - static final String AUTH_ADMIN = "admin:adminPass"; - static final String EMPTY_JSON = "[]"; - static final String HEADER_JSON = "'Content-Type: application/json'"; - static final String HTTP = "http://"; + static final String AUTH_USER = "user:userPass"; + static final String AUTH_ADMIN = "admin:adminPass"; + static final String EMPTY_JSON = "[]"; + static final String HEADER_JSON = "application/json"; + static final String HEADER_BASIC = "Basic "; + static final String HTTP = "http://"; static final String IP_PORT_OLOG = "127.0.0.1:8080/Olog"; static final String IP_PORT_ELASTICSEARCH = "127.0.0.1:9200"; @@ -76,27 +75,10 @@ public class ITUtil { static final String HTTP_IP_PORT_OLOG = HTTP + IP_PORT_OLOG; static final String HTTP_IP_PORT_ELASTICSEARCH = HTTP + IP_PORT_ELASTICSEARCH; - static final String HTTP_IP_PORT_OLOG_LOGBOOKS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + LOGBOOKS; - static final String HTTP_AUTH_USER_IP_PORT_OLOG_LOGBOOKS = ITUtil.HTTP + ITUtil.AUTH_USER + "@" + ITUtil.IP_PORT_OLOG + LOGBOOKS; - static final String HTTP_AUTH_ADMIN_IP_PORT_OLOG_LOGBOOKS = ITUtil.HTTP + ITUtil.AUTH_ADMIN + "@" + ITUtil.IP_PORT_OLOG + LOGBOOKS; - - static final String HTTP_IP_PORT_OLOG_LOGS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + LOGS; - static final String HTTP_AUTH_USER_IP_PORT_OLOG_LOGS = ITUtil.HTTP + ITUtil.AUTH_USER + "@" + ITUtil.IP_PORT_OLOG + LOGS; - static final String HTTP_AUTH_ADMIN_IP_PORT_OLOG_LOGS = ITUtil.HTTP + ITUtil.AUTH_ADMIN + "@" + ITUtil.IP_PORT_OLOG + LOGS; - - static final String HTTP_IP_PORT_OLOG_PROPERTIES = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + PROPERTIES; - static final String HTTP_AUTH_USER_IP_PORT_OLOG_PROPERTIES = ITUtil.HTTP + ITUtil.AUTH_USER + "@" + ITUtil.IP_PORT_OLOG + PROPERTIES; - static final String HTTP_AUTH_ADMIN_IP_PORT_OLOG_PROPERTIES = ITUtil.HTTP + ITUtil.AUTH_ADMIN + "@" + ITUtil.IP_PORT_OLOG + PROPERTIES; - - static final String HTTP_IP_PORT_OLOG_TAGS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + TAGS; - static final String HTTP_AUTH_USER_IP_PORT_OLOG_TAGS = ITUtil.HTTP + ITUtil.AUTH_USER + "@" + ITUtil.IP_PORT_OLOG + TAGS; - static final String HTTP_AUTH_ADMIN_IP_PORT_OLOG_TAGS = ITUtil.HTTP + ITUtil.AUTH_ADMIN + "@" + ITUtil.IP_PORT_OLOG + TAGS; - - private static final String BRACKET_BEGIN = "["; - private static final String BRACKET_END = "]"; - private static final String CURLY_BRACE_BEGIN = "{"; - private static final String CURLY_BRACE_END = "}"; - private static final String HTTP_REPLY = "HTTP"; + static final String HTTP_IP_PORT_OLOG_LOGBOOKS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + LOGBOOKS; + static final String HTTP_IP_PORT_OLOG_LOGS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + LOGS; + static final String HTTP_IP_PORT_OLOG_PROPERTIES = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + PROPERTIES; + static final String HTTP_IP_PORT_OLOG_TAGS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + TAGS; // integration test - docker @@ -228,57 +210,17 @@ static String[] sendRequest(String str) throws URISyntaxException, IOException, } /** - * Run a shell command and return response with string array with response code and response string. + * Send request and return response with string array with response code and response string. * - * @param command shell command + * @param request request * @return string array with response code and response string - * - * @throws IOException * @throws InterruptedException - * @throws Exception + * @throws IOException */ - static String[] runShellCommand(String command) throws IOException, InterruptedException, Exception { - // run shell command & return http response code if available - - final ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.command("bash", "-c", command); - - String responseCode = null; - String responseContent = null; - try { - final Process process = processBuilder.start(); - final BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); - final BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream())); - final boolean processFinished = process.waitFor(30, TimeUnit.SECONDS); - - String line = null; - while ((line = inputStream.readLine()) != null) { - if (line.startsWith(HTTP_REPLY)) { - // response code, e.g. "HTTP/1.1 200", "HTTP/1.1 401", "HTTP/1.1 500" - String[] s = line.trim().split(" "); - if (s != null && s.length == 2) { - responseCode = s[1]; - } - } else if ((line.startsWith(BRACKET_BEGIN) && line.endsWith(BRACKET_END)) - || (line.startsWith(CURLY_BRACE_BEGIN) && line.endsWith(CURLY_BRACE_END))) { - // response string, json - responseContent = line; - } - } + static String[] sendRequest(HttpRequest request) throws IOException, InterruptedException { + HttpResponse response = CLIENT.send(request, BodyHandlers.ofString()); - if (!processFinished) { - throw new Exception("Timed out waiting to execute command: " + command); - } - if (process.exitValue() != 0) { - throw new Exception( - String.format("Shell command finished with status %d error: %s", - process.exitValue(), - errorStream.lines().collect(Collectors.joining()))); - } - } catch (IOException | InterruptedException e) { - throw e; - } - return new String[] {responseCode, responseContent}; + return new String[] {String.valueOf(response.statusCode()), response.body()}; } // ---------------------------------------------------------------------------------------------------- @@ -293,83 +235,87 @@ static enum AuthorizationChoice {NONE, USER, ADMIN}; static enum EndpointChoice {LOGBOOKS, LOGS, PROPERTIES, TAGS}; /** - * Prepare curl command for test to run for contacting server. + * Utility method to build http request for test to run. * * @param methodChoice method choice * @param authorizationChoice authorization choice * @param endpointChoice endpoint choice * @param path particular path * @param json json data - * @return curl command to run + * @return http request to run + * @throws URISyntaxException If request is created with non-legal URI characters */ - static String curlMethodAuthEndpointPathJson(MethodChoice methodChoice, AuthorizationChoice authorizationChoice, EndpointChoice endpointChoice, String path, String json) { + static HttpRequest buildRequest(MethodChoice methodChoice, AuthorizationChoice authorizationChoice, EndpointChoice endpointChoice, String path, String json) throws URISyntaxException { String pathstr = !StringUtils.isEmpty(path) ? path : ""; - String data = !StringUtils.isEmpty(json) - ? " -d '" + json + "'" - : ""; + String str = + ITUtil.HTTP + + ITUtil.IP_PORT_OLOG + + ITUtil.buildUri(endpointChoice) + + pathstr; + + HttpRequest.Builder builder = HttpRequest.newBuilder() + .uri(new URI(str)) + .header(HttpHeaders.CONTENT_TYPE, HEADER_JSON); + + builder = buildAuthorization(builder, authorizationChoice); - return "curl" - + " -H " + ITUtil.HEADER_JSON - + " -X" + ITUtil.getMethodString(methodChoice) - + " -i " - + ITUtil.HTTP - + ITUtil.getAuthorizationString(authorizationChoice) - + ITUtil.IP_PORT_OLOG - + ITUtil.getEndpointString(endpointChoice) - + pathstr - + data; + return buildMethodJson(builder, methodChoice, json).build(); } /** - * Utility method to return string for http method. To be used when constructing url to send query to server. + * Utility method to build http request for method and json data. * + * @param builder http request builder * @param methodChoice method choice, i.e. POST, GET, PUT, DELETE, PATCH - * @return string for http method + * @param json json data + * @return http request builder */ - private static String getMethodString(MethodChoice methodChoice) { + private static HttpRequest.Builder buildMethodJson(HttpRequest.Builder builder, MethodChoice methodChoice, String json) { switch (methodChoice) { case POST: - return "POST"; + return builder.POST(HttpRequest.BodyPublishers.ofString(json)); case GET: - return "GET"; + return builder.GET(); case PUT: - return "PUT"; + return builder.PUT(HttpRequest.BodyPublishers.ofString(json)); case DELETE: - return "DELETE"; + return builder.DELETE(); default: - return "GET"; + return builder.GET(); } } /** - * Utility method to return string for authorization. To be used when constructing url to send query to server. + * Utility method to build http client for authentication and authorization. + * Http basic authorization is used. * + * @param builder http request builder * @param authorizationChoice authorization choice - * @return string for authorization + * @return http request builder */ - private static String getAuthorizationString(AuthorizationChoice authorizationChoice) { + private static HttpRequest.Builder buildAuthorization(HttpRequest.Builder builder, AuthorizationChoice authorizationChoice) { switch (authorizationChoice) { case ADMIN: - return ITUtil.AUTH_ADMIN + "@"; + return builder.headers(HttpHeaders.AUTHORIZATION, HEADER_BASIC + Base64.getEncoder().encodeToString(AUTH_ADMIN.getBytes())); case USER: - return ITUtil.AUTH_USER + "@"; + return builder.headers(HttpHeaders.AUTHORIZATION, HEADER_BASIC + Base64.getEncoder().encodeToString(AUTH_USER.getBytes())); case NONE: - return StringUtils.EMPTY; + return builder; default: - return StringUtils.EMPTY; + return builder; } } /** - * Utility method to return string for endpoint. To be used when constructing url to send query to server. + * Utility method to build string for endpoint. To be used when constructing uri to send query to server. * * @param endpointChoice endpoint choice * @return string for endpoint */ - private static String getEndpointString(EndpointChoice endpointChoice) { + private static String buildUri(EndpointChoice endpointChoice) { switch (endpointChoice) { case LOGBOOKS: return ITUtil.LOGBOOKS; diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java b/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java index d3b9af4..a5c3344 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilLogbooks.java @@ -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; @@ -107,27 +106,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.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = 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; } // ---------------------------------------------------------------------------------------------------- @@ -148,16 +141,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.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGBOOKS); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Logbook[].class); } - // expected number of items in list // (if non-negative number) // expectedGreaterThanOrEqual <= nbr of items <= expectedLessThanOrEqual @@ -167,19 +158,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; } // ---------------------------------------------------------------------------------------------------- @@ -218,27 +203,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); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -271,27 +250,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); } - if (expected != null) { ITUtil.assertEqualsLogbooks(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -311,12 +284,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(); } diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java b/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java index 1415cc9..0f67fd1 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilLogs.java @@ -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 java.util.List; @@ -103,27 +102,21 @@ public static Log assertRetrieveLog(String path) { * @param expected expected response log */ public static Log assertRetrieveLog(String path, int expectedResponseCode, Log expected) { + Log actual = null; try { - String[] response = null; - Log actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + path); - response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Log.class); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -151,16 +144,14 @@ public static Log[] assertListLogs(String queryString, int expectedEqual, Log... * @return number of logs */ public static Log[] assertListLogs(String queryString, int expectedResponseCode, int expectedGreaterThanOrEqual, int expectedLessThanOrEqual, Log... expected) { + Log[] actual = null; try { - String[] response = null; - Log[] actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + queryString); - response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + queryString); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Log[].class); } - // expected number of items in list // (if non-negative number) // expectedGreaterThanOrEqual <= nbr of items <= expectedLessThanOrEqual @@ -170,19 +161,13 @@ public static Log[] assertListLogs(String queryString, int expectedResponseCode, if (expectedLessThanOrEqual >= 0) { assertTrue(actual.length <= expectedLessThanOrEqual); } - - // expected content if (expected != null && expected.length > 0) { ITUtil.assertEqualsLogs(actual, expected); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -210,16 +195,14 @@ public static SearchResult assertSearchLogs(String queryString, int expectedEqua * @return number of logs */ public static SearchResult assertSearchLogs(String queryString, int expectedResponseCode, int expectedGreaterThanOrEqual, int expectedLessThanOrEqual, Log... expected) { + SearchResult actual = null; try { - String[] response = null; - SearchResult actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + "/search" + queryString); - response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + "/search" + queryString); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], SearchResult.class); } - // expected number of items in list // (if non-negative number) // expectedGreaterThanOrEqual <= nbr of items <= expectedLessThanOrEqual @@ -229,19 +212,13 @@ public static SearchResult assertSearchLogs(String queryString, int expectedResp if (expectedLessThanOrEqual >= 0) { assertTrue(actual.getHitCount() <= expectedLessThanOrEqual); } - - // expected content if (expected != null && expected.length > 0) { ITUtil.assertEqualsLogs((Log[]) actual.getLogs().toArray(), expected); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -280,27 +257,21 @@ public static Log assertCreateLog(AuthorizationChoice authorizationChoice, Strin * @param expected expected response log */ public static Log assertCreateLog(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Log expected) { + Log actual = null; try { - String[] response = null; - Log actual = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.PUT, authorizationChoice, EndpointChoice.LOGS, path, json)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.PUT, authorizationChoice, EndpointChoice.LOGS, path, json)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Log.class); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -322,27 +293,21 @@ public static Log assertUpdateLog(Log value) { * @return */ public static Log assertUpdateLog(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Log expected) { + Log actual = null; try { - String[] response = null; - Log actual = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.POST, authorizationChoice, EndpointChoice.LOGS, path, json)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.POST, authorizationChoice, EndpointChoice.LOGS, path, json)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Log.class); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -362,37 +327,12 @@ public static void assertGroupLogs(List logEntryIds) { */ public static void assertGroupLogs(AuthorizationChoice authorizationChoice, List logEntryIds, int expectedResponseCode) { try { - String[] response = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.POST, authorizationChoice, EndpointChoice.LOGS, "/group", mapper.writeValueAsString(logEntryIds))); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.POST, authorizationChoice, EndpointChoice.LOGS, "/group", mapper.writeValueAsString(logEntryIds))); ITUtil.assertResponseLength2Code(response, expectedResponseCode); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } } - // ---------------------------------------------------------------------------------------------------- - - /** - * Utility method to return curl to create log for regular user. - * - * @param logJson log json - * @return curl to create log - */ - public static String createCurlLogForUser(String logJson) { - return "curl -H " + ITUtil.HEADER_JSON + " -XPUT -i " + ITUtil.HTTP_AUTH_USER_IP_PORT_OLOG_LOGS + " -d '" + logJson + "'"; - } - - /** - * Utility method to return curl to create log for admin user. - * - * @param logJson log json - * @return curl to create log - */ - public static String createCurlLogForAdmin(String logJson) { - return "curl -H " + ITUtil.HEADER_JSON + " -XPUT -i " + ITUtil.HTTP_AUTH_ADMIN_IP_PORT_OLOG_LOGS + " -d '" + logJson + "'"; - } - } diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java b/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java index e40bf8d..fc21be4 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java @@ -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; @@ -107,27 +106,21 @@ public static Property assertRetrieveProperty(String path, Property expected) { * @param expected expected response property */ public static Property assertRetrieveProperty(String path, int expectedResponseCode, Property expected) { + Property actual = null; try { - String[] response = null; - Property actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + path); - response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Property.class); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -155,16 +148,14 @@ public static Property[] assertListProperties(String queryString, int expectedEq * @return number of properties */ public static Property[] assertListProperties(String queryString, int expectedResponseCode, int expectedGreaterThanOrEqual, int expectedLessThanOrEqual, Property... expected) { + Property[] actual = null; try { - String[] response = null; - Property[] actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + queryString); - response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_PROPERTIES + queryString); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Property[].class); } - // expected number of items in list // (if non-negative number) // expectedGreaterThanOrEqual <= nbr of items <= expectedLessThanOrEqual @@ -174,19 +165,13 @@ public static Property[] assertListProperties(String queryString, int expectedRe if (expectedLessThanOrEqual >= 0) { assertTrue(actual.length <= expectedLessThanOrEqual); } - - // expected content if (expected != null && expected.length > 0) { ITUtil.assertEqualsProperties(actual, expected); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -225,27 +210,21 @@ public static Property assertCreateProperty(AuthorizationChoice authorizationCho * @param expected expected response property */ public static Property assertCreateProperty(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Property expected) { + Property actual = null; try { - String[] response = null; - Property actual = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.PUT, authorizationChoice, EndpointChoice.PROPERTIES, path, json)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.PUT, authorizationChoice, EndpointChoice.PROPERTIES, path, json)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Property.class); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -278,27 +257,21 @@ public static Property[] assertCreateProperties(AuthorizationChoice authorizatio * @param expected expected response properties */ public static Property[] assertCreateProperties(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Property[] expected) { + Property[] actual = null; try { - String[] response = null; - Property[] actual = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.PUT, authorizationChoice, EndpointChoice.PROPERTIES, path, json)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.PUT, authorizationChoice, EndpointChoice.PROPERTIES, path, json)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Property[].class); } - if (expected != null) { ITUtil.assertEqualsProperties(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -318,12 +291,9 @@ public static void assertRemoveProperty(String path) { */ public static void assertRemoveProperty(AuthorizationChoice authorizationChoice, String path, int expectedResponseCode) { try { - String[] response = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.DELETE, authorizationChoice, EndpointChoice.PROPERTIES, path, null)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.DELETE, authorizationChoice, EndpointChoice.PROPERTIES, path, null)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilTags.java b/src/test/java/org/phoebus/olog/docker/ITUtilTags.java index d0f16f4..2f56ac9 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilTags.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilTags.java @@ -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; @@ -107,27 +106,21 @@ public static Tag assertRetrieveTag(String path, Tag expected) { * @param expected expected response tag */ public static Tag assertRetrieveTag(String path, int expectedResponseCode, Tag expected) { + Tag actual = null; try { - String[] response = null; - Tag actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_TAGS + path); - response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_TAGS + path); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Tag.class); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -148,16 +141,14 @@ public static Tag[] assertListTags(int expectedEqual, Tag... expected) { * @return number of tags */ public static Tag[] assertListTags(int expectedResponseCode, int expectedGreaterThanOrEqual, int expectedLessThanOrEqual, Tag... expected) { + Tag[] actual = null; try { - String[] response = null; - Tag[] actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_TAGS); - response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_TAGS); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Tag[].class); } - // expected number of items in list // (if non-negative number) // expectedGreaterThanOrEqual <= nbr of items <= expectedLessThanOrEqual @@ -167,19 +158,13 @@ public static Tag[] assertListTags(int expectedResponseCode, int expectedGreater if (expectedLessThanOrEqual >= 0) { assertTrue(actual.length <= expectedLessThanOrEqual); } - - // expected content if (expected != null && expected.length > 0) { ITUtil.assertEqualsTags(actual, expected); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -218,27 +203,21 @@ public static Tag assertCreateTag(AuthorizationChoice authorizationChoice, Strin * @param expected expected response tag */ public static Tag assertCreateTag(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Tag expected) { + Tag actual = null; try { - String[] response = null; - Tag actual = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.PUT, authorizationChoice, EndpointChoice.TAGS, path, json)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.PUT, authorizationChoice, EndpointChoice.TAGS, path, json)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Tag.class); } - if (expected != null) { assertEquals(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -271,27 +250,21 @@ public static Tag[] assertCreateTags(AuthorizationChoice authorizationChoice, St * @param expected expected response tags */ public static Tag[] assertCreateTags(AuthorizationChoice authorizationChoice, String path, String json, int expectedResponseCode, Tag[] expected) { + Tag[] actual = null; try { - String[] response = null; - Tag[] actual = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.PUT, authorizationChoice, EndpointChoice.TAGS, path, json)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.PUT, authorizationChoice, EndpointChoice.TAGS, path, json)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Tag[].class); } - if (expected != null) { ITUtil.assertEqualsTags(expected, actual); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } // ---------------------------------------------------------------------------------------------------- @@ -311,12 +284,9 @@ public static void assertRemoveTag(String path) { */ public static void assertRemoveTag(AuthorizationChoice authorizationChoice, String path, int expectedResponseCode) { try { - String[] response = null; + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.DELETE, authorizationChoice, EndpointChoice.TAGS, path, null)); - response = ITUtil.runShellCommand(ITUtil.curlMethodAuthEndpointPathJson(MethodChoice.DELETE, authorizationChoice, EndpointChoice.TAGS, path, null)); ITUtil.assertResponseLength2Code(response, expectedResponseCode); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } diff --git a/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java b/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java index 8d60bf2..d55f4b7 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java @@ -28,7 +28,6 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.IOException; import java.net.HttpURLConnection; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -258,8 +257,6 @@ void handleLogbookCreateCheckJson() { ITUtilLogbooks.assertListLogbooks(2, default_logbooks[1], default_logbooks[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -358,8 +355,6 @@ void handleLogbook() { ITUtilLogbooks.assertListLogbooks(2, default_logbooks[1], default_logbooks[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -423,8 +418,6 @@ void handleLogbook2() { ITUtilLogbooks.assertListLogbooks(2, default_logbooks[1], default_logbooks[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -484,8 +477,6 @@ void handleLogbook3ChangeState() { ITUtilLogbooks.assertListLogbooks(2, default_logbooks[1], default_logbooks[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -634,8 +625,6 @@ void handleLogbooks() { ITUtilLogbooks.assertListLogbooks(2, default_logbooks[1], default_logbooks[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } diff --git a/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java b/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java index ff949bc..acb2f99 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java @@ -29,7 +29,6 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.IOException; import java.net.HttpURLConnection; import java.util.HashSet; @@ -461,8 +460,6 @@ void handleProperty() { default_properties[0]); ITUtilProperties.assertListProperties(1, default_properties[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -547,8 +544,6 @@ void handleProperty2() { default_properties[0]); ITUtilProperties.assertListProperties(1, default_properties[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -602,8 +597,6 @@ void handleProperty3ChangeState() { ITUtilProperties.assertRetrieveProperty("/p1", property_p1_owner_a_state_i_attributes); ITUtilProperties.assertListProperties(1, default_properties[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -815,8 +808,6 @@ void handleProperties() { properties_inactive); ITUtilProperties.assertListProperties(1, default_properties[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } diff --git a/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java b/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java index 5f41d8e..57bcc42 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java @@ -28,7 +28,6 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.IOException; import java.net.HttpURLConnection; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -334,8 +333,6 @@ void handleTag() { ITUtilTags.assertRetrieveTag("/t1", tag_t1_state_i); ITUtilTags.assertListTags(1, default_tags[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -393,8 +390,6 @@ void handleTag2() { ITUtilTags.assertRetrieveTag("/t2", tag_t2_state_i); ITUtilTags.assertListTags(1, default_tags[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -447,8 +442,6 @@ void handleTag3ChangeState() { ITUtilTags.assertRetrieveTag("/t1", tag_t1_state_i); ITUtilTags.assertListTags(1, default_tags[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } @@ -586,8 +579,6 @@ void handleTags() { ITUtil.assertRefreshElasticIndices(); ITUtilTags.assertListTags(1, default_tags[0]); - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } From f807c7ce0c7d7ab968c498c4f805578fb6056d17 Mon Sep 17 00:00:00 2001 From: Lars Johansson Date: Wed, 10 Jul 2024 10:08:55 +0200 Subject: [PATCH 3/5] Refactor and cleanup of integration test utilities --- .../java/org/phoebus/olog/docker/ITUtil.java | 360 +++++++++--------- .../phoebus/olog/docker/ITUtilLogbooks.java | 19 +- .../org/phoebus/olog/docker/ITUtilLogs.java | 22 +- .../phoebus/olog/docker/ITUtilProperties.java | 19 +- .../org/phoebus/olog/docker/ITUtilTags.java | 19 +- .../INTEGRATIONTEST_DOCKER_TUTORIAL.md | 28 +- 6 files changed, 217 insertions(+), 250 deletions(-) diff --git a/src/test/java/org/phoebus/olog/docker/ITUtil.java b/src/test/java/org/phoebus/olog/docker/ITUtil.java index dbf017d..92efebb 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtil.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtil.java @@ -28,6 +28,7 @@ import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.shaded.org.apache.commons.lang3.StringUtils; +import com.fasterxml.jackson.databind.ObjectMapper; import com.github.dockerjava.api.DockerClient; import java.io.File; @@ -53,37 +54,36 @@ */ public class ITUtil { - static final String OLOG = "olog"; + // note + // port numbers can be exposed differently to avoid interference with any running instance - static final String UTF_8 = "UTF-8"; + public static final String UTF_8 = "UTF-8"; - static final String AUTH_USER = "user:userPass"; - static final String AUTH_ADMIN = "admin:adminPass"; - static final String EMPTY_JSON = "[]"; - static final String HEADER_JSON = "application/json"; - static final String HEADER_BASIC = "Basic "; - static final String HTTP = "http://"; + private static final String AUTH_USER = "user:userPass"; + private static final String AUTH_ADMIN = "admin:adminPass"; + private static final String HEADER_JSON = "application/json"; + private static final String HEADER_BASIC = "Basic "; - static final String IP_PORT_OLOG = "127.0.0.1:8080/Olog"; - static final String IP_PORT_ELASTICSEARCH = "127.0.0.1:9200"; + private static final String LOGBOOKS = "/logbooks"; + private static final String LOGS = "/logs"; + private static final String PROPERTIES = "/properties"; + private static final String TAGS = "/tags"; - static final String LOGBOOKS = "/logbooks"; - static final String LOGS = "/logs"; - static final String PROPERTIES = "/properties"; - static final String TAGS = "/tags"; + public static final String HTTP_IP_PORT_OLOG = "http://127.0.0.1:8080/Olog"; + public static final String HTTP_IP_PORT_ELASTICSEARCH = "http://127.0.0.1:9200"; - static final String HTTP_IP_PORT_OLOG = HTTP + IP_PORT_OLOG; - static final String HTTP_IP_PORT_ELASTICSEARCH = HTTP + IP_PORT_ELASTICSEARCH; - - static final String HTTP_IP_PORT_OLOG_LOGBOOKS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + LOGBOOKS; - static final String HTTP_IP_PORT_OLOG_LOGS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + LOGS; - static final String HTTP_IP_PORT_OLOG_PROPERTIES = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + PROPERTIES; - static final String HTTP_IP_PORT_OLOG_TAGS = ITUtil.HTTP + ITUtil.IP_PORT_OLOG + TAGS; + public static final String HTTP_IP_PORT_OLOG_LOGBOOKS = ITUtil.HTTP_IP_PORT_OLOG + LOGBOOKS; + public static final String HTTP_IP_PORT_OLOG_LOGS = ITUtil.HTTP_IP_PORT_OLOG + LOGS; + public static final String HTTP_IP_PORT_OLOG_PROPERTIES = ITUtil.HTTP_IP_PORT_OLOG + PROPERTIES; + public static final String HTTP_IP_PORT_OLOG_TAGS = ITUtil.HTTP_IP_PORT_OLOG + TAGS; // integration test - docker private static final HttpClient CLIENT = HttpClient.newHttpClient(); + public static final ObjectMapper MAPPER = new ObjectMapper(); + + public static final String OLOG = "olog"; public static final String INTEGRATIONTEST_DOCKER_COMPOSE = "docker-compose-integrationtest.yml"; public static final String INTEGRATIONTEST_LOG_MESSAGE = ".*Started Application.*"; @@ -144,83 +144,160 @@ public static void extractJacocoReport(ComposeContainer environment, String dest } } + // ---------------------------------------------------------------------------------------------------- + /** - * Refresh Elastic indices and return response code and string. + * Assert that response object is as expected, an array with 2 elements + * of which first contains response code OK (200). * - * @return response code and string - * @throws InterruptedException - * @throws IOException - * @throws URISyntaxException + * @param response string array with response of http request, response code and content + * + * @see HttpURLConnection#HTTP_OK */ - static String[] refreshElasticIndices() throws URISyntaxException, IOException, InterruptedException { - return sendRequest(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); + static void assertResponseLength2CodeOK(String[] response) { + assertResponseLength2Code(response, HttpURLConnection.HTTP_OK); } /** - * Refresh Elastic indices and assert response is of length 2 and has response code HttpURLConnection.HTTP_OK. - * @throws InterruptedException - * @throws IOException - * @throws URISyntaxException + * Assert that response object is as expected, an array with 2 elements + * of which first element contains given response code. + * + * @param response string array with response of http request, response code and content + * @param expectedResponseCode expected response code * + * @see HttpURLConnection for available response codes */ - static void assertRefreshElasticIndices() throws URISyntaxException, IOException, InterruptedException { - String[] response = sendRequest(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); - - ITUtil.assertResponseLength2CodeOK(response); + static void assertResponseLength2Code(String[] response, int expectedResponseCode) { + assertNotNull(response); + assertEquals(2, response.length); + assertEquals(expectedResponseCode, Integer.parseInt(response[0])); } /** - * Send GET request with given string as URI and return response status code. + * Assert that response object is as expected, an array with 2 elements + * of which first element contains response code OK (200) and second element contains given response content. * - * @param str string to parse as URI - * @return response status code - * @throws URISyntaxException If request is created with non-legal URI characters - * @throws InterruptedException - * @throws IOException + * @param response string array with response of http request, response code and content + * @param expectedResponseContent expected response content + * + * @see HttpURLConnection#HTTP_OK */ - static int sendRequestStatusCode(String str) throws URISyntaxException, IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI(str)) - .GET() - .build(); + static void assertResponseLength2CodeOKContent(String[] response, String expectedResponseContent) { + assertResponseLength2CodeContent(response, HttpURLConnection.HTTP_OK, expectedResponseContent); + } - HttpResponse response = CLIENT.send(request, BodyHandlers.ofString()); + /** + * Assert that response object is as expected, an array with 2 elements + * of which first element contains given response code and second element contains given response content. + * + * @param response string array with response of http request, response code and content + * @param expectedResponseCode expected response code + * @param expectedResponseContent expected response content + * + * @see HttpURLConnection for available response codes + */ + static void assertResponseLength2CodeContent(String[] response, int expectedResponseCode, String expectedResponseContent) { + assertResponseLength2Code(response, expectedResponseCode); + assertEquals(expectedResponseContent, response[1]); + } - return response.statusCode(); + /** + * Assert that arrays are equal with same length and same content in each array position. + * + * @param actual actual array of Tag objects + * @param expected expected arbitrary number of Tag objects + */ + static void assertEqualsTags(Tag[] actual, Tag... expected) { + if (expected != null) { + assertNotNull(actual); + assertEquals(expected.length, actual.length); + for (int i=0; i response = CLIENT.send(request, BodyHandlers.ofString()); + /** + * Assert that arrays are equal with same length and same content in each array position. + * + * @param actual actual array of Property objects + * @param expected expected arbitrary number of Property objects + */ + static void assertEqualsProperties(Property[] actual, Property... expected) { + if (expected != null) { + assertNotNull(actual); + assertEquals(expected.length, actual.length); + for (int i=0; i response = CLIENT.send(request, BodyHandlers.ofString()); + static String[] refreshElasticIndices() throws URISyntaxException, IOException, InterruptedException { + return sendRequest(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); + } - return new String[] {String.valueOf(response.statusCode()), response.body()}; + /** + * Refresh Elastic indices and assert response is of length 2 and has response code HttpURLConnection.HTTP_OK. + * @throws InterruptedException + * @throws IOException + * @throws URISyntaxException + * + */ + static void assertRefreshElasticIndices() throws URISyntaxException, IOException, InterruptedException { + String[] response = sendRequest(HTTP_IP_PORT_ELASTICSEARCH + "/_refresh"); + + ITUtil.assertResponseLength2CodeOK(response); } // ---------------------------------------------------------------------------------------------------- @@ -251,8 +328,7 @@ static HttpRequest buildRequest(MethodChoice methodChoice, AuthorizationChoice a : ""; String str = - ITUtil.HTTP - + ITUtil.IP_PORT_OLOG + ITUtil.HTTP_IP_PORT_OLOG + ITUtil.buildUri(endpointChoice) + pathstr; @@ -330,134 +406,54 @@ private static String buildUri(EndpointChoice endpointChoice) { } } - // ---------------------------------------------------------------------------------------------------- - - /** - * Assert that response object is as expected, an array with 2 elements - * of which first contains response code OK (200). - * - * @param response string array with response of http request, response code and content - * - * @see HttpURLConnection#HTTP_OK - */ - static void assertResponseLength2CodeOK(String[] response) { - assertResponseLength2Code(response, HttpURLConnection.HTTP_OK); - } - /** - * Assert that response object is as expected, an array with 2 elements - * of which first element contains given response code. - * - * @param response string array with response of http request, response code and content - * @param expectedResponseCode expected response code - * - * @see HttpURLConnection for available response codes - */ - static void assertResponseLength2Code(String[] response, int expectedResponseCode) { - assertNotNull(response); - assertEquals(2, response.length); - assertEquals(expectedResponseCode, Integer.parseInt(response[0])); - } - - /** - * Assert that response object is as expected, an array with 2 elements - * of which first element contains response code OK (200) and second element contains given response content. - * - * @param response string array with response of http request, response code and content - * @param expectedResponseContent expected response content + * Send GET request with given string as URI and return response status code. * - * @see HttpURLConnection#HTTP_OK + * @param str string to parse as URI + * @return response status code + * @throws URISyntaxException If request is created with non-legal URI characters + * @throws InterruptedException + * @throws IOException */ - static void assertResponseLength2CodeOKContent(String[] response, String expectedResponseContent) { - assertResponseLength2CodeContent(response, HttpURLConnection.HTTP_OK, expectedResponseContent); - } + static int sendRequestStatusCode(String str) throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(str)) + .GET() + .build(); - /** - * Assert that response object is as expected, an array with 2 elements - * of which first element contains given response code and second element contains given response content. - * - * @param response string array with response of http request, response code and content - * @param expectedResponseCode expected response code - * @param expectedResponseContent expected response content - * - * @see HttpURLConnection for available response codes - */ - static void assertResponseLength2CodeContent(String[] response, int expectedResponseCode, String expectedResponseContent) { - assertResponseLength2Code(response, expectedResponseCode); - assertEquals(expectedResponseContent, response[1]); + return Integer.parseInt(sendRequest(request)[0]); } /** - * Assert that arrays are equal with same length and same content in each array position. + * Send GET request with given string as URI and return string array with response status code and response body. * - * @param actual actual array of Tag objects - * @param expected expected arbitrary number of Tag objects + * @param str string to parse as URI + * @return string array with response status code and response body + * @throws URISyntaxException If request is created with non-legal URI characters + * @throws InterruptedException + * @throws IOException */ - static void assertEqualsTags(Tag[] actual, Tag... expected) { - if (expected != null) { - assertNotNull(actual); - assertEquals(expected.length, actual.length); - for (int i=0; i response = CLIENT.send(request, BodyHandlers.ofString()); - /** - * Assert that arrays are equal with same length and same content in each array position. - * - * @param actual actual array of Log objects - * @param expected expected arbitrary number of Log objects - */ - static void assertEqualsLogs(Log[] actual, Log... expected) { - if (expected != null) { - assertNotNull(actual); - assertEquals(expected.length, actual.length); - for (int i=0; i logEntryIds) { */ public static void assertGroupLogs(AuthorizationChoice authorizationChoice, List logEntryIds, int expectedResponseCode) { try { - String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.POST, authorizationChoice, EndpointChoice.LOGS, "/group", mapper.writeValueAsString(logEntryIds))); + String[] response = ITUtil.sendRequest(ITUtil.buildRequest(MethodChoice.POST, authorizationChoice, EndpointChoice.LOGS, "/group", ITUtil.MAPPER.writeValueAsString(logEntryIds))); ITUtil.assertResponseLength2Code(response, expectedResponseCode); } catch (Exception e) { diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java b/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java index fc21be4..b794140 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilProperties.java @@ -30,7 +30,6 @@ import org.phoebus.olog.entity.Property; 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 property endpoints. @@ -41,10 +40,8 @@ */ public class ITUtilProperties { - static final ObjectMapper mapper = new ObjectMapper(); - - static final Property[] PROPERTIES_NULL = null; - static final Property PROPERTY_NULL = null; + private static final Property[] PROPERTIES_NULL = null; + private static final Property PROPERTY_NULL = null; /** * This class is not to be instantiated. @@ -63,7 +60,7 @@ private ITUtilProperties() { */ static String object2Json(Property value) { try { - return mapper.writeValueAsString(value); + return ITUtil.MAPPER.writeValueAsString(value); } catch (JsonProcessingException e) { fail(); } @@ -77,7 +74,7 @@ static String object2Json(Property value) { */ static String object2Json(Property[] value) { try { - return mapper.writeValueAsString(value); + return ITUtil.MAPPER.writeValueAsString(value); } catch (JsonProcessingException e) { fail(); } @@ -112,7 +109,7 @@ public static Property assertRetrieveProperty(String path, int expectedResponseC ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Property.class); + actual = ITUtil.MAPPER.readValue(response[1], Property.class); } if (expected != null) { assertEquals(expected, actual); @@ -154,7 +151,7 @@ public static Property[] assertListProperties(String queryString, int expectedRe ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Property[].class); + actual = ITUtil.MAPPER.readValue(response[1], Property[].class); } // expected number of items in list // (if non-negative number) @@ -216,7 +213,7 @@ public static Property assertCreateProperty(AuthorizationChoice authorizationCho ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Property.class); + actual = ITUtil.MAPPER.readValue(response[1], Property.class); } if (expected != null) { assertEquals(expected, actual); @@ -263,7 +260,7 @@ public static Property[] assertCreateProperties(AuthorizationChoice authorizatio ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Property[].class); + actual = ITUtil.MAPPER.readValue(response[1], Property[].class); } if (expected != null) { ITUtil.assertEqualsProperties(expected, actual); diff --git a/src/test/java/org/phoebus/olog/docker/ITUtilTags.java b/src/test/java/org/phoebus/olog/docker/ITUtilTags.java index 2f56ac9..5d7ad19 100644 --- a/src/test/java/org/phoebus/olog/docker/ITUtilTags.java +++ b/src/test/java/org/phoebus/olog/docker/ITUtilTags.java @@ -30,7 +30,6 @@ import org.phoebus.olog.entity.Tag; 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 tag endpoints. @@ -41,10 +40,8 @@ */ public class ITUtilTags { - static final ObjectMapper mapper = new ObjectMapper(); - - static final Tag[] TAGS_NULL = null; - static final Tag TAG_NULL = null; + private static final Tag[] TAGS_NULL = null; + private static final Tag TAG_NULL = null; /** * This class is not to be instantiated. @@ -63,7 +60,7 @@ private ITUtilTags() { */ static String object2Json(Tag value) { try { - return mapper.writeValueAsString(value); + return ITUtil.MAPPER.writeValueAsString(value); } catch (JsonProcessingException e) { fail(); } @@ -77,7 +74,7 @@ static String object2Json(Tag value) { */ static String object2Json(Tag[] value) { try { - return mapper.writeValueAsString(value); + return ITUtil.MAPPER.writeValueAsString(value); } catch (JsonProcessingException e) { fail(); } @@ -112,7 +109,7 @@ public static Tag assertRetrieveTag(String path, int expectedResponseCode, Tag e ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Tag.class); + actual = ITUtil.MAPPER.readValue(response[1], Tag.class); } if (expected != null) { assertEquals(expected, actual); @@ -147,7 +144,7 @@ public static Tag[] assertListTags(int expectedResponseCode, int expectedGreater ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Tag[].class); + actual = ITUtil.MAPPER.readValue(response[1], Tag[].class); } // expected number of items in list // (if non-negative number) @@ -209,7 +206,7 @@ public static Tag assertCreateTag(AuthorizationChoice authorizationChoice, Strin ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Tag.class); + actual = ITUtil.MAPPER.readValue(response[1], Tag.class); } if (expected != null) { assertEquals(expected, actual); @@ -256,7 +253,7 @@ public static Tag[] assertCreateTags(AuthorizationChoice authorizationChoice, St ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { - actual = mapper.readValue(response[1], Tag[].class); + actual = ITUtil.MAPPER.readValue(response[1], Tag[].class); } if (expected != null) { ITUtil.assertEqualsTags(expected, actual); diff --git a/src/test/resources/INTEGRATIONTEST_DOCKER_TUTORIAL.md b/src/test/resources/INTEGRATIONTEST_DOCKER_TUTORIAL.md index 23cc654..31f07e6 100644 --- a/src/test/resources/INTEGRATIONTEST_DOCKER_TUTORIAL.md +++ b/src/test/resources/INTEGRATIONTEST_DOCKER_TUTORIAL.md @@ -99,11 +99,10 @@ class OlogIT { @Test void ologUp() { try { - String address = ITUtil.HTTP_IP_PORT_OLOG; - int responseCode = ITUtil.doGet(address); + int responseCode = ITUtil.sendRequestStatusCode(ITUtil.HTTP_IP_PORT_OLOG); assertEquals(HttpURLConnection.HTTP_OK, responseCode); - } catch (IOException e) { + } catch (Exception e) { fail(); } } @@ -167,14 +166,7 @@ class OlogPropertiesIT { // what // user with required role // create property - // -------------------------------------------------------------------------------- - // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Property - // x List Properties - // x Create a Property - // Create Properties - // x Remove Property + // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list try { ITUtilProperties.assertListProperties(1, default_properties[0]); @@ -325,16 +317,14 @@ public class ITUtilLogs { * @return number of logs */ public static Log[] assertListLogs(String queryString, int expectedResponseCode, int expectedGreaterThanOrEqual, int expectedLessThanOrEqual, Log... expected) { + Log[] actual = null; try { - String[] response = null; - Log[] actual = null; + String[] response = ITUtil.sendRequest(ITUtil.HTTP_IP_PORT_OLOG_LOGS + queryString); - response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_OLOG_LOGS + queryString); ITUtil.assertResponseLength2Code(response, expectedResponseCode); if (HttpURLConnection.HTTP_OK == expectedResponseCode) { actual = mapper.readValue(response[1], Log[].class); } - // expected number of items in list // (if non-negative number) // expectedGreaterThanOrEqual <= nbr of items <= expectedLessThanOrEqual @@ -344,19 +334,13 @@ public class ITUtilLogs { if (expectedLessThanOrEqual >= 0) { assertTrue(actual.length <= expectedLessThanOrEqual); } - - // expected content if (expected != null && expected.length > 0) { ITUtil.assertEqualsLogs(actual, expected); } - - return actual; - } catch (IOException e) { - fail(); } catch (Exception e) { fail(); } - return null; + return actual; } ``` From 033eaceb3f58cf7f9c40461716bc4136c9621900 Mon Sep 17 00:00:00 2001 From: Lars Johansson Date: Wed, 10 Jul 2024 10:32:14 +0200 Subject: [PATCH 4/5] Cleanup of integration tests --- .../phoebus/olog/docker/OlogLogbooksIT.java | 80 +++---------------- .../org/phoebus/olog/docker/OlogLogsIT.java | 40 ++-------- .../phoebus/olog/docker/OlogLogsQueryIT.java | 19 ++--- .../phoebus/olog/docker/OlogPropertiesIT.java | 80 +++---------------- .../org/phoebus/olog/docker/OlogTagsIT.java | 80 +++---------------- 5 files changed, 49 insertions(+), 250 deletions(-) diff --git a/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java b/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java index d55f4b7..de4f40d 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java @@ -67,13 +67,13 @@ class OlogLogbooksIT { // Olog - Service Documentation // https://olog.readthedocs.io/en/latest/ // ------------------------------------------------------------------------------------------------ - // OLOG API LogbooksResource - // -------------------- -------------------- - // Retrieve a Logbook .../logbooks/ (GET) findByTitle(String) - // List Logbooks .../logbooks (GET) findAll() - // Create a Logbook .../logbooks/ (PUT) createLogbook(String, Logbook, Principal) - // Create Logbooks .../logbooks (PUT) updateLogbooks(List) - // Remove Logbook .../logbooks/ (DELETE) deleteLogbook(String) + // OLOG API + // -------------------- + // Retrieve a Logbook .../logbooks/ GET + // List Logbooks .../logbooks GET + // Create a Logbook .../logbooks/ PUT + // Create Logbooks .../logbooks PUT + // Remove Logbook .../logbooks/ DELETE // ------------------------------------------------------------------------------------------------ // test data @@ -166,12 +166,6 @@ void handleLogbookRetrieveCheck() { // check(s) for retrieve logbook // e.g. // retrieve non-existing logbook - // -------------------------------------------------------------------------------- - // x Retrieve a Logbook - // List Logbooks - // Create a Logbook - // Create Logbooks - // Remove Logbook ITUtilLogbooks.assertRetrieveLogbook("/l11", HttpURLConnection.HTTP_NOT_FOUND); } @@ -185,12 +179,6 @@ void handleLogbookRemoveCheck() { // check(s) for remove logbook // e.g. // remove non-existing logbook - // -------------------------------------------------------------------------------- - // Retrieve a Logbook - // List Logbooks - // Create a Logbook - // Create Logbooks - // x Remove Logbook // might be both 401, 404 // 401 UNAUTHORIZED @@ -216,12 +204,6 @@ void handleLogbookCreateCheckJson() { // name - null, empty // owner - null, empty // state - null (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Logbook - // x List Logbooks - // x Create a Logbook - // Create Logbooks - // Remove Logbook String json_incomplete1 = "{\"incomplete\"}"; String json_incomplete2 = "{\"incomplete\""; @@ -276,12 +258,6 @@ void handleLogbookCreateCheck() { // name - null, empty // owner - null, empty // state - null (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Logbook - // x List Logbooks - // x Create a Logbook - // Create Logbooks - // Remove Logbook Logbook logbook_check = new Logbook(); @@ -316,14 +292,7 @@ void handleLogbook() { // what // user with required role // create tag - // -------------------------------------------------------------------------------- - // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Logbook - // x List Logbooks - // x Create a Logbook - // Create Logbooks - // x Remove Logbook + // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list try { ITUtilLogbooks.assertListLogbooks(2, @@ -367,14 +336,7 @@ void handleLogbook() { void handleLogbook2() { // what // create logbooks, one by one - // -------------------------------------------------------------------------------- - // list, create (2), list/retrieve, remove, list/retrieve, remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Logbook - // x List Logbooks - // x Create a Logbook - // Create Logbooks - // x Remove Logbook + // list, create (2), list/retrieve, remove, list/retrieve, remove, retrieve/list try { ITUtilLogbooks.assertListLogbooks(2, @@ -430,14 +392,7 @@ void handleLogbook2() { void handleLogbook3ChangeState() { // what // replace logbook, change state - // -------------------------------------------------------------------------------- - // list, create, list/retrieve, update, list/retrieve, remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Logbook - // x List Logbooks - // x Create a Logbook - // Create Logbooks - // x Remove Logbook + // list, create, list/retrieve, update, list/retrieve, remove, retrieve/list try { ITUtilLogbooks.assertListLogbooks(2, @@ -496,12 +451,6 @@ void handleLogbooksCreateCheck() { // name - null, empty // owner - null, empty // state - null, (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Logbook - // x List Logbooks - // Create a Logbook - // x Create Logbooks - // Remove Logbook Logbook logbook_check = new Logbook(); Logbook[] logbooks = new Logbook[] { @@ -546,14 +495,7 @@ void handleLogbooksCreateCheck() { void handleLogbooks() { // what // create logbooks - // -------------------------------------------------------------------------------- - // list, create (10), list/retrieve, delete (5), list/retrieve, delete (5), retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Logbook - // x List Logbooks - // Create a Logbook - // x Create Logbooks - // x Remove Logbook + // list, create (10), list/retrieve, delete (5), list/retrieve, delete (5), retrieve/list Logbook[] logbooks_active_inactive = new Logbook[] { logbook_l1_owner_a_state_a, diff --git a/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java b/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java index f075efb..eeebc64 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogLogsIT.java @@ -70,17 +70,14 @@ class OlogLogsIT { // Olog - Service Documentation // https://olog.readthedocs.io/en/latest/ // ------------------------------------------------------------------------------------------------ - // OLOG API LogbooksResource - // -------------------- -------------------- - // Retrieve a Log .../logs/ (GET) getLog(String) - // Retrieve attachment for Log .../logs/attachments/{logId}/{attachmentName} - // (GET) findResources(String, String) - // List Logs / Query by Pattern .../logs (GET) findAll() - // Create a Log .../logs (PUT) createLog(String, Log, Principal) - // Upload attachment .../logs/attachments/{logId} - // (POST) uploadAttachment(String, MultipartFile, String, String, String) - // Upload multiple attachments .../logs/attachments-multi/{logId} - // (POST) uploadMultipleAttachments(String, MultipartFile[]) + // OLOG API + // -------------------- + // Retrieve a Log .../logs/ GET + // Retrieve attachment for Log .../logs/attachments/{logId}/{attachmentName} GET + // List Logs / Query by Pattern .../logs GET + // Create a Log .../logs PUT + // Upload attachment .../logs/attachments/{logId} POST + // Upload multiple attachments .../logs/attachments-multi/{logId} POST // ------------------------------------------------------------------------------------------------ @Container @@ -113,13 +110,6 @@ void handleLogRetrieveCheck() { // check(s) for retrieve log // e.g. // retrieve non-existing log - // -------------------------------------------------------------------------------- - // x Retrieve a Log - // Retrieve attachment for Log - // List Logs / Query by Pattern - // Create a Log - // Upload attachment - // Upload multiple attachments ITUtilTags.assertRetrieveTag("/l11", HttpURLConnection.HTTP_NOT_FOUND); } @@ -138,13 +128,6 @@ void handleLogCreateCheckJson() { // name - null, empty // owner - null, empty // state - null (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Log - // Retrieve attachment for Log - // x List Logs / Query by Pattern - // x Create a Log - // Upload attachment - // Upload multiple attachments String json_incomplete1 = "{\"incomplete\"}"; String json_incomplete2 = "{\"incomplete\""; @@ -185,13 +168,6 @@ void handleLogCreateCheck() { // name - null, empty // owner - null, empty // state - null (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Log - // Retrieve attachment for Log - // x List Logs / Query by Pattern - // x Create a Log - // Upload attachment - // Upload multiple attachments Log log_check = new Log.LogBuilder().build(); diff --git a/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java b/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java index 7bef232..c1ef0cf 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java @@ -67,17 +67,14 @@ class OlogLogsQueryIT { // Olog - Service Documentation // https://olog.readthedocs.io/en/latest/ // ------------------------------------------------------------------------------------------------ - // OLOG API LogbooksResource - // -------------------- -------------------- - // Retrieve a Log .../logs/ (GET) getLog(String) - // Retrieve attachment for Log .../logs/attachments/{logId}/{attachmentName} - // (GET) findResources(String, String) - // List Logs / Query by Pattern .../logs (GET) findAll() - // Create a Log .../logs (PUT) createLog(String, Log, Principal) - // Upload attachment .../logs/attachments/{logId} - // (POST) uploadAttachment(String, MultipartFile, String, String, String) - // Upload multiple attachments .../logs/attachments-multi/{logId} - // (POST) uploadMultipleAttachments(String, MultipartFile[]) + // OLOG API + // -------------------- + // Retrieve a Log .../logs/ GET + // Retrieve attachment for Log .../logs/attachments/{logId}/{attachmentName} GET + // List Logs / Query by Pattern .../logs GET + // Create a Log .../logs PUT + // Upload attachment .../logs/attachments/{logId} POST + // Upload multiple attachments .../logs/attachments-multi/{logId} POST // ------------------------------------------------------------------------------------------------ @Container diff --git a/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java b/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java index acb2f99..552ccc5 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java @@ -69,13 +69,13 @@ class OlogPropertiesIT { // Olog - Service Documentation // https://olog.readthedocs.io/en/latest/ // ------------------------------------------------------------------------------------------------ - // OLOG API PropertiesResource - // -------------------- -------------------- - // Retrieve a Property .../properties/ (GET) findByTitle(String) - // List Properties .../properties (GET) findAll(boolean) - // Create a Property .../properties/ (PUT) createProperty(String, Property, Principal) - // Create Properties .../properties (PUT) updateProperty(List) - // Remove Property .../properties/ (DELETE) deleteProperty(String) + // OLOG API + // -------------------- + // Retrieve a Property .../properties/ GET + // List Properties .../properties GET + // Create a Property .../properties/ PUT + // Create Properties .../properties PUT + // Remove Property .../properties/ DELETE // ------------------------------------------------------------------------------------------------ // test data @@ -240,12 +240,6 @@ void handlePropertyRetrieveCheck() { // check(s) for retrieve property // e.g. // retrieve non-existing property - // -------------------------------------------------------------------------------- - // x Retrieve a Property - // List Properties - // Create a Property - // Create Properties - // Remove Property ITUtilProperties.assertRetrieveProperty("/p11", HttpURLConnection.HTTP_NOT_FOUND); } @@ -259,12 +253,6 @@ void handlePropertyRemoveCheck() { // check(s) for remove property // e.g. // remove non-existing property - // -------------------------------------------------------------------------------- - // Retrieve a Property - // List Properties - // Create a Property - // Create Properties - // x Remove Property // might be both 401, 404 // 401 UNAUTHORIZED @@ -293,12 +281,6 @@ void handlePropertyCreateCheckJson() { // attributes - null // name - null, empty // state - null (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Property - // x List Properties - // x Create a Property - // Create Properties - // Remove Property String json_incomplete1 = "{\"incomplete\"}"; String json_incomplete2 = "{\"incomplete\""; @@ -347,12 +329,6 @@ void handlePropertyCreateCheck() { // attributes - null // name - null, empty // state - null (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Property - // x List Properties - // x Create a Property - // Create Properties - // Remove Property Property property_check = new Property(); Attribute attribute_check = new Attribute(); @@ -411,14 +387,7 @@ void handleProperty() { // what // user with required role // create property - // -------------------------------------------------------------------------------- - // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Property - // x List Properties - // x Create a Property - // Create Properties - // x Remove Property + // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list try { ITUtilProperties.assertListProperties(1, default_properties[0]); @@ -472,14 +441,7 @@ void handleProperty() { void handleProperty2() { // what // create properties, one by one - // -------------------------------------------------------------------------------- - // list, create (2), list/retrieve, remove, list/retrieve, remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Property - // x List Properties - // x Create a Property - // Create Properties - // x Remove Property + // list, create (2), list/retrieve, remove, list/retrieve, remove, retrieve/list try { ITUtilProperties.assertListProperties(1, default_properties[0]); @@ -556,14 +518,7 @@ void handleProperty2() { void handleProperty3ChangeState() { // what // replace property, change state - // -------------------------------------------------------------------------------- - // list, create, list/retrieve, update, list/retrieve, remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Property - // x List Properties - // x Create a Property - // Create Properties - // x Remove Property + // list, create, list/retrieve, update, list/retrieve, remove, retrieve/list try { ITUtilProperties.assertListProperties(1, default_properties[0]); @@ -619,12 +574,6 @@ void handlePropertiesCreateCheck() { // attributes - null // name - null, empty // state - null (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Property - // x List Properties - // Create a Property - // x Create Properties - // Remove Property Property property_check = new Property(); Property[] properties = new Property[] { @@ -697,14 +646,7 @@ void handlePropertiesCreateCheck() { void handleProperties() { // what // create properties - // -------------------------------------------------------------------------------- - // list, create (10), list/retrieve, delete (5), list/retrieve, delete (5), retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Property - // x List Properties - // Create a Property - // x Create Properties - // x Remove Property + // list, create (10), list/retrieve, delete (5), list/retrieve, delete (5), retrieve/list Property[] properties_active_inactive = new Property[] { property_p1_owner_a_state_a_attributes, diff --git a/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java b/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java index 57bcc42..e6abc23 100644 --- a/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java +++ b/src/test/java/org/phoebus/olog/docker/OlogTagsIT.java @@ -67,13 +67,13 @@ class OlogTagsIT { // Olog - Service Documentation // https://olog.readthedocs.io/en/latest/ // ------------------------------------------------------------------------------------------------ - // OLOG API TagResource - // -------------------- -------------------- - // Retrieve a Tag .../tags/ (GET) findByTitle(String) - // List Tags .../tags (GET) findAll() - // Create a Tag .../tags/ (PUT) createTag(String, Tag) - // Create Tags .../tags (PUT) updateTag(List) - // Remove Tag .../tags/ (DELETE) deleteTag(String) + // OLOG API + // -------------------- + // Retrieve a Tag .../tags/ GET + // List Tags .../tags GET + // Create a Tag .../tags/ PUT + // Create Tags .../tags PUT + // Remove Tag .../tags/ DELETE // ------------------------------------------------------------------------------------------------ // test data @@ -164,12 +164,6 @@ void handleTagRetrieveCheck() { // check(s) for retrieve tag // e.g. // retrieve non-existing tag - // -------------------------------------------------------------------------------- - // x Retrieve a Tag - // List Tags - // Create a Tag - // Create Tags - // Remove Tag ITUtilTags.assertRetrieveTag("/t11", HttpURLConnection.HTTP_NOT_FOUND); } @@ -183,12 +177,6 @@ void handleTagRemoveCheck() { // check(s) for remove tag // e.g. // remove non-existing tag - // -------------------------------------------------------------------------------- - // Retrieve a Tag - // List Tags - // Create a Tag - // Create Tags - // x Remove Tag // might be both 401, 404 // 401 UNAUTHORIZED @@ -213,12 +201,6 @@ void handleTagCreateCheckJson() { // json - incomplete // name - null, empty // state - null, (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Tag - // x List Tags - // x Create a Tag - // Create Tags - // Remove Tag String json_incomplete1 = "{\"incomplete\"}"; String json_incomplete2 = "{\"incomplete\""; @@ -263,12 +245,6 @@ void handleTagCreateCheck() { // json - incomplete // name - null, empty // state - null, (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Tag - // x List Tags - // x Create a Tag - // Create Tags - // Remove Tag Tag tag_check = new Tag(); @@ -299,14 +275,7 @@ void handleTag() { // what // user with required role TagMod // create tag - // -------------------------------------------------------------------------------- - // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Tag - // x List Tags - // x Create a Tag - // Create Tags - // x Remove Tag + // list, create, list/retrieve, remove (unauthorized), remove, retrieve/list try { ITUtilTags.assertListTags(1, default_tags[0]); @@ -345,14 +314,7 @@ void handleTag() { void handleTag2() { // what // create tags, one by one - // -------------------------------------------------------------------------------- - // list, create (2), list/retrieve, remove, list/retrieve, remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Tag - // x List Tags - // x Create a Tag - // Create Tags - // x Remove Tag + // list, create (2), list/retrieve, remove, list/retrieve, remove, retrieve/list try { ITUtilTags.assertListTags(1, default_tags[0]); @@ -402,14 +364,7 @@ void handleTag2() { void handleTag3ChangeState() { // what // replace tag, change state - // -------------------------------------------------------------------------------- - // list, create, list/retrieve, update, list/retrieve, remove, retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Tag - // x List Tags - // x Create a Tag - // Create Tags - // x Remove Tag + // list, create, list/retrieve, update, list/retrieve, remove, retrieve/list try { ITUtilTags.assertListTags(1, default_tags[0]); @@ -460,12 +415,6 @@ void handleTagsCreateCheck() { // json - incomplete // name - null, empty // state - null, (empty, incorrect value (ok: Active, Inactive)) - // -------------------------------------------------------------------------------- - // Retrieve a Tag - // x List Tags - // Create a Tag - // x Create Tags - // Remove Tag Tag tag_check = new Tag(); Tag[] tags = new Tag[] { @@ -506,14 +455,7 @@ void handleTagsCreateCheck() { void handleTags() { // what // create tags - // -------------------------------------------------------------------------------- - // list, create (10), list/retrieve, delete (5), list/retrieve, delete (5), retrieve/list - // -------------------------------------------------------------------------------- - // x Retrieve a Tag - // x List Tags - // Create a Tag - // x Create Tags - // x Remove Tag + // list, create (10), list/retrieve, delete (5), list/retrieve, delete (5), retrieve/list Tag[] tags_active_inactive = new Tag[] { tag_t1_state_a, From e96b849cf28dae02e6eb22d6b77fec0141dc9f7d Mon Sep 17 00:00:00 2001 From: Lars Johansson Date: Wed, 10 Jul 2024 15:05:15 +0200 Subject: [PATCH 5/5] Fix instructions on how to run integration tests --- src/test/resources/INTEGRATIONTEST_DOCKER_RUN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/INTEGRATIONTEST_DOCKER_RUN.md b/src/test/resources/INTEGRATIONTEST_DOCKER_RUN.md index 2d652c7..4b4d90a 100644 --- a/src/test/resources/INTEGRATIONTEST_DOCKER_RUN.md +++ b/src/test/resources/INTEGRATIONTEST_DOCKER_RUN.md @@ -66,7 +66,7 @@ mvn clean install test-compile failsafe:integration-test failsafe:verify --batch To build and run all unit tests and integration tests (Docker) with code coverage. ``` -mvn clean install test-compile failsafe:integration-test failsafe:verify verify --batch-mode --fail-at-end -Djacoco.skip=false -DskipITs=false -DskipITCoverage=false -Pdeployable-jar -Pintegrationtest-docker +mvn clean install test-compile failsafe:integration-test failsafe:verify --batch-mode --fail-at-end -Djacoco.skip=false -DskipITs=false -DskipITCoverage=false -Pdeployable-jar -Pintegrationtest-docker ``` ### Note