blob: b9395167e25d78a107a386eec64d9f7e6a924b11 [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.io.File;
20import java.io.IOException;
Bharat saraswald6f12412016-03-28 15:50:13 +053021import java.lang.reflect.Constructor;
22import java.lang.reflect.InvocationTargetException;
b.janani68c55e12016-02-24 12:23:03 +053023
24import org.apache.maven.project.MavenProject;
Bharat saraswald6f12412016-03-28 15:50:13 +053025import org.junit.Rule;
26import org.junit.Test;
27import org.junit.rules.ExpectedException;
28import org.onosproject.yangutils.utils.UtilConstants;
b.janani68c55e12016-02-24 12:23:03 +053029import org.sonatype.plexus.build.incremental.BuildContext;
30import org.sonatype.plexus.build.incremental.DefaultBuildContext;
31
b.janani68c55e12016-02-24 12:23:03 +053032import static org.hamcrest.core.Is.is;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053033import static org.hamcrest.core.IsNot.not;
Bharat saraswald6f12412016-03-28 15:50:13 +053034import static org.junit.Assert.assertThat;
35import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
36import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswald6f12412016-03-28 15:50:13 +053038import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
39import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani68c55e12016-02-24 12:23:03 +053040
41/**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053042 * Unit tests for YANG IO utils.
b.janani68c55e12016-02-24 12:23:03 +053043 */
44public final class YangIoUtilsTest {
45
Bharat saraswald6f12412016-03-28 15:50:13 +053046 private static final String BASE_DIR = "target/UnitTestCase";
47 private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
48 private static final String CHECK_STRING = "one, two, three, four, five, six";
49 private static final String TRIM_STRING = "one, two, three, four, five, ";
b.janani68c55e12016-02-24 12:23:03 +053050
Bharat saraswald6f12412016-03-28 15:50:13 +053051 /**
52 * Expected exceptions.
53 */
b.janani68c55e12016-02-24 12:23:03 +053054 @Rule
55 public ExpectedException thrown = ExpectedException.none();
56
57 /**
58 * This test case checks whether the package-info file is created.
Bharat saraswald6f12412016-03-28 15:50:13 +053059 *
60 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +053061 */
62 @Test
63 public void addPackageInfoTest() throws IOException {
64
Bharat saraswald6f12412016-03-28 15:50:13 +053065 File dirPath = new File(CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +053066 dirPath.mkdirs();
Bharat saraswald6f12412016-03-28 15:50:13 +053067 addPackageInfo(dirPath, "check1", CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +053068 File filePath = new File(dirPath + File.separator + "package-info.java");
69 assertThat(filePath.isFile(), is(true));
70 }
71
72 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053073 * This test case checks with an additional info in the path.
Bharat saraswald6f12412016-03-28 15:50:13 +053074 *
75 * @throws IOException when fails to do IO operations for test case
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053076 */
77 @Test
78 public void addPackageInfoWithPathTest() throws IOException {
79
Bharat saraswald6f12412016-03-28 15:50:13 +053080 File dirPath = new File(CREATE_PATH);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053081 dirPath.mkdirs();
Bharat saraswald6f12412016-03-28 15:50:13 +053082 addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + CREATE_PATH);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053083 File filePath = new File(dirPath + File.separator + "package-info.java");
84 assertThat(filePath.isFile(), is(true));
85 }
86
87 /**
b.janani68c55e12016-02-24 12:23:03 +053088 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswald6f12412016-03-28 15:50:13 +053089 *
90 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +053091 */
92 @Test
93 public void addPackageInfoWithEmptyPathTest() throws IOException {
94
b.janani1fef2732016-03-04 12:29:05 +053095 File dirPath = new File("invalid/check");
b.janani68c55e12016-02-24 12:23:03 +053096 thrown.expect(IOException.class);
97 thrown.expectMessage("Exception occured while creating package info file.");
Bharat saraswald6f12412016-03-28 15:50:13 +053098 addPackageInfo(dirPath, "check1", CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +053099 File filePath1 = new File(dirPath + File.separator + "package-info.java");
100 assertThat(filePath1.isFile(), is(false));
101 }
102
103 /**
104 * A private constructor is tested.
105 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530106 * @throws SecurityException if any security violation is observed
107 * @throws NoSuchMethodException if when the method is not found
108 * @throws IllegalArgumentException if there is illegal argument found
109 * @throws InstantiationException if instantiation is provoked for the private constructor
110 * @throws IllegalAccessException if instance is provoked or a method is provoked
111 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +0530112 */
113 @Test
114 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +0530116
117 Class<?>[] classesToConstruct = {YangIoUtils.class };
118 for (Class<?> clazz : classesToConstruct) {
119 Constructor<?> constructor = clazz.getDeclaredConstructor();
120 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530121 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +0530122 }
123 }
124
125 /**
126 * This test case checks if the directory is cleaned.
Bharat saraswald6f12412016-03-28 15:50:13 +0530127 *
128 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +0530129 */
130 @Test
131 public void cleanGeneratedDirTest() throws IOException {
132
Bharat saraswald6f12412016-03-28 15:50:13 +0530133 File baseDirPath = new File(BASE_DIR);
134 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani68c55e12016-02-24 12:23:03 +0530135 createNewDir.mkdirs();
136 File createFile = new File(createNewDir + File.separator + "check1.java");
137 createFile.createNewFile();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani68c55e12016-02-24 12:23:03 +0530139 }
140
141 /**
142 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswald6f12412016-03-28 15:50:13 +0530143 *
144 * @throws IOException when fails to do IO operations for test case
b.janani68c55e12016-02-24 12:23:03 +0530145 */
146 @Test
147 public void cleanWithInvalidDirTest() throws IOException {
148
Bharat saraswald6f12412016-03-28 15:50:13 +0530149 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530150 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani68c55e12016-02-24 12:23:03 +0530151 }
152
153 /**
154 * This test case tests whether the directories are getting created.
155 */
156 @Test
157 public void createDirectoryTest() {
158
Bharat saraswald6f12412016-03-28 15:50:13 +0530159 File dirPath = createDirectories(CREATE_PATH);
b.janani68c55e12016-02-24 12:23:03 +0530160 assertThat(dirPath.isDirectory(), is(true));
161 }
162
163 /**
Bharat saraswald6f12412016-03-28 15:50:13 +0530164 * This test case checks whether the source is getting added.
b.janani68c55e12016-02-24 12:23:03 +0530165 */
166 @Test
167 public void testForAddSource() {
168
169 MavenProject project = new MavenProject();
170 BuildContext context = new DefaultBuildContext();
Bharat saraswald6f12412016-03-28 15:50:13 +0530171 File sourceDir = new File(BASE_DIR + File.separator + "yang");
b.janani68c55e12016-02-24 12:23:03 +0530172 sourceDir.mkdirs();
Bharat saraswald6f12412016-03-28 15:50:13 +0530173 addToSource(sourceDir.toString(), project, context);
174 }
175
176 /*
177 * Unit test case for trim at last method.
178 */
179 @Test
180 public void testForTrimAtLast() {
181
182 String test = trimAtLast(CHECK_STRING, "six");
183 assertThat(test.contains(TRIM_STRING), is(true));
b.janani68c55e12016-02-24 12:23:03 +0530184 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530185
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530186}