Browse Source

added validation on init

pull/11520/head
IrynaMatveieva 2 years ago
parent
commit
b56f75a996
  1. 25
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/aws/lambda/TbAwsLambdaNode.java
  2. 72
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/aws/lambda/TbAwsLambdaNodeTest.java

25
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/aws/lambda/TbAwsLambdaNode.java

@ -62,9 +62,7 @@ public class TbAwsLambdaNode extends TbAbstractExternalNode {
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
config = TbNodeUtils.convert(configuration, TbAwsLambdaNodeConfiguration.class);
if (StringUtils.isBlank(config.getFunctionName())) {
throw new TbNodeException("Function name must be set!", true);
}
validateConfig();
try {
AWSCredentials awsCredentials = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
client = AWSLambdaAsyncClientBuilder.standard()
@ -138,6 +136,27 @@ public class TbAwsLambdaNode extends TbAbstractExternalNode {
return TbMsg.transformMsgMetadata(origMsg, metaData);
}
private void validateConfig() throws TbNodeException {
if (StringUtils.isBlank(config.getFunctionName())) {
throw new TbNodeException("Function name must be set!", true);
}
if (StringUtils.isBlank(config.getAccessKey())) {
throw new TbNodeException("Access Key must be set!", true);
}
if (StringUtils.isBlank(config.getSecretKey())) {
throw new TbNodeException("Secret Access Key must be set!", true);
}
if (StringUtils.isBlank(config.getRegion())) {
throw new TbNodeException("Region must be set!", true);
}
if (config.getConnectionTimeout() < 0) {
throw new TbNodeException("Min connection timeout is 0!", true);
}
if (config.getRequestTimeout() < 0) {
throw new TbNodeException("Min request timeout is 0!", true);
}
}
@Override
public void destroy() {
if (client != null) {

72
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/aws/lambda/TbAwsLambdaNodeTest.java

@ -75,10 +75,14 @@ public class TbAwsLambdaNodeTest {
void setUp() {
node = new TbAwsLambdaNode();
config = new TbAwsLambdaNodeConfiguration().defaultConfiguration();
config.setAccessKey("accessKey");
config.setSecretKey("secretKey");
config.setFunctionName("new-function");
}
@Test
public void verifyDefaultConfig() {
config = new TbAwsLambdaNodeConfiguration().defaultConfiguration();
assertThat(config.getAccessKey()).isNull();
assertThat(config.getSecretKey()).isNull();
assertThat(config.getRegion()).isEqualTo(("us-east-1"));
@ -97,7 +101,70 @@ public class TbAwsLambdaNodeTest {
var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, configuration))
.isInstanceOf(TbNodeException.class)
.hasMessage("Function name must be set!");
.hasMessage("Function name must be set!")
.extracting(e -> ((TbNodeException) e).isUnrecoverable())
.isEqualTo(true);
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = " ")
public void givenInvalidAccessKey_whenInit_thenThrowsException(String accessKey) {
config.setAccessKey(accessKey);
var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, configuration))
.isInstanceOf(TbNodeException.class)
.hasMessage("Access Key must be set!")
.extracting(e -> ((TbNodeException) e).isUnrecoverable())
.isEqualTo(true);
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = " ")
public void givenInvalidSecretAccessKey_whenInit_thenThrowsException(String secretAccessKey) {
config.setSecretKey(secretAccessKey);
var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, configuration))
.isInstanceOf(TbNodeException.class)
.hasMessage("Secret Access Key must be set!")
.extracting(e -> ((TbNodeException) e).isUnrecoverable())
.isEqualTo(true);
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = " ")
public void givenInvalidRegion_whenInit_thenThrowsException(String region) {
config.setRegion(region);
var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, configuration))
.isInstanceOf(TbNodeException.class)
.hasMessage("Region must be set!")
.extracting(e -> ((TbNodeException) e).isUnrecoverable())
.isEqualTo(true);
}
@Test
public void givenInvalidConnectionTimeout_whenInit_thenThrowsException() {
config.setConnectionTimeout(-100);
var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, configuration))
.isInstanceOf(TbNodeException.class)
.hasMessage("Min connection timeout is 0!")
.extracting(e -> ((TbNodeException) e).isUnrecoverable())
.isEqualTo(true);
}
@Test
public void givenInvalidRequestTimeout_whenInit_thenThrowsException() {
config.setRequestTimeout(-100);
var configuration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, configuration))
.isInstanceOf(TbNodeException.class)
.hasMessage("Min request timeout is 0!")
.extracting(e -> ((TbNodeException) e).isUnrecoverable())
.isEqualTo(true);
}
@ParameterizedTest
@ -281,9 +348,6 @@ public class TbAwsLambdaNodeTest {
}
private void init() {
config.setAccessKey("accessKey");
config.setSecretKey("secretKey");
config.setFunctionName("new-function");
ReflectionTestUtils.setField(node, "client", clientMock);
ReflectionTestUtils.setField(node, "config", config);
}

Loading…
Cancel
Save