From fd667ac0a8e3624e306f821ac70f1dd8c1f6d4af Mon Sep 17 00:00:00 2001 From: Alberto Codutti Date: Tue, 21 Jan 2025 09:45:35 +0100 Subject: [PATCH] :recycle: [Test] Improvements on readyness checks of resources Signed-off-by: Alberto Codutti --- .../qa/integration/steps/DockerSteps.java | 33 ++----- ....java => TestReadinessHttpConnection.java} | 26 +++--- .../TestReadinessMqttBrokerConnection.java | 86 +++++++++++++++++++ 3 files changed, 105 insertions(+), 40 deletions(-) rename qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/{TestReadinessConnection.java => TestReadinessHttpConnection.java} (80%) create mode 100644 qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessMqttBrokerConnection.java diff --git a/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java index 8e6c0a35f86..e3347a20d92 100644 --- a/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java +++ b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java @@ -40,12 +40,9 @@ import org.eclipse.kapua.qa.common.BasicSteps; import org.eclipse.kapua.qa.common.DBHelper; import org.eclipse.kapua.qa.common.StepData; -import org.eclipse.kapua.qa.integration.steps.utils.TestReadinessConnection; -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.kapua.qa.integration.steps.utils.TestReadinessHttpConnection; +import org.eclipse.kapua.qa.integration.steps.utils.TestReadinessMqttBrokerConnection; import org.eclipse.paho.client.mqttv3.MqttException; -import org.eclipse.paho.client.mqttv3.MqttSecurityException; -import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -715,8 +712,8 @@ private void waitJobEngineContainer(String name) throws Exception{ * @since 2.1.0 */ private boolean isJobEngineContainerReady(String name) throws Exception { - try (TestReadinessConnection testReadinessConnection = new TestReadinessConnection(JOB_ENGINE_ADDRESS_EXTERNAL)){ - return testReadinessConnection.isReady(); + try (TestReadinessHttpConnection testReadinessHttpConnection = new TestReadinessHttpConnection(JOB_ENGINE_ADDRESS_EXTERNAL)){ + return testReadinessHttpConnection.isReady(); } catch (Exception e) { // Ignoring... @@ -806,26 +803,8 @@ private void waitMessageBrokerContainer(String name) throws Exception{ * @since 2.1.0 */ private boolean isMessageBrokerContainerReady(String name) { - try (MqttClient testReadinessClient = new MqttClient(MESSAGE_BROKER_ADDRESS_EXTERNAL, "test-readiness", new MemoryPersistence())){ - - // These username and password do not match any entry. - // We need just to receive the "Not authorized to connect" from the broker on connection attempt - MqttConnectOptions clientOpts = new MqttConnectOptions(); - clientOpts.setUserName("test-readiness-user"); // This user do - clientOpts.setPassword("test-readiness-password".toCharArray()); - clientOpts.setConnectionTimeout(1); - - try { - testReadinessClient.connect(clientOpts); - } - catch (MqttSecurityException mse) { - // When the Message Broker is ready will accept connection attempts. - // Since we are not providing valid username and password we are interested on - // receiving a MqttSecurityException with the following message. - if ("Not authorized to connect".equals(mse.getMessage())) { - return true; - } - } + try (TestReadinessMqttBrokerConnection testReadinessConnection = new TestReadinessMqttBrokerConnection(MESSAGE_BROKER_ADDRESS_EXTERNAL)){ + return testReadinessConnection.isReady(); } catch (Exception e) { // Ignoring... diff --git a/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessConnection.java b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessHttpConnection.java similarity index 80% rename from qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessConnection.java rename to qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessHttpConnection.java index 81388bd4dd9..9cf05473db8 100644 --- a/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessConnection.java +++ b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessHttpConnection.java @@ -21,9 +21,10 @@ * * @since 2.1.0 */ -public class TestReadinessConnection implements AutoCloseable { +public class TestReadinessHttpConnection implements AutoCloseable { - private final HttpURLConnection testReadinessConnection; + private final URL testReadinessURL; + private HttpURLConnection testReadinessConnection; private final int readyResponseCode; /** @@ -33,7 +34,7 @@ public class TestReadinessConnection implements AutoCloseable { * @throws Exception * @since 2.1.0 */ - public TestReadinessConnection(String testUrl) throws Exception { + public TestReadinessHttpConnection(String testUrl) throws Exception { this(testUrl, 200); } @@ -43,17 +44,11 @@ public TestReadinessConnection(String testUrl) throws Exception { * * @param testUrl The HTTP URL to check for readiness * @param readyResponseCode Which HTTP response code consider valid for readiness - * @throws Exception + * @throws IOException * @since 2.1.0 */ - public TestReadinessConnection(String testUrl, int readyResponseCode) throws IOException { - URL testReadinessURL = new URL(testUrl); - - testReadinessConnection = (HttpURLConnection) testReadinessURL.openConnection(); - testReadinessConnection.setConnectTimeout(5000); - testReadinessConnection.setReadTimeout(5000); - testReadinessConnection.setRequestMethod("GET"); - + public TestReadinessHttpConnection(String testUrl, int readyResponseCode) throws IOException { + this.testReadinessURL = new URL(testUrl); this.readyResponseCode = readyResponseCode; } @@ -65,11 +60,16 @@ public TestReadinessConnection(String testUrl, int readyResponseCode) throws IOE * @since 2.1.0 */ public boolean isReady() throws IOException { + testReadinessConnection = (HttpURLConnection) testReadinessURL.openConnection(); + testReadinessConnection.setConnectTimeout(5000); + testReadinessConnection.setReadTimeout(5000); + testReadinessConnection.setRequestMethod("GET"); + return testReadinessConnection.getResponseCode() == readyResponseCode; } /** - * Invokes {@link HttpURLConnection#disconnect()} + * Invokes {@link HttpURLConnection#disconnect()} to clean up resources. * * @since 2.1.0 */ diff --git a/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessMqttBrokerConnection.java b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessMqttBrokerConnection.java new file mode 100644 index 00000000000..2004272beeb --- /dev/null +++ b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/utils/TestReadinessMqttBrokerConnection.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2025, 2025 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech + *******************************************************************************/ +package org.eclipse.kapua.qa.integration.steps.utils; + +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttSecurityException; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; + +import java.net.HttpURLConnection; + +/** + * Wrapper for {@link HttpURLConnection} that implements {@link AutoCloseable} + * + * @since 2.1.0 + */ +public class TestReadinessMqttBrokerConnection implements AutoCloseable { + + private final String testReadinessAddress; + private MqttClient testReadinessClient; + + /** + * Constructor. + * + * @param testReadinessAddress The mqtt address to check for readiness + * @since 2.1.0 + */ + public TestReadinessMqttBrokerConnection(String testReadinessAddress) { + this.testReadinessAddress = testReadinessAddress; + } + + /** + * Checks that the HTTP returns the expected HTTP response code. + * + * @return {@code true} if expected code is returned, {@code false} otherwise + * @throws MqttException + * @since 2.1.0 + */ + public boolean isReady() throws MqttException { + testReadinessClient = new MqttClient(testReadinessAddress, "test-readiness", new MemoryPersistence()); + + // These username and password do not match any entry. + // We need just to receive the "Not authorized to connect" from the broker on connection attempt + MqttConnectOptions clientOpts = new MqttConnectOptions(); + clientOpts.setUserName("test-readiness-user"); // This user do + clientOpts.setPassword("test-readiness-password".toCharArray()); + clientOpts.setConnectionTimeout(1); + + try { + testReadinessClient.connect(clientOpts); + } + catch (MqttSecurityException mse) { + // When the Message Broker is ready will accept connection attempts. + // Since we are not providing valid username and password we are interested on + // receiving a MqttSecurityException with the following message. + if ("Not authorized to connect".equals(mse.getMessage())) { + return true; + } + } + + return false; + } + + /** + * Invokes {@link HttpURLConnection#disconnect()} to clean up resources. + * + * @since 2.1.0 + */ + @Override + public void close() throws MqttException { + if (testReadinessClient != null) { + testReadinessClient.close(); + } + } +}