2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base;
25 import android.test.InstrumentationTestCase;
27 import java.security.InvalidParameterException;
28 import java.util.Arrays;
29 import java.util.LinkedList;
30 import java.util.List;
32 public class OcRepresentationTest extends InstrumentationTestCase {
34 private static final String TAG = "OcRepresentationTest";
37 protected void setUp() throws Exception {
41 public void testChildrenManagement() throws OcException {
42 OcRepresentation representation = new OcRepresentation();
44 List<OcRepresentation> emptyList = representation.getChildren();
45 assertTrue(emptyList.isEmpty());
47 OcRepresentation child1 = new OcRepresentation();
48 OcRepresentation child2 = new OcRepresentation();
52 child1.setValue(key, value);
53 child2.setValue(key, value);
54 representation.addChild(child1);
55 representation.addChild(child2);
56 List<OcRepresentation> twoChildren = representation.getChildren();
57 assertEquals(2, twoChildren.size());
58 for (OcRepresentation rep : twoChildren) {
59 assertEquals(value, rep.getValue(key));
62 representation.clearChildren();
63 emptyList = representation.getChildren();
64 assertTrue(emptyList.isEmpty());
67 public void testUriGetSet() {
68 OcRepresentation representation = new OcRepresentation();
70 String emptyUri = representation.getUri();
71 assertTrue(emptyUri.isEmpty());
73 String expected = "a/resource/uri";
74 representation.setUri(expected);
75 String actual = representation.getUri();
76 assertEquals(expected, actual);
79 public void testResourceTypesGetSet() {
80 OcRepresentation representation = new OcRepresentation();
82 List<String> emptyResourceTypeList = representation.getResourceTypes();
83 assertTrue(emptyResourceTypeList.isEmpty());
85 representation.setResourceTypes(emptyResourceTypeList);
86 emptyResourceTypeList = representation.getResourceTypes();
87 assertTrue(emptyResourceTypeList.isEmpty());
89 List<String> resourceTypeListExpected = new LinkedList<String>();
90 resourceTypeListExpected.add("type1");
91 resourceTypeListExpected.add("type2");
92 resourceTypeListExpected.add("type3");
94 representation.setResourceTypes(resourceTypeListExpected);
95 List<String> resourceTypeListActual = representation.getResourceTypes();
96 assertEquals(resourceTypeListExpected.size(), resourceTypeListActual.size());
97 for (int i = 0; i < resourceTypeListExpected.size(); i++) {
98 assertEquals(resourceTypeListExpected.get(i), resourceTypeListActual.get(i));
101 boolean thrown = false;
103 representation.setResourceTypes(null);
104 } catch (InvalidParameterException e) {
110 public void testResourceInterfacesGetSet() {
111 OcRepresentation representation = new OcRepresentation();
113 List<String> emptyResourceInterfaceList = representation.getResourceInterfaces();
114 assertTrue(emptyResourceInterfaceList.isEmpty());
116 representation.setResourceInterfaces(emptyResourceInterfaceList);
117 emptyResourceInterfaceList = representation.getResourceInterfaces();
118 assertTrue(emptyResourceInterfaceList.isEmpty());
120 List<String> resourceInterfaceListExpected = new LinkedList<String>();
121 resourceInterfaceListExpected.add("Interface1");
122 resourceInterfaceListExpected.add("Interface2");
123 resourceInterfaceListExpected.add("Interface3");
125 representation.setResourceInterfaces(resourceInterfaceListExpected);
126 List<String> resourceInterfaceListActual = representation.getResourceInterfaces();
127 assertEquals(resourceInterfaceListExpected.size(), resourceInterfaceListActual.size());
128 for (int i = 0; i < resourceInterfaceListExpected.size(); i++) {
129 assertEquals(resourceInterfaceListExpected.get(i), resourceInterfaceListActual.get(i));
132 boolean thrown = false;
134 representation.setResourceInterfaces(null);
135 } catch (InvalidParameterException e) {
141 public void testAttributeManagement() {
142 OcRepresentation representation = new OcRepresentation();
144 assertTrue(representation.isEmpty());
145 assertEquals(0, representation.size());
148 String integerKey = "integerKey";
149 int integerValue = 75;
150 representation.setValue(integerKey, integerValue);
151 assertFalse(representation.isEmpty());
152 assertEquals(1, representation.size());
154 int actualIntValue = representation.getValue(integerKey);
155 assertEquals(integerValue, actualIntValue);
157 String stringKey = "stringKey";
158 String stringValue = "stringValue";
159 representation.setValue(stringKey, stringValue);
160 assertEquals(2, representation.size());
162 assertTrue(representation.hasAttribute(integerKey));
163 representation.remove(integerKey);
164 assertFalse(representation.hasAttribute(integerKey));
165 assertEquals(1, representation.size());
167 representation.setValue(integerKey, integerValue);
168 assertFalse(representation.isNull(integerKey));
169 representation.setNull(integerKey);
170 assertTrue(representation.isNull(integerKey));
171 } catch (OcException e) {
175 String nonexistentKey = "nonexistentKey";
176 assertFalse(representation.hasAttribute(nonexistentKey));
177 representation.setNull(nonexistentKey);
178 assertTrue(representation.isNull(nonexistentKey));
180 String nonexistentKey2 = "nonexistentKey2";
181 boolean thrown = false;
183 boolean nonexistentValue = representation.getValue(nonexistentKey2);
184 } catch (OcException e) {
190 public void testAttributeAccessByType() throws OcException {
191 OcRepresentation rep = new OcRepresentation();
194 String intK = "intK";
196 rep.setValue(intK, intV);
197 int intVa = rep.getValue(intK);
198 assertEquals(intV, intVa);
201 String doubleK = "doubleK";
202 double doubleV = 4.5;
203 rep.setValue(doubleK, doubleV);
204 double doubleVa = rep.getValue(doubleK);
205 assertEquals(doubleV, doubleVa);
208 String booleanK = "booleanK";
209 boolean booleanV = true;
210 rep.setValue(booleanK, booleanV);
211 boolean booleanVa = rep.getValue(booleanK);
212 assertEquals(booleanV, booleanVa);
215 String stringK = "stringK";
216 String stringV = "stringV";
217 rep.setValue(stringK, stringV);
218 String stringVa = rep.getValue(stringK);
219 assertEquals(stringV, stringVa);
222 String repK = "repK";
223 OcRepresentation repV = new OcRepresentation();
224 repV.setValue(intK, intV);
225 rep.setValue(repK, repV);
226 OcRepresentation repVa = rep.getValue(repK);
227 assertEquals(intV, repVa.getValue(intK));
230 public void testAttributeAccessBySequenceType() throws OcException {
231 OcRepresentation rep = new OcRepresentation();
234 String intK = "intK";
235 int[] intArrV = {1, 2, 3, 4};
236 rep.setValue(intK, intArrV);
237 int[] intArrVa = rep.getValue(intK);
238 assertTrue(Arrays.equals(intArrV, intArrVa));
240 int[] intArrVEmpty = {};
241 rep.setValue(intK, intArrVEmpty);
242 int[] intArrVEmptyA = rep.getValue(intK);
243 assertTrue(Arrays.equals(intArrVEmpty, intArrVEmptyA));
246 String doubleK = "doubleK";
247 double[] doubleArrV = {1.1, 2.2, 3.3, 4.4};
248 rep.setValue(doubleK, doubleArrV);
249 double[] doubleArrVa = rep.getValue(doubleK);
250 assertTrue(Arrays.equals(doubleArrV, doubleArrVa));
252 double[] doubleArrVEmpty = {};
253 rep.setValue(doubleK, doubleArrVEmpty);
254 double[] doubleArrVEmptyA = rep.getValue(doubleK);
255 assertTrue(Arrays.equals(doubleArrVEmpty, doubleArrVEmptyA));
258 String booleanK = "booleanK";
259 boolean[] booleanArrV = {true, false, true, false};
260 rep.setValue(booleanK, booleanArrV);
261 boolean[] booleanArrVa = rep.getValue(booleanK);
262 assertTrue(Arrays.equals(booleanArrV, booleanArrVa));
264 boolean[] booleanArrVEmpty = {};
265 rep.setValue(booleanK, booleanArrVEmpty);
266 boolean[] booleanArrVEmptyA = rep.getValue(booleanK);
267 assertTrue(Arrays.equals(booleanArrVEmpty, booleanArrVEmptyA));
270 String stringK = "stringK";
271 String[] stringArrV = {"aaa", "bbb", "ccc", "ddd"};
272 rep.setValue(stringK, stringArrV);
273 String[] stringArrVa = rep.getValue(stringK);
274 assertTrue(Arrays.equals(stringArrV, stringArrVa));
276 String[] stringArrVEmpty = {};
277 rep.setValue(stringK, stringArrVEmpty);
278 String[] stringArrVEmptyA = rep.getValue(stringK);
279 assertTrue(Arrays.equals(stringArrVEmpty, stringArrVEmptyA));
282 String representationK = "representationK";
283 OcRepresentation[] representationArrV = {
284 new OcRepresentation(),
285 new OcRepresentation(),
286 new OcRepresentation(),
287 new OcRepresentation()};
288 representationArrV[0].setValue(intK, 0);
289 representationArrV[1].setValue(intK, 1);
290 representationArrV[2].setValue(intK, 2);
291 representationArrV[3].setValue(intK, 3);
293 rep.setValue(representationK, representationArrV);
294 OcRepresentation[] representationArrVa = rep.getValue(representationK);
296 assertEquals(representationArrV.length, representationArrVa.length);
297 for (int i = 0; i < representationArrV.length; ++i) {
298 assertEquals(representationArrV[i].getValue(intK),
299 representationArrVa[i].getValue(intK));
302 OcRepresentation[] representationArrVEmpty = {};
303 rep.setValue(representationK, representationArrVEmpty);
304 OcRepresentation[] representationArrVEmptyA = rep.getValue(representationK);
305 assertEquals(representationArrVEmpty.length, representationArrVEmptyA.length);
308 public void testAttributeAccessBy2DType() throws OcException {
309 OcRepresentation rep = new OcRepresentation();
311 String int2DK = "int2DK";
312 int[] intArrV1 = {1, 2, 3, 4};
313 int[] intArrV2 = {5, 6, 7, 8};
314 int[][] int2DArrV = {intArrV1, intArrV2};
315 rep.setValue(int2DK, int2DArrV);
316 int[][] int2DArrVa = rep.getValue(int2DK);
317 for (int i = 0; i < int2DArrV.length; i++) {
318 assertTrue(Arrays.equals(int2DArrV[i], int2DArrVa[i]));
321 String double2DK = "double2DK";
322 double[] doubleArrV1 = {1.1, 2.2, 3.3, 4.4};
323 double[] doubleArrV2 = {5, 6, 7, 8};
324 double[][] double2DArrV = {doubleArrV1, doubleArrV2};
325 rep.setValue(double2DK, double2DArrV);
326 double[][] double2DArrVa = rep.getValue(double2DK);
327 for (int i = 0; i < double2DArrV.length; i++) {
328 assertTrue(Arrays.equals(double2DArrV[i], double2DArrVa[i]));
330 double[][] double2DArrVEmpty = {{}};
331 rep.setValue(double2DK, double2DArrVEmpty);
332 double[][] double2DArrVEmptyA = rep.getValue(double2DK);
333 for (int i = 0; i < double2DArrVEmpty.length; i++) {
334 assertTrue(Arrays.equals(double2DArrVEmpty[i], double2DArrVEmptyA[i]));
337 String boolean2DK = "boolean2DK";
338 boolean[] booleanArrV1 = {true, true, false};
339 boolean[] booleanArrV2 = {true, false, false, true};
340 boolean[][] boolean2DArrV = {booleanArrV1, booleanArrV2};
341 rep.setValue(boolean2DK, boolean2DArrV);
342 boolean[][] boolean2DArrVa = rep.getValue(boolean2DK);
343 for (int i = 0; i < boolean2DArrV.length; i++) {
344 assertTrue(Arrays.equals(boolean2DArrV[i], boolean2DArrVa[i]));
346 boolean[][] boolean2DArrVEmpty = {{}};
347 rep.setValue(boolean2DK, boolean2DArrVEmpty);
348 boolean[][] boolean2DArrVEmptyA = rep.getValue(boolean2DK);
349 for (int i = 0; i < boolean2DArrVEmpty.length; i++) {
350 assertTrue(Arrays.equals(boolean2DArrVEmpty[i], boolean2DArrVEmptyA[i]));
354 String string2DK = "string2DK";
355 String[] stringArrV1 = {"aaa", "bbb", "ccc"};
356 String[] stringArrV2 = {"111", "222", "333", "444"};
357 String[][] string2DArrV = {stringArrV1, stringArrV2};
358 rep.setValue(string2DK, string2DArrV);
359 String[][] string2DArrVa = rep.getValue(string2DK);
360 for (int i = 0; i < string2DArrV.length; i++) {
361 assertTrue(Arrays.equals(string2DArrV[i], string2DArrVa[i]));
363 String[][] string2DArrVEmpty = {{}};
364 rep.setValue(string2DK, string2DArrVEmpty);
365 String[][] string2DArrVEmptyA = rep.getValue(string2DK);
366 for (int i = 0; i < string2DArrVEmpty.length; i++) {
367 assertTrue(Arrays.equals(string2DArrVEmpty[i], string2DArrVEmptyA[i]));
371 String intK = "intK";
372 String representation2DK = "representation2DK";
373 OcRepresentation[] representation2DArrV1 = {
374 new OcRepresentation(),
375 new OcRepresentation(),
376 new OcRepresentation(),
377 new OcRepresentation()};
378 representation2DArrV1[0].setValue(intK, 0);
379 representation2DArrV1[1].setValue(intK, 1);
380 representation2DArrV1[2].setValue(intK, 2);
381 representation2DArrV1[3].setValue(intK, 3);
383 OcRepresentation[] representation2DArrV2 = {
384 new OcRepresentation(),
385 new OcRepresentation(),
386 new OcRepresentation(),
387 new OcRepresentation()};
388 representation2DArrV2[0].setValue(intK, 4);
389 representation2DArrV2[1].setValue(intK, 5);
390 representation2DArrV2[2].setValue(intK, 6);
391 representation2DArrV2[3].setValue(intK, 7);
393 OcRepresentation[][] representation2DArrV = {representation2DArrV1, representation2DArrV2};
394 rep.setValue(representation2DK, representation2DArrV);
395 OcRepresentation[][] representation2DArrVa = rep.getValue(representation2DK);
396 assertEquals(representation2DArrV.length, representation2DArrVa.length);
397 for (int i = 0; i < representation2DArrV.length; ++i) {
398 OcRepresentation[] repArrV = representation2DArrV[i];
399 OcRepresentation[] repArrVa = representation2DArrVa[i];
400 assertEquals(repArrV.length, repArrVa.length);
401 for (int j = 0; j < representation2DArrV.length; ++j) {
402 assertEquals(repArrV[j].getValue(intK),
403 repArrVa[j].getValue(intK));
407 OcRepresentation[][] representation2DArrVEmpty = {{}};
408 rep.setValue(representation2DK, representation2DArrVEmpty);
409 OcRepresentation[][] representation2DArrVEmptyA = rep.getValue(representation2DK);
410 assertEquals(representation2DArrVEmpty.length, representation2DArrVEmptyA.length);
413 public void testAttributeAccessBy3DType() throws OcException {
414 OcRepresentation rep = new OcRepresentation();
416 String int3DK = "int3DK";
417 int[] intArrV1 = {0, 1, 2, 3, 4};
418 int[] intArrV2 = {5, 6, 7, 8};
419 int[][] int2DArrV1 = {intArrV1, intArrV2};
420 int[] intArrV3 = {9, 10};
421 int[] intArrV4 = {11};
422 int[][] int2DArrV2 = {intArrV3, intArrV4};
423 int[][][] int3DArrV = {int2DArrV1, int2DArrV2};
424 rep.setValue(int3DK, int3DArrV);
425 int[][][] int3DArrVa = rep.getValue(int3DK);
426 assertEquals(int3DArrV.length, int3DArrVa.length);
427 for (int i = 0; i < int3DArrV.length; i++) {
428 int[][] int2DT = int3DArrV[i];
429 int[][] int2DTa = int3DArrVa[i];
430 assertEquals(int2DT.length, int2DTa.length);
431 for (int j = 0; j < int2DT.length; j++) {
432 assertTrue(Arrays.equals(int2DT[j], int2DTa[j]));
436 String double3DK = "double3DK";
437 double[] doubleArrV1 = {0.0, 1.1, 2.2, 3.3, 4.4};
438 double[] doubleArrV2 = {5.5, 6.6, 7.7, 8.8};
439 double[][] double2DArrV1 = {doubleArrV1, doubleArrV2};
440 double[] doubleArrV3 = {9.9, 10.1};
441 double[] doubleArrV4 = {11.1};
442 double[][] double2DArrV2 = {doubleArrV3, doubleArrV4};
443 double[][][] double3DArrV = {double2DArrV1, double2DArrV2};
444 rep.setValue(double3DK, double3DArrV);
445 double[][][] double3DArrVa = rep.getValue(double3DK);
446 assertEquals(double3DArrV.length, double3DArrVa.length);
447 for (int i = 0; i < double3DArrV.length; i++) {
448 double[][] double2DT = double3DArrV[i];
449 double[][] double2DTa = double3DArrVa[i];
450 assertEquals(double2DT.length, double2DTa.length);
451 for (int j = 0; j < double2DT.length; j++) {
452 assertTrue(Arrays.equals(double2DT[j], double2DTa[j]));
455 double[][][] double3DArrVEmpty = {};
456 rep.setValue(double3DK, double3DArrVEmpty);
457 double[][][] double3DArrVEmptyA = rep.getValue(double3DK);
458 assertEquals(double3DArrVEmpty.length, double3DArrVEmptyA.length);
459 for (int i = 0; i < double3DArrVEmpty.length; i++) {
460 double[][] double2DT = double3DArrVEmpty[i];
461 double[][] double2DTa = double3DArrVEmptyA[i];
462 assertEquals(double2DT.length, double2DTa.length);
463 for (int j = 0; j < double2DT.length; j++) {
464 assertTrue(Arrays.equals(double2DT[j], double2DTa[j]));
469 String boolean3DK = "boolean3DK";
470 boolean[] booleanArrV1 = {true, false, true, true, false};
471 boolean[] booleanArrV2 = {false, false, false, true};
472 boolean[][] boolean2DArrV1 = {booleanArrV1, booleanArrV2};
473 boolean[] booleanArrV3 = {true, true};
474 boolean[] booleanArrV4 = {false};
475 boolean[][] boolean2DArrV2 = {booleanArrV3, booleanArrV4};
476 boolean[][][] boolean3DArrV = {boolean2DArrV1, boolean2DArrV2};
477 rep.setValue(boolean3DK, boolean3DArrV);
478 boolean[][][] boolean3DArrVa = rep.getValue(boolean3DK);
479 assertEquals(boolean3DArrV.length, boolean3DArrVa.length);
480 for (int i = 0; i < boolean3DArrV.length; i++) {
481 boolean[][] boolean2DT = boolean3DArrV[i];
482 boolean[][] boolean2DTa = boolean3DArrVa[i];
483 assertEquals(boolean2DT.length, boolean2DTa.length);
484 for (int j = 0; j < boolean2DT.length; j++) {
485 assertTrue(Arrays.equals(boolean2DT[j], boolean2DTa[j]));
488 boolean[][][] boolean3DArrVEmpty = {};
489 rep.setValue(boolean3DK, boolean3DArrVEmpty);
490 boolean[][][] boolean3DArrVEmptyA = rep.getValue(boolean3DK);
491 assertEquals(boolean3DArrVEmpty.length, boolean3DArrVEmptyA.length);
492 for (int i = 0; i < boolean3DArrVEmpty.length; i++) {
493 boolean[][] boolean2DT = boolean3DArrVEmpty[i];
494 boolean[][] boolean2DTa = boolean3DArrVEmptyA[i];
495 assertEquals(boolean2DT.length, boolean2DTa.length);
496 for (int j = 0; j < boolean2DT.length; j++) {
497 assertTrue(Arrays.equals(boolean2DT[j], boolean2DTa[j]));
502 String string3DK = "string3DK";
503 String[] stringArrV1 = {"a", "bb", "ccc", "dddd", "eeee"};
504 String[] stringArrV2 = {"f", "gg", "hhh", "ii"};
505 String[][] string2DArrV1 = {stringArrV1, stringArrV2};
506 String[] stringArrV3 = {"j", "jj"};
507 String[] stringArrV4 = {"jjj"};
508 String[][] string2DArrV2 = {stringArrV3, stringArrV4};
509 String[][][] string3DArrV = {string2DArrV1, string2DArrV2};
510 rep.setValue(string3DK, string3DArrV);
511 String[][][] string3DArrVa = rep.getValue(string3DK);
512 assertEquals(string3DArrV.length, string3DArrVa.length);
513 for (int i = 0; i < string3DArrV.length; i++) {
514 String[][] string2DT = string3DArrV[i];
515 String[][] string2DTa = string3DArrVa[i];
516 assertEquals(string2DT.length, string2DTa.length);
517 for (int j = 0; j < string2DT.length; j++) {
518 assertTrue(Arrays.equals(string2DT[j], string2DTa[j]));
521 String[][][] string3DArrVEmpty = {};
522 rep.setValue(string3DK, string3DArrVEmpty);
523 String[][][] string3DArrVEmptyA = rep.getValue(string3DK);
524 assertEquals(string3DArrVEmpty.length, string3DArrVEmptyA.length);
525 for (int i = 0; i < string3DArrVEmpty.length; i++) {
526 String[][] string2DT = string3DArrVEmpty[i];
527 String[][] string2DTa = string3DArrVEmptyA[i];
528 assertEquals(string2DT.length, string2DTa.length);
529 for (int j = 0; j < string2DT.length; j++) {
530 assertTrue(Arrays.equals(string2DT[j], string2DTa[j]));
535 String intK = "intK";
536 String representation3DK = "representation3DK";
537 OcRepresentation[] representation2DArrV1 = {
538 new OcRepresentation(),
539 new OcRepresentation(),
540 new OcRepresentation(),
541 new OcRepresentation()};
542 representation2DArrV1[0].setValue(intK, 0);
543 representation2DArrV1[1].setValue(intK, 1);
544 representation2DArrV1[2].setValue(intK, 2);
545 representation2DArrV1[3].setValue(intK, 3);
547 OcRepresentation[] representation2DArrV2 = {
548 new OcRepresentation(),
549 new OcRepresentation(),
550 new OcRepresentation(),
551 new OcRepresentation()};
552 representation2DArrV2[0].setValue(intK, 4);
553 representation2DArrV2[1].setValue(intK, 5);
554 representation2DArrV2[2].setValue(intK, 6);
555 representation2DArrV2[3].setValue(intK, 7);
557 OcRepresentation[][] representation2DArrV = {representation2DArrV1, representation2DArrV2};
558 OcRepresentation[][][] representation3DArrV = {representation2DArrV, representation2DArrV};
560 rep.setValue(representation3DK, representation3DArrV);
561 OcRepresentation[][][] representation3DArrVa = rep.getValue(representation3DK);
562 assertEquals(representation3DArrV.length, representation3DArrVa.length);
563 for (int i = 0; i < representation3DArrV.length; ++i) {
564 OcRepresentation[][] repArr2V = representation3DArrV[i];
565 OcRepresentation[][] repArr2Va = representation3DArrVa[i];
566 assertEquals(repArr2V.length, repArr2Va.length);
567 for (int j = 0; j < repArr2V.length; ++j) {
568 OcRepresentation[] repArrV = repArr2V[j];
569 OcRepresentation[] repArrVa = repArr2Va[j];
570 assertEquals(repArrV.length, repArrVa.length);
571 for (int k = 0; k < repArrV.length; ++k) {
572 assertEquals(repArrV[k].getValue(intK), repArrVa[k].getValue(intK));