2929import static org .thingsboard .server .transport .lwm2m .Lwm2mTestHelper .RESOURCE_ID_2 ;
3030import static org .thingsboard .server .transport .lwm2m .Lwm2mTestHelper .RESOURCE_ID_3 ;
3131import static org .thingsboard .server .transport .lwm2m .Lwm2mTestHelper .RESOURCE_ID_4 ;
32+ import static org .thingsboard .server .transport .lwm2m .Lwm2mTestHelper .RESOURCE_ID_5 ;
3233import static org .thingsboard .server .transport .lwm2m .Lwm2mTestHelper .RESOURCE_ID_8 ;
3334import static org .thingsboard .server .transport .lwm2m .Lwm2mTestHelper .RESOURCE_ID_9 ;
3435
@@ -90,26 +91,101 @@ public void testExecuteRegistrationUpdateTriggerById_Result_CHANGED() throws Exc
9091
9192
9293 /**
93- * execute_resource_with_parameters (execute reboot after 60 seconds on device)
94- * Execute {"id":"3/0/4","value":60 }
94+ * execute_resource_with_parameters (execute reboot if digit = 5 on device)
95+ * Execute {"id":"3/0/4","value":5 }
9596 * {"result":"CHANGED"}
9697 */
9798 @ Test
98- public void testExecuteResourceWithParametersById_Result_CHANGED () throws Exception {
99+ public void testExecuteResourceWithParametersSingleDigitValueById_Result_Ok () throws Exception {
99100 String expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_4 ;
100- Object expectedValue = 60 ;
101+ Object expectedValue = 5 ;
101102 String actualResult = sendRPCExecuteWithValueById (expectedPath , expectedValue );
102103 ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
103104 assertEquals (ResponseCode .CHANGED .getName (), rpcActualResult .get ("result" ).asText ());
104105 }
105106
107+ /**
108+ * execute_resource_with_parameters (execute Factory Reset if digit = 2 -> after 60 seconds on device)
109+ * Execute {"id":"3/0/5","value":"2='60'"}
110+
111+ */
112+ @ Test
113+ public void testExecuteResourceWithParametersArgumentIdAndValueById_Result_Ok () throws Exception {
114+ String expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_5 ;
115+ Object expectedValue = "2='60'" ;
116+ String actualResult = sendRPCExecuteWithValueById (expectedPath , expectedValue );
117+ ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
118+ assertEquals (ResponseCode .CHANGED .getName (), rpcActualResult .get ("result" ).asText ());
119+ }
120+
121+ /**
122+ * execute_resource_with_parameters (execute Factory Reset with two arguments:
123+ * digit 2 without a value and digit 0 with the link value on device)
124+ * Execute {"id":"3/0/5","value":"2,0='https://thingsboard.io/docs/reference/lwm2m-api/'"}
125+ */
126+ @ Test
127+ public void testExecuteResourceWithParametersMultipleArgumentsIncludingLinkById_Result_Ok () throws Exception {
128+ String expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_5 ;
129+ Object expectedValue = "2,0='https://thingsboard.io/docs/reference/lwm2m-api/'" ;
130+ String actualResult = sendRPCExecuteWithValueById (expectedPath , expectedValue );
131+ ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
132+ assertEquals (ResponseCode .CHANGED .getName (), rpcActualResult .get ("result" ).asText ());
133+ }
134+
135+ /**
136+ * execute_resource_with_parameters (execute Factory Reset with multiple arguments without values)
137+ * According to the OMA LwM2M execute arguments format, this represents ten arguments (digits 0-9), all without values.
138+ * Execute {"id":"3/0/5","value":"0,1,2,3,4,5,6,7,8,9"}
139+ */
140+ @ Test
141+ public void testExecuteResourceWithParametersMultipleArgumentsById_Result_Ok () throws Exception {
142+ String expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_5 ;
143+ Object expectedValue = "0,1,2,3,4,5,6,7,8,9" ;
144+ String actualResult = sendRPCExecuteWithValueById (expectedPath , expectedValue );
145+ ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
146+ assertEquals (ResponseCode .CHANGED .getName (), rpcActualResult .get ("result" ).asText ());
147+ }
148+
149+
150+ /**
151+ * execute_resource_with_parameters (execute Factory Reset after 60 seconds on device)
152+ * Execute {"id":"3/0/5","value":"'60'"}
153+ */
154+ @ Test
155+ public void testExecuteResourceWithParametersSingleDigitValueInvalidById_Result_BAD_REQUEST_Error_IntegerBetween_0_And_9_Expected () throws Exception {
156+ String expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_5 ;
157+ Object expectedValue = "'60'" ;
158+ String actualResult = sendRPCExecuteWithValueById (expectedPath , expectedValue );
159+ ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
160+ assertEquals (ResponseCode .BAD_REQUEST .getName (), rpcActualResult .get ("result" ).asText ());
161+ String expected = "Unable to parse Arguments [" + expectedValue + "] : Invalid digit ['] (an integer between 0 and 9 is expected)" ;
162+ String actual = rpcActualResult .get ("error" ).asText ();
163+ assertTrue (actual .contains (expected ));
164+ }
165+
166+ /**
167+ * execute_resource_with_parameters (execute Bad with Unable to parse Arguments)
168+ * Execute {"id":"3/0/5","value":"0,1,2,3,4,5,6,7,8,9,60"}
169+ */
170+ @ Test
171+ public void testExecuteResourceWithParametersMultipleArgumentsById_Result_BAD_REQUEST_Error_UnableParseArguments () throws Exception {
172+ String expectedPath = objectInstanceIdVer_3 + "/" + RESOURCE_ID_5 ;
173+ Object expectedValue = "0,1,2,3,4,5,6,7,8,9,60" ;
174+ String actualResult = sendRPCExecuteWithValueById (expectedPath , expectedValue );;
175+ ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
176+ assertEquals (ResponseCode .BAD_REQUEST .getName (), rpcActualResult .get ("result" ).asText ());
177+ String expected = "Unable to parse Arguments [" + expectedValue + "] : [,] separator expected at index 21 after [0,1,2,3,4,5,6,7,8,9,6]" ;
178+ String actual = rpcActualResult .get ("error" ).asText ();
179+ assertTrue (actual .contains (expected ));
180+ }
181+
106182 /**
107183 * Bootstrap-Request Trigger
108184 * Execute {"id":"1/0/9"}
109185 * {"result":"BAD_REQUEST","error":"probably no bootstrap server configured"}
110186 */
111187 @ Test
112- public void testExecuteBootstrapRequestTriggerById_Result_BAD_REQUEST_Error_NoBootstrapServerConfigured () throws Exception {
188+ public void testExecuteBootstrapRequestTriggerById_Result_BAD_REQUEST_Error_NoBootstrapServer () throws Exception {
113189 String expectedPath = objectInstanceIdVer_1 + "/" + RESOURCE_ID_9 ;
114190 String actualResult = sendRPCExecuteById (expectedPath );
115191 ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
@@ -125,7 +201,7 @@ public void testExecuteBootstrapRequestTriggerById_Result_BAD_REQUEST_Error_NoBo
125201 * {"result":"BAD_REQUEST","error":"Resource with /5_1.0/0/3 is not executable."}
126202 */
127203 @ Test
128- public void testExecuteResourceWithOperationNotExecuteById_Result_METHOD_NOT_ALLOWED () throws Exception {
204+ public void testExecuteResourceWithOperationNotExecuteById_Result_BAD_REQUEST_Error_Is_Not_Executable () throws Exception {
129205 String expectedPath = objectInstanceIdVer_5 + "/" + RESOURCE_ID_3 ;
130206 String actualResult = sendRPCExecuteById (expectedPath );
131207 ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
@@ -141,7 +217,7 @@ public void testExecuteResourceWithOperationNotExecuteById_Result_METHOD_NOT_ALL
141217 * {"result":"BAD_REQUEST","error":"Specified object id 50 absent in the list supported objects of the client or is security object!"}
142218 */
143219 @ Test
144- public void testExecuteNonExistingResourceOnNonExistingObjectById_Result_BAD_REQUEST () throws Exception {
220+ public void testExecuteNonExistingResourceOnNonExistingObjectById_Result_BAD_REQUEST_Error_Specified_Object_Absent_List_Supported () throws Exception {
145221 String expectedPath = OBJECT_ID_VER_50 + "/" + OBJECT_INSTANCE_ID_0 + "/" + RESOURCE_ID_3 ;
146222 String actualResult = sendRPCExecuteById (expectedPath );
147223 ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
@@ -159,7 +235,7 @@ public void testExecuteNonExistingResourceOnNonExistingObjectById_Result_BAD_REQ
159235 * {"result":"BAD_REQUEST","error":"Specified object id 0 absent in the list supported objects of the client or is security object!"}
160236 */
161237 @ Test
162- public void testExecuteSecurityObjectById_Result_NOT_FOUND () throws Exception {
238+ public void testExecuteSecurityObjectById_Result_BAD_REQUEST_Error_SpecifiedObjectAbsent () throws Exception {
163239 String expectedPath = objectIdVer_0 + "/" + OBJECT_INSTANCE_ID_0 + "/" + RESOURCE_ID_3 ;
164240 String actualResult = sendRPCExecuteById (expectedPath );
165241 ObjectNode rpcActualResult = JacksonUtil .fromString (actualResult , ObjectNode .class );
@@ -178,8 +254,26 @@ private String sendRPCExecuteById(String path) throws Exception {
178254 }
179255
180256 private String sendRPCExecuteWithValueById (String path , Object value ) throws Exception {
181- String setRpcRequest = "{\" method\" : \" Execute\" , \" params\" : {\" id\" : \" " + path + "\" , \" value\" : " + value + " }}" ;
182- return doPostAsync ("/api/plugins/rpc/twoway/" + lwM2MTestClient .getDeviceIdStr (), setRpcRequest , String .class , status ().isOk ());
257+ ObjectNode params = JacksonUtil .newObjectNode ();
258+ params .put ("id" , path );
259+
260+ // Jackson сам вирішить: ставити лапки (рядок) чи ні (число/boolean/null)
261+ if (value instanceof String ) {
262+ params .put ("value" , (String ) value );
263+ } else if (value instanceof Integer ) {
264+ params .put ("value" , (Integer ) value );
265+ } else if (value instanceof Boolean ) {
266+ params .put ("value" , (Boolean ) value );
267+ } else {
268+ params .set ("value" , JacksonUtil .valueToTree (value ));
269+ }
270+
271+ ObjectNode setRpcRequest = JacksonUtil .newObjectNode ();
272+ setRpcRequest .put ("method" , "Execute" );
273+ setRpcRequest .set ("params" , params );
274+
275+ return doPostAsync ("/api/plugins/rpc/twoway/" + lwM2MTestClient .getDeviceIdStr (),
276+ JacksonUtil .toString (setRpcRequest ), String .class , status ().isOk ());
183277 }
184278
185279}
0 commit comments