blob: 4d62b73d2c54b1315b175c94236b5d37492668b6 [file] [log] [blame]
Bharat saraswald6f12412016-03-28 15:50:13 +05301/*
2 * Copyright 2016 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.yangutils.translator.tojava.utils;
18
19import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
22import org.junit.Test;
23import org.onosproject.yangutils.datamodel.YangDataTypes;
24import org.onosproject.yangutils.datamodel.YangType;
25
26import static org.hamcrest.core.Is.is;
27import static org.junit.Assert.assertNotNull;
28import static org.junit.Assert.assertThat;
29import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN;
30import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32;
31import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
32import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT8;
33import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaDataType;
34import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
35import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
36import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
37
38/**
39 * Unit test case for attribute java data type.
40 */
41public class AttributesJavaDataTypeTest {
42
43 private static final YangDataTypes TYPE1 = STRING;
44 private static final YangDataTypes TYPE2 = INT32;
45 private static final YangDataTypes TYPE3 = BOOLEAN;
46 private static final YangDataTypes TYPE4 = UINT8;
47 private static final String CLASS_INFO1 = "String";
48 private static final String CLASS_INFO2 = "int";
49 private static final String CLASS_INFO3 = "boolean";
50 private static final String CLASS_INFO4 = "short";
51 private static final String CLASS_INFO5 = "Integer";
52 private static String test = "";
53
54 /**
55 * Unit test for private constructor.
56 *
57 * @throws SecurityException if any security violation is observed
58 * @throws NoSuchMethodException if when the method is not found
59 * @throws IllegalArgumentException if there is illegal argument found
60 * @throws InstantiationException if instantiation is provoked for the private constructor
61 * @throws IllegalAccessException if instance is provoked or a method is provoked
62 * @throws InvocationTargetException when an exception occurs by the method or constructor
63 */
64 @Test
65 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
66 InstantiationException, IllegalAccessException, InvocationTargetException {
67
68 Class<?>[] classesToConstruct = {AttributesJavaDataType.class };
69 for (Class<?> clazz : classesToConstruct) {
70 Constructor<?> constructor = clazz.getDeclaredConstructor();
71 constructor.setAccessible(true);
72 assertNotNull(constructor.newInstance());
73 }
74 }
75
76 /**
77 * Unit test for java class info method test.
78 */
79 @Test
80 public void testgetJavaClassInfo() {
81
82 test = getJavaImportClass(getStubYangType(TYPE1), false);
83 assertThat(true, is(test.equals(CLASS_INFO1)));
84
85 test = getJavaImportClass(getStubYangType(TYPE2), true);
86 assertThat(true, is(test.equals(CLASS_INFO5)));
87
88 test = getJavaImportClass(getStubYangType(TYPE3), false);
89 assertThat(null, is(test));
90
91 test = getJavaImportClass(getStubYangType(TYPE4), false);
92 assertThat(null, is(test));
93 }
94
95 /**
96 * Unit test for java data type method.
97 */
98 @Test
99 public void testgetJavaDataType() {
100
101 test = getJavaDataType(getStubYangType(TYPE1));
102 assertThat(true, is(test.equals(CLASS_INFO1)));
103
104 test = getJavaDataType(getStubYangType(TYPE2));
105 assertThat(true, is(test.equals(CLASS_INFO2)));
106
107 test = getJavaDataType(getStubYangType(TYPE3));
108 assertThat(true, is(test.equals(CLASS_INFO3)));
109
110 test = getJavaDataType(getStubYangType(TYPE4));
111 assertThat(true, is(test.equals(CLASS_INFO4)));
112 }
113
114 /**
115 * Unit test for java package info method.
116 */
117 @Test
118 public void testgetJavaPkgInfo() {
119
120 test = getJavaImportPackage(getStubYangType(TYPE1), false, CLASS_INFO1);
121 assertThat(true, is(test.equals(JAVA_LANG)));
122
123 test = getJavaImportPackage(getStubYangType(TYPE2), true, CLASS_INFO5);
124 assertThat(true, is(test.equals(JAVA_LANG)));
125
126 test = getJavaImportPackage(getStubYangType(TYPE3), false, CLASS_INFO3);
127 assertThat(null, is(test));
128
129 test = getJavaImportPackage(getStubYangType(TYPE4), false, CLASS_INFO4);
130 assertThat(null, is(test));
131 }
132
133 /**
134 * Returns stub YANG type for test.
135 *
136 * @param dataTypes YANG data types
137 * @return YANG type
138 */
139 private YangType<?> getStubYangType(YangDataTypes dataTypes) {
140
141 YangType<?> type = new YangType();
142 type.setDataType(dataTypes);
143 return type;
144 }
145}