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