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 | |
| 19 | import org.junit.Test; |
| 20 | import static org.hamcrest.core.Is.is; |
| 21 | import static org.hamcrest.Matchers.nullValue; |
| 22 | import static org.junit.Assert.assertThat; |
| 23 | import static org.junit.Assert.assertNotNull; |
| 24 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 25 | import org.onosproject.yangutils.datamodel.YangType; |
| 26 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 27 | import org.onosproject.yangutils.translator.tojava.AttributeInfo; |
| 28 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
| 29 | |
| 30 | import java.lang.reflect.Constructor; |
| 31 | import java.lang.reflect.InvocationTargetException; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for generated methods from the file type. |
| 37 | */ |
| 38 | public final class MethodsGeneratorTest { |
| 39 | |
| 40 | public static AttributeInfo testAttr = new AttributeInfo(); |
| 41 | public static YangType<?> attrType = new YangType<>(); |
| 42 | |
| 43 | /** |
| 44 | * Unit test for private constructor. |
| 45 | * |
| 46 | * @throws SecurityException if any security violation is observed. |
| 47 | * @throws NoSuchMethodException if when the method is not found. |
| 48 | * @throws IllegalArgumentException if there is illegal argument found. |
| 49 | * @throws InstantiationException if instantiation is provoked for the private constructor. |
| 50 | * @throws IllegalAccessException if instance is provoked or a method is provoked. |
| 51 | * @throws InvocationTargetException when an exception occurs by the method or constructor. |
| 52 | */ |
| 53 | @Test |
| 54 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
| 55 | InstantiationException, IllegalAccessException, InvocationTargetException { |
| 56 | |
| 57 | Class<?>[] classesToConstruct = {MethodsGenerator.class }; |
| 58 | for (Class<?> clazz : classesToConstruct) { |
| 59 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 60 | constructor.setAccessible(true); |
| 61 | assertNotNull(constructor.newInstance()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Unit test for checking the generated builder class method. |
| 67 | */ |
| 68 | @Test |
| 69 | public void getMethodBuilderClassTest() { |
| 70 | |
| 71 | attrType.setDataTypeName("integer"); |
| 72 | attrType.getDataTypeName(); |
| 73 | attrType.setDataType(YangDataTypes.INT8); |
| 74 | attrType.getDataType(); |
| 75 | testAttr.setAttributeName("attributeBuilderClassTest"); |
| 76 | testAttr.setAttributeType(attrType); |
| 77 | String builderClassMethod = MethodsGenerator.getMethodString(testAttr, GeneratedFileType.BUILDER_CLASS); |
| 78 | assertThat(builderClassMethod.contains("public Byte getAttributeBuilderClassTest() {"), is(true)); |
| 79 | assertThat(builderClassMethod.contains( |
| 80 | "public testnameof setAttributeBuilderClassTest(Byte attributeBuilderClassTest) {"), is(true)); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Unit test for checking the generated builder interface method. |
| 85 | */ |
| 86 | @Test |
| 87 | public void getMethodBuilderInterfaceTest() { |
| 88 | |
| 89 | attrType.setDataTypeName("integer16"); |
| 90 | attrType.getDataTypeName(); |
| 91 | attrType.setDataType(YangDataTypes.INT16); |
| 92 | attrType.getDataType(); |
| 93 | testAttr.setAttributeName("attributeBuilderInterfaceTest"); |
| 94 | testAttr.setAttributeType(attrType); |
| 95 | String builderInterfaceMethod = MethodsGenerator.getMethodString(testAttr, GeneratedFileType.BUILDER_INTERFACE); |
| 96 | assertThat(builderInterfaceMethod.contains("Returns the attribute attributeBuilderInterfaceTest.") |
| 97 | && builderInterfaceMethod.contains("Short getAttributeBuilderInterfaceTest();") |
| 98 | && builderInterfaceMethod.contains("Returns the builder object of attributeBuilderInterfaceTest.") |
| 99 | && builderInterfaceMethod |
| 100 | .contains("Builder setAttributeBuilderInterfaceTest(Short attributeBuilderInterfaceTest);"), |
| 101 | is(true)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Unit test for checking the generated impl method. |
| 106 | */ |
| 107 | @Test |
| 108 | public void getMethodImplTest() { |
| 109 | |
| 110 | attrType.setDataTypeName("integer16"); |
| 111 | attrType.getDataTypeName(); |
| 112 | attrType.setDataType(YangDataTypes.INT16); |
| 113 | attrType.getDataType(); |
| 114 | testAttr.setAttributeName("attributeImplTest"); |
| 115 | testAttr.setAttributeType(attrType); |
| 116 | String implMethod = MethodsGenerator.getMethodString(testAttr, GeneratedFileType.IMPL); |
| 117 | assertThat(implMethod.contains("public Short getAttributeImplTest() {") |
| 118 | && implMethod.contains("return attributeImplTest;"), is(true)); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Unit test for checking the generated interface method. |
| 123 | */ |
| 124 | @Test |
| 125 | public void getMethodInterfaceTest() { |
| 126 | |
| 127 | attrType.setDataTypeName("binary"); |
| 128 | attrType.getDataTypeName(); |
| 129 | attrType.setDataType(YangDataTypes.INT32); |
| 130 | attrType.getDataType(); |
| 131 | testAttr.setAttributeName("attributeInterfaceTest"); |
| 132 | testAttr.setAttributeType(attrType); |
| 133 | String interfaceMethod = MethodsGenerator.getMethodString(testAttr, GeneratedFileType.INTERFACE); |
| 134 | assertThat(interfaceMethod.contains("Returns the attribute attributeInterfaceTest.") |
| 135 | && interfaceMethod.contains("@return attributeInterfaceTest") |
| 136 | && interfaceMethod.contains("Int getAttributeInterfaceTest();"), is(true)); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Unit test for checking the response for an invalid input. |
| 141 | */ |
| 142 | @Test |
| 143 | public void getMethodInvalidTest() { |
| 144 | |
| 145 | attrType.setDataTypeName("decimal64"); |
| 146 | attrType.getDataTypeName(); |
| 147 | attrType.setDataType(YangDataTypes.DECIMAL64); |
| 148 | attrType.getDataType(); |
| 149 | testAttr.setAttributeName("attributeInvalidTest"); |
| 150 | testAttr.setAttributeType(attrType); |
| 151 | String invalidMethod = MethodsGenerator.getMethodString(testAttr, GeneratedFileType.ALL); |
| 152 | assertThat(invalidMethod, is(nullValue())); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Unit test for checking the generated construct method info. |
| 157 | */ |
| 158 | @Test |
| 159 | public void constructMethodInfoTest() { |
| 160 | |
| 161 | attrType.setDataTypeName("decimal64"); |
| 162 | attrType.getDataTypeName(); |
| 163 | attrType.setDataType(YangDataTypes.DECIMAL64); |
| 164 | attrType.getDataType(); |
| 165 | MethodsGenerator.setBuilderClassName("testnameof"); |
| 166 | String builderClassName = MethodsGenerator.getBuilderClassName(); |
| 167 | assertThat(builderClassName.equals("testnameof"), is(true)); |
| 168 | String implTypenullMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.IMPL, "testname", |
| 169 | GeneratedMethodTypes.GETTER, null); |
| 170 | assertThat(implTypenullMethod.contains("public Testname getTestname() {") |
| 171 | && implTypenullMethod.contains("return testname;"), is(true)); |
| 172 | String implTypeGetterMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.IMPL, "testname", |
| 173 | GeneratedMethodTypes.GETTER, attrType); |
| 174 | assertThat(implTypeGetterMethod.contains("public Decimal64 getTestname()") |
| 175 | && implTypeGetterMethod.contains("return testname;"), is(true)); |
| 176 | String implTypeConstructorMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.IMPL, "testname", |
| 177 | GeneratedMethodTypes.CONSTRUCTOR, attrType); |
| 178 | assertThat(implTypeConstructorMethod.contains("public testnameImpl(testnameBuilder testnameObject) {") |
| 179 | && implTypeConstructorMethod.contains("}"), is(true)); |
| 180 | String implTypeDefaultConstructorMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.IMPL, |
| 181 | "testname", GeneratedMethodTypes.DEFAULT_CONSTRUCTOR, attrType); |
| 182 | assertThat(implTypeDefaultConstructorMethod.contains("public testnameImpl() {") |
| 183 | && implTypeDefaultConstructorMethod.contains("}"), is(true)); |
| 184 | String implTypeSetterMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.IMPL, "testname", |
| 185 | GeneratedMethodTypes.SETTER, attrType); |
| 186 | assertThat(implTypeSetterMethod, is(nullValue())); |
| 187 | String builderInterfaceTypeSetterMethod = MethodsGenerator.constructMethodInfo( |
| 188 | GeneratedFileType.BUILDER_INTERFACE, "testname2", GeneratedMethodTypes.SETTER, attrType); |
| 189 | assertThat(builderInterfaceTypeSetterMethod.contains("Builder setTestname2(Decimal64 testname2);"), is(true)); |
| 190 | String builderInterfaceTypeGetterMethod = MethodsGenerator.constructMethodInfo( |
| 191 | GeneratedFileType.BUILDER_INTERFACE, "testname2", GeneratedMethodTypes.GETTER, attrType); |
| 192 | assertThat(builderInterfaceTypeGetterMethod.contains("Decimal64 getTestname2();"), is(true)); |
| 193 | String builderInterfaceTypeBuildMethod = MethodsGenerator.constructMethodInfo( |
| 194 | GeneratedFileType.BUILDER_INTERFACE, "testname2", GeneratedMethodTypes.BUILD, attrType); |
| 195 | assertThat(builderInterfaceTypeBuildMethod.contains("testname2 build();"), is(true)); |
| 196 | String builderInterfaceTypeConstructorMethod = MethodsGenerator.constructMethodInfo( |
| 197 | GeneratedFileType.BUILDER_INTERFACE, "testname2", GeneratedMethodTypes.CONSTRUCTOR, attrType); |
| 198 | assertThat(builderInterfaceTypeConstructorMethod, is(nullValue())); |
| 199 | String builderClassTypeBuildMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.BUILDER_CLASS, |
| 200 | "testname2", GeneratedMethodTypes.BUILD, attrType); |
| 201 | assertThat(builderClassTypeBuildMethod.contains("public testname2 build() {") |
| 202 | && builderClassTypeBuildMethod.contains("return new testname2Impl(this);"), is(true)); |
| 203 | String builderClassTypeGetterMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.BUILDER_CLASS, |
| 204 | "testname2", GeneratedMethodTypes.GETTER, attrType); |
| 205 | assertThat(builderClassTypeGetterMethod.contains("public Decimal64 getTestname2() {") |
| 206 | && builderClassTypeGetterMethod.contains("return testname2;"), is(true)); |
| 207 | String builderClassTypeSetterMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.BUILDER_CLASS, |
| 208 | "testname2", GeneratedMethodTypes.SETTER, attrType); |
| 209 | assertThat(builderClassTypeSetterMethod.contains("public testnameof setTestname2(Decimal64 testname2) {") |
| 210 | && builderClassTypeSetterMethod.contains("this.testname2 = testname2;"), is(true)); |
| 211 | String builderClassTypeDefaultConstructorMethod = MethodsGenerator.constructMethodInfo( |
| 212 | GeneratedFileType.BUILDER_CLASS, "testname2", GeneratedMethodTypes.DEFAULT_CONSTRUCTOR, attrType); |
| 213 | assertThat(builderClassTypeDefaultConstructorMethod.contains("public testname2Builder() {"), is(true)); |
| 214 | String builderClassTypeConstructorMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.BUILDER_CLASS, |
| 215 | "testname2", GeneratedMethodTypes.CONSTRUCTOR, attrType); |
| 216 | assertThat(builderClassTypeConstructorMethod, is(nullValue())); |
| 217 | String invalidMethod = MethodsGenerator.constructMethodInfo(GeneratedFileType.ALL, "testname2", |
| 218 | GeneratedMethodTypes.CONSTRUCTOR, attrType); |
| 219 | assertThat(invalidMethod, is(nullValue())); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Unit test for checking the method constructor. |
| 224 | */ |
| 225 | @Test |
| 226 | public void getMethodConstructorTest() { |
| 227 | |
| 228 | MethodsGenerator.parseBuilderInterfaceBuildMethodString("testname7"); |
| 229 | attrType.setDataTypeName("binary"); |
| 230 | attrType.getDataTypeName(); |
| 231 | attrType.setDataType(YangDataTypes.BINARY); |
| 232 | attrType.getDataType(); |
| 233 | testAttr.setAttributeName("attributeTest"); |
| 234 | testAttr.setAttributeType(attrType); |
| 235 | List<AttributeInfo> settingAttributes = new ArrayList<AttributeInfo>(); |
| 236 | settingAttributes.add(testAttr); |
| 237 | MethodsGenerator.setAttrInfo(settingAttributes); |
| 238 | String methodConstructor = MethodsGenerator.constructMethodInfo(GeneratedFileType.IMPL, "testname", |
| 239 | GeneratedMethodTypes.CONSTRUCTOR, attrType); |
| 240 | assertThat( |
| 241 | methodConstructor.contains("public testnameImpl(testnameBuilder testnameObject) {") |
| 242 | && methodConstructor.contains("this.attributeTest = testnameObject.getAttributeTest();"), |
| 243 | is(true)); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Unit test for checking the values received from constructor, default constructor and build string formation. |
| 248 | */ |
| 249 | @Test |
| 250 | public void getValuesTest() { |
| 251 | String stringConstructor = MethodsGenerator.getConstructorString("testname"); |
| 252 | assertThat( |
| 253 | stringConstructor.contains("Construct the object of testnameImpl.") |
| 254 | && stringConstructor.contains("@param testnameObject builder object of testname") |
| 255 | && stringConstructor.contains("public testnameImpl(testnameBuilder testnameObject) {"), |
| 256 | is(true)); |
| 257 | String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString(GeneratedFileType.BUILDER_CLASS, |
| 258 | "testname"); |
| 259 | assertThat(stringDefaultConstructor.contains("Default Constructor.") |
| 260 | && stringDefaultConstructor.contains("public testnameBuilder() {") |
| 261 | && stringDefaultConstructor.contains("}"), is(true)); |
| 262 | String stringBuild = MethodsGenerator.getBuildString("testname"); |
| 263 | assertThat( |
| 264 | stringBuild.contains("public testname build() {") |
| 265 | && stringBuild.contains("return new testnameImpl(this);") && stringBuild.contains("}"), |
| 266 | is(true)); |
| 267 | } |
| 268 | } |