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