b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 19 | import java.lang.reflect.Constructor; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 22 | import org.junit.Test; |
| 23 | import org.onosproject.yangutils.datamodel.YangType; |
| 24 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
| 25 | import org.onosproject.yangutils.utils.UtilConstants; |
| 26 | |
| 27 | import static org.hamcrest.core.Is.is; |
| 28 | import static org.junit.Assert.assertNotNull; |
| 29 | import static org.junit.Assert.assertThat; |
| 30 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 31 | /** |
| 32 | * Unit tests for generated methods from the file type. |
| 33 | */ |
| 34 | public final class MethodsGeneratorTest { |
| 35 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 36 | public static JavaAttributeInfo testAttr; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 37 | public static YangType<?> attrType = new YangType<>(); |
| 38 | |
| 39 | /** |
| 40 | * Unit test for private constructor. |
| 41 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 42 | * @throws SecurityException if any security violation is observed |
| 43 | * @throws NoSuchMethodException if when the method is not found |
| 44 | * @throws IllegalArgumentException if there is illegal argument found |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 45 | * @throws InstantiationException if instantiation is provoked for the |
| 46 | * private constructor |
| 47 | * @throws IllegalAccessException if instance is provoked or a method is |
| 48 | * provoked |
| 49 | * @throws InvocationTargetException when an exception occurs by the method |
| 50 | * or constructor |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 51 | */ |
| 52 | @Test |
| 53 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 54 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 55 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 56 | Class<?>[] classesToConstruct = { |
| 57 | MethodsGenerator.class |
| 58 | }; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 59 | for (Class<?> clazz : classesToConstruct) { |
| 60 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 61 | constructor.setAccessible(true); |
| 62 | assertNotNull(constructor.newInstance()); |
| 63 | } |
| 64 | } |
| 65 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 66 | // /** |
| 67 | // * Unit test case for checking the parse builder and typedef constructor. |
| 68 | // */ |
| 69 | // @Test |
| 70 | // public void getParseBuilderInterfaceMethodConstructorTest() { |
| 71 | // |
| 72 | // JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo(); |
| 73 | // attrType.setDataTypeName("binary"); |
| 74 | // attrType.getDataTypeName(); |
| 75 | // attrType.setDataType(YangDataTypes.BINARY); |
| 76 | // attrType.getDataType(); |
| 77 | // testAttr.setAttributeName("attributeTest"); |
| 78 | // testAttr.setAttributeType(attrType); |
| 79 | // forSetter.setPkgInfo("test1/test3"); |
| 80 | // forSetter.setClassInfo("This class contains"); |
| 81 | // testAttr.setImportInfo(forSetter); |
| 82 | // String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname"); |
| 83 | // } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 84 | |
| 85 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 86 | * Unit test case for checking the values received from constructor, default |
| 87 | * constructor and build string formation. |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 88 | */ |
| 89 | @Test |
| 90 | public void getValuesTest() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 91 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 92 | String stringConstructor = MethodsGenerator.getConstructorString("testname"); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 93 | assertThat(stringConstructor.contains(UtilConstants.JAVA_DOC_CONSTRUCTOR) |
| 94 | && stringConstructor.contains(UtilConstants.JAVA_DOC_PARAM) |
| 95 | && stringConstructor.contains(UtilConstants.BUILDER_OBJECT), is(true)); |
| 96 | String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString("testnameBuilder", "public"); |
| 97 | assertThat(stringDefaultConstructor.contains(UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR) |
| 98 | && stringDefaultConstructor.contains(UtilConstants.BUILDER) |
| 99 | && stringDefaultConstructor.contains("testname"), is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 100 | String stringBuild = MethodsGenerator.getBuildString("testname"); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 101 | assertThat(stringBuild.contains(UtilConstants.OVERRIDE) && stringBuild.contains(UtilConstants.BUILD) |
| 102 | && stringBuild.contains(UtilConstants.RETURN), is(true)); |
| 103 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 104 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 105 | |
| 106 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 107 | * Unit test for checking the values received for class getter, class and |
| 108 | * typedef setters with list data type. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 109 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 110 | // @Test |
| 111 | // public void getGetterSetterTest() { |
| 112 | // |
| 113 | // JavaQualifiedTypeInfo forGetterSetter = new JavaQualifiedTypeInfo(); |
| 114 | // attrType.setDataTypeName("int"); |
| 115 | // attrType.getDataTypeName(); |
| 116 | // attrType.setDataType(YangDataTypes.UINT8); |
| 117 | // attrType.getDataType(); |
| 118 | // testAttr.setAttributeName("AttributeTest1"); |
| 119 | // testAttr.setAttributeType(attrType); |
| 120 | // forGetterSetter.setPkgInfo("null"); |
| 121 | // forGetterSetter.setClassInfo("This class contains"); |
| 122 | // testAttr.setImportInfo(forGetterSetter); |
| 123 | // testAttr.setListAttr(true); |
| 124 | // String getterForClass = MethodsGenerator.getGetterForClass(testAttr); |
| 125 | // assertThat(getterForClass.contains(UtilConstants.GET_METHOD_PREFIX) && getterForClass.contains("List<") |
| 126 | // && getterForClass.contains("attributeTest1"), is(true)); |
| 127 | // String setterForClass = MethodsGenerator.getSetterForClass(testAttr, "TestThis"); |
| 128 | // assertThat(setterForClass.contains(UtilConstants.SET_METHOD_PREFIX) && setterForClass.contains("List<") |
| 129 | // && setterForClass.contains("attributeTest1"), is(true)); |
| 130 | // String typeDefSetter = MethodsGenerator.getSetterForTypeDefClass(testAttr); |
| 131 | // assertThat(typeDefSetter.contains(UtilConstants.SET_METHOD_PREFIX) && typeDefSetter.contains("List<") |
| 132 | // && typeDefSetter.contains("attributeTest1") && typeDefSetter.contains("this."), is(true)); |
| 133 | // } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 134 | |
| 135 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 136 | * Unit test case for checking the parse builder and typedef constructor |
| 137 | * with list data type. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 138 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 139 | // @Test |
| 140 | // public void getConstructorWithListTypeTest() { |
| 141 | // |
| 142 | // JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo(); |
| 143 | // attrType.setDataTypeName("binary"); |
| 144 | // attrType.getDataTypeName(); |
| 145 | // attrType.setDataType(YangDataTypes.BINARY); |
| 146 | // attrType.getDataType(); |
| 147 | // testAttr.setAttributeName("attributeTest"); |
| 148 | // testAttr.setAttributeType(attrType); |
| 149 | // forSetter.setPkgInfo("null"); |
| 150 | // forSetter.setClassInfo("This class contains"); |
| 151 | // testAttr.setImportInfo(forSetter); |
| 152 | // testAttr.setListAttr(true); |
| 153 | // String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname"); |
| 154 | // assertThat(stringTypeDef.contains("(List<") && stringTypeDef.contains("Testname") |
| 155 | // && stringTypeDef.contains(UtilConstants.THIS), is(true)); |
| 156 | // } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 157 | } |