"java.nio.Files#delete" should be preferred
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / test / java / org / onap / vnfsdk / marketplace / filemanage / FileManageTest.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.vnfsdk.marketplace.filemanage;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.Modifier;
27
28 import autovalue.shaded.org.apache.commons.lang.ObjectUtils;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil;
32
33 import mockit.Mock;
34 import mockit.MockUp;
35
36 public class FileManageTest {
37
38     @Test
39     public void testCreateFileManager() {
40         new MockUp<FileManagerFactory>() {
41             @Mock
42             private FileManagerType getType()  {
43                 return FileManagerType.FTP;
44             }
45         };
46         FileManager manager = FileManagerFactory.createFileManager();
47
48         assertNull(manager);
49     }
50
51     @Test
52     public void testFileManagerFactoryConstructor() {
53         try {
54             Constructor<FileManagerFactory> constructor = FileManagerFactory.class.getDeclaredConstructor();
55             assertTrue(Modifier.isPrivate(constructor.getModifiers()));
56         } catch (NoSuchMethodException e) {
57             e.printStackTrace();
58         }
59     }
60
61     @Before
62     public void createTestFile() throws IOException
63     {
64         String srcPath = "." + File.separator + "srcPathForTest";
65         String dstPath = "." + File.separator + "dstPathForTest";
66         String testFilePath = srcPath + File.separator + "fileForTest";
67         File src = new File(srcPath);
68         File dst = new File(dstPath);
69         File testFile = new File(testFilePath);
70         src.mkdir();
71         dst.mkdir();
72         testFile.createNewFile();
73     }
74
75     @Test
76     public void testDelete() throws IOException {
77         new MockUp<FileManagerFactory>() {
78             @Mock
79             private FileManagerType getType()  {
80                 return FileManagerType.HTTP;
81             }
82         };
83         new MockUp<ToolUtil>() {
84             @Mock
85             private String getHttpServerAbsolutePath() {
86                 return "";
87             }
88         };
89
90         FileManager ManagerImpl = FileManagerFactory.createFileManager();
91         String dstPath = "./dstPathForTest";
92
93         assertEquals(true,ManagerImpl.delete(dstPath));
94     }
95
96     @Test
97     public void testUpload() throws IOException {
98         new MockUp<FileManagerFactory>() {
99             @Mock
100             private FileManagerType getType()  {
101                 return FileManagerType.HTTP;
102             }
103         };
104         new MockUp<ToolUtil>() {
105             @Mock
106             private String getHttpServerAbsolutePath() {
107                 return "";
108             }
109         };
110
111         FileManager ManagerImpl = FileManagerFactory.createFileManager();
112         String srcPath = "./srcPathForTest";
113         String dstPath = "./dstPathForTest";
114
115         assertEquals(true,ManagerImpl.upload(srcPath, dstPath) );
116
117         File srcDir = new File(srcPath);
118         if (srcDir.exists())
119         {
120             ManagerImpl.delete(srcPath);
121         }
122
123         assertEquals(false,ManagerImpl.upload(srcPath, dstPath) );
124     }
125
126     @Test
127     public void testCreateDir(){
128         String dstPath = "./dstPathForTest1";
129         File dst = new File(dstPath);
130         dst.mkdir();
131         assertTrue(ToolUtil.createDir(dstPath));
132         if (dst.exists())
133             dst.delete();
134     }
135
136 }