Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +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.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import java.lang.reflect.Constructor; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 24 | import org.onosproject.yangutils.datamodel.YangType; |
| 25 | |
| 26 | import static org.hamcrest.core.Is.is; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 27 | import static org.hamcrest.core.IsNot.not; |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 28 | import static org.junit.Assert.assertThat; |
| 29 | import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN; |
| 30 | import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32; |
| 31 | import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING; |
| 32 | import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT8; |
| 33 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaDataType; |
| 34 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass; |
| 35 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG; |
| 37 | |
| 38 | /** |
| 39 | * Unit test case for attribute java data type. |
| 40 | */ |
| 41 | public 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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 72 | assertThat(null, not(constructor.newInstance())); |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Unit test for java class info method test. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testgetJavaClassInfo() { |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 81 | 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 saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 99 | 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 saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 117 | 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 saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 137 | YangType<?> type = new YangType(); |
| 138 | type.setDataType(dataTypes); |
| 139 | return type; |
| 140 | } |
| 141 | } |