blob: 3be736c069a5e80d02f43d39f288f5a1fa860e77 [file] [log] [blame]
Bharat saraswald6f12412016-03-28 15:50:13 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswald6f12412016-03-28 15:50:13 +05303 *
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;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053027import static org.hamcrest.core.IsNot.not;
Bharat saraswald6f12412016-03-28 15:50:13 +053028import 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);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053072 assertThat(null, not(constructor.newInstance()));
Bharat saraswald6f12412016-03-28 15:50:13 +053073 }
74 }
75
76 /**
77 * Unit test for java class info method test.
78 */
79 @Test
80 public void testgetJavaClassInfo() {
Bharat saraswald6f12412016-03-28 15:50:13 +053081 test = getJavaImportClass(getStubYangType(TYPE1), false);
82 assertThat(true, is(test.equals(CLASS_INFO1)));
83
84 test = getJavaImportClass(getStubYangType(TYPE2), true);
85 assertThat(true, is(test.equals(CLASS_INFO5)));
86
87 test = getJavaImportClass(getStubYangType(TYPE3), false);
88 assertThat(null, is(test));
89
90 test = getJavaImportClass(getStubYangType(TYPE4), false);
91 assertThat(null, is(test));
92 }
93
94 /**
95 * Unit test for java data type method.
96 */
97 @Test
98 public void testgetJavaDataType() {
Bharat saraswald6f12412016-03-28 15:50:13 +053099 test = getJavaDataType(getStubYangType(TYPE1));
100 assertThat(true, is(test.equals(CLASS_INFO1)));
101
102 test = getJavaDataType(getStubYangType(TYPE2));
103 assertThat(true, is(test.equals(CLASS_INFO2)));
104
105 test = getJavaDataType(getStubYangType(TYPE3));
106 assertThat(true, is(test.equals(CLASS_INFO3)));
107
108 test = getJavaDataType(getStubYangType(TYPE4));
109 assertThat(true, is(test.equals(CLASS_INFO4)));
110 }
111
112 /**
113 * Unit test for java package info method.
114 */
115 @Test
116 public void testgetJavaPkgInfo() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530117 test = getJavaImportPackage(getStubYangType(TYPE1), false, CLASS_INFO1);
118 assertThat(true, is(test.equals(JAVA_LANG)));
119
120 test = getJavaImportPackage(getStubYangType(TYPE2), true, CLASS_INFO5);
121 assertThat(true, is(test.equals(JAVA_LANG)));
122
123 test = getJavaImportPackage(getStubYangType(TYPE3), false, CLASS_INFO3);
124 assertThat(null, is(test));
125
126 test = getJavaImportPackage(getStubYangType(TYPE4), false, CLASS_INFO4);
127 assertThat(null, is(test));
128 }
129
130 /**
131 * Returns stub YANG type for test.
132 *
133 * @param dataTypes YANG data types
134 * @return YANG type
135 */
136 private YangType<?> getStubYangType(YangDataTypes dataTypes) {
Bharat saraswald6f12412016-03-28 15:50:13 +0530137 YangType<?> type = new YangType();
138 type.setDataType(dataTypes);
139 return type;
140 }
141}