blob: 22daf1a849410dba68e7899baa78216e5c43b804 [file] [log] [blame]
Bharat saraswal4bf8b152016-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
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.junit.Test;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Bharat saraswale2d51d62016-03-23 19:40:35 +053024
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.core.Is.is;
27import static org.junit.Assert.assertNotNull;
28import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
29import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getImportText;
30import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
31import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
32import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefStart;
33import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getListAttribute;
34import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
35import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
36import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
37import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
38import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
39import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
40import static org.onosproject.yangutils.utils.UtilConstants.LIST;
41import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
42import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
43import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
44import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
45import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
46import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
47import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
48import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
Vinod Kumar S38046502016-03-23 15:30:27 +053049
Bharat saraswal4bf8b152016-02-25 02:26:43 +053050/**
51 * Unit test cases for java code snippet generator.
52 */
53public class JavaCodeSnippetGenTest {
54
55 private static final String PKG_INFO = "org.onosproject.unittest";
56 private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
Bharat saraswale2d51d62016-03-23 19:40:35 +053057 private static final int FILE_GEN_TYPE = INTERFACE_MASK;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053058 private static final String YANG_NAME = "Test";
Bharat saraswal4bf8b152016-02-25 02:26:43 +053059
60 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053061 * Unit test for private constructor.
62 *
63 * @throws SecurityException if any security violation is observed
64 * @throws NoSuchMethodException if when the method is not found
65 * @throws IllegalArgumentException if there is illegal argument found
66 * @throws InstantiationException if instantiation is provoked for the private constructor
67 * @throws IllegalAccessException if instance is provoked or a method is provoked
68 * @throws InvocationTargetException when an exception occurs by the method or constructor
69 */
70 @Test
71 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
72 InstantiationException, IllegalAccessException, InvocationTargetException {
Bharat saraswale2d51d62016-03-23 19:40:35 +053073
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053074 Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class };
75 for (Class<?> clazz : classesToConstruct) {
76 Constructor<?> constructor = clazz.getDeclaredConstructor();
77 constructor.setAccessible(true);
78 assertNotNull(constructor.newInstance());
79 }
80 }
81
82 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +053083 * Unit test case for import text.
84 */
85 @Test
86 public void testForImportText() {
Bharat saraswale2d51d62016-03-23 19:40:35 +053087
Vinod Kumar S38046502016-03-23 15:30:27 +053088 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
Bharat saraswal4bf8b152016-02-25 02:26:43 +053089 importInfo.setPkgInfo(PKG_INFO);
90 importInfo.setClassInfo(CLASS_INFO);
91
Bharat saraswale2d51d62016-03-23 19:40:35 +053092 String imports = getImportText(importInfo);
Bharat saraswal4bf8b152016-02-25 02:26:43 +053093
Bharat saraswale2d51d62016-03-23 19:40:35 +053094 assertThat(true, is(imports.equals(IMPORT + PKG_INFO + PERIOD + CLASS_INFO + SEMI_COLAN + NEW_LINE)));
Bharat saraswal4bf8b152016-02-25 02:26:43 +053095 }
96
97 /**
98 * Unit test case for java class definition start.
99 */
100 @Test
101 public void testForJavaClassDefStart() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530102
103 String classDef = getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
104 assertThat(true, is(classDef
105 .equals(PUBLIC + SPACE + INTERFACE + SPACE + YANG_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530106
107 }
108
109 /**
110 * Unit test case for list attribute.
111 */
112 @Test
113 public void testForListAttribute() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530114
115 String listAttribute = getListAttribute(STRING_DATA_TYPE);
116 assertThat(true,
117 is(listAttribute.equals(LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET)));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530118 }
119
120 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530121 * Unit test case for java class interface definition close.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530122 */
123 @Test
Bharat saraswale2d51d62016-03-23 19:40:35 +0530124 public void testForJavaClassDefClose() {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530125
Bharat saraswale2d51d62016-03-23 19:40:35 +0530126 String interfaceDef = getJavaClassDefClose();
127 assertThat(true, is(interfaceDef.equals(CLOSE_CURLY_BRACKET)));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530128 }
129
130 /**
131 * Unit test case for java attribute info.
132 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530133 @Test
134 public void testForJavaAttributeInfo() {
135
Bharat saraswale2d51d62016-03-23 19:40:35 +0530136 String attributeWithoutTypePkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, false);
137 assertThat(true, is(attributeWithoutTypePkg.equals(
138 PRIVATE + SPACE + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
139
140 String attributeWithTypePkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, false);
141 assertThat(true, is(attributeWithTypePkg.equals(PRIVATE + SPACE + JAVA_LANG + PERIOD
142 + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
143
144 String attributeWithListPkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, true);
145 assertThat(true, is(attributeWithListPkg.equals(
146 PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + JAVA_LANG + PERIOD + STRING_DATA_TYPE
147 + DIAMOND_CLOSE_BRACKET + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
148
149 String attributeWithListWithoutPkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, true);
150 assertThat(true, is(attributeWithListWithoutPkg.equals(
151 PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET + SPACE
152 + YANG_NAME + SEMI_COLAN + NEW_LINE)));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530153 }
154}