Skip to content

Commit

Permalink
fix: properly invoke withPort (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
yitingb authored Mar 22, 2024
1 parent 6e32f8b commit ba33ca3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/aws/greengrass/MqttConnectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public MqttClientConnection getMqttConnection(MqttConnectionParameters mqttConne
Class<?> classObj = builder.getClass();
try {
Method method = classObj.getDeclaredMethod("withPort", int.class);
method.invoke(method, mqttConnectionParameters.getMqttPort());
method.invoke(builder, mqttConnectionParameters.getMqttPort());
} catch (NoSuchMethodException e) {
try {
Method method = classObj.getDeclaredMethod("withPort", short.class);
method.invoke(method, mqttConnectionParameters.getMqttPort().shortValue());
method.invoke(builder, mqttConnectionParameters.getMqttPort().shortValue());
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
logger.atWarn().log("Can not successfully use port: "
+ mqttConnectionParameters.getMqttPort().shortValue(), ex);
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/com/aws/greengrass/MqttConnectionHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package com.aws.greengrass;

import com.aws.greengrass.testcommons.testutilities.GGExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.crt.io.ClientBootstrap;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;


@ExtendWith({MockitoExtension.class, GGExtension.class})
public class MqttConnectionHelperTest {

private static final String MOCK_IOT_DATA_ENDPOINT = "MOCK_IOT_DATA_ENDPOINT";
private static final String MOCK_CLIENT_ID = "MOCK_CLIENT_ID";
private static final Integer MOCK_PORT_NUMBER = 8883;

@TempDir
Path rootDir;
Path claimCertificatePath;
Path privateKeyPath;
Path rootCAPath;
private final ClientBootstrap mockClientBootstrap = new ClientBootstrap(null, null);
@BeforeEach
public void setup() throws IOException {
claimCertificatePath = rootDir.resolve("claimCert.crt");
Files.createFile(claimCertificatePath);
privateKeyPath = rootDir.resolve("privateKey.key");
Files.createFile(privateKeyPath);
rootCAPath = rootDir.resolve("rootCA.pem");
Files.createFile(rootCAPath);
}

@Test
public void GIVEN_MQTT_port_number_THEN_sdk_successfully_invoke_withPort_method(){
MqttConnectionHelper mockmqttConnectionHelper = new MqttConnectionHelper();
MqttConnectionHelper.MqttConnectionParameters parameters = createMqttConnectionParams();
assertDoesNotThrow(() -> mockmqttConnectionHelper.getMqttConnection(parameters));
}
private MqttConnectionHelper.MqttConnectionParameters createMqttConnectionParams() {
return new MqttConnectionHelper.MqttConnectionParameters(
claimCertificatePath.toString(),
privateKeyPath.toString(),
rootCAPath.toString(),
MOCK_IOT_DATA_ENDPOINT,
MOCK_CLIENT_ID,
MOCK_PORT_NUMBER,
mockClientBootstrap,
null
);
}
}

0 comments on commit ba33ca3

Please sign in to comment.