blob: f810ec93a2e914948d40e4eab4f341bf4b1d771d [file] [log] [blame]
Bharat saraswalc46ee2a2016-02-25 02:26:43 +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 org.junit.Test;
20import org.onosproject.yangutils.datamodel.YangDataTypes;
21import org.onosproject.yangutils.datamodel.YangType;
22import org.onosproject.yangutils.translator.GeneratedFileType;
23import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
24import org.onosproject.yangutils.translator.tojava.ImportInfo;
25import org.onosproject.yangutils.utils.UtilConstants;
26
27import static org.hamcrest.MatcherAssert.assertThat;
Bharat saraswal022dae92016-03-04 20:08:09 +053028import static org.junit.Assert.assertNotNull;
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053029import static org.hamcrest.core.Is.is;
30
Bharat saraswal022dae92016-03-04 20:08:09 +053031import java.lang.reflect.Constructor;
32import java.lang.reflect.InvocationTargetException;
33
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053034/**
35 * Unit test cases for java code snippet generator.
36 */
37public class JavaCodeSnippetGenTest {
38
39 private static final String PKG_INFO = "org.onosproject.unittest";
40 private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
Vinod Kumar S08710982016-03-03 19:55:30 +053041 private static final int FILE_GEN_TYPE = GeneratedFileType.INTERFACE_MASK;
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053042 private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER;
43 private static final String YANG_NAME = "Test";
44 private static final String STRING = "String";
45
46 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053047 * Unit test for private constructor.
48 *
49 * @throws SecurityException if any security violation is observed
50 * @throws NoSuchMethodException if when the method is not found
51 * @throws IllegalArgumentException if there is illegal argument found
52 * @throws InstantiationException if instantiation is provoked for the private constructor
53 * @throws IllegalAccessException if instance is provoked or a method is provoked
54 * @throws InvocationTargetException when an exception occurs by the method or constructor
55 */
56 @Test
57 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
58 InstantiationException, IllegalAccessException, InvocationTargetException {
59 Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class };
60 for (Class<?> clazz : classesToConstruct) {
61 Constructor<?> constructor = clazz.getDeclaredConstructor();
62 constructor.setAccessible(true);
63 assertNotNull(constructor.newInstance());
64 }
65 }
66
67 /**
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053068 * Unit test case for import text.
69 */
70 @Test
71 public void testForImportText() {
72 ImportInfo importInfo = new ImportInfo();
73 importInfo.setPkgInfo(PKG_INFO);
74 importInfo.setClassInfo(CLASS_INFO);
75
76 String imports = JavaCodeSnippetGen.getImportText(importInfo);
77
78 assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO
79 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
80 }
81
82 /**
83 * Unit test case for java class definition start.
84 */
85 @Test
86 public void testForJavaClassDefStart() {
87 String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
88 assertThat(true,
89 is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE
90 + UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
91 + UtilConstants.NEW_LINE)));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053092
93 }
94
95 /**
96 * Unit test case for list attribute.
97 */
98 @Test
99 public void testForListAttribute() {
100 String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING);
101 assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING
102 + UtilConstants.DIAMOND_CLOSE_BRACKET)));
103 }
104
105 /**
Bharat saraswal022dae92016-03-04 20:08:09 +0530106 * Unit test case for java class interface definition close.
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530107 */
108 @Test
Bharat saraswal022dae92016-03-04 20:08:09 +0530109 public void testForJavaClassDefInterfaceClose() {
110 String interfaceDef = JavaCodeSnippetGen.getJavaClassDefClose(FILE_GEN_TYPE, YANG_NAME);
111 assertThat(true, is(interfaceDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530112 }
113
114 /**
Bharat saraswal022dae92016-03-04 20:08:09 +0530115 * Unit test case for java class builder class definition close.
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530116 */
117 @Test
Bharat saraswal022dae92016-03-04 20:08:09 +0530118 public void testForJavaClassDefBuilderClassClose() {
119 String builderClassDef = JavaCodeSnippetGen.getJavaClassDefClose(GeneratedFileType.BUILDER_CLASS_MASK,
120 YANG_NAME);
121 assertThat(true, is(builderClassDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
122 }
123
124 /**
125 * Unit test case for java class typedef definition close.
126 */
127 @Test
128 public void testForJavaClassDefTypeDefClose() {
129 String typeDef = JavaCodeSnippetGen.getJavaClassDefClose(GeneratedFileType.GENERATE_TYPEDEF_CLASS, YANG_NAME);
130 assertThat(true, is(typeDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
131 }
132
133 /**
134 * Unit test case for java attribute info.
135 */
136 @SuppressWarnings("rawtypes")
137 @Test
138 public void testForJavaAttributeInfo() {
139
140 String attributeWithoutTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, "String", YANG_NAME,
141 false);
142 assertThat(true, is(attributeWithoutTypePkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + "String"
143 + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
144 String attributeWithTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination("java.lang", "String", YANG_NAME,
145 false);
146 assertThat(true, is(attributeWithTypePkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + "java.lang."
147 + "String" + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
148 String attributeWithListPkg = JavaCodeSnippetGen.getJavaAttributeDefination("java.lang", "String", YANG_NAME,
149 true);
150 assertThat(true,
151 is(attributeWithListPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
152 + UtilConstants.DIAMOND_OPEN_BRACKET + "java.lang."
153 + "String" + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME
154 + UtilConstants.SEMI_COLAN)));
155 String attributeWithListWithoutPkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, "String", YANG_NAME,
156 true);
157 assertThat(true,
158 is(attributeWithListWithoutPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
159 + UtilConstants.DIAMOND_OPEN_BRACKET + "String"
160 + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME
161 + UtilConstants.SEMI_COLAN)));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530162 }
163
164 /**
165 * Returns YANG type.
166 *
167 * @return type
168 */
169 @SuppressWarnings("rawtypes")
170 private YangType<?> getType() {
171 YangType<?> type = new YangType();
172 type.setDataTypeName(STRING);
173 type.setDataType(YangDataTypes.STRING);
174 return type;
175 }
176}