blob: 5b36403cee0360b4de0b40c8138faea61b34dd5e [file] [log] [blame]
b.janani68c55e12016-02-24 12:23:03 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani68c55e12016-02-24 12:23:03 +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.utils.io.impl;
18
b.janani68c55e12016-02-24 12:23:03 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
Bharat saraswal6ef0b762016-04-05 12:45:45 +053022import org.junit.Rule;
23import org.junit.Test;
24import org.junit.rules.ExpectedException;
25
26import static org.hamcrest.core.Is.is;
27import static org.hamcrest.core.IsNot.not;
28import static org.junit.Assert.assertThat;
29import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
30import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
31import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
32import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
33import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
34import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
35import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
36import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
37import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
38import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
39import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
40import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
b.janani68c55e12016-02-24 12:23:03 +053041
42/**
43 * Tests the java doc that is generated.
44 */
45public final class JavaDocGenTest {
46
Bharat saraswal6ef0b762016-04-05 12:45:45 +053047 private static final String TEST_NAME = "testName";
48 private static final String END_STRING = " */\n";
49
b.janani68c55e12016-02-24 12:23:03 +053050 @Rule
51 public ExpectedException thrown = ExpectedException.none();
52
53 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053054 * This test case checks the content received for the builder class java doc.
b.janani68c55e12016-02-24 12:23:03 +053055 */
56 @Test
57 public void builderClassGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +053058 String builderClassJavaDoc = getJavaDoc(BUILDER_CLASS, TEST_NAME, false, getStubPluginConfig());
Gaurav Agrawal338735b2016-04-18 18:53:11 +053059 assertThat(true, is(builderClassJavaDoc.contains("Represents the builder implementation of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +053060 && builderClassJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053061 }
62
63 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053064 * This test case checks the content received for the builder interface ge java doc.
b.janani68c55e12016-02-24 12:23:03 +053065 */
66 @Test
67 public void builderInterfaceGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +053068 String builderInterfaceJavaDoc = getJavaDoc(BUILDER_INTERFACE, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +053069 assertThat(true,
Bharat saraswal33dfa012016-05-17 19:59:16 +053070 is(builderInterfaceJavaDoc.contains("Builder for")
71 && builderInterfaceJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053072 }
73
74 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053075 * This test case checks the content received for the build java doc.
b.janani68c55e12016-02-24 12:23:03 +053076 */
77 @Test
78 public void buildGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +053079 String buildDoc = getJavaDoc(BUILD_METHOD, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +053080 assertThat(true, is(buildDoc.contains("Builds object of") && buildDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053081 }
82
83 /**
84 * A private constructor is tested.
85 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053086 * @throws SecurityException if any security violation is observed
87 * @throws NoSuchMethodException if when the method is not found
88 * @throws IllegalArgumentException if there is illegal argument found
89 * @throws InstantiationException if instantiation is provoked for the private constructor
90 * @throws IllegalAccessException if instance is provoked or a method is provoked
91 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053092 */
93 @Test
Bharat saraswal33dfa012016-05-17 19:59:16 +053094 public void callPrivateConstructors()
95 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal6ef0b762016-04-05 12:45:45 +053096 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053097
98 Class<?>[] classesToConstruct = {JavaDocGen.class };
99 for (Class<?> clazz : classesToConstruct) {
100 Constructor<?> constructor = clazz.getDeclaredConstructor();
101 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530102 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +0530103 }
104 }
105
106 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530107 * This test case checks the content received for the constructor java doc.
b.janani68c55e12016-02-24 12:23:03 +0530108 */
109 @Test
110 public void constructorGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530111 String constructorDoc = getJavaDoc(CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530112 assertThat(true,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530113 is(constructorDoc.contains("Creates an instance of ")
114 && constructorDoc.contains("builder object of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530115 && constructorDoc.contains("@param") && constructorDoc.contains("*/\n")));
b.janani68c55e12016-02-24 12:23:03 +0530116 }
117
118 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530119 * This test case checks the content received for the default constructor java doc.
b.janani68c55e12016-02-24 12:23:03 +0530120 */
121 @Test
122 public void defaultConstructorGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530123 String defaultConstructorDoc = getJavaDoc(DEFAULT_CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530124 assertThat(true, is(defaultConstructorDoc.contains("Creates an instance of ")
125 && defaultConstructorDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530126 }
127
128 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530129 * This test case checks the content received for the getter java doc.
b.janani68c55e12016-02-24 12:23:03 +0530130 */
131 @Test
132 public void getterGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530133 String getterJavaDoc = getJavaDoc(GETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
134 assertThat(true,
135 is(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530136 }
137
138 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530139 * This test case checks the content received for the impl class java doc.
b.janani68c55e12016-02-24 12:23:03 +0530140 */
141 @Test
142 public void implClassGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530143 String implClassJavaDoc = getJavaDoc(IMPL_CLASS, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530144 assertThat(true,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530145 is(implClassJavaDoc.contains("Represents the implementation of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530146 && implClassJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530147 }
148
149 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530150 * This test case checks the content received for the interface java doc.
b.janani68c55e12016-02-24 12:23:03 +0530151 */
152 @Test
153 public void interfaceGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530154 String interfaceJavaDoc = getJavaDoc(INTERFACE, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530155 assertThat(true,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530156 is(interfaceJavaDoc.contains("Abstraction of an entity which represents the functionality of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530157 && interfaceJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530158 }
159
160 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530161 * This test case checks the content received for the package info java doc.
b.janani68c55e12016-02-24 12:23:03 +0530162 */
163 @Test
164 public void packageInfoGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530165 String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, false, getStubPluginConfig());
166 assertThat(true,
167 is(packageInfo.contains("Implementation of YANG node") && packageInfo.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530168 }
169
170 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530171 * This test case checks the content received for the package info java doc.
172 */
173 @Test
174 public void packageInfoGenerationForChildNodeTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530175 String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, true, getStubPluginConfig());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530176 assertThat(true, is(packageInfo.contains("Implementation of YANG node testName's children nodes")
177 && packageInfo.contains(END_STRING)));
178 }
179
180 /**
181 * This test case checks the content received for the setter java doc.
b.janani68c55e12016-02-24 12:23:03 +0530182 */
183 @Test
184 public void setterGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530185 String setterJavaDoc = getJavaDoc(SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530186 assertThat(true,
187 is(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530188 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530189
190 /**
191 * This test case checks the content received for the typedef setter java doc.
192 */
193 @Test
194 public void typeDefSetterGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530195 String typeDefSetter = getJavaDoc(TYPE_DEF_SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530196 assertThat(true, is(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(END_STRING)));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530197 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530198
199 /**
200 * Returns stub pluginConfig.
201 *
202 * @return stub pluginConfig
203 */
204 private YangPluginConfig getStubPluginConfig() {
205 YangPluginConfig pluginConfig = new YangPluginConfig();
206 pluginConfig.setConflictResolver(null);
207 return pluginConfig;
208 }
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530209}