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