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;
46 import ch.qos.logback.classic.Level;
49 * The Class TreeWalkerTest.
51 public class TreeWalkerTest {
57 * @throws Exception the exception
60 public void init() throws Exception {
64 * Validate json node conversion null input.
67 public void validateJsonNodeConversionNullInput() {
69 TreeWalker walker = new TreeWalker();
72 JsonNode convertedNode = walker.convertJsonToNode(null);
73 assertNull("Converted node should have be null", convertedNode);
75 } catch (JsonProcessingException exc) {
77 } catch (IOException exc) {
84 * Validate json node conversion empty non json input.
87 public void validateJsonNodeConversionEmptyNonJsonInput() {
89 TreeWalker walker = new TreeWalker();
92 JsonNode convertedNode = walker.convertJsonToNode("");
93 assertNull("Converted node should have be null", convertedNode);
95 } catch (JsonProcessingException exc) {
97 } catch (IOException exc) {
104 * Validate json node conversion empty json input.
107 public void validateJsonNodeConversionEmptyJsonInput() {
109 TreeWalker walker = new TreeWalker();
112 JsonNode convertedNode = walker.convertJsonToNode("{}");
113 assertNotNull("Converted node should not be null", convertedNode);
115 ObjectMapper objectMapper = new ObjectMapper();
116 String convertedNodeAsStr = objectMapper.writeValueAsString(convertedNode);
118 assertEquals("{}", convertedNodeAsStr);
120 } catch (JsonProcessingException exc) {
122 } catch (IOException exc) {
129 * Validate walk tree null input.
132 public void validateWalkTreeNullInput() {
134 TreeWalker walker = new TreeWalker();
136 List<String> paths = new ArrayList<String>();
137 walker.walkTree(paths, null);
138 assertEquals(0, paths.size());
143 * Validate walk tree empty node.
146 public void validateWalkTreeEmptyNode() {
149 TreeWalker walker = new TreeWalker();
150 List<String> paths = new ArrayList<String>();
151 walker.walkTree(paths, walker.convertJsonToNode("{}"));
152 assertEquals(0, paths.size());
153 } catch (JsonProcessingException exc) {
155 } catch (IOException exc) {
162 * Validate walk tree one parent node.
165 public void validateWalkTreeOneParentNode() {
168 TreeWalker walker = new TreeWalker();
169 List<String> paths = new ArrayList<String>();
170 walker.walkTree(paths, walker.convertJsonToNode("{ \"root\" : { } }"));
171 assertEquals(1, paths.size());
172 } catch (JsonProcessingException exc) {
174 } catch (IOException exc) {
181 * Validate walk tree one parent node with object array.
184 public void validateWalkTreeOneParentNodeWithObjectArray() {
188 "{\"Employee\":[{\"id\":\"101\",\"name\":\"Pushkar\",\"salary\":\"5000\"},"
189 + "{\"id\":\"102\",\"name\":\"Rahul\",\"salary\":\"4000\"},"
190 + "{\"id\":\"103\",\"name\":\"tanveer\",\"salary\":\"56678\"}]}";
191 TreeWalker walker = new TreeWalker();
192 List<String> paths = new ArrayList<String>();
193 walker.walkTree(paths, walker.convertJsonToNode(jsonStr));
194 assertEquals(9, paths.size());
195 } catch (JsonProcessingException exc) {
197 } catch (IOException exc) {
204 * Validate walk tree one parent node with value array.
207 public void validateWalkTreeOneParentNodeWithValueArray() {
210 String jsonStr = "{ \"colors\" : [ \"yellow\", \"blue\", \"red\" ] }";
211 TreeWalker walker = new TreeWalker();
212 List<String> paths = new ArrayList<String>();
213 walker.walkTree(paths, walker.convertJsonToNode(jsonStr));
215 assertEquals(3, paths.size());
216 } catch (JsonProcessingException exc) {
218 } catch (IOException exc) {
225 * Test walk for complex entity type aai entity node descriptors.
228 public void testWalkForComplexEntityType_AaiEntityNodeDescriptors() {
232 "{ \"generalNodeClass\": { \"class\": \"aai-entity-node general-node\","
233 + " \"visualElements\": [ { \"type\": \"circle\","
234 + " \"class\": \"outer\", \"svgAttributes\": {"
235 + " \"r\": \"20\" } }, {"
236 + " \"type\": \"circle\", \"class\": \"inner\", "
237 + " \"svgAttributes\": { \"r\": \"10\" "
238 + "} }, { \"type\": \"text\", "
239 + "\"class\": \"id-type-label\", \"displayKey\": \"itemType\", "
240 + " \"shapeAttributes\": { \"offset\": { "
241 + " \"x\": \"0\", \"y\": \"30\" } "
242 + " } }, { \"type\": \"text\", "
243 + " \"class\": \"id-value-label\", \"displayKey\":"
244 + " \"itemNameValue\", \"shapeAttributes\": { "
245 + " \"offset\": { \"x\": \"0\", "
246 + " \"y\": \"40\" } } } ] "
247 + " }, \"searchedNodeClass\": { \"class\": \"aai-entity-node search-node\","
248 + " \"visualElements\": [ { \"type\": \"circle\","
249 + " \"class\": \"outer\", \"svgAttributes\": { "
250 + " \"r\": \"20\" } }, { "
251 + " \"type\": \"circle\", \"class\": \"inner\", "
252 + " \"svgAttributes\": { \"r\": \"10\" }"
253 + " }, { \"type\": \"text\", "
254 + "\"class\": \"id-type-label\", \"displayKey\": \"itemType\", "
255 + " \"shapeAttributes\": { \"offset\": { "
256 + " \"x\": \"0\", \"y\": \"30\" }"
257 + " } }, { \"type\": \"text\", "
258 + " \"class\": \"id-value-label\", "
259 + "\"displayKey\": \"itemNameValue\", \"shapeAttributes\": {"
260 + " \"offset\": { \"x\": \"0\","
261 + " \"y\": \"40\" } }"
262 + " } ] }, \"selectedSearchedNodeClass\": { "
263 + "\"class\": \"aai-entity-node selected-search-node\", \"visualElements\": ["
264 + " { \"type\": \"circle\", "
265 + "\"class\": \"outer\", \"svgAttributes\": {"
266 + " \"r\": \"20\" } }, {"
267 + " \"type\": \"circle\", \"class\": \"inner\","
268 + " \"svgAttributes\": { \"r\": \"10\" "
269 + " } }, { \"type\": \"text\", "
270 + " \"class\": \"id-type-label\", \"displayKey\": \"itemType\","
271 + " \"shapeAttributes\": { \"offset\": {"
272 + " \"x\": \"0\", \"y\": \"30\""
274 + " \"type\": \"text\", \"class\": \"id-value-label\", "
275 + " \"displayKey\": \"itemNameValue\", \"shapeAttributes\": {"
276 + " \"offset\": { \"x\": \"0\", "
277 + " \"y\": \"40\" } } } ]"
278 + " }, \"selectedNodeClass\": { \"class\":"
279 + " \"aai-entity-node selected-node\","
280 + " \"visualElements\": [ { \"type\": \"circle\","
281 + " \"class\": \"outer\", \"svgAttributes\": {"
282 + " \"r\": \"20\" } }, {"
283 + " \"type\": \"circle\", \"class\": \"inner\","
284 + " \"svgAttributes\": { \"r\": \"10\" "
285 + " } }, { \"type\": \"text\", "
286 + " \"class\": \"id-type-label\", \"displayKey\": \"itemType\","
287 + " \"shapeAttributes\": { \"offset\": "
289 + " \"x\": \"0\", \"y\": \"30\" } "
290 + " } }, { \"type\": \"text\","
291 + " \"class\": \"id-value-label\", \"displayKey\":"
292 + " \"itemNameValue\", \"shapeAttributes\": { "
293 + "\"offset\": { \"x\": \"0\", "
294 + "\"y\": \"40\" } } } ] }}";
295 TreeWalker walker = new TreeWalker();
296 List<String> paths = new ArrayList<String>();
297 walker.walkTree(paths, walker.convertJsonToNode(jsonStr));
299 assertEquals(68, paths.size());
302 * Example of expected value
304 * generalNodeClass.class=aai-entity-node general-node
305 * generalNodeClass.visualElements.type=circle generalNodeClass.visualElements.class=outer
306 * generalNodeClass.visualElements.svgAttributes.r=20
307 * generalNodeClass.visualElements.type=circle generalNodeClass.visualElements.class=inner
308 * generalNodeClass.visualElements.svgAttributes.r=10
309 * generalNodeClass.visualElements.type=text
310 * generalNodeClass.visualElements.class=id-type-label
311 * generalNodeClass.visualElements.displayKey=itemType
312 * generalNodeClass.visualElements.shapeAttributes.offset.x=0
313 * generalNodeClass.visualElements.shapeAttributes.offset.y=30
314 * generalNodeClass.visualElements.type=text
315 * generalNodeClass.visualElements.class=id-value-label
316 * generalNodeClass.visualElements.displayKey=itemNameValue
317 * generalNodeClass.visualElements.shapeAttributes.offset.x=0
318 * generalNodeClass.visualElements.shapeAttributes.offset.y=40
319 * searchedNodeClass.class=aai-entity-node search-node
320 * searchedNodeClass.visualElements.type=circle searchedNodeClass.visualElements.class=outer
321 * searchedNodeClass.visualElements.svgAttributes.r=20
322 * searchedNodeClass.visualElements.type=circle searchedNodeClass.visualElements.class=inner
323 * searchedNodeClass.visualElements.svgAttributes.r=10
324 * searchedNodeClass.visualElements.type=text
325 * searchedNodeClass.visualElements.class=id-type-label
326 * searchedNodeClass.visualElements.displayKey=itemType
327 * searchedNodeClass.visualElements.shapeAttributes.offset.x=0
328 * searchedNodeClass.visualElements.shapeAttributes.offset.y=30
329 * searchedNodeClass.visualElements.type=text
330 * searchedNodeClass.visualElements.class=id-value-label
331 * searchedNodeClass.visualElements.displayKey=itemNameValue
332 * searchedNodeClass.visualElements.shapeAttributes.offset.x=0
333 * searchedNodeClass.visualElements.shapeAttributes.offset.y=40
334 * selectedSearchedNodeClass.class=aai-entity-node selected-search-node
335 * selectedSearchedNodeClass.visualElements.type=circle
336 * selectedSearchedNodeClass.visualElements.class=outer
337 * selectedSearchedNodeClass.visualElements.svgAttributes.r=20
338 * selectedSearchedNodeClass.visualElements.type=circle
339 * selectedSearchedNodeClass.visualElements.class=inner
340 * selectedSearchedNodeClass.visualElements.svgAttributes.r=10
341 * selectedSearchedNodeClass.visualElements.type=text
342 * selectedSearchedNodeClass.visualElements.class=id-type-label
343 * selectedSearchedNodeClass.visualElements.displayKey=itemType
344 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.x=0
345 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.y=30
346 * selectedSearchedNodeClass.visualElements.type=text
347 * selectedSearchedNodeClass.visualElements.class=id-value-label
348 * selectedSearchedNodeClass.visualElements.displayKey=itemNameValue
349 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.x=0
350 * selectedSearchedNodeClass.visualElements.shapeAttributes.offset.y=40
351 * selectedNodeClass.class=aai-entity-node selected-node
352 * selectedNodeClass.visualElements.type=circle selectedNodeClass.visualElements.class=outer
353 * selectedNodeClass.visualElements.svgAttributes.r=20
354 * selectedNodeClass.visualElements.type=circle selectedNodeClass.visualElements.class=inner
355 * selectedNodeClass.visualElements.svgAttributes.r=10
356 * selectedNodeClass.visualElements.type=text
357 * selectedNodeClass.visualElements.class=id-type-label
358 * selectedNodeClass.visualElements.displayKey=itemType
359 * selectedNodeClass.visualElements.shapeAttributes.offset.x=0
360 * selectedNodeClass.visualElements.shapeAttributes.offset.y=30
361 * selectedNodeClass.visualElements.type=text
362 * selectedNodeClass.visualElements.class=id-value-label
363 * selectedNodeClass.visualElements.displayKey=itemNameValue
364 * selectedNodeClass.visualElements.shapeAttributes.offset.x=0
365 * selectedNodeClass.visualElements.shapeAttributes.offset.y=40
368 } catch (JsonProcessingException exc) {
370 } catch (IOException exc) {
377 * Test complex object inversion equality.
380 public void testComplexObjectInversionEquality() {
383 * Dave Adams (1-Nov-2016):
385 * Ok.. I agree...weird title of the test-case. This test is focused on the isEqual equality
386 * test within the NodeUtils helper class which compares the sorted structural paths of two Json
387 * Object representations. I attempted to normalize unordered structures to produce an equality
388 * result, as there doesn't seem to be any natural equality test between two JsonNode objects
389 * that I could find to date.
391 * Basically, this test is confirming that if the same object values are present in different
392 * orders, they are effectively the same Json Object representation, and pass, at least my
393 * structural value equality test.
395 * I reordered the aaiEntityNodeDescriptors top level class types, and change the order of the
396 * x,y coordinates to be y,x. Same values different order. Once again, the expectation is that
397 * both representations are objectively equal, they just have different json representations.
402 "{ \"generalNodeClass\": { \"class\": \"aai-entity-node general-node\","
403 + " \"visualElements\": [ { \"type\": \"circle\","
404 + " \"class\": \"outer\", \"svgAttributes\": {"
405 + " \"r\": \"20\" } }, {"
406 + " \"type\": \"circle\", \"class\": \"inner\","
407 + " \"svgAttributes\": { \"r\": \"10\""
408 + " } }, { \"type\": \"text\","
409 + " \"class\": \"id-type-label\", \"displayKey\":"
410 + " \"itemType\", \"shapeAttributes\": { \"offset\":"
411 + " { \"x\": \"0\", \"y\": \"30\""
413 + " \"type\": \"text\", \"class\": \"id-value-label\","
414 + " \"displayKey\": \"itemNameValue\","
415 + " \"shapeAttributes\": { \"offset\":"
416 + " { \"x\": \"0\", \"y\": \"40\""
418 + " \"searchedNodeClass\": { \"class\": \"aai-entity-node search-node\","
419 + " \"visualElements\": [ { \"type\": \"circle\","
420 + " \"class\": \"outer\", \"svgAttributes\": {"
421 + " \"r\": \"20\" } }, {"
422 + " \"type\": \"circle\", \"class\": \"inner\","
423 + " \"svgAttributes\": { \"r\": \"10\""
424 + " } }, { \"type\": \"text\","
425 + " \"class\": \"id-type-label\", \"displayKey\":"
426 + " \"itemType\", \"shapeAttributes\": { \"offset\": {"
427 + " \"x\": \"0\", \"y\": \"30\""
429 + " \"type\": \"text\", \"class\": \"id-value-label\","
430 + " \"displayKey\": \"itemNameValue\","
431 + " \"shapeAttributes\": { \"offset\": {"
432 + " \"x\": \"0\", \"y\": \"40\""
434 + " \"selectedSearchedNodeClass\": { \"class\":"
435 + " \"aai-entity-node selected-search-node\", \"visualElements\": ["
436 + " { \"type\": \"circle\", \"class\":"
437 + " \"outer\", \"svgAttributes\": { \"r\": \"20\""
438 + " } }, { \"type\": \"circle\","
439 + " \"class\": \"inner\", \"svgAttributes\": {"
440 + " \"r\": \"10\" } }, {"
441 + " \"type\": \"text\", \"class\": \"id-type-label\","
442 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
443 + " \"offset\": { \"x\": \"0\","
444 + " \"y\": \"30\" } }"
445 + " }, { \"type\": \"text\","
446 + " \"class\": \"id-value-label\","
447 + " \"displayKey\": \"itemNameValue\","
448 + " \"shapeAttributes\": { \"offset\": {"
449 + " \"x\": \"0\", \"y\": \"40\""
451 + " \"selectedNodeClass\": { \"class\": \"aai-entity-node selected-node\","
452 + " \"visualElements\": [ { \"type\": \"circle\","
453 + " \"class\": \"outer\", \"svgAttributes\": {"
454 + " \"r\": \"20\" } }, {"
455 + " \"type\": \"circle\", \"class\": \"inner\","
456 + " \"svgAttributes\": { \"r\": \"10\""
457 + " } }, { \"type\": \"text\","
458 + " \"class\": \"id-type-label\", \"displayKey\":"
459 + " \"itemType\", \"shapeAttributes\": {"
460 + " \"offset\": { \"x\": \"0\","
462 + " } }, { \"type\": \"text\","
463 + " \"class\": \"id-value-label\", \"displayKey\":"
464 + " \"itemNameValue\", \"shapeAttributes\": {"
465 + " \"offset\": { \"x\": \"0\","
466 + " \"y\": \"40\" } }"
469 "{ \"searchedNodeClass\": { \"class\": \"aai-entity-node search-node\","
470 + " \"visualElements\": [ { \"type\": \"circle\","
471 + " \"class\": \"outer\", \"svgAttributes\": {"
472 + " \"r\": \"20\" } },"
473 + " { \"type\": \"circle\","
474 + " \"class\": \"inner\", \"svgAttributes\": {"
475 + " \"r\": \"10\" } }, {"
476 + " \"type\": \"text\", \"class\": \"id-type-label\","
477 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
478 + " \"offset\": { \"y\": \"30\","
479 + " \"x\": \"0\" } }"
480 + " }, { \"type\": \"text\","
481 + " \"class\": \"id-value-label\","
482 + " \"displayKey\": \"itemNameValue\","
483 + " \"shapeAttributes\": { \"offset\": {"
484 + " \"y\": \"40\", \"x\": \"0\""
486 + " \"selectedSearchedNodeClass\": { \"class\":"
487 + " \"aai-entity-node selected-search-node\", \"visualElements\": ["
488 + " { \"type\": \"circle\", \"class\":"
489 + " \"outer\", \"svgAttributes\": { \"r\": \"20\""
490 + " } }, { \"type\": \"circle\","
491 + " \"class\": \"inner\", \"svgAttributes\": {"
492 + " \"r\": \"10\" } }, {"
493 + " \"type\": \"text\", \"class\": \"id-type-label\","
494 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
495 + " \"offset\": { \"y\": \"30\","
496 + " \"x\": \"0\" } }"
497 + " }, { \"type\": \"text\","
498 + " \"class\": \"id-value-label\","
499 + " \"displayKey\": \"itemNameValue\","
500 + " \"shapeAttributes\": { \"offset\": {"
501 + " \"y\": \"40\", \"x\": \"0\""
503 + " \"selectedNodeClass\": { \"class\": \"aai-entity-node selected-node\","
504 + " \"visualElements\": [ { \"type\": \"circle\","
505 + " \"class\": \"outer\", \"svgAttributes\": {"
506 + " \"r\": \"20\" } }, {"
507 + " \"type\": \"circle\", \"class\": \"inner\","
508 + " \"svgAttributes\": { \"r\": \"10\""
509 + " } }, { \"type\": \"text\","
510 + " \"class\": \"id-type-label\","
511 + " \"displayKey\": \"itemType\", \"shapeAttributes\": {"
512 + " \"offset\": { \"y\": \"30\","
513 + " \"x\": \"0\" } }"
514 + " }, { \"type\": \"text\","
515 + " \"class\": \"id-value-label\","
516 + " \"displayKey\": \"itemNameValue\","
517 + " \"shapeAttributes\": { \"offset\": {"
518 + " \"y\": \"40\", \"x\": \"0\""
520 + " \"generalNodeClass\": { \"class\":"
521 + " \"aai-entity-node general-node\", \"visualElements\": ["
522 + " { \"type\": \"circle\","
523 + " \"class\": \"outer\", \"svgAttributes\": {"
524 + " \"r\": \"20\" } },"
525 + " { \"type\": \"circle\","
526 + " \"class\": \"inner\", \"svgAttributes\": {"
527 + " \"r\": \"10\" } },"
528 + " { \"type\": \"text\","
529 + " \"class\": \"id-type-label\", \"displayKey\":"
530 + " \"itemType\", \"shapeAttributes\": {"
531 + " \"offset\": { \"y\": \"30\","
534 + " \"type\": \"text\","
535 + " \"class\": \"id-value-label\", \"displayKey\":"
536 + " \"itemNameValue\", \"shapeAttributes\": {"
537 + " \"offset\": { \"y\": \"40\","
541 TreeWalker walker = new TreeWalker();
542 List<String> n1Paths = new ArrayList<String>();
543 List<String> n2Paths = new ArrayList<String>();
545 JsonNode n1 = walker.convertJsonToNode(n1Str);
546 JsonNode n2 = walker.convertJsonToNode(n2Str);
547 walker.walkTree(n1Paths, n1);
548 walker.walkTree(n2Paths, n2);
550 assertEquals(68, n1Paths.size());
551 assertEquals(68, n2Paths.size());
553 assertTrue(NodeUtils.isEqual(n1, n2));
555 } catch (JsonProcessingException exc) {
557 } catch (IOException exc) {