Skip to content

Commit

Permalink
Add test cases for handling new SecurityParams interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
gfinocchiaro committed Jan 9, 2018
1 parent 9376693 commit 8cfcacd
Showing 1 changed file with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import cool.mqtt.hooks.MqttBrokerConfig;
import cool.mqtt.hooks.MqttMessage;
import cool.mqtt.hooks.QoS;
import cool.mqtt.hooks.SecurityParams;

import java.nio.file.Paths;

import org.junit.Before;
import org.junit.Rule;
Expand All @@ -23,29 +26,41 @@ public class MqttBrokerConfigBuilderTest {

@Before
public void setup() {
builder = new MqttBrokerConfigBuilder("tcp://localhost:1883");
builder = new MqttBrokerConfigBuilder("mqtts://localhost:8883");
}

@Test
public void shouldBuildConfigWithDefaultValues() {
MqttBrokerConfig config = builder.build();
assertThat(config.getAddress(), is("tcp://localhost:1883"));
assertThat(config.getAddress(), is("mqtts://localhost:8883"));
assertThat(config.getClientIdPrefix(), nullValue());
assertThat(config.getConnectionTimeout(), nullValue());
assertThat(config.getKeepAlive(), nullValue());
assertThat(config.getPassword(), nullValue());
assertThat(config.getUsername(), nullValue());
assertThat(config.getPassword(), nullValue());
assertThat(config.getWillMessage(), nullValue());
assertThat(config.getSecurityParams(), nullValue());
}

@Test
public void shouldBuildConfigWithCustomValues() {
MqttBrokerConfig config = builder.username("username").password("password")
.clientIdPrefix("client_id_prefix").connectionTimeout(5).keepAlive(10)
.willMessage("topic", new byte[] {1, 2, 3}, QoS.AT_MOST_ONCE, true).build();

assertThat(config.getAddress(), is("tcp://localhost:1883"));
MqttBrokerConfig config = builder
.username("username")
.password("password")
.clientIdPrefix("client_id_prefix")
.connectionTimeout(5)
.keepAlive(10)
.willMessage("topic", new byte[] {1, 2, 3}, QoS.AT_MOST_ONCE, true)
.securityProtocol("TLSV1.1")
.truststorePath(Paths.get("truststore.jks"))
.truststorePassword("truststorepassword".toCharArray())
.keystorePath(Paths.get("keystore.jks"))
.keystorePassword("keystorepassword".toCharArray())
.privateKeyPassword("privatekeypassword".toCharArray())
.build();

assertThat(config.getAddress(), is("mqtts://localhost:8883"));
assertThat(config.getClientIdPrefix(), is("client_id_prefix"));
assertThat(config.getConnectionTimeout(), is(5));
assertThat(config.getKeepAlive(), is(10));
Expand All @@ -57,6 +72,50 @@ public void shouldBuildConfigWithCustomValues() {
assertThat(willMessage.getTopicName(), is("topic"));
assertThat(willMessage.getQos(), is(QoS.AT_MOST_ONCE));
assertThat(willMessage.getApplicationMessage(), is(new byte[] {1, 2, 3}));

SecurityParams securityParams = config.getSecurityParams();
assertThat(securityParams, notNullValue());
assertThat(securityParams.getTruststorePath(), is(Paths.get("truststore.jks")));
assertThat(securityParams.getTruststorePassword(), is("truststorepassword".toCharArray()));
assertThat(securityParams.getKeystorePath(), is(Paths.get("keystore.jks")));
assertThat(securityParams.getKeystorePassword(), is("keystorepassword".toCharArray()));
assertThat(securityParams.getPrivateKeyPassword(), is("privatekeypassword".toCharArray()));
}

@Test
public void shouldReturnSecurityParamsWhenProvidingSecurityProtocol() {
MqttBrokerConfig config = builder.securityProtocol("TLSV1.2").build();
SecurityParams secParams = config.getSecurityParams();
assertThat(secParams, notNullValue());
assertThat(secParams.getSecurityProtocol(), is("TLSV1.2"));
}

@Test
public void shouldReturnSecurityParamsWhenProvidingTruststore() {
MqttBrokerConfig config = builder.truststorePath(Paths.get("truststore.jks")).build();
SecurityParams secParams = config.getSecurityParams();
assertThat(secParams, notNullValue());
assertThat(secParams.getTruststorePath(), is(Paths.get("truststore.jks")));
}

@Test
public void shouldReturnSecurityParamsWhenProvidingKeystore() {
MqttBrokerConfig config = builder.keystorePath(Paths.get("keystore.jks")).build();
SecurityParams secParams = config.getSecurityParams();
assertThat(secParams, notNullValue());
assertThat(secParams.getKeystorePath(), is(Paths.get("keystore.jks")));
}

@Test
public void shouldNotReturnSecurityParams() {
MqttBrokerConfig config = builder.securityProtocol("").build();
assertThat(config.getSecurityParams(), nullValue());

config = builder.keystorePassword("keystorepassword".toCharArray()).build();
assertThat(config.getSecurityParams(), nullValue());

config = builder.privateKeyPassword("privatekeypassword".toCharArray()).build();
assertThat(config.getSecurityParams(), nullValue());
}

@Test
Expand Down

0 comments on commit 8cfcacd

Please sign in to comment.