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.Assert; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.yms.app.ydt.YdtTestUtils; |
| 22 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
| 23 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 24 | import org.onosproject.yms.ydt.YdtContext; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.lang.reflect.Field; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static org.junit.Assert.assertEquals; |
| 31 | import static org.junit.Assert.assertNotNull; |
| 32 | import static org.junit.Assert.assertNull; |
| 33 | import static org.junit.Assert.assertTrue; |
| 34 | |
| 35 | public class YobListTest { |
| 36 | |
| 37 | @Test |
| 38 | public void listwithoutcontainerTest() { |
| 39 | YangRequestWorkBench defaultYdtBuilder = |
| 40 | YdtTestUtils.listWithoutContainerYdt(); |
| 41 | validateYangObjectList(defaultYdtBuilder); |
| 42 | } |
| 43 | |
| 44 | private void validateYangObjectList( |
| 45 | YangRequestWorkBench defaultYdtBuilder) { |
| 46 | |
| 47 | YdtContext rootCtx = defaultYdtBuilder.getRootNode(); |
| 48 | |
| 49 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 50 | |
| 51 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 52 | |
| 53 | Object yangObject = builder.getYangObject( |
| 54 | (YdtExtendedContext) childCtx, YdtTestUtils. |
| 55 | getSchemaRegistry()); |
| 56 | assertNotNull(yangObject); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 57 | assertTrue("RootlistOpParam".equals(yangObject.getClass().getSimpleName())); |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 58 | try { |
| 59 | |
| 60 | Field field = |
| 61 | yangObject.getClass().getDeclaredField("listwithcontainer"); |
| 62 | field.setAccessible(true); |
| 63 | List listwithcontainer = (List) field.get(yangObject); |
| 64 | assertNull(listwithcontainer); |
| 65 | Field field1 = yangObject.getClass() |
| 66 | .getDeclaredField("listwithoutcontainer"); |
| 67 | field1.setAccessible(true); |
| 68 | List listwithoutcontainer = (List) field1.get(yangObject); |
| 69 | assertEquals(false, listwithoutcontainer.isEmpty()); |
| 70 | Field invalidinterval = listwithoutcontainer.get(0).getClass() |
| 71 | .getDeclaredField("invalidinterval"); |
| 72 | invalidinterval.setAccessible(true); |
| 73 | assertEquals("12", invalidinterval.get(listwithoutcontainer.get(0)) |
| 74 | .toString()); |
| 75 | } catch (NoSuchFieldException | IllegalAccessException e) { |
| 76 | Assert.fail(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void listwithcontainerTest() |
| 82 | throws IOException { |
| 83 | YangRequestWorkBench defaultYdtBuilder = |
| 84 | YdtTestUtils.listWithContainerYdt(); |
| 85 | |
| 86 | validateYangObject(defaultYdtBuilder); |
| 87 | } |
| 88 | |
| 89 | public void validateYangObject(YangRequestWorkBench defaultYdtBuilder) { |
| 90 | |
| 91 | YdtContext ydtContext = defaultYdtBuilder.getRootNode(); |
| 92 | |
| 93 | YdtContext ydtContext1 = ydtContext.getFirstChild(); |
| 94 | |
| 95 | DefaultYobBuilder defaultYobBuilder = new DefaultYobBuilder(); |
| 96 | |
| 97 | Object yangObject = defaultYobBuilder.getYangObject( |
| 98 | (YdtExtendedContext) ydtContext1, YdtTestUtils |
| 99 | .getSchemaRegistry()); |
| 100 | assertNotNull(yangObject); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 101 | assertTrue("RootlistOpParam".equals(yangObject.getClass().getSimpleName())); |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 102 | try { |
| 103 | |
| 104 | Field field = yangObject.getClass() |
| 105 | .getDeclaredField("listwithoutcontainer"); |
| 106 | field.setAccessible(true); |
| 107 | List listwithoutcontainer = (List) field.get(yangObject); |
| 108 | assertNull(listwithoutcontainer); |
| 109 | Field listwithcontainerField = |
| 110 | yangObject.getClass().getDeclaredField("listwithcontainer"); |
| 111 | listwithcontainerField.setAccessible(true); |
| 112 | List listwithcontainer = |
| 113 | (List) listwithcontainerField.get(yangObject); |
| 114 | Field invalid = listwithcontainer.get(0).getClass() |
| 115 | .getDeclaredField("invalid"); |
| 116 | invalid.setAccessible(true); |
| 117 | assertEquals("12", |
| 118 | invalid.get(listwithcontainer.get(0)).toString()); |
| 119 | Field invalidinterval = listwithcontainer.get(0).getClass() |
| 120 | .getDeclaredField("invalidinterval"); |
| 121 | invalidinterval.setAccessible(true); |
| 122 | List invalidintervalList = |
| 123 | (List) invalidinterval.get(listwithcontainer.get(0)); |
| 124 | assertEquals("1", invalidintervalList.get(0).toString()); |
| 125 | assertEquals("2", invalidintervalList.get(1).toString()); |
| 126 | } catch (NoSuchFieldException | IllegalAccessException e) { |
| 127 | Assert.fail(); |
| 128 | } |
| 129 | } |
| 130 | } |