blob: 200a4480e3102cee0790df0257faabd4ddd3f9c5 [file] [log] [blame]
Bharat saraswald6f12412016-03-28 15:50:13 +05301
b.janani68c55e12016-02-24 12:23:03 +05302/*
3 * Copyright 2016 Open Networking Laboratory
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.onosproject.yangutils.utils.io.impl;
19
b.janani68c55e12016-02-24 12:23:03 +053020import java.io.File;
21import java.io.IOException;
Bharat saraswald6f12412016-03-28 15:50:13 +053022import java.lang.reflect.Constructor;
23import java.lang.reflect.InvocationTargetException;
b.janani68c55e12016-02-24 12:23:03 +053024
25import org.apache.maven.project.MavenProject;
Bharat saraswald6f12412016-03-28 15:50:13 +053026import org.junit.Rule;
27import org.junit.Test;
28import org.junit.rules.ExpectedException;
29import org.onosproject.yangutils.utils.UtilConstants;
b.janani68c55e12016-02-24 12:23:03 +053030import org.sonatype.plexus.build.incremental.BuildContext;
31import org.sonatype.plexus.build.incremental.DefaultBuildContext;
32
b.janani68c55e12016-02-24 12:23:03 +053033import static org.hamcrest.core.Is.is;
Bharat saraswald6f12412016-03-28 15:50:13 +053034import static org.junit.Assert.assertNotNull;
35import static org.junit.Assert.assertThat;
36import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
37import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
38import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
39import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
40import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani68c55e12016-02-24 12:23:03 +053041
42/**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053043 * Unit tests for YANG io utils.
b.janani68c55e12016-02-24 12:23:03 +053044 */
45public final class YangIoUtilsTest {
46
Bharat saraswald6f12412016-03-28 15:50:13 +053047 private static final String BASE_DIR = "target/UnitTestCase";
48 private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
49 private static final String CHECK_STRING = "one, two, three, four, five, six";
50 private static final String TRIM_STRING = "one, two, three, four, five, ";
b.janani68c55e12016-02-24 12:23:03 +053051
Bharat saraswald6f12412016-03-28 15:50:13 +053052 /**
53 * Expected exceptions.
54 */
b.janani68c55e12016-02-24 12:23:03 +053055 @Rule
56 public ExpectedException thrown = ExpectedException.none();
57
58 /**
59 * This test case checks whether the package-info file is created.
Bharat saraswald6f12412016-03-28 15:50:13 +053060 *
61 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +053062 */
63 @Test
64 public void addPackageInfoTest() throws IOException {
65
Bharat saraswald6f12412016-03-28 15:50:13 +053066 File dirPath = new File(CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +053067 dirPath.mkdirs();
Bharat saraswald6f12412016-03-28 15:50:13 +053068 addPackageInfo(dirPath, "check1", CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +053069 File filePath = new File(dirPath + File.separator + "package-info.java");
70 assertThat(filePath.isFile(), is(true));
71 }
72
73 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053074 * This test case checks with an additional info in the path.
Bharat saraswald6f12412016-03-28 15:50:13 +053075 *
76 * @throws IOException when fails to do IO operations for test case
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053077 */
78 @Test
79 public void addPackageInfoWithPathTest() throws IOException {
80
Bharat saraswald6f12412016-03-28 15:50:13 +053081 File dirPath = new File(CREATE_PATH);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053082 dirPath.mkdirs();
Bharat saraswald6f12412016-03-28 15:50:13 +053083 addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + CREATE_PATH);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053084 File filePath = new File(dirPath + File.separator + "package-info.java");
85 assertThat(filePath.isFile(), is(true));
86 }
87
88 /**
b.janani68c55e12016-02-24 12:23:03 +053089 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswald6f12412016-03-28 15:50:13 +053090 *
91 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +053092 */
93 @Test
94 public void addPackageInfoWithEmptyPathTest() throws IOException {
95
b.janani1fef2732016-03-04 12:29:05 +053096 File dirPath = new File("invalid/check");
b.janani68c55e12016-02-24 12:23:03 +053097 thrown.expect(IOException.class);
98 thrown.expectMessage("Exception occured while creating package info file.");
Bharat saraswald6f12412016-03-28 15:50:13 +053099 addPackageInfo(dirPath, "check1", CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +0530100 File filePath1 = new File(dirPath + File.separator + "package-info.java");
101 assertThat(filePath1.isFile(), is(false));
102 }
103
104 /**
105 * A private constructor is tested.
106 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530107 * @throws SecurityException if any security violation is observed
108 * @throws NoSuchMethodException if when the method is not found
109 * @throws IllegalArgumentException if there is illegal argument found
110 * @throws InstantiationException if instantiation is provoked for the private constructor
111 * @throws IllegalAccessException if instance is provoked or a method is provoked
112 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +0530113 */
114 @Test
115 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +0530117
118 Class<?>[] classesToConstruct = {YangIoUtils.class };
119 for (Class<?> clazz : classesToConstruct) {
120 Constructor<?> constructor = clazz.getDeclaredConstructor();
121 constructor.setAccessible(true);
122 assertNotNull(constructor.newInstance());
123 }
124 }
125
126 /**
127 * This test case checks if the directory is cleaned.
Bharat saraswald6f12412016-03-28 15:50:13 +0530128 *
129 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +0530130 */
131 @Test
132 public void cleanGeneratedDirTest() throws IOException {
133
Bharat saraswald6f12412016-03-28 15:50:13 +0530134 File baseDirPath = new File(BASE_DIR);
135 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani68c55e12016-02-24 12:23:03 +0530136 createNewDir.mkdirs();
137 File createFile = new File(createNewDir + File.separator + "check1.java");
138 createFile.createNewFile();
Bharat saraswald6f12412016-03-28 15:50:13 +0530139 clean(baseDirPath.getAbsolutePath());
b.janani68c55e12016-02-24 12:23:03 +0530140 }
141
142 /**
143 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswald6f12412016-03-28 15:50:13 +0530144 *
145 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +0530146 */
147 @Test
148 public void cleanWithInvalidDirTest() throws IOException {
149
Bharat saraswald6f12412016-03-28 15:50:13 +0530150 File baseDirPath = new File(BASE_DIR + "invalid");
151 clean(baseDirPath.getAbsolutePath());
b.janani68c55e12016-02-24 12:23:03 +0530152 }
153
154 /**
155 * This test case tests whether the directories are getting created.
156 */
157 @Test
158 public void createDirectoryTest() {
159
Bharat saraswald6f12412016-03-28 15:50:13 +0530160 File dirPath = createDirectories(CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +0530161 assertThat(dirPath.isDirectory(), is(true));
162 }
163
164 /**
Bharat saraswald6f12412016-03-28 15:50:13 +0530165 * This test case checks whether the source is getting added.
b.janani68c55e12016-02-24 12:23:03 +0530166 */
167 @Test
168 public void testForAddSource() {
169
170 MavenProject project = new MavenProject();
171 BuildContext context = new DefaultBuildContext();
Bharat saraswald6f12412016-03-28 15:50:13 +0530172 File sourceDir = new File(BASE_DIR + File.separator + "yang");
b.janani68c55e12016-02-24 12:23:03 +0530173 sourceDir.mkdirs();
Bharat saraswald6f12412016-03-28 15:50:13 +0530174 addToSource(sourceDir.toString(), project, context);
175 }
176
177 /*
178 * Unit test case for trim at last method.
179 */
180 @Test
181 public void testForTrimAtLast() {
182
183 String test = trimAtLast(CHECK_STRING, "six");
184 assertThat(test.contains(TRIM_STRING), is(true));
b.janani68c55e12016-02-24 12:23:03 +0530185 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530186}