901251c3716fb15531fb592475bf915696f468cc
[vnfsdk/refrepo.git] /
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 org.junit.Before;
29 import org.junit.Test;
30 import org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34
35 public class FileManageTest {
36
37     @Test
38     public void testCreateFileManager() {
39         new MockUp<FileManagerFactory>() {
40             @Mock
41             private FileManagerType getType()  {
42                 return FileManagerType.ftp;
43             }
44         };
45         FileManager manager = FileManagerFactory.createFileManager();
46
47         assertNull(manager);
48     }
49
50     @Test
51     public void testFileManagerFactoryConstructor() {
52         try {
53             Constructor<FileManagerFactory> constructor = FileManagerFactory.class.getDeclaredConstructor();
54             assertTrue(Modifier.isPrivate(constructor.getModifiers()));
55         } catch (NoSuchMethodException e) {
56             e.printStackTrace();
57         }
58     }
59
60     @Before
61     public void createTestFile() throws IOException
62     {
63         String srcPath = "." + File.separator + "srcPathForTest";
64         String dstPath = "." + File.separator + "dstPathForTest";
65         String testFilePath = srcPath + File.separator + "fileForTest";
66         File src = new File(srcPath);
67         File dst = new File(dstPath);
68         File testFile = new File(testFilePath);
69         src.mkdir();
70         dst.mkdir();
71         testFile.createNewFile();
72     }
73
74     @Test
75     public void testDelete() throws IOException {
76         new MockUp<FileManagerFactory>() {
77             @Mock
78             private FileManagerType getType()  {
79                 return FileManagerType.http;
80             }
81         };
82         new MockUp<ToolUtil>() {
83             @Mock
84             private String getHttpServerAbsolutePath() {
85                 return "";
86             }
87         };
88
89         FileManager ManagerImpl = FileManagerFactory.createFileManager();
90         String dstPath = "./dstPathForTest";
91
92         assertEquals(ManagerImpl.delete(dstPath), true);
93     }
94
95     @Test
96     public void testUpload() throws IOException {
97         new MockUp<FileManagerFactory>() {
98             @Mock
99             private FileManagerType getType()  {
100                 return FileManagerType.http;
101             }
102         };
103         new MockUp<ToolUtil>() {
104             @Mock
105             private String getHttpServerAbsolutePath() {
106                 return "";
107             }
108         };
109
110         FileManager ManagerImpl = FileManagerFactory.createFileManager();
111         String srcPath = "./srcPathForTest";
112         String dstPath = "./dstPathForTest";
113
114         assertEquals(ManagerImpl.upload(srcPath, dstPath), true);
115
116         File srcDir = new File(srcPath);
117         if (srcDir.exists())
118         {
119             ManagerImpl.delete(srcPath);
120         }
121
122         assertEquals(ManagerImpl.upload(srcPath, dstPath), false);
123     }
124
125 }