9fc383e349559b4680a73728563d586e4d3f1285
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / common / FileUtilsTest.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
18
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.Modifier;
28 import java.util.List;
29
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class FileUtilsTest {
34
35     FileUtils fileUtils;
36
37     @Before
38     public void setUp() {
39         fileUtils.getAppAbsoluteUrl();
40     }
41
42     @Test
43     public void testGetClassPath() throws Exception {
44         assertNotNull(FileUtils.getClassPath());
45     }
46
47     @Test
48     public void testReadFileNull() throws Exception {
49         assertNotNull(FileUtils.readFile(null, null));
50     }
51
52     @Test
53     public void testReadFile() throws Exception {
54         assertNotNull(FileUtils.readFile(
55                 new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtil.java"), "utf-8"));
56     }
57
58     @Test(expected = IOException.class)
59     public void testReadFileFail() throws Exception {
60         FileUtils.readFile(new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/aaa.java"),
61                 "utf-8");
62     }
63
64     @Test
65     public void testWriteFile() throws Exception {
66         String filePath = "";
67         byte[] bytes = new byte[] {40, 50};
68         int b = FileUtils.writeFile(bytes, filePath);
69         assertNotNull(b);
70     }
71
72     @Test
73     public void testWriteFileSuccess() throws Exception {
74         String filePath = "src/test/java/FileTest";
75         byte[] bytes = new byte[] {40, 50};
76         int b = FileUtils.writeFile(bytes, filePath);
77         assertNotNull(b);
78     }
79
80     @Test
81     public void testListFiles() throws Exception {
82         String file = ".";
83         File f = new File(file);
84         List<File> files = FileUtils.listFiles(f);
85         assertNotNull(files);
86
87     }
88
89     @Test(expected = FileNotFoundException.class)
90     public void testListFilesFail() throws Exception {
91         String file = "abc";
92         File f = new File(file);
93         List<File> files = FileUtils.listFiles(f);
94         assertNotNull(files);
95
96     }
97
98     @Test
99     public void testMkDirs() throws Exception {
100         File resourcesDirectory = new File("src/test/resources");
101         String path = resourcesDirectory.getAbsolutePath() + "/TestDir";
102         resourcesDirectory.getAbsolutePath();
103         FileUtils.newFloder(path);
104         FileUtils.mkDirs(path);
105         FileUtils.newFloder(path);
106     }
107
108     @Test
109     public void testDelFiles() throws Exception {
110         File resourcesDirectory = new File("src/test/resources/TestDir/Test.txt");
111         assertTrue(FileUtils.delFiles(resourcesDirectory.getAbsolutePath()));
112     }
113
114     @Test
115     public void testgetFiles() throws Exception {
116         File resourcesDirectory = new File("src/test/resources");
117         List<File> files = FileUtils.getFiles(resourcesDirectory.getAbsolutePath());
118         assertNotNull(files);
119     }
120
121     @Test
122     public void testCopy() throws Exception {
123         File oldfile = new File("");
124         File newfile = new File("");
125         FileUtils.copy(oldfile.getAbsolutePath(), newfile.getAbsolutePath(), true);
126     }
127
128     @Test
129     public void testPrivateConstructor() throws Exception {
130         Constructor constructor = FileUtils.class.getDeclaredConstructor();
131         assertTrue("Constructor  private", Modifier.isPrivate(constructor.getModifiers()));
132         constructor.setAccessible(true);
133         constructor.newInstance();
134     }
135
136     @Test(expected = FileNotFoundException.class)
137     public void testIsUsed() throws Exception {
138         assertNotNull(FileUtils.isUsed("abc.txt"));
139
140     }
141
142     @Test
143     public void testGetBaseFileName() throws Exception {
144         assertNotNull(FileUtils.getBaseFileName(new File("aaa.txt")));
145         assertNotNull(FileUtils.getBaseFileName(new File("aaa")));
146
147     }
148
149     @Test
150     public void testFixPath() throws Exception {
151         assertNull(FileUtils.fixPath(null));
152         assertNotNull(FileUtils.fixPath("/abc"));
153         assertNotNull(FileUtils.fixPath("/abc/"));
154         assertNotNull(FileUtils.fixPath("abc"));
155         assertNotNull(FileUtils.fixPath("abc/"));
156
157     }
158
159     @Test
160     public void testGetFiendlyPath() throws Exception {
161         assertNull(FileUtils.getFriendlyPath(null));
162         assertNotNull(FileUtils.getFriendlyPath("/abc"));
163         assertNotNull(FileUtils.getFriendlyPath("/abc/"));
164         assertNotNull(FileUtils.getFriendlyPath("abc"));
165         assertNotNull(FileUtils.getFriendlyPath("abc/"));
166
167     }
168 }