blob: bec2eef20a1a0b5eeeb716a7b2306ade7978893f [file] [log] [blame]
Mahesh Poojary Huaweia330c582016-08-26 10:38:10 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yms.app.ypm;
18
19import org.junit.Test;
20
21import static org.hamcrest.MatcherAssert.assertThat;
22import static org.hamcrest.Matchers.is;
23import static org.hamcrest.Matchers.notNullValue;
24import static org.hamcrest.Matchers.nullValue;
25
26import org.onosproject.yms.ypm.YpmContext;
27import org.onosproject.yms.ypm.DefaultYpmNode;
28
29/**
30 * Unit tests for DefaultYpmNode class.
31 */
32public class DefaultYpmNodeTest {
33 private final String logicalName = "logicalYpmNode";
34 private final String moduleName1 = "portPairModule1";
35 private final String moduleName2 = "portPairModule2";
36 private final String xNodeName = "x";
37 private final String yNodeName = "y";
38 private final String zNodeName = "z";
39 private final String x1NodeName = "x1";
40 private final String x2NodeName = "x2";
41 private final String y1NodeName = "y1";
42 private final String y2NodeName = "y2";
43 private final String z1NodeName = "z1";
44 private final String z2NodeName = "z2";
45
46 /**
47 * Constructs ypm tree with single module.
48 *
49 * @return ypm tree root node
50 */
51 private YpmContext constructYpmTreeSingleModule() {
52 // Create logical node
53 DefaultYpmNode rootNode = new DefaultYpmNode(logicalName);
54 // Create module node with moduleName1
55 rootNode.addChild(moduleName1); // child to logical node
56 YpmContext moduleNode = rootNode.getChild(moduleName1);
57 moduleNode.addChild(xNodeName); // child to module node
58 moduleNode.addChild(yNodeName); // sibling node to child node "x"
59 YpmContext xNode = moduleNode.getChild("x");
60 xNode.addSibling(zNodeName); // sibling node to child node "x"
61 xNode.addChild(x1NodeName); // child to node x
62 xNode.addChild(x2NodeName); // child to node x
63 YpmContext yNode = moduleNode.getChild(yNodeName);
64 yNode.addChild(y1NodeName); // child to node y
65 yNode.addChild(y2NodeName); // child to node y
66 YpmContext zNode = moduleNode.getChild(zNodeName);
67 zNode.addChild(z1NodeName); // child to node z
68 zNode.addChild(z2NodeName); // child to node z
69 return rootNode;
70 }
71
72 /**
73 * Constructs ypm tree with multi module.
74 *
75 * @return ypm tree root node
76 */
77 private YpmContext constructYpmTreeMultiModule(DefaultYpmNode rootNode) {
78 rootNode.addChild(moduleName2); // child to logical node
79 YpmContext moduleNode = rootNode.getChild(moduleName2);
80 moduleNode.addChild(xNodeName); // child to module node
81 moduleNode.addChild(yNodeName); // sibling node to child node "x"
82 YpmContext xNode = moduleNode.getChild("x");
83 xNode.addSibling(zNodeName); // sibling node to child node "x"
84 xNode.addChild(x1NodeName); // child to node x
85 xNode.addChild(x2NodeName); // child to node x
86 YpmContext yNode = moduleNode.getChild(yNodeName);
87 yNode.addChild(y1NodeName); // child to node y
88 yNode.addChild(y2NodeName); // child to node y
89 YpmContext zNode = moduleNode.getChild(zNodeName);
90 zNode.addChild(z1NodeName); // child to node z
91 zNode.addChild(z2NodeName); // child to node z
92 return rootNode;
93 }
94
95 /**
96 * Checks ypm tree single module construction.
97 */
98 @Test
99 public void testYpmTreeSingleModuleConstruction() {
100 DefaultYpmNode rootNode = (DefaultYpmNode) constructYpmTreeSingleModule();
101 // Check one by one node
102 String name = rootNode.getName();
103 assertThat(name, is(logicalName));
104 YpmContext moduleNode = rootNode.getChild(moduleName1);
105 assertThat(moduleNode.getName(), is(moduleName1));
106 YpmContext ypmNode = moduleNode.getChild(xNodeName);
107 assertThat(ypmNode.getName(), is(xNodeName));
108 // Check sibling by using getNextSibling();
109 ypmNode = ypmNode.getNextSibling();
110 assertThat(ypmNode, notNullValue()); // either y or z should be there as sibling
111 ypmNode = ypmNode.getNextSibling();
112 assertThat(ypmNode, notNullValue()); // either y or z should be there as sibling
113 ypmNode = ypmNode.getNextSibling();
114 assertThat(ypmNode, nullValue()); // last sibling point to next sibling as null
115 // Check sibling by using getPreviousSibling()
116 ypmNode = moduleNode.getChild(zNodeName);
117 assertThat(ypmNode.getName(), is(zNodeName));
118 ypmNode = ypmNode.getPreviousSibling();
119 assertThat(ypmNode, notNullValue()); // either x or y should be there as sibling
120 ypmNode = ypmNode.getPreviousSibling();
121 assertThat(ypmNode, notNullValue()); // either x or y should be there as sibling
122 ypmNode = ypmNode.getPreviousSibling();
123 assertThat(ypmNode, nullValue()); // last sibling point to next sibling as null
124 // Checks the child x1 and x2
125 ypmNode = moduleNode.getChild(xNodeName);
126 ypmNode = ypmNode.getChild(x1NodeName);
127 assertThat(ypmNode.getName(), is(x1NodeName));
128 ypmNode = ypmNode.getSibling(x2NodeName);
129 assertThat(ypmNode.getName(), is(x2NodeName));
130 // Checks the child y1 and y2
131 ypmNode = moduleNode.getChild(yNodeName);
132 ypmNode = ypmNode.getChild(y1NodeName);
133 assertThat(ypmNode.getName(), is(y1NodeName));
134 ypmNode = ypmNode.getSibling(y2NodeName);
135 assertThat(ypmNode.getName(), is(y2NodeName));
136 // Checks the child z1 and z2
137 ypmNode = moduleNode.getChild(zNodeName);
138 ypmNode = ypmNode.getChild(z1NodeName);
139 assertThat(ypmNode.getName(), is(z1NodeName));
140 ypmNode = ypmNode.getSibling(z2NodeName);
141 assertThat(ypmNode.getName(), is(z2NodeName));
142 }
143
144 /**
145 * Checks ypm tree multiple module construction.
146 */
147 @Test
148 public void testYpmTreeMultiModuleConstruction() {
149 DefaultYpmNode rootNode = (DefaultYpmNode) constructYpmTreeSingleModule();
150 rootNode = (DefaultYpmNode) constructYpmTreeMultiModule(rootNode);
151 // Check one by one node
152 String name = rootNode.getName();
153 assertThat(name, is(logicalName));
154 YpmContext moduleNode = rootNode.getChild(moduleName2);
155 assertThat(moduleNode.getName(), is(moduleName2));
156 YpmContext ypmNode = moduleNode.getChild(xNodeName);
157 assertThat(ypmNode.getName(), is(xNodeName));
158 // Check sibling by using getNextSibling();
159 ypmNode = ypmNode.getNextSibling();
160 assertThat(ypmNode, notNullValue()); // either y or z should be there as sibling
161 ypmNode = ypmNode.getNextSibling();
162 assertThat(ypmNode, notNullValue()); // either y or z should be there as sibling
163 ypmNode = ypmNode.getNextSibling();
164 assertThat(ypmNode, nullValue()); // last sibling point to next sibling as null
165 // Check sibling by using getPreviousSibling()
166 ypmNode = moduleNode.getChild(zNodeName);
167 assertThat(ypmNode.getName(), is(zNodeName));
168 ypmNode = ypmNode.getPreviousSibling();
169 assertThat(ypmNode, notNullValue()); // either x or y should be there as sibling
170 ypmNode = ypmNode.getPreviousSibling();
171 assertThat(ypmNode, notNullValue()); // either x or y should be there as sibling
172 ypmNode = ypmNode.getPreviousSibling();
173 assertThat(ypmNode, nullValue()); // last sibling point to next sibling as null
174 // Checks the child x1 and x2
175 ypmNode = moduleNode.getChild(xNodeName);
176 ypmNode = ypmNode.getChild(x1NodeName);
177 assertThat(ypmNode.getName(), is(x1NodeName));
178 ypmNode = ypmNode.getSibling(x2NodeName);
179 assertThat(ypmNode.getName(), is(x2NodeName));
180 // Checks the child y1 and y2
181 ypmNode = moduleNode.getChild(yNodeName);
182 ypmNode = ypmNode.getChild(y1NodeName);
183 assertThat(ypmNode.getName(), is(y1NodeName));
184 ypmNode = ypmNode.getSibling(y2NodeName);
185 assertThat(ypmNode.getName(), is(y2NodeName));
186 // Checks the child z1 and z2
187 ypmNode = moduleNode.getChild(zNodeName);
188 ypmNode = ypmNode.getChild(z1NodeName);
189 assertThat(ypmNode.getName(), is(z1NodeName));
190 ypmNode = ypmNode.getSibling(z2NodeName);
191 assertThat(ypmNode.getName(), is(z2NodeName));
192 }
193}