[ONOS-4350] Inter Jar dependency implementation and code restructuring.
Change-Id: Iacac75e4187aed93ce1754c170a9c19707e5b8c3
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
new file mode 100644
index 0000000..40ac92c
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Calendar;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.apache.commons.io.FileUtils.contentEquals;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader;
+
+/**
+ * Unit Tests for the CopyrightHeader contents.
+ */
+public final class CopyrightHeaderTest {
+
+ private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
+ + "-present Open Networking Laboratory\n";
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * Unit test for testing private constructor.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {CopyrightHeader.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks the received copyright header contents.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void testGetCopyrightHeader() throws IOException {
+
+ String path = "src/test/resources/CopyrightHeader.txt";
+
+ File testRsc = new File(path);
+ FileInputStream in = new FileInputStream(testRsc);
+
+ File testFile = new File("target/TestHeader.txt");
+ FileOutputStream out = new FileOutputStream(testFile);
+
+ out.write(COPYRIGHTS_FIRST_LINE.getBytes());
+ int c = 0;
+ while ((c = in.read()) != -1) {
+ out.write(c);
+ }
+
+ String licenseHeader = getCopyrightHeader();
+ File test = new File("target/TestCopyrightHeader.txt");
+
+ FileWriter writer = new FileWriter(test);
+ writer.write(licenseHeader);
+ writer.close();
+ out.close();
+ out.flush();
+ in.close();
+
+ assertThat(true, is(contentEquals(test, testFile)));
+ test.delete();
+ testFile.delete();
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtilTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtilTest.java
new file mode 100644
index 0000000..5fd728b
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtilTest.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
+import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.doesPackageExist;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
+
+/**
+ * Tests the file handle utilities.
+ */
+public final class FileSystemUtilTest {
+
+ private static final String BASE_DIR_PKG = "target.UnitTestCase.";
+ private static final String BASE_PKG = "target/UnitTestCase";
+ private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
+ private static final String TEST_DATA_2 = "This is next second line\n";
+ private static final String TEST_DATA_3 = "This is next third line in the file";
+ private static final String TEST_FILE = "testFile";
+ private static final String SOURCE_TEST_FILE = "sourceTestFile";
+ private static final String DIR_PATH = "exist1.exist2.exist3";
+ private static final String PKG_INFO = "package-info.java";
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {FileSystemUtil.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks the contents to be written in the file.
+ *
+ * @throws IOException when fails to create a test file
+ */
+ @Test
+ public void updateFileHandleTest() throws IOException {
+
+ File dir = new File(BASE_PKG + SLASH + TEST_FILE);
+ dir.mkdirs();
+ File createFile = new File(dir + TEST_FILE);
+ createFile.createNewFile();
+ File createSourceFile = new File(dir + SOURCE_TEST_FILE);
+ createSourceFile.createNewFile();
+ updateFileHandle(createFile, TEST_DATA_1, false);
+ updateFileHandle(createFile, TEST_DATA_2, false);
+ updateFileHandle(createFile, TEST_DATA_3, false);
+ appendFileContents(createFile, createSourceFile);
+ updateFileHandle(createFile, null, true);
+ deleteDirectory(dir);
+ }
+
+ /**
+ * This test case checks whether the package is existing.
+ *
+ * @throws IOException when failed to create a test file
+ */
+ @Test
+ public void packageExistTest() throws IOException {
+
+ String strPath = BASE_DIR_PKG + DIR_PATH;
+ File createDir = new File(strPath.replace(PERIOD, SLASH));
+ createDir.mkdirs();
+ File createFile = new File(createDir + SLASH + PKG_INFO);
+ createFile.createNewFile();
+ assertThat(true, is(doesPackageExist(strPath)));
+ createPackage(getStubNode());
+ createDir.delete();
+ deleteDirectory(createDir);
+ }
+
+ /**
+ * Returns stub YANG node.
+ *
+ * @return stub node
+ */
+ private YangNode getStubNode() {
+ YangJavaModule module = new YangJavaModule();
+ module.setName(TEST_DATA_1);
+ JavaFileInfo javafileInfo = new JavaFileInfo();
+ javafileInfo.setJavaName(TEST_DATA_1);
+ javafileInfo.setBaseCodeGenPath("");
+ javafileInfo.setPackageFilePath(BASE_PKG);
+ javafileInfo.setPluginConfig(getStubPluginConfig());
+ module.setJavaFileInfo(javafileInfo);
+ return module;
+ }
+
+ /**
+ * Returns stub pluginConfig.
+ *
+ * @return stub pluginConfig
+ */
+ private YangPluginConfig getStubPluginConfig() {
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ pluginConfig.setConflictResolver(null);
+ return pluginConfig;
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
new file mode 100644
index 0000000..2e9f436
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
+
+/**
+ * Tests the java doc that is generated.
+ */
+public final class JavaDocGenTest {
+
+ private static final String TEST_NAME = "testName";
+ private static final String END_STRING = " */\n";
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * This test case checks the content received for the builder class java doc.
+ */
+ @Test
+ public void builderClassGenerationTest() {
+ String builderClassJavaDoc = getJavaDoc(BUILDER_CLASS, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(builderClassJavaDoc.contains("Represents the builder implementation of")
+ && builderClassJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the builder interface ge java doc.
+ */
+ @Test
+ public void builderInterfaceGenerationTest() {
+ String builderInterfaceJavaDoc = getJavaDoc(BUILDER_INTERFACE, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(builderInterfaceJavaDoc.contains("Builder for")
+ && builderInterfaceJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the build java doc.
+ */
+ @Test
+ public void buildGenerationTest() {
+ String buildDoc = getJavaDoc(BUILD_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(buildDoc.contains("Builds object of") && buildDoc.contains(END_STRING)));
+ }
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {JavaDocGen.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks the content received for the constructor java doc.
+ */
+ @Test
+ public void constructorGenerationTest() {
+ String constructorDoc = getJavaDoc(CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(constructorDoc.contains("Creates an instance of ")
+ && constructorDoc.contains("builder object of")
+ && constructorDoc.contains("@param") && constructorDoc.contains("*/\n")));
+ }
+
+ /**
+ * This test case checks the content received for the default constructor java doc.
+ */
+ @Test
+ public void defaultConstructorGenerationTest() {
+ String defaultConstructorDoc = getJavaDoc(DEFAULT_CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(defaultConstructorDoc.contains("Creates an instance of ")
+ && defaultConstructorDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the getter java doc.
+ */
+ @Test
+ public void getterGenerationTest() {
+ String getterJavaDoc = getJavaDoc(GETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the impl class java doc.
+ */
+ @Test
+ public void implClassGenerationTest() {
+ String implClassJavaDoc = getJavaDoc(IMPL_CLASS, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(implClassJavaDoc.contains("Represents the implementation of")
+ && implClassJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the interface java doc.
+ */
+ @Test
+ public void interfaceGenerationTest() {
+ String interfaceJavaDoc = getJavaDoc(INTERFACE, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(interfaceJavaDoc.contains("Abstraction of an entity which represents the functionality of")
+ && interfaceJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the package info java doc.
+ */
+ @Test
+ public void packageInfoGenerationTest() {
+ String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(packageInfo.contains("Implementation of YANG node") && packageInfo.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the package info java doc.
+ */
+ @Test
+ public void packageInfoGenerationForChildNodeTest() {
+ String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, true, getStubPluginConfig());
+ assertThat(true, is(packageInfo.contains("Implementation of YANG node testName's children nodes")
+ && packageInfo.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the setter java doc.
+ */
+ @Test
+ public void setterGenerationTest() {
+ String setterJavaDoc = getJavaDoc(SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the typedef setter java doc.
+ */
+ @Test
+ public void typeDefSetterGenerationTest() {
+ String typeDefSetter = getJavaDoc(TYPE_DEF_SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(END_STRING)));
+ }
+
+ /**
+ * Returns stub pluginConfig.
+ *
+ * @return stub pluginConfig
+ */
+ private YangPluginConfig getStubPluginConfig() {
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ pluginConfig.setConflictResolver(null);
+ return pluginConfig;
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java
new file mode 100644
index 0000000..566d24b
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static java.io.File.separator;
+
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles;
+import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles;
+
+/**
+ * Test the file scanner service.
+ */
+public final class YangFileScannerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ String baseDir = "target/UnitTestCase";
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {YangFileScanner.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks for a .java file inside the specified dir.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void checkJavaFileInsideDirTest() throws IOException {
+
+ String dir = baseDir + separator + "scanner2";
+ File path = createDirectory(dir);
+ createFile(path, "testScanner.java");
+ List<String> dirContents = getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ expectedContents.add(path.getCanonicalPath() + separator + "testScanner.java");
+ assertThat(true, is(dirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+ /**
+ * Method used for creating multiple directories inside the target file.
+ *
+ * @param path where directories should be created
+ * @return the directory path that is created
+ */
+ private File createDirectory(String path) {
+
+ File myDir = new File(path);
+ myDir.mkdirs();
+ return myDir;
+ }
+
+ /**
+ * Method used for creating file inside the specified directory.
+ *
+ * @param myDir the path where file has to be created inside
+ * @param fileName the name of the file to be created
+ */
+ private void createFile(File myDir, String fileName) throws IOException {
+
+ File file = null;
+ file = new File(myDir + separator + fileName);
+ file.createNewFile();
+ }
+
+ /**
+ * This testcase checks for a java file inside an empty directory.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void emptyDirJavaScannerTest() throws IOException {
+
+ String emptyDir = baseDir + separator + "scanner1";
+ File path = createDirectory(emptyDir);
+ List<String> emptyDirContents = getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertThat(true, is(emptyDirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+ /**
+ * This testcase checks for a yang file inside an empty directory.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void emptyDirYangScannerTest() throws IOException {
+
+ String emptyYangDir = baseDir + separator + "scanner1";
+ File path = createDirectory(emptyYangDir);
+ List<String> emptyDirContents = getYangFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertThat(true, is(emptyDirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+ /**
+ * This test case checks with the sub directories in the given path for java files.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void emptySubDirScannerTest() throws IOException {
+
+ String dir = baseDir + separator + "scanner3";
+ File path = createDirectory(dir);
+ String subDir = path.toString() + separator + "subDir1";
+ createDirectory(subDir);
+ createFile(path, "invalidFile.txt");
+ List<String> emptySubDirContents = getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertThat(true, is(emptySubDirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
new file mode 100644
index 0000000..c10ed03
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.project.MavenProject;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+import org.onosproject.yangutils.utils.UtilConstants;
+import org.sonatype.plexus.build.incremental.BuildContext;
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;
+
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToCompilationRoot;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
+
+/**
+ * Unit tests for YANG IO utils.
+ */
+public final class YangIoUtilsTest {
+
+ private static final String BASE_DIR = "target/UnitTestCase";
+ private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
+ private static final String CHECK_STRING = "one, two, three, four, five, six";
+ private static final String TRIM_STRING = "one, two, three, four, five, ";
+ private static final String CHECK1 = "check1";
+ private static final String PKG_INFO = "package-info.java";
+ private static final String PATH = "src/main/yangmodel/";
+ private static final String MSG = "Exception occured while creating package info file.";
+
+ /**
+ * Expected exceptions.
+ */
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * This test case checks whether the package-info file is created.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoTest() throws IOException {
+
+ File dirPath = new File(CREATE_PATH);
+ dirPath.mkdirs();
+ addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
+ File filePath = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath.isFile(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks with an additional info in the path.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoWithPathTest() throws IOException {
+
+ File dirPath = new File(CREATE_PATH);
+ dirPath.mkdirs();
+ addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig());
+ File filePath = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath.isFile(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks with a child node.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoWithChildNode() throws IOException {
+
+ File dirPath = new File(CREATE_PATH);
+ dirPath.mkdirs();
+ addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig());
+ File filePath = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath.isFile(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks whether the package-info file is created when invalid path is given.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoWithEmptyPathTest() throws IOException {
+
+ File dirPath = new File("invalid/check");
+ thrown.expect(IOException.class);
+ thrown.expectMessage(MSG);
+ addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
+ File filePath1 = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath1.isFile(), is(false));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {YangIoUtils.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks if the directory is cleaned.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void cleanGeneratedDirTest() throws IOException {
+
+ File baseDirPath = new File(BASE_DIR);
+ File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
+ createNewDir.mkdirs();
+ File createFile = new File(createNewDir + File.separator + "check1.java");
+ createFile.createNewFile();
+ deleteDirectory(baseDirPath.getAbsolutePath());
+ FileUtils.deleteDirectory(createNewDir);
+ FileUtils.deleteDirectory(baseDirPath);
+ }
+
+ /**
+ * This test case checks the cleaning method when an invalid path is provided.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void cleanWithInvalidDirTest() throws IOException {
+
+ File baseDirPath = new File(BASE_DIR + "invalid");
+ deleteDirectory(baseDirPath.getAbsolutePath());
+ }
+
+ /**
+ * This test case tests whether the directories are getting created.
+ */
+ @Test
+ public void createDirectoryTest() throws IOException {
+
+ File dirPath = createDirectories(CREATE_PATH);
+ assertThat(dirPath.isDirectory(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks whether the source is getting added.
+ */
+ @Test
+ public void testForAddSource() throws IOException {
+
+ MavenProject project = new MavenProject();
+ BuildContext context = new DefaultBuildContext();
+ File sourceDir = new File(BASE_DIR + File.separator + "yang");
+ sourceDir.mkdirs();
+ addToCompilationRoot(sourceDir.toString(), project, context);
+ FileUtils.deleteDirectory(sourceDir);
+ }
+
+ /**
+ * Unit test case for trim at last method.
+ */
+ @Test
+ public void testForTrimAtLast() {
+
+ String test = trimAtLast(CHECK_STRING, "six");
+ assertThat(test.contains(TRIM_STRING), is(true));
+ }
+
+ /**
+ * Returns stub pluginConfig.
+ *
+ * @return stub pluginConfig
+ */
+ private YangPluginConfig getStubPluginConfig() {
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ pluginConfig.setConflictResolver(null);
+ return pluginConfig;
+ }
+}