2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.util;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertTrue;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.aai.sparky.util.NodeUtils;
40 import org.onap.aai.sparky.util.TreeWalker;
42 import com.fasterxml.jackson.core.JsonProcessingException;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
47 * The Class TreeWalkerTest.
49 public class TreeWalkerTest {
55 * @throws Exception the exception
58 public void init() throws Exception {}
61 * Validate json node conversion null input.
64 public void validateJsonNodeConversionNullInput() {
66 TreeWalker walker = new TreeWalker();
69 JsonNode convertedNode = walker.convertJsonToNode(null);
70 assertNull("Converted node should have be null", convertedNode);
72 } catch (JsonProcessingException exc) {
74 } catch (IOException exc) {
81 * Validate json node conversion empty non json input.
84 public void validateJsonNodeConversionEmptyNonJsonInput() {
86 TreeWalker walker = new TreeWalker();
89 JsonNode convertedNode = walker.convertJsonToNode("");
90 assertNull("Converted node should have be null", convertedNode);
92 } catch (JsonProcessingException exc) {
94 } catch (IOException exc) {
101 * Validate json node conversion empty json input.
104 public void validateJsonNodeConversionEmptyJsonInput() {
106 TreeWalker walker = new TreeWalker();
109 JsonNode convertedNode = walker.convertJsonToNode("{}");
110 assertNotNull("Converted node should not be null", convertedNode);
112 ObjectMapper objectMapper = new ObjectMapper();
113 String convertedNodeAsStr = objectMapper.writeValueAsString(convertedNode);
115 assertEquals("{}", convertedNodeAsStr);
117 } catch (JsonProcessingException exc) {
119 } catch (IOException exc) {
126 * Validate walk tree null input.
129 public void validateWalkTreeNullInput() {
131 TreeWalker walker = new TreeWalker();
133 List<String> paths = new ArrayList<String>();
134 walker.walkTree(paths, null);
135 assertEquals(0, paths.size());
140 * Validate walk tree empty node.
143 public void validateWalkTreeEmptyNode() {
146 TreeWalker walker = new TreeWalker();
147 List<String> paths = new ArrayList<String>();
148 walker.walkTree(paths, walker.convertJsonToNode("{}"));
149 assertEquals(0, paths.size());
150 } catch (JsonProcessingException exc) {
152 } catch (IOException exc) {
159 * Validate walk tree one parent node.
162 public void validateWalkTreeOneParentNode() {
165 TreeWalker walker = new TreeWalker();
166 List<String> paths = new ArrayList<String>();
167 walker.walkTree(paths, walker.convertJsonToNode("{ \"root\" : { } }"));
168 assertEquals(1, paths.size());
169 } catch (JsonProcessingException exc) {
171 } catch (IOException exc) {
178 * Validate walk tree one parent node with object array.
181 public void validateWalkTreeOneParentNodeWithObjectArray() {
184 String jsonStr = "{\"Employee\":[{\"id\":\"101\",\"name\":\"Pushkar\",\"salary\":\"5000\"},"
185 + "{\"id\":\"102\",\"name\":\"Rahul\",\"salary\":\"4000\"},"
186 + "{\"id\":\"103\",\"name\":\"tanveer\",\"salary\":\"56678\"}]}";
187 TreeWalker walker = new TreeWalker();
188 List<String> paths = new ArrayList<String>();
189 walker.walkTree(paths, walker.convertJsonToNode(jsonStr));
190 assertEquals(9, paths.size());
191 } catch (JsonProcessingException exc) {
193 } catch (IOException exc) {
200 * Validate walk tree one parent node with value array.
203 public void validateWalkTreeOneParentNodeWithValueArray() {
206 String jsonStr = "{ \"colors\" : [ \"yellow\", \"blue\", \"red\" ] }";
207 TreeWalker walker = new TreeWalker();
208 List<String> paths = new ArrayList<String>();
209 walker.walkTree(paths, walker.convertJsonToNode(jsonStr));
211 assertEquals(3, paths.size());
212 } catch (JsonProcessingException exc) {
214 } catch (IOException exc) {
221 * Test walk for complex entity type aai entity node descriptors.
224 public void testWalkForComplexEntityType_AaiEntityNodeDescriptors() {
228 "{ \"generalNodeClass\": { \"class\": \"aai-entity-node general-node\","
229 + " \"visualElements\": [ { \"type\": \"circle\","
230 + " \"class\": \"outer\", \"svgAttributes\": {"
231 + " \"r\": \"20\" } }, {"
232 + " \"type\": \"circle\", \"class\": \"inner\", "
233 + " \"svgAttributes\": { \"r\": \"10\" "
234 + "} }, { \"type\": \"text\", "
235 + "\"class\": \"id-type-label\", \"displayKey\": \"itemType\", "
236 + " \"shapeAttributes\": { \"offset\": { "
237 + " \"x\": \"0\", \"y\": \"30\" } "
238 + " } }, { \"type\": \"text\", "
239 + " \"class\": \"id-value-label\", \"displayKey\":"
240 + " \"itemNameValue\", \"shapeAttributes\": { "
241 + " \"offset\": { \"x\": \"0\", "
242 + " \"y\": \"40\" } } } ] "
243 + " }, \"searchedNodeClass\": { \"class\": \"aai-entity-node search-node\","
244 + " \"visualElements\": [ { \"type\": \"circle\","
245 + " \"class\": \"outer\", \"svgAttributes\": { "
246 + " \"r\": \"20\" } }, { "
247 + " \"type\": \"circle\", \"class\": \"inner\", "
248 + " \"svgAttributes\": { \"r\": \"10\" }"
249 + " }, { \"type\": \"text\", "
250 + "\"class\": \"id-type-label\", \"displayKey\": \"itemType\", "
251 + " \"shapeAttributes\": { \"offset\": { "
252 + " \"x\": \"0\", \"y\": \"30\" }"
253 + " } }, { \"type\": \"text\", "
254 + " \"class\": \"id-value-label\", "
255 + "\"displayKey\": \"itemNameValue\", \"shapeAttributes\": {"
256 + " \"offset\": { \"x\": \"0\","
257 + " \"y\": \"40\" } }"
258 + " } ] }, \"selectedSearchedNodeClass\": { "
259 + "\"class\": \"aai-entity-node selected-search-node\", \"visualElements\": ["
260 + " { \"type\": \"circle\", "
261 + "\"class\": \"outer\", \"svgAttributes\": {"
262 + " \"r\": \"20\" } }, {"
263 + " \"type\": \"circle\", \"class\": \"inner\","
264 + " \"svgAttributes\": { \"r\": \"10\" "
265 + " } }, { \"type\": \"text\", "
266 + " \"class\": \"id-type-label\", \"displayKey\": \"itemType\","
267 + " \"shapeAttributes\": { \"offset\": {"
268 + " \"x\": \"0\", \"y\": \"30\""
270 + " \"type\": \"text\", \"class\": \"id-value-label\", "
271 + " \"displayKey\": \"itemNameValue\", \"shapeAttributes\": {"
272 + " \"offset\": { \"x\": \"0\", "
273 + " \"y\": \"40\" } } } ]"
274 + " }, \"selectedNodeClass\": { \"class\":"
275 + " \"aai-entity-node selected-node\","
276 + " \"visualElements\": [ { \"type\": \"circle\","
277 + " \"class\": \"outer\", \"svgAttributes\": {"
278 + " \"r\": \"20\" } }, {"
279 + " \"type\": \"circle\", \"class\": \"inner\","
280 + " \"svgAttributes\": { \"r\": \"10\" "
281 + " } }, { \"type\": \"text\", "
282 + " \"class\": \"id-type-label\", \"displayKey\": \"itemType\","
283 + " \"shapeAttributes\": { \"offset\": "
285 + " \"x\": \"0\", \"y\": \"30\" } "
286 + " } }, { \"type\": \"text\","
287 + " \"class\": \"id-value-label\", \"displayKey\":"
288 + " \"itemNameValue\", \"shapeAttributes\": { "
289 + "\"offset\": { \"x\": \"0\", "
290 + "\"y\": \"40\" } } } ] }}";
291 TreeWalker walker = new TreeWalker();
292 List<String> paths = new ArrayList<String>();
293 walker.walkTree(paths, walker.convertJsonToNode(jsonStr));
295 assertEquals(68, paths.size());
298 * Example of expected value
300 * generalNodeClass.class=aai-entity-node general-node
301 * generalNodeClass.visualElements.type=circle generalNodeClass.visualElements.class=outer
302 * generalNodeClass.visualElements.svgAttributes.r=20
303 * generalNodeClass.visualElements.type=circle generalNodeClass.visualElements.class=inner
304 * generalNodeClass.visualElements.svgAttributes.r=10
305 * generalNodeClass.visualElements.type=text
306 * generalNodeClass.visualElements.class=id-type-label
307 * generalNodeClass.visualElements.displayKey=itemType
308 * generalNodeClass.visualElements.shapeAttributes.offset.x=0
309 * generalNodeClass.visualElements.shapeAttributes.offset.y=30
310 * generalNodeClass.visualElements.type=text
311 * generalNodeClass.visualElements.class=id-value-label
312 * generalNodeClass.visualElements.displayKey=itemNameValue
313 * generalNodeClass.visualElements.shapeAttributes.offset.x=0
314 * generalNodeClass.visualElements.shapeAttributes.offset.y=40
315 * searchedNodeClass.class=aai-entity-node search-node
316 * searchedNodeClass.visualElements.type=circle searchedNodeClass.visualElements.class=outer
317 * searchedNodeClass.visualElements.svgAttributes.r=20
318 * searchedNodeClass.visualElements.type=circle searchedNodeClass.visualElements.class=inner
319 * searchedNodeClass.visualElements.svgAttributes.r=10
320 * searchedNodeClass.visualElements.type=text
321 * searchedNodeClass.visualElements.class=id-type-label
322 * searchedNodeClass.visualElements.displayKey=itemType
323 * searchedNodeClass.visualElements.shapeAttributes.offset.x=0
324 * searchedNodeClass.visualElements.shapeAttributes.offset.y=30
325 * searchedNodeClass.visualElements.type=text
326 * searchedNodeClass.visualElements.class=id-value-label
327 * searchedNodeClass.visualElements.displayKey=itemNameValue
328 * searchedNodeClass.visualElements.shapeAttributes.offset.x=0
329 * searchedNodeClass.visualElements.shapeAttributes.offset.y=40
330 * selectedSearchedNodeClass.class=aai-entity-node selected-search-node
331 * selectedSearchedNodeClass.visualElements.type=circle
332 * selectedSearchedNodeClass.visualElements.class=outer
333 * selectedSearchedNodeClass.visualElements.svgAttributes.r=20
334 * selectedSearchedNodeClass.visualElements.type=circle
335 * selectedSearchedNodeClass.visualElements.class=inner
336 * selectedSearchedNodeClass.visualElements.svgAttributes.r=10
337 * selectedSearchedNodeClass.visualElements.type=text
338 * selectedSearchedNodeClass.visualElements.class=id-type-label
339 * selectedSearchedNodeClass.visualElements.displayKey=itemType
340 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.x=0
341 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.y=30
342 * selectedSearchedNodeClass.visualElements.type=text
343 * selectedSearchedNodeClass.visualElements.class=id-value-label
344 * selectedSearchedNodeClass.visualElements.displayKey=itemNameValue
345 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.x=0
346 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.y=40
347 * selectedNodeClass.class=aai-entity-node selected-node
348 * selectedNodeClass.visualElements.type=circle selectedNodeClass.visualElements.class=outer
349 * selectedNodeClass.visualElements.svgAttributes.r=20
350 * selectedNodeClass.visualElements.type=circle selectedNodeClass.visualElements.class=inner
351 * selectedNodeClass.visualElements.svgAttributes.r=10
352 * selectedNodeClass.visualElements.type=text
353 * selectedNodeClass.visualElements.class=id-type-label
354 * selectedNodeClass.visualElements.displayKey=itemType
355 * selectedNodeClass.visualElements.shapeAttributes.offset.x=0
356 * selectedNodeClass.visualElements.shapeAttributes.offset.y=30
357 * selectedNodeClass.visualElements.type=text
358 * selectedNodeClass.visualElements.class=id-value-label
359 * selectedNodeClass.visualElements.displayKey=itemNameValue
360 * selectedNodeClass.visualElements.shapeAttributes.offset.x=0
361 * selectedNodeClass.visualElements.shapeAttributes.offset.y=40
364 } catch (JsonProcessingException exc) {
366 } catch (IOException exc) {
373 * Test complex object inversion equality.
376 public void testComplexObjectInversionEquality() {
379 * Dave Adams (1-Nov-2016):
381 * Ok.. I agree...weird title of the test-case. This test is focused on the isEqual equality
382 * test within the NodeUtils helper class which compares the sorted structural paths of two Json
383 * Object representations. I attempted to normalize unordered structures to produce an equality
384 * result, as there doesn't seem to be any natural equality test between two JsonNode objects
385 * that I could find to date.
387 * Basically, this test is confirming that if the same object values are present in different
388 * orders, they are effectively the same Json Object representation, and pass, at least my
389 * structural value equality test.
391 * I reordered the aaiEntityNodeDescriptors top level class types, and change the order of the
392 * x,y coordinates to be y,x. Same values different order. Once again, the expectation is that
393 * both representations are objectively equal, they just have different json representations.
398 "{ \"generalNodeClass\": { \"class\": \"aai-entity-node general-node\","
399 + " \"visualElements\": [ { \"type\": \"circle\","
400 + " \"class\": \"outer\", \"svgAttributes\": {"
401 + " \"r\": \"20\" } }, {"
402 + " \"type\": \"circle\", \"class\": \"inner\","
403 + " \"svgAttributes\": { \"r\": \"10\""
404 + " } }, { \"type\": \"text\","
405 + " \"class\": \"id-type-label\", \"displayKey\":"
406 + " \"itemType\", \"shapeAttributes\": { \"offset\":"
407 + " { \"x\": \"0\", \"y\": \"30\""
409 + " \"type\": \"text\", \"class\": \"id-value-label\","
410 + " \"displayKey\": \"itemNameValue\","
411 + " \"shapeAttributes\": { \"offset\":"
412 + " { \"x\": \"0\", \"y\": \"40\""
414 + " \"searchedNodeClass\": { \"class\": \"aai-entity-node search-node\","
415 + " \"visualElements\": [ { \"type\": \"circle\","
416 + " \"class\": \"outer\", \"svgAttributes\": {"
417 + " \"r\": \"20\" } }, {"
418 + " \"type\": \"circle\", \"class\": \"inner\","
419 + " \"svgAttributes\": { \"r\": \"10\""
420 + " } }, { \"type\": \"text\","
421 + " \"class\": \"id-type-label\", \"displayKey\":"
422 + " \"itemType\", \"shapeAttributes\": { \"offset\": {"
423 + " \"x\": \"0\", \"y\": \"30\""
425 + " \"type\": \"text\", \"class\": \"id-value-label\","
426 + " \"displayKey\": \"itemNameValue\","
427 + " \"shapeAttributes\": { \"offset\": {"
428 + " \"x\": \"0\", \"y\": \"40\""
430 + " \"selectedSearchedNodeClass\": { \"class\":"
431 + " \"aai-entity-node selected-search-node\", \"visualElements\": ["
432 + " { \"type\": \"circle\", \"class\":"
433 + " \"outer\", \"svgAttributes\": { \"r\": \"20\""
434 + " } }, { \"type\": \"circle\","
435 + " \"class\": \"inner\", \"svgAttributes\": {"
436 + " \"r\": \"10\" } }, {"
437 + " \"type\": \"text\", \"class\": \"id-type-label\","
438 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
439 + " \"offset\": { \"x\": \"0\","
440 + " \"y\": \"30\" } }"
441 + " }, { \"type\": \"text\","
442 + " \"class\": \"id-value-label\","
443 + " \"displayKey\": \"itemNameValue\","
444 + " \"shapeAttributes\": { \"offset\": {"
445 + " \"x\": \"0\", \"y\": \"40\""
447 + " \"selectedNodeClass\": { \"class\": \"aai-entity-node selected-node\","
448 + " \"visualElements\": [ { \"type\": \"circle\","
449 + " \"class\": \"outer\", \"svgAttributes\": {"
450 + " \"r\": \"20\" } }, {"
451 + " \"type\": \"circle\", \"class\": \"inner\","
452 + " \"svgAttributes\": { \"r\": \"10\""
453 + " } }, { \"type\": \"text\","
454 + " \"class\": \"id-type-label\", \"displayKey\":"
455 + " \"itemType\", \"shapeAttributes\": {"
456 + " \"offset\": { \"x\": \"0\","
458 + " } }, { \"type\": \"text\","
459 + " \"class\": \"id-value-label\", \"displayKey\":"
460 + " \"itemNameValue\", \"shapeAttributes\": {"
461 + " \"offset\": { \"x\": \"0\","
462 + " \"y\": \"40\" } }"
465 "{ \"searchedNodeClass\": { \"class\": \"aai-entity-node search-node\","
466 + " \"visualElements\": [ { \"type\": \"circle\","
467 + " \"class\": \"outer\", \"svgAttributes\": {"
468 + " \"r\": \"20\" } },"
469 + " { \"type\": \"circle\","
470 + " \"class\": \"inner\", \"svgAttributes\": {"
471 + " \"r\": \"10\" } }, {"
472 + " \"type\": \"text\", \"class\": \"id-type-label\","
473 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
474 + " \"offset\": { \"y\": \"30\","
475 + " \"x\": \"0\" } }"
476 + " }, { \"type\": \"text\","
477 + " \"class\": \"id-value-label\","
478 + " \"displayKey\": \"itemNameValue\","
479 + " \"shapeAttributes\": { \"offset\": {"
480 + " \"y\": \"40\", \"x\": \"0\""
482 + " \"selectedSearchedNodeClass\": { \"class\":"
483 + " \"aai-entity-node selected-search-node\", \"visualElements\": ["
484 + " { \"type\": \"circle\", \"class\":"
485 + " \"outer\", \"svgAttributes\": { \"r\": \"20\""
486 + " } }, { \"type\": \"circle\","
487 + " \"class\": \"inner\", \"svgAttributes\": {"
488 + " \"r\": \"10\" } }, {"
489 + " \"type\": \"text\", \"class\": \"id-type-label\","
490 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
491 + " \"offset\": { \"y\": \"30\","
492 + " \"x\": \"0\" } }"
493 + " }, { \"type\": \"text\","
494 + " \"class\": \"id-value-label\","
495 + " \"displayKey\": \"itemNameValue\","
496 + " \"shapeAttributes\": { \"offset\": {"
497 + " \"y\": \"40\", \"x\": \"0\""
499 + " \"selectedNodeClass\": { \"class\": \"aai-entity-node selected-node\","
500 + " \"visualElements\": [ { \"type\": \"circle\","
501 + " \"class\": \"outer\", \"svgAttributes\": {"
502 + " \"r\": \"20\" } }, {"
503 + " \"type\": \"circle\", \"class\": \"inner\","
504 + " \"svgAttributes\": { \"r\": \"10\""
505 + " } }, { \"type\": \"text\","
506 + " \"class\": \"id-type-label\","
507 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
508 + " \"offset\": { \"y\": \"30\","
509 + " \"x\": \"0\" } }"
510 + " }, { \"type\": \"text\","
511 + " \"class\": \"id-value-label\","
512 + " \"displayKey\": \"itemNameValue\","
513 + " \"shapeAttributes\": { \"offset\": {"
514 + " \"y\": \"40\", \"x\": \"0\""
516 + " \"generalNodeClass\": { \"class\":"
517 + " \"aai-entity-node general-node\", \"visualElements\": ["
518 + " { \"type\": \"circle\","
519 + " \"class\": \"outer\", \"svgAttributes\": {"
520 + " \"r\": \"20\" } },"
521 + " { \"type\": \"circle\","
522 + " \"class\": \"inner\", \"svgAttributes\": {"
523 + " \"r\": \"10\" } },"
524 + " { \"type\": \"text\","
525 + " \"class\": \"id-type-label\", \"displayKey\":"
526 + " \"itemType\", \"shapeAttributes\": {"
527 + " \"offset\": { \"y\": \"30\","
530 + " \"type\": \"text\","
531 + " \"class\": \"id-value-label\", \"displayKey\":"
532 + " \"itemNameValue\", \"shapeAttributes\": {"
533 + " \"offset\": { \"y\": \"40\","
537 TreeWalker walker = new TreeWalker();
538 List<String> n1Paths = new ArrayList<String>();
539 List<String> n2Paths = new ArrayList<String>();
541 JsonNode n1 = walker.convertJsonToNode(n1Str);
542 JsonNode n2 = walker.convertJsonToNode(n2Str);
543 walker.walkTree(n1Paths, n1);
544 walker.walkTree(n2Paths, n2);
546 assertEquals(68, n1Paths.size());
547 assertEquals(68, n2Paths.size());
549 assertTrue(NodeUtils.isEqual(n1, n2));
551 } catch (JsonProcessingException exc) {
553 } catch (IOException exc) {