blob: 9fd9d62848491842fdb6a224d7cfd214fa7d32b0 [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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053025import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053026
27import static org.hamcrest.core.Is.is;
28import static org.hamcrest.core.IsNot.not;
29import static org.junit.Assert.assertThat;
30import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
31import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
32import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
33import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
34import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
35import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
36import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
37import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
38import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
39import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
40import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
41import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
b.janani68c55e12016-02-24 12:23:03 +053042
43/**
44 * Tests the java doc that is generated.
45 */
46public final class JavaDocGenTest {
47
Bharat saraswal6ef0b762016-04-05 12:45:45 +053048 private static final String TEST_NAME = "testName";
49 private static final String END_STRING = " */\n";
50
b.janani68c55e12016-02-24 12:23:03 +053051 @Rule
52 public ExpectedException thrown = ExpectedException.none();
53
54 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053055 * This test case checks the content received for the builder class java doc.
b.janani68c55e12016-02-24 12:23:03 +053056 */
57 @Test
58 public void builderClassGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +053059 String builderClassJavaDoc = getJavaDoc(BUILDER_CLASS, TEST_NAME, false, getStubPluginConfig());
Gaurav Agrawal338735b2016-04-18 18:53:11 +053060 assertThat(true, is(builderClassJavaDoc.contains("Represents the builder implementation of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +053061 && builderClassJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053062 }
63
64 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053065 * This test case checks the content received for the builder interface ge java doc.
b.janani68c55e12016-02-24 12:23:03 +053066 */
67 @Test
68 public void builderInterfaceGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +053069 String builderInterfaceJavaDoc = getJavaDoc(BUILDER_INTERFACE, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +053070 assertThat(true,
Bharat saraswal33dfa012016-05-17 19:59:16 +053071 is(builderInterfaceJavaDoc.contains("Builder for")
72 && builderInterfaceJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053073 }
74
75 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053076 * This test case checks the content received for the build java doc.
b.janani68c55e12016-02-24 12:23:03 +053077 */
78 @Test
79 public void buildGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +053080 String buildDoc = getJavaDoc(BUILD_METHOD, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +053081 assertThat(true, is(buildDoc.contains("Builds object of") && buildDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053082 }
83
84 /**
85 * A private constructor is tested.
86 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053087 * @throws SecurityException if any security violation is observed
88 * @throws NoSuchMethodException if when the method is not found
89 * @throws IllegalArgumentException if there is illegal argument found
90 * @throws InstantiationException if instantiation is provoked for the private constructor
91 * @throws IllegalAccessException if instance is provoked or a method is provoked
92 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053093 */
94 @Test
Bharat saraswal33dfa012016-05-17 19:59:16 +053095 public void callPrivateConstructors()
96 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal6ef0b762016-04-05 12:45:45 +053097 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053098
99 Class<?>[] classesToConstruct = {JavaDocGen.class };
100 for (Class<?> clazz : classesToConstruct) {
101 Constructor<?> constructor = clazz.getDeclaredConstructor();
102 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530103 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +0530104 }
105 }
106
107 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530108 * This test case checks the content received for the constructor java doc.
b.janani68c55e12016-02-24 12:23:03 +0530109 */
110 @Test
111 public void constructorGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530112 String constructorDoc = getJavaDoc(CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530113 assertThat(true,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530114 is(constructorDoc.contains("Creates an instance of ")
115 && constructorDoc.contains("builder object of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530116 && constructorDoc.contains("@param") && constructorDoc.contains("*/\n")));
b.janani68c55e12016-02-24 12:23:03 +0530117 }
118
119 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530120 * This test case checks the content received for the default constructor java doc.
b.janani68c55e12016-02-24 12:23:03 +0530121 */
122 @Test
123 public void defaultConstructorGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530124 String defaultConstructorDoc = getJavaDoc(DEFAULT_CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530125 assertThat(true, is(defaultConstructorDoc.contains("Creates an instance of ")
126 && defaultConstructorDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530127 }
128
129 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530130 * This test case checks the content received for the getter java doc.
b.janani68c55e12016-02-24 12:23:03 +0530131 */
132 @Test
133 public void getterGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530134 String getterJavaDoc = getJavaDoc(GETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
135 assertThat(true,
136 is(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530137 }
138
139 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530140 * This test case checks the content received for the impl class java doc.
b.janani68c55e12016-02-24 12:23:03 +0530141 */
142 @Test
143 public void implClassGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530144 String implClassJavaDoc = getJavaDoc(IMPL_CLASS, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530145 assertThat(true,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530146 is(implClassJavaDoc.contains("Represents the implementation of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530147 && implClassJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530148 }
149
150 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530151 * This test case checks the content received for the interface java doc.
b.janani68c55e12016-02-24 12:23:03 +0530152 */
153 @Test
154 public void interfaceGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530155 String interfaceJavaDoc = getJavaDoc(INTERFACE, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530156 assertThat(true,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530157 is(interfaceJavaDoc.contains("Abstraction of an entity which represents the functionality of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530158 && interfaceJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530159 }
160
161 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530162 * This test case checks the content received for the package info java doc.
b.janani68c55e12016-02-24 12:23:03 +0530163 */
164 @Test
165 public void packageInfoGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530166 String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, false, getStubPluginConfig());
167 assertThat(true,
168 is(packageInfo.contains("Implementation of YANG node") && packageInfo.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530169 }
170
171 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530172 * This test case checks the content received for the package info java doc.
173 */
174 @Test
175 public void packageInfoGenerationForChildNodeTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530176 String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, true, getStubPluginConfig());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530177 assertThat(true, is(packageInfo.contains("Implementation of YANG node testName's children nodes")
178 && packageInfo.contains(END_STRING)));
179 }
180
181 /**
182 * This test case checks the content received for the setter java doc.
b.janani68c55e12016-02-24 12:23:03 +0530183 */
184 @Test
185 public void setterGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530186 String setterJavaDoc = getJavaDoc(SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530187 assertThat(true,
188 is(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530189 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530190
191 /**
192 * This test case checks the content received for the typedef setter java doc.
193 */
194 @Test
195 public void typeDefSetterGenerationTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530196 String typeDefSetter = getJavaDoc(TYPE_DEF_SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530197 assertThat(true, is(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(END_STRING)));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530198 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530199
200 /**
201 * Returns stub pluginConfig.
202 *
203 * @return stub pluginConfig
204 */
205 private YangPluginConfig getStubPluginConfig() {
206 YangPluginConfig pluginConfig = new YangPluginConfig();
207 pluginConfig.setConflictResolver(null);
208 return pluginConfig;
209 }
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530210}