From d769f6749c773b99a077fccd5f333124d4be6e4b Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Mon, 14 Jun 2021 15:19:44 +0300 Subject: [PATCH] updated deviceProfile proto transport configuration validation tests --- .../server/controller/AbstractWebTest.java | 8 +- .../BaseDeviceProfileControllerTest.java | 162 ++++++++++++++++-- .../controller/ControllerSqlTestSuite.java | 1 + .../dao/device/DeviceProfileServiceImpl.java | 2 +- 4 files changed, 155 insertions(+), 18 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java b/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java index 937244fc93..7be184e036 100644 --- a/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java @@ -352,10 +352,12 @@ public abstract class AbstractWebTest { return mqttDeviceProfileTransportConfiguration; } - protected ProtoTransportPayloadConfiguration createProtoTransportPayloadConfiguration(String deviceAttributesProtoSchema, String deviceTelemetryProtoSchema) { + protected ProtoTransportPayloadConfiguration createProtoTransportPayloadConfiguration(String attributesProtoSchema, String telemetryProtoSchema, String rpcRequestProtoSchema, String rpcResponseProtoSchema) { ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = new ProtoTransportPayloadConfiguration(); - protoTransportPayloadConfiguration.setDeviceAttributesProtoSchema(deviceAttributesProtoSchema); - protoTransportPayloadConfiguration.setDeviceTelemetryProtoSchema(deviceTelemetryProtoSchema); + protoTransportPayloadConfiguration.setDeviceAttributesProtoSchema(attributesProtoSchema); + protoTransportPayloadConfiguration.setDeviceTelemetryProtoSchema(telemetryProtoSchema); + protoTransportPayloadConfiguration.setDeviceRpcRequestProtoSchema(rpcRequestProtoSchema); + protoTransportPayloadConfiguration.setDeviceRpcResponseProtoSchema(rpcResponseProtoSchema); return protoTransportPayloadConfiguration; } diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java index 43989ea9cf..937283b31c 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java @@ -364,7 +364,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "package schemavalidation;\n" + "\n" + "message SchemaValidationTest {\n" + - " int32 parameter = 1;\n" + + " optional int32 parameter = 1;\n" + "}", "[Transport Configuration] invalid attributes proto schema provided! Schema options don't support!"); } @@ -377,7 +377,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "package schemavalidation;\n" + "\n" + "message SchemaValidationTest {\n" + - " int32 parameter = 1;\n" + + " optional int32 parameter = 1;\n" + "}", "[Transport Configuration] invalid attributes proto schema provided! Schema public imports don't support!"); } @@ -390,7 +390,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "package schemavalidation;\n" + "\n" + "message SchemaValidationTest {\n" + - " int32 parameter = 1;\n" + + " optional int32 parameter = 1;\n" + "}", "[Transport Configuration] invalid attributes proto schema provided! Schema imports don't support!"); } @@ -419,7 +419,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "}\n" + "\n" + "message testMessage {\n" + - " int32 parameter = 1;\n" + + " optional int32 parameter = 1;\n" + "}", "[Transport Configuration] invalid attributes proto schema provided! Enum definitions options are not supported!"); } @@ -444,7 +444,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "\n" + "message testMessage {\n" + " option allow_alias = true;\n" + - " int32 parameter = 1;\n" + + " optional int32 parameter = 1;\n" + "}", "[Transport Configuration] invalid attributes proto schema provided! Message definition options don't support!"); } @@ -479,8 +479,8 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "\n" + "message TestMessage {\n" + " repeated group Result = 1 {\n" + - " string url = 2;\n" + - " string title = 3;\n" + + " optional string url = 2;\n" + + " optional string title = 3;\n" + " repeated string snippets = 4;\n" + " }\n" + "}", "[Transport Configuration] invalid attributes proto schema provided! Message definition groups don't support!"); @@ -513,15 +513,15 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "message Outer {\n" + " message MiddleAA {\n" + " message Inner {\n" + - " int64 ival = 1;\n" + - " bool booly = 2;\n" + + " optional int64 ival = 1;\n" + + " optional bool booly = 2;\n" + " }\n" + " Inner inner = 1;\n" + " }\n" + " message MiddleBB {\n" + " message Inner {\n" + - " int32 ival = 1;\n" + - " bool booly = 2;\n" + + " optional int32 ival = 1;\n" + + " optional bool booly = 2;\n" + " }\n" + " Inner inner = 1;\n" + " }\n" + @@ -599,6 +599,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "}\n" + "\n" + "message SampleMessage {\n" + + " optional int32 id = 1;\n" + " oneof testOneOf {\n" + " string name = 4;\n" + " SubMessage subMessage = 9;\n" + @@ -616,7 +617,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController assertNotNull(sampleMsgDescriptor); List fields = sampleMsgDescriptor.getFields(); - assertEquals(2, fields.size()); + assertEquals(3, fields.size()); DynamicMessage sampleMsg = sampleMsgBuilder .setField(sampleMsgDescriptor.findFieldByName("name"), "Bob") .build(); @@ -700,8 +701,133 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController "}", "[Transport Configuration] invalid telemetry proto schema provided! Field 'values' has invalid data type. Only message type is supported!"); } + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaMethodDateType() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional int32 method = 1;\n" + + " optional int32 requestId = 2;\n" + + " optional string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Field 'method' has invalid data type. Only string type is supported!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaRequestIdDateType() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional string method = 1;\n" + + " optional int64 requestId = 2;\n" + + " optional string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Field 'requestId' has invalid data type. Only int32 type is supported!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaMethodLabel() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " repeated string method = 1;\n" + + " optional int32 requestId = 2;\n" + + " optional string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Field 'method' has invalid label!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaRequestIdLabel() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional string method = 1;\n" + + " repeated int32 requestId = 2;\n" + + " optional string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Field 'requestId' has invalid label!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaParamsLabel() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional string method = 1;\n" + + " optional int32 requestId = 2;\n" + + " repeated string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Field 'params' has invalid label!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaFieldsCount() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional int32 requestId = 2;\n" + + " repeated string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! RpcRequestMsg message should always contains 3 fields: method, requestId and params!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaFieldMethodIsNoSet() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional string methodName = 1;\n" + + " optional int32 requestId = 2;\n" + + " repeated string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Failed to get field descriptor for field: method!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaFieldRequestIdIsNotSet() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional string method = 1;\n" + + " optional int32 requestIdentifier = 2;\n" + + " repeated string params = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Failed to get field descriptor for field: requestId!"); + } + + @Test + public void testSaveProtoDeviceProfileWithInvalidRpcRequestSchemaFieldParamsIsNotSet() throws Exception { + testSaveDeviceProfileWithInvalidRpcRequestProtoSchema("syntax =\"proto3\";\n" + + "\n" + + "package schemavalidation;\n" + + "\n" + + "message RpcRequestMsg {\n" + + " optional string method = 1;\n" + + " optional int32 requestId = 2;\n" + + " repeated string parameters = 3;\n" + + " \n" + + "}", "[Transport Configuration] invalid rpc request proto schema provided! Failed to get field descriptor for field: params!"); + } + private DeviceProfile testSaveDeviceProfileWithProtoPayloadType(String schema) throws Exception { - ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = this.createProtoTransportPayloadConfiguration(schema, schema); + ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = this.createProtoTransportPayloadConfiguration(schema, schema, null, null); MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = this.createMqttDeviceProfileTransportConfiguration(protoTransportPayloadConfiguration); DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", mqttDeviceProfileTransportConfiguration); DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); @@ -711,7 +837,15 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController } private void testSaveDeviceProfileWithInvalidProtoSchema(String schema, String errorMsg) throws Exception { - ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = this.createProtoTransportPayloadConfiguration(schema, schema); + ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = this.createProtoTransportPayloadConfiguration(schema, schema, null, null); + MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = this.createMqttDeviceProfileTransportConfiguration(protoTransportPayloadConfiguration); + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", mqttDeviceProfileTransportConfiguration); + doPost("/api/deviceProfile", deviceProfile).andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(errorMsg))); + } + + private void testSaveDeviceProfileWithInvalidRpcRequestProtoSchema(String schema, String errorMsg) throws Exception { + ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = this.createProtoTransportPayloadConfiguration(schema, schema, schema, null); MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = this.createMqttDeviceProfileTransportConfiguration(protoTransportPayloadConfiguration); DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", mqttDeviceProfileTransportConfiguration); doPost("/api/deviceProfile", deviceProfile).andExpect(status().isBadRequest()) diff --git a/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java index 653f6978f6..e30ef07b5b 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/controller/ControllerSqlTestSuite.java @@ -29,6 +29,7 @@ import java.util.Arrays; // "org.thingsboard.server.controller.sql.WebsocketApiSqlTest", // "org.thingsboard.server.controller.sql.EntityQueryControllerSqlTest", // "org.thingsboard.server.controller.sql.TbResourceControllerSqlTest", +// "org.thingsboard.server.controller.sql.DeviceProfileControllerSqlTest", "org.thingsboard.server.controller.sql.*Test", }) public class ControllerSqlTestSuite { diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java index c7c59ab1f1..3979203c26 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java @@ -675,7 +675,7 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D throw new DataValidationException(invalidSchemaProvidedMessage(RPC_REQUEST_PROTO_SCHEMA) + " Failed to get field descriptor for field: params!"); } else { if (paramsFieldDescriptor.isRepeated()) { - throw new DataValidationException(invalidSchemaProvidedMessage(RPC_REQUEST_PROTO_SCHEMA) + "Field 'params' has invalid label!"); + throw new DataValidationException(invalidSchemaProvidedMessage(RPC_REQUEST_PROTO_SCHEMA) + " Field 'params' has invalid label!"); } } }