b8e3367efdcdad242b3d1685a222dcbac334b383
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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.openo.commontosca.catalog.wrapper;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21
22 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.openo.commontosca.catalog.CatalogAppConfiguration;
31 import org.openo.commontosca.catalog.common.Config;
32 import org.openo.commontosca.catalog.common.HttpServerAddrConfig;
33 import org.openo.commontosca.catalog.common.HttpServerPathConfig;
34 import org.openo.commontosca.catalog.common.MsbAddrConfig;
35 import org.openo.commontosca.catalog.db.dao.DaoManager;
36 import org.openo.commontosca.catalog.db.entity.PackageData;
37 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
38 import org.openo.commontosca.catalog.db.resource.PackageManager;
39 import org.openo.commontosca.catalog.db.util.H2DbServer;
40 import org.openo.commontosca.catalog.db.util.HibernateSession;
41 import org.openo.commontosca.catalog.entity.EnumOnboardState;
42 import org.openo.commontosca.catalog.entity.EnumOperationalState;
43 import org.openo.commontosca.catalog.entity.EnumProcessState;
44 import org.openo.commontosca.catalog.entity.EnumUsageState;
45 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
46 import org.openo.commontosca.catalog.entity.response.PackageMeta;
47 import org.openo.commontosca.catalog.filemanage.entity.FileLink;
48
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.FileNotFoundException;
52 import java.io.InputStream;
53 import java.net.URISyntaxException;
54 import java.util.ArrayList;
55
56 import javax.ws.rs.core.Response;
57
58 public class PackageWrapperTest {
59   private static String resourcePath;
60
61   static {
62     MsbAddrConfig.setMsbAddress("http://127.0.0.1:80");
63   }
64   
65   static {
66     HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080");
67   }
68   
69   static {
70     HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/");
71   }
72
73   private static PackageManager manager;
74
75
76   /**
77    * startup db session before class.
78    * @throws Exception e
79    */
80   @BeforeClass
81   public static void setUpBeforeClass() throws CatalogResourceException {
82     H2DbServer.startUp();
83     DaoManager.getInstance().setSessionFactory(HibernateSession.init());
84     manager = PackageManager.getInstance();
85     CatalogAppConfiguration configuration = new CatalogAppConfiguration();
86     Config.setConfigration(configuration);
87     System.out.println("Set up before class");
88   }
89
90   /**
91    * create data before test.
92    */
93   @Before
94   public void setUp() throws Exception {
95     ArrayList<PackageData> packageList = manager.queryPackage(null, null, null, null, null);
96     if (packageList != null && packageList.size() > 0) {
97       for (int i = 0; i < packageList.size(); i++) {
98         String packageOid = packageList.get(i).getCsarId();
99         manager.deletePackage(packageOid);
100       }
101     }
102
103     // PowerMockito.mockStatic(PackageManager.class);
104     // PowerMockito.when(manager.addPackage(packageData)).thenReturn(packageData);
105     // PowerMock.replayAll();
106     PackageData packageData = new PackageData();
107     packageData = getPackageData();
108     manager.addPackage(packageData);
109   }
110
111   // @Ignore
112   @Test
113   public void testUploadPackage() throws Exception {
114     InputStream ins = null;
115     Response result = null;
116     Response result1 = null;
117     Response result2 = null;
118     // PackageData packageData = new PackageData();
119     // packageData = getPackageData();
120
121     FormDataContentDisposition fileDetail =
122         FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();
123
124     try {
125       resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
126     } catch (URISyntaxException e1) {
127       e1.printStackTrace();
128     }
129     final String filename = "NanocellGW.csar";
130     File packageFile = new File(resourcePath + filename);
131     try {
132       ins = new FileInputStream(packageFile);
133     } catch (FileNotFoundException e2) {
134       e2.printStackTrace();
135     }
136     if (ins != null) {
137       try {
138         result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
139       } catch (Exception e3) {
140         e3.printStackTrace();
141       }
142     }
143     assertEquals(200, result.getStatus());
144     assertNotNull(result.getEntity());
145
146     try {
147       result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
148     } catch (Exception e4) {
149       e4.printStackTrace();
150     }
151     assertEquals(500, result1.getStatus());
152
153     try {
154       result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
155     } catch (Exception e5) {
156       e5.printStackTrace();
157     }
158     assertEquals(500, result2.getStatus());
159   }
160
161   @Test
162   public void testQueryPackageById() throws Exception {
163     ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
164     metas = getPackageMetaList();
165     // PackageManager packageManager = mock(PackageManager.class);
166     // when(packageManager.queryPackageByCsarId(anyString())).thenThrow(new Exception());
167     // PowerMockito.whenNew(PackageManager.class).withNoArguments().thenReturn(packageManager);
168     // PowerMockito.mockStatic(PackageManager.class);
169     // PowerMockito.when(PackageManager.getInstance()).thenReturn(manager);
170     // PowerMock.replayAll();
171     // when(manager.queryPackageByCsarId(anyString())).thenThrow(new Exception());
172     // PowerMockito.whenNew(PackageManager.class).withNoArguments().thenReturn(manager);
173
174     Response result = PackageWrapper.getInstance().queryPackageById("1");
175     assertEquals(200, result.getStatus());
176     assertEquals(metas, result.getEntity());
177
178   }
179
180   @Test
181   public void testQueryPackageByCond() {
182     ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
183     metas = getPackageMetaList();
184     System.out.println("Test query package by Id");
185     Response result =
186         PackageWrapper.getInstance().queryPackageListByCond("NanocellGW", "ZTE", "V1.0", "false",
187             "NSAR");
188     assertEquals(200, result.getStatus());
189     assertEquals(metas, result.getEntity());
190   }
191
192   @Test
193   public void testUpdatePackageStatus() {
194     System.out.println("Test update package status");
195     Response result =
196         PackageWrapper.getInstance().updatePackageStatus("1", "Enabled", "NotInUse", "true",
197             "onBoarding", "true");
198     assertEquals(200, result.getStatus());
199   }
200
201   @Test
202   public void testGetCsarFileUri() {
203     System.out.println("Test get csar file uri ");
204     CsarFileUriResponse expectResult = new CsarFileUriResponse();
205     String csarFileUri =
206         MsbAddrConfig.getMsbAddress() + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
207     String localUri =
208         HttpServerPathConfig.getHttpServerPath()
209             + "NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
210     File srcDir = new File(localUri);
211     String localPath = srcDir.getAbsolutePath().replace("\\", "/");
212
213     expectResult.setDownloadUri(csarFileUri);
214     expectResult.setLocalPath(localPath);
215     Response result = PackageWrapper.getInstance().getCsarFileUri("1", "/images/segw.img");
216     assertEquals(200, result.getStatus());
217     assertEquals(expectResult, result.getEntity());
218   }
219
220   @Ignore
221   @Test
222   public void testGetPlansUri() {
223     System.out.println("Test get csar plans uri ");
224     ArrayList<FileLink> expectResult = new ArrayList<FileLink>();
225     FileLink fileLink1 = new FileLink();
226     fileLink1.setFileName("init.zip");
227     fileLink1.setDownloadUri(MsbAddrConfig.getMsbAddress()
228         + "/NSAR/ZTE/NanocellGW/v1.0/Plans/init.zip");
229     expectResult.add(fileLink1);
230     FileLink fileLink2 = new FileLink();
231     fileLink2.setFileName("delete.zip");
232     fileLink2.setDownloadUri(MsbAddrConfig.getMsbAddress()
233         + "/NSAR/ZTE/NanocellGW/v1.0/Plans/delete.zip");
234     expectResult.add(fileLink2);
235     Response result = PackageWrapper.getInstance().getCsarPlansUri("1");
236     assertEquals(200, result.getStatus());
237     assertEquals(expectResult, result.getEntity());
238   }
239
240   @Test
241   public void testDelPackage() {
242     System.out.println("Test delete package ");
243     Response result = PackageWrapper.getInstance().delPackage("1");
244     assertEquals(204, result.getStatus());
245     try {
246       Thread.sleep(5000);
247     } catch (InterruptedException e1) {
248       e1.printStackTrace();
249     }
250   }
251
252   /**
253    * delete data after test.
254    */
255   @After
256   public void tearDown() throws Exception {
257     ArrayList<PackageData> packageList = manager.queryPackageByCsarId("1");
258     if (packageList != null && packageList.size() != 0) {
259       manager.deletePackage("1");
260     } else {
261       return;
262     }
263     System.out.println("Tear down");
264   }
265
266   /**
267    * destory db session after class.
268    * @throws Exception e
269    */
270   @AfterClass
271   public static void tearDownAfterClass() {
272     try {
273       HibernateSession.destory();
274       DaoManager.getInstance().setDaoNull();
275       H2DbServer.shutDown();
276     } catch (Exception e1) {
277       Assert.fail("Exception" + e1.getMessage());
278     }
279   }
280
281   private PackageData getPackageData() {
282     PackageData packageData = new PackageData();
283     packageData.setCsarId("1");
284     packageData.setCreateTime("2016-06-29 03:33:15");
285     packageData.setDeletionPending("false");
286     packageData.setDownloadUri("/NSAR/ZTE/NanocellGW/v1.0/");
287     packageData.setFormat("yml");
288     packageData.setModifyTime("2016-06-29 03:33:15");
289     packageData.setName("NanocellGW");
290     packageData.setOnBoardState("non-onBoarded");
291     packageData.setOperationalState("Disabled");
292     packageData.setProvider("ZTE");
293     packageData.setSize("0.93M");
294     packageData.setType("NSAR");
295     packageData.setUsageState("InUse");
296     packageData.setVersion("V1.0");
297     packageData.setProcessState("normal");
298     return packageData;
299   }
300
301   private ArrayList<PackageData> getPackageDataList() {
302     ArrayList<PackageData> packageDataList = new ArrayList<PackageData>();
303     PackageData packageData = new PackageData();
304     packageData = getPackageData();
305     packageDataList.add(packageData);
306     return packageDataList;
307   }
308
309   private ArrayList<PackageMeta> getPackageMetaList() {
310     PackageMeta meta = new PackageMeta();
311     meta.setCreateTime("2016-06-29 03:33:15");
312     meta.setCsarId("1");
313     meta.setDeletionPending(false);
314     meta.setDownloadUri(MsbAddrConfig.getMsbAddress() 
315         + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW.csar");
316     meta.setFormat("yml");
317     meta.setModifyTime("2016-06-29 03:33:15");
318     meta.setName("NanocellGW");
319     meta.setOperationalState(EnumOperationalState.valueOf("Disabled"));
320     meta.setProvider("ZTE");
321     meta.setSize("0.93M");
322     meta.setType("NSAR");
323     meta.setUsageState(EnumUsageState.valueOf("InUse"));
324     meta.setVersion("V1.0");
325     meta.setOnBoardState(EnumOnboardState.nonOnBoarded.getValue());
326     meta.setProcessState(EnumProcessState.valueOf("normal"));
327     ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
328     metas.add(meta);
329     return metas;
330   }
331 }