blob: c4558f6b22087bfc97ec81993d65ff62092ac156 [file] [log] [blame]
b.janani66b749c2016-02-24 12:23:03 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani66b749c2016-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.janani66b749c2016-02-24 12:23:03 +053019import java.io.File;
20import java.io.IOException;
Bharat saraswalc34b9442016-03-28 15:50:13 +053021import java.lang.reflect.Constructor;
22import java.lang.reflect.InvocationTargetException;
b.janani66b749c2016-02-24 12:23:03 +053023
24import org.apache.maven.project.MavenProject;
Bharat saraswalc34b9442016-03-28 15:50:13 +053025import org.junit.Rule;
26import org.junit.Test;
27import org.junit.rules.ExpectedException;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053028import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Bharat saraswalc34b9442016-03-28 15:50:13 +053029import org.onosproject.yangutils.utils.UtilConstants;
b.janani66b749c2016-02-24 12:23:03 +053030import org.sonatype.plexus.build.incremental.BuildContext;
31import org.sonatype.plexus.build.incremental.DefaultBuildContext;
32
b.janani66b749c2016-02-24 12:23:03 +053033import static org.hamcrest.core.Is.is;
Bharat saraswal780eca32016-04-05 12:45:45 +053034import static org.hamcrest.core.IsNot.not;
Bharat saraswalc34b9442016-03-28 15:50:13 +053035import 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;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053038import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswalc34b9442016-03-28 15:50:13 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
40import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani66b749c2016-02-24 12:23:03 +053041
42/**
Bharat saraswal780eca32016-04-05 12:45:45 +053043 * Unit tests for YANG IO utils.
b.janani66b749c2016-02-24 12:23:03 +053044 */
45public final class YangIoUtilsTest {
46
Bharat saraswalc34b9442016-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, ";
Bharat saraswal250a7472016-05-12 13:16:57 +053051 private static final String CHECK1 = "check1";
52 private static final String PKG_INFO = "package-info.java";
53 private static final String PATH = "src/main/yangmodel/";
54 private static final String MSG = "Exception occured while creating package info file.";
b.janani66b749c2016-02-24 12:23:03 +053055
Bharat saraswalc34b9442016-03-28 15:50:13 +053056 /**
57 * Expected exceptions.
58 */
b.janani66b749c2016-02-24 12:23:03 +053059 @Rule
60 public ExpectedException thrown = ExpectedException.none();
61
62 /**
63 * This test case checks whether the package-info file is created.
Bharat saraswalc34b9442016-03-28 15:50:13 +053064 *
65 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +053066 */
67 @Test
68 public void addPackageInfoTest() throws IOException {
69
Bharat saraswalc34b9442016-03-28 15:50:13 +053070 File dirPath = new File(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +053071 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053072 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053073 File filePath = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +053074 assertThat(filePath.isFile(), is(true));
75 }
76
77 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053078 * This test case checks with an additional info in the path.
Bharat saraswalc34b9442016-03-28 15:50:13 +053079 *
80 * @throws IOException when fails to do IO operations for test case
Bharat saraswal022dae92016-03-04 20:08:09 +053081 */
82 @Test
83 public void addPackageInfoWithPathTest() throws IOException {
84
Bharat saraswalc34b9442016-03-28 15:50:13 +053085 File dirPath = new File(CREATE_PATH);
Bharat saraswal022dae92016-03-04 20:08:09 +053086 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053087 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053088 File filePath = new File(dirPath + File.separator + PKG_INFO);
89 assertThat(filePath.isFile(), is(true));
90 }
91
92 /**
93 * This test case checks with a child node.
94 *
95 * @throws IOException when fails to do IO operations for test case
96 */
97 @Test
98 public void addPackageInfoWithChildNode() throws IOException {
99
100 File dirPath = new File(CREATE_PATH);
101 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530102 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530103 File filePath = new File(dirPath + File.separator + PKG_INFO);
Bharat saraswal022dae92016-03-04 20:08:09 +0530104 assertThat(filePath.isFile(), is(true));
105 }
106
107 /**
b.janani66b749c2016-02-24 12:23:03 +0530108 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530109 *
110 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530111 */
112 @Test
113 public void addPackageInfoWithEmptyPathTest() throws IOException {
114
b.jananie6d43af2016-03-04 12:29:05 +0530115 File dirPath = new File("invalid/check");
b.janani66b749c2016-02-24 12:23:03 +0530116 thrown.expect(IOException.class);
Bharat saraswal250a7472016-05-12 13:16:57 +0530117 thrown.expectMessage(MSG);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530118 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530119 File filePath1 = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +0530120 assertThat(filePath1.isFile(), is(false));
121 }
122
123 /**
124 * A private constructor is tested.
125 *
Bharat saraswal022dae92016-03-04 20:08:09 +0530126 * @throws SecurityException if any security violation is observed
127 * @throws NoSuchMethodException if when the method is not found
128 * @throws IllegalArgumentException if there is illegal argument found
129 * @throws InstantiationException if instantiation is provoked for the private constructor
130 * @throws IllegalAccessException if instance is provoked or a method is provoked
131 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +0530132 */
133 @Test
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530134 public void callPrivateConstructors()
135 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +0530137
138 Class<?>[] classesToConstruct = {YangIoUtils.class };
139 for (Class<?> clazz : classesToConstruct) {
140 Constructor<?> constructor = clazz.getDeclaredConstructor();
141 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +0530142 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +0530143 }
144 }
145
146 /**
147 * This test case checks if the directory is cleaned.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530148 *
149 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530150 */
151 @Test
152 public void cleanGeneratedDirTest() throws IOException {
153
Bharat saraswalc34b9442016-03-28 15:50:13 +0530154 File baseDirPath = new File(BASE_DIR);
155 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani66b749c2016-02-24 12:23:03 +0530156 createNewDir.mkdirs();
157 File createFile = new File(createNewDir + File.separator + "check1.java");
158 createFile.createNewFile();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530159 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530160 }
161
162 /**
163 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530164 *
165 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530166 */
167 @Test
168 public void cleanWithInvalidDirTest() throws IOException {
169
Bharat saraswalc34b9442016-03-28 15:50:13 +0530170 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530171 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530172 }
173
174 /**
175 * This test case tests whether the directories are getting created.
176 */
177 @Test
178 public void createDirectoryTest() {
179
Bharat saraswalc34b9442016-03-28 15:50:13 +0530180 File dirPath = createDirectories(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +0530181 assertThat(dirPath.isDirectory(), is(true));
182 }
183
184 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530185 * This test case checks whether the source is getting added.
b.janani66b749c2016-02-24 12:23:03 +0530186 */
187 @Test
188 public void testForAddSource() {
189
190 MavenProject project = new MavenProject();
191 BuildContext context = new DefaultBuildContext();
Bharat saraswalc34b9442016-03-28 15:50:13 +0530192 File sourceDir = new File(BASE_DIR + File.separator + "yang");
b.janani66b749c2016-02-24 12:23:03 +0530193 sourceDir.mkdirs();
Bharat saraswalc34b9442016-03-28 15:50:13 +0530194 addToSource(sourceDir.toString(), project, context);
195 }
196
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530197 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530198 * Unit test case for trim at last method.
199 */
200 @Test
201 public void testForTrimAtLast() {
202
203 String test = trimAtLast(CHECK_STRING, "six");
204 assertThat(test.contains(TRIM_STRING), is(true));
b.janani66b749c2016-02-24 12:23:03 +0530205 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530206
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530207 /**
208 * Returns stub pluginConfig.
209 *
210 * @return stub pluginConfig
211 */
212 private YangPluginConfig getStubPluginConfig() {
213 YangPluginConfig pluginConfig = new YangPluginConfig();
214 pluginConfig.setConflictResolver(null);
215 return pluginConfig;
216 }
Bharat saraswal022dae92016-03-04 20:08:09 +0530217}