Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.parseutils; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
| 21 | |
| 22 | import static org.hamcrest.core.Is.is; |
| 23 | import static org.junit.Assert.assertThat; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.clearOccurrenceCount; |
| 25 | import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.createValidNameForAugment; |
| 26 | import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.getAugmentJavaFileNameList; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.updateNameWhenHasMultipleOuccrrence; |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 28 | |
| 29 | /** |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 30 | * Unit test case for augment listener utility. |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 31 | */ |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 32 | public class AugmentListnerUtilTest { |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 33 | |
| 34 | private static final String TEST1 = "test1Node"; |
| 35 | private static final String PARENT_PREFIX = "if"; |
| 36 | private static final String NODE_PREFIX = "rf"; |
| 37 | |
| 38 | private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX = "AugmentedTest1Node"; |
| 39 | private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI1 = "AugmentedTest1Node1"; |
| 40 | private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2 = "AugmentedTest1Node2"; |
| 41 | private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3 = "AugmentedTest1Node3"; |
| 42 | |
| 43 | private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX = "AugmentedRfTest1Node"; |
| 44 | private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI1 = "AugmentedRfTest1Node1"; |
| 45 | private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI2 = "AugmentedRfTest1Node2"; |
| 46 | private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI3 = "AugmentedRfTest1Node3"; |
| 47 | |
| 48 | private static String testString = ""; |
| 49 | |
| 50 | /** |
| 51 | * Unit test case when parent's prefix is present and one occurrence of augment node to update same target node. |
| 52 | */ |
| 53 | @Test |
| 54 | public void testForAugmentNameWhenOneOuccrrenceWithParentPrefix() { |
| 55 | clearData(); |
| 56 | testString = createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(), |
| 57 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 58 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX))); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Unit test case when no prefix and one occurrence of augment node to update same target node. |
| 63 | */ |
| 64 | @Test |
| 65 | public void testForAugmentNameWhenOneOuccrrenceWithNoPrefix() { |
| 66 | clearData(); |
| 67 | testString = createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(), |
| 68 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 69 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX))); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Unit test case when different prefix then parent is present and |
| 74 | * one occurrence of augment node to update same target node. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testForAugmentNameWhenOneOuccrrenceWithDiffPrefix() { |
| 78 | clearData(); |
| 79 | testString = createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(), |
| 80 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 81 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX))); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Unit test case when parent's prefix is present and two occurrence of augment node to update |
| 86 | * same target node is present. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testForAugmentNameWhenTwoOuccrrenceWithParentPrefix() { |
| 90 | clearData(); |
| 91 | |
| 92 | createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(), |
| 93 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 94 | testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(), |
| 95 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 96 | |
| 97 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2))); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Unit test case when no prefix and two occurrence of augment node to update |
| 102 | * same target node is present. |
| 103 | */ |
| 104 | @Test |
| 105 | public void testForAugmentNameWhenTwoOuccrrenceWithNoPrefix() { |
| 106 | clearData(); |
| 107 | |
| 108 | createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(), |
| 109 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 110 | testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(), |
| 111 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 112 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2))); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Unit test case when different prefix then parent is present and |
| 117 | * two occurrence of augment node to update same target node is present. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testForAugmentNameWhenTwoOuccrrenceWithDiffPrefix() { |
| 121 | clearData(); |
| 122 | |
| 123 | createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(), |
| 124 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 125 | testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(), |
| 126 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 127 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI2))); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Unit test case when parent prefix and three occurrence of augment node to update |
| 132 | * same target node is present. |
| 133 | */ |
| 134 | @Test |
| 135 | public void testForAugmentNameWhenThreeOuccrrenceWithParentPrefix() { |
| 136 | clearData(); |
| 137 | |
| 138 | createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(), |
| 139 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 140 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(), |
| 141 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 142 | |
| 143 | testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(), |
| 144 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 145 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3))); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Unit test case when no prefix and three occurrence of augment node to update |
| 150 | * same target node is present. |
| 151 | */ |
| 152 | @Test |
| 153 | public void testForAugmentNameWhenThreeOuccrrenceNoPrefix() { |
| 154 | clearData(); |
| 155 | |
| 156 | createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(), |
| 157 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 158 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(), |
| 159 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 160 | |
| 161 | testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(), |
| 162 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 163 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3))); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Unit test case when different prefix and three occurrence of augment node to update |
| 168 | * same target node is present. |
| 169 | */ |
| 170 | @Test |
| 171 | public void testForAugmentNameWhenThreeOuccrrenceWithDiffPrefix() { |
| 172 | clearData(); |
| 173 | |
| 174 | createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(), |
| 175 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 176 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(), |
| 177 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 178 | |
| 179 | testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(), |
| 180 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 181 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI3))); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Unit test case for when three occurrence is there and parent prefix is present, |
| 186 | * all the names need to be updated in list. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testForPreviousNamesGotUpdatedWhenParentPrefix() { |
| 190 | clearData(); |
| 191 | |
| 192 | createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(), |
| 193 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 194 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(), |
| 195 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 196 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(), |
| 197 | isPrefixPresent(getStubNodeIdetifierWithParentPrefix())); |
| 198 | |
| 199 | testString = getAugmentJavaFileNameList().get(0); |
| 200 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI1))); |
| 201 | |
| 202 | testString = getAugmentJavaFileNameList().get(1); |
| 203 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2))); |
| 204 | |
| 205 | testString = getAugmentJavaFileNameList().get(2); |
| 206 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3))); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Unit test case for when three occurrence is there and no prefix is present, |
| 211 | * all the names need to be updated in list. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testForPreviousNamesGotUpdatedWhenNoPrefix() { |
| 215 | clearData(); |
| 216 | |
| 217 | createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(), |
| 218 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 219 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(), |
| 220 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 221 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(), |
| 222 | isPrefixPresent(getStubNodeIdetifierWithNoPrefix())); |
| 223 | |
| 224 | testString = getAugmentJavaFileNameList().get(0); |
| 225 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI1))); |
| 226 | |
| 227 | testString = getAugmentJavaFileNameList().get(1); |
| 228 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2))); |
| 229 | |
| 230 | testString = getAugmentJavaFileNameList().get(2); |
| 231 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3))); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Unit test case for when three occurrence is there and different prefix is present, |
| 236 | * all the names need to be updated in list. |
| 237 | */ |
| 238 | @Test |
| 239 | public void testForPreviousNamesGotUpdatedWhenDifferentPrefix() { |
| 240 | clearData(); |
| 241 | |
| 242 | createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(), |
| 243 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 244 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(), |
| 245 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 246 | updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(), |
| 247 | isPrefixPresent(getStubNodeIdetifierWithDiffPrefix())); |
| 248 | |
| 249 | testString = getAugmentJavaFileNameList().get(0); |
| 250 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI1))); |
| 251 | |
| 252 | testString = getAugmentJavaFileNameList().get(1); |
| 253 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI2))); |
| 254 | |
| 255 | testString = getAugmentJavaFileNameList().get(2); |
| 256 | assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI3))); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Returns stub node identifier when parent prefix is used. |
| 261 | * |
| 262 | * @param name name of node |
| 263 | * @param prefix prefix of node |
| 264 | * @return node identifier for node |
| 265 | */ |
| 266 | private YangNodeIdentifier getStubNodeIdetifierWithParentPrefix() { |
| 267 | YangNodeIdentifier nodeId = new YangNodeIdentifier(); |
| 268 | nodeId.setName(TEST1); |
| 269 | nodeId.setPrefix(PARENT_PREFIX); |
| 270 | return nodeId; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Returns stub node identifier when no prefix is used. |
| 275 | * |
| 276 | * @param name name of node |
| 277 | * @param prefix prefix of node |
| 278 | * @return node identifier for node |
| 279 | */ |
| 280 | private YangNodeIdentifier getStubNodeIdetifierWithNoPrefix() { |
| 281 | YangNodeIdentifier nodeId = new YangNodeIdentifier(); |
| 282 | nodeId.setName(TEST1); |
| 283 | nodeId.setPrefix(null); |
| 284 | return nodeId; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Returns stub node identifier when different prefix is used. |
| 289 | * |
| 290 | * @param name name of node |
| 291 | * @param prefix prefix of node |
| 292 | * @return node identifier for node |
| 293 | */ |
| 294 | private YangNodeIdentifier getStubNodeIdetifierWithDiffPrefix() { |
| 295 | YangNodeIdentifier nodeId = new YangNodeIdentifier(); |
| 296 | nodeId.setName(TEST1); |
| 297 | nodeId.setPrefix(NODE_PREFIX); |
| 298 | return nodeId; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns true if a prefix is present and it is not equals to parents prefix. |
| 303 | * |
| 304 | * @param nodeId YANG node identifier |
| 305 | * @param parentsPrefix parent's prefix |
| 306 | * @return true if a prefix is present and it is not equals to parents prefix |
| 307 | */ |
| 308 | private static boolean isPrefixPresent(YangNodeIdentifier nodeId) { |
| 309 | return nodeId.getPrefix() != null && nodeId.getPrefix() != PARENT_PREFIX; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Clears list of names and occurrence count after each test case. |
| 314 | */ |
| 315 | private void clearData() { |
| 316 | getAugmentJavaFileNameList().clear(); |
| 317 | clearOccurrenceCount(); |
| 318 | } |
| 319 | |
| 320 | } |