Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 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.uniontest.rev20160524.UniontestOpParam; |
| 21 | import org.onosproject.yang.gen.v1.ydt.uniontest.rev20160524.uniontest.cont1.AugmentedCont1; |
| 22 | import org.onosproject.yang.gen.v1.ydt.uniontest.rev20160524.uniontest.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 union. |
| 36 | */ |
| 37 | public class YobUnionTest { |
| 38 | |
| 39 | private YobTestUtils utils = YobTestUtils.instance(); |
| 40 | |
| 41 | @Test |
| 42 | public void testUnionInLeaf() throws IOException { |
| 43 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 44 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 45 | ydtBuilder.addChild("uniontest", "ydt.uniontest"); |
| 46 | ydtBuilder.addChild("unionList", null); |
| 47 | ydtBuilder.addLeaf("id", null, "YmluYXJ5"); |
| 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 | UniontestOpParam unionTestOpParam = ((UniontestOpParam) yangObject); |
| 58 | |
| 59 | byte[] binaryValue = unionTestOpParam.unionList().get(0).id().binary(); |
| 60 | String value = new String(binaryValue); |
| 61 | assertThat(value, is("binary")); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testUnionInTypedef() throws IOException { |
| 66 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 67 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 68 | ydtBuilder.addChild("uniontest", "ydt.uniontest"); |
| 69 | ydtBuilder.addLeaf("name", null, "bit1"); |
| 70 | ydtBuilder.traverseToParent(); |
| 71 | ydtBuilder.traverseToParent(); |
| 72 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 73 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 74 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 75 | Object yangObject = builder.getYangObject( |
| 76 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 77 | assertThat(yangObject, notNullValue()); |
| 78 | UniontestOpParam unionTestOpParam = ((UniontestOpParam) yangObject); |
| 79 | assertThat(unionTestOpParam.name().union().bits().get(1), is(true)); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testUnionInGrouping() throws IOException { |
| 84 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 85 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 86 | ydtBuilder.addChild("uniontest", "ydt.uniontest"); |
| 87 | ydtBuilder.addChild("cont1", "ydt.uniontest"); |
| 88 | ydtBuilder.addLeaf("surname", null, "yang"); |
| 89 | ydtBuilder.traverseToParent(); |
| 90 | ydtBuilder.traverseToParent(); |
| 91 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 92 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 93 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 94 | Object yangObject = builder.getYangObject( |
| 95 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 96 | assertThat(yangObject, notNullValue()); |
| 97 | UniontestOpParam uniontestOpParam = ((UniontestOpParam) yangObject); |
| 98 | assertThat(uniontestOpParam.cont1().surname().string(), is("yang")); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testUnionInAugment() throws IOException { |
| 103 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 104 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 105 | ydtBuilder.addChild("uniontest", "ydt.uniontest"); |
| 106 | ydtBuilder.addChild("cont1", "ydt.uniontest"); |
| 107 | ydtBuilder.addLeaf("lastname", null, "bit0"); |
| 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 | UniontestOpParam uniontestOpParam = ((UniontestOpParam) yangObject); |
| 117 | |
| 118 | AugmentedCont1 augmentedCont1 = (AugmentedCont1) uniontestOpParam |
| 119 | .cont1().yangAugmentedInfo(AugmentedCont1.class); |
| 120 | assertThat(augmentedCont1.lastname().bits().get(0), is(true)); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testUnionInCase() throws IOException { |
| 125 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 126 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 127 | ydtBuilder.addChild("uniontest", "ydt.uniontest"); |
| 128 | ydtBuilder.addChild("food", "ydt.uniontest"); |
| 129 | ydtBuilder.addLeaf("pretzel", null, "YmluYXJ5"); |
| 130 | ydtBuilder.traverseToParent(); |
| 131 | ydtBuilder.traverseToParent(); |
| 132 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 133 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 134 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 135 | Object yangObject = builder.getYangObject( |
| 136 | (YdtExtendedContext) childCtx, utils.schemaRegistry()); |
| 137 | assertThat(yangObject, notNullValue()); |
| 138 | UniontestOpParam uniontestOpParam = ((UniontestOpParam) yangObject); |
| 139 | Sportsarena sportsArena = ((Sportsarena) uniontestOpParam.food() |
| 140 | .snack()); |
| 141 | byte[] binaryValue = sportsArena.pretzel().binary(); |
| 142 | String value = new String(binaryValue); |
| 143 | assertThat(value, is("binary")); |
| 144 | } |
| 145 | } |