Skip to content

Commit

Permalink
♻️ [Test] Improvements on readyness checks of resources
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Jan 21, 2025
1 parent 96fae35 commit 5dd3b3d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@
import org.eclipse.kapua.qa.common.DBHelper;
import org.eclipse.kapua.qa.common.StepData;
import org.eclipse.kapua.qa.integration.steps.utils.TestReadinessHttpConnection;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
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;
Expand Down Expand Up @@ -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...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* 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.io.IOException;
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
* @throws IOException
* @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 IOException
* @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();
}
}
}

0 comments on commit 5dd3b3d

Please sign in to comment.