Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +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.yms.app.yob; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yang.gen.v1.ydt.leafreftest.rev20160524.LeafreftestOpParam; |
| 21 | import org.onosproject.yang.gen.v1.ydt.leafreftest.rev20160524.leafreftest.cont1.AugmentedCont1; |
| 22 | import org.onosproject.yang.gen.v1.ydt.leafreftest.rev20160524.leafreftest.food.snack.Sportsarena; |
| 23 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
| 24 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 25 | import org.onosproject.yms.ydt.YdtContext; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | |
| 29 | import static org.hamcrest.CoreMatchers.is; |
| 30 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.onosproject.yms.app.yob.YobTestUtils.ROOT_DATA_RESOURCE; |
| 33 | |
| 34 | /** |
| 35 | * Test the YANG object building for the YANG data tree based on the leaf ref. |
| 36 | */ |
| 37 | public class YobLeafRefTest { |
| 38 | |
| 39 | private YobTestUtils utils = YobTestUtils.instance(); |
| 40 | |
| 41 | @Test |
| 42 | public void testLeafrefInLeaf() throws IOException { |
| 43 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 44 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 45 | ydtBuilder.addChild("leafreftest", "ydt.leafreftest"); |
| 46 | ydtBuilder.addChild("leafrefList", null); |
| 47 | ydtBuilder.addLeaf("id", null, "leafref"); |
| 48 | ydtBuilder.traverseToParent(); |
| 49 | ydtBuilder.traverseToParent(); |
| 50 | ydtBuilder.traverseToParent(); |
| 51 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 52 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 53 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 54 | Object yangObject = builder.getYangObject( |
| 55 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 56 | assertThat(yangObject, notNullValue()); |
| 57 | LeafreftestOpParam leafrefTestOpParam = ((LeafreftestOpParam) |
| 58 | yangObject); |
| 59 | assertThat(leafrefTestOpParam.leafrefList().get(0).id().toString(), |
| 60 | is("leafref")); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testLeafrefInTypedef() throws IOException { |
| 65 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 66 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 67 | ydtBuilder.addChild("leafreftest", "ydt.leafreftest"); |
| 68 | ydtBuilder.addLeaf("name", null, "leafref"); |
| 69 | ydtBuilder.traverseToParent(); |
| 70 | ydtBuilder.traverseToParent(); |
| 71 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 72 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 73 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 74 | Object yangObject = builder.getYangObject( |
| 75 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 76 | assertThat(yangObject, notNullValue()); |
| 77 | LeafreftestOpParam leafrefTestOpParam = ((LeafreftestOpParam) yangObject); |
| 78 | assertThat(leafrefTestOpParam.name().toString(), is("leafref")); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testLeafrefInGrouping() throws IOException { |
| 83 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 84 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 85 | ydtBuilder.addChild("leafreftest", "ydt.leafreftest"); |
| 86 | ydtBuilder.addChild("cont1", "ydt.leafreftest"); |
| 87 | ydtBuilder.addLeaf("surname", null, "leafref"); |
| 88 | ydtBuilder.traverseToParent(); |
| 89 | ydtBuilder.traverseToParent(); |
| 90 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 91 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 92 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 93 | Object yangObject = builder.getYangObject( |
| 94 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 95 | assertThat(yangObject, notNullValue()); |
| 96 | LeafreftestOpParam leafreftestOpParam = ((LeafreftestOpParam) yangObject); |
| 97 | assertThat(leafreftestOpParam.cont1().surname().toString(), |
| 98 | is("leafref")); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testLeafrefInAugment() throws IOException { |
| 103 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 104 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 105 | ydtBuilder.addChild("leafreftest", "ydt.leafreftest"); |
| 106 | ydtBuilder.addChild("cont1", "ydt.leafreftest"); |
| 107 | ydtBuilder.addLeaf("lastname", null, "yang"); |
| 108 | ydtBuilder.traverseToParent(); |
| 109 | ydtBuilder.traverseToParent(); |
| 110 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 111 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 112 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 113 | Object yangObject = builder.getYangObject( |
| 114 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 115 | assertThat(yangObject, notNullValue()); |
| 116 | LeafreftestOpParam leafreftestOpParam = ((LeafreftestOpParam) yangObject); |
| 117 | AugmentedCont1 augmentedCont1 = (AugmentedCont1) leafreftestOpParam |
| 118 | .cont1().yangAugmentedInfo(AugmentedCont1.class); |
| 119 | assertThat(augmentedCont1.lastname().toString(), is("yang")); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testLeafrefInCase() throws IOException { |
| 124 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 125 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 126 | ydtBuilder.addChild("leafreftest", "ydt.leafreftest"); |
| 127 | ydtBuilder.addChild("food", "ydt.leafreftest"); |
| 128 | ydtBuilder.addLeaf("pretzel", null, "yang"); |
| 129 | ydtBuilder.traverseToParent(); |
| 130 | ydtBuilder.traverseToParent(); |
| 131 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 132 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 133 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 134 | Object yangObject = builder.getYangObject( |
| 135 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 136 | assertThat(yangObject, notNullValue()); |
| 137 | LeafreftestOpParam leafreftestOpParam = ((LeafreftestOpParam) |
| 138 | yangObject); |
| 139 | Sportsarena sportsArena = ((Sportsarena) leafreftestOpParam.food() |
| 140 | .snack()); |
| 141 | assertThat(sportsArena.pretzel().toString(), is("yang")); |
| 142 | } |
| 143 | } |