blob: e5021d37f31911f215b4fb91ad871ccc52709a3a [file] [log] [blame]
Bharat saraswal4bf8b152016-02-25 02:26:43 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal4bf8b152016-02-25 02:26:43 +05303 *
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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053023import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
Bharat saraswale2d51d62016-03-23 19:40:35 +053024
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.core.Is.is;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053027import static org.hamcrest.core.IsNot.not;
Bharat saraswale2d51d62016-03-23 19:40:35 +053028import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getImportText;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053029import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefinition;
Bharat saraswale2d51d62016-03-23 19:40:35 +053030import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswale2d51d62016-03-23 19:40:35 +053031import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
32import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
33import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
34import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
Bharat saraswale2d51d62016-03-23 19:40:35 +053035import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
36import static org.onosproject.yangutils.utils.UtilConstants.LIST;
37import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053038import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
39import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053040import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
41import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
42import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
Vinod Kumar S38046502016-03-23 15:30:27 +053043
Bharat saraswal4bf8b152016-02-25 02:26:43 +053044/**
45 * Unit test cases for java code snippet generator.
46 */
47public class JavaCodeSnippetGenTest {
48
49 private static final String PKG_INFO = "org.onosproject.unittest";
50 private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
Bharat saraswal4bf8b152016-02-25 02:26:43 +053051 private static final String YANG_NAME = "Test";
Bharat saraswal4bf8b152016-02-25 02:26:43 +053052
53 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053054 * Unit test for private constructor.
55 *
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053056 * @throws SecurityException if any security violation is observed
57 * @throws NoSuchMethodException if when the method is not found
58 * @throws IllegalArgumentException if there is illegal argument found
59 * @throws InstantiationException if instantiation is provoked for the private constructor
60 * @throws IllegalAccessException if instance is provoked or a method is provoked
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053061 * @throws InvocationTargetException when an exception occurs by the method or constructor
62 */
63 @Test
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053064 public void callPrivateConstructors()
65 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053066 InstantiationException, IllegalAccessException, InvocationTargetException {
Bharat saraswale2d51d62016-03-23 19:40:35 +053067
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053068 Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class};
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053069 for (Class<?> clazz : classesToConstruct) {
70 Constructor<?> constructor = clazz.getDeclaredConstructor();
71 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053072 assertThat(null, not(constructor.newInstance()));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053073 }
74 }
75
76 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +053077 * Unit test case for import text.
78 */
79 @Test
80 public void testForImportText() {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053081 JavaQualifiedTypeInfoTranslator importInfo = new JavaQualifiedTypeInfoTranslator();
Bharat saraswal4bf8b152016-02-25 02:26:43 +053082 importInfo.setPkgInfo(PKG_INFO);
83 importInfo.setClassInfo(CLASS_INFO);
84
Bharat saraswale2d51d62016-03-23 19:40:35 +053085 String imports = getImportText(importInfo);
Bharat saraswal4bf8b152016-02-25 02:26:43 +053086
Bharat saraswale2d51d62016-03-23 19:40:35 +053087 assertThat(true, is(imports.equals(IMPORT + PKG_INFO + PERIOD + CLASS_INFO + SEMI_COLAN + NEW_LINE)));
Bharat saraswal4bf8b152016-02-25 02:26:43 +053088 }
89
90 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053091 * Unit test case for java class interface definition close.
Bharat saraswal4bf8b152016-02-25 02:26:43 +053092 */
93 @Test
Bharat saraswale2d51d62016-03-23 19:40:35 +053094 public void testForJavaClassDefClose() {
Bharat saraswale2d51d62016-03-23 19:40:35 +053095 String interfaceDef = getJavaClassDefClose();
96 assertThat(true, is(interfaceDef.equals(CLOSE_CURLY_BRACKET)));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053097 }
98
99 /**
100 * Unit test case for java attribute info.
101 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530102 @Test
103 public void testForJavaAttributeInfo() {
104
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530105 String attributeWithoutTypePkg = getJavaAttributeDefinition(null, STRING_DATA_TYPE, YANG_NAME,
106 false, PRIVATE);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530107 assertThat(true, is(attributeWithoutTypePkg.equals(
108 PRIVATE + SPACE + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
109
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530110 String attributeWithTypePkg = getJavaAttributeDefinition(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME,
111 false, PRIVATE);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530112 assertThat(true, is(attributeWithTypePkg.equals(PRIVATE + SPACE + JAVA_LANG + PERIOD
113 + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
114
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530115 String attributeWithListPkg = getJavaAttributeDefinition(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME,
116 true, PRIVATE);
117 assertThat(true, is(attributeWithListPkg.contains(
Bharat saraswale2d51d62016-03-23 19:40:35 +0530118 PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + JAVA_LANG + PERIOD + STRING_DATA_TYPE
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530119 + DIAMOND_CLOSE_BRACKET + SPACE + YANG_NAME)));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530121 String attributeWithListWithoutPkg = getJavaAttributeDefinition(null, STRING_DATA_TYPE, YANG_NAME,
122 true, PRIVATE);
123 assertThat(true, is(attributeWithListWithoutPkg.contains(
Bharat saraswale2d51d62016-03-23 19:40:35 +0530124 PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET + SPACE
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530125 + YANG_NAME)));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530126 }
127}