2 * Copyright 2016 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.commontosca.catalog.wrapper;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
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;
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;
56 import javax.ws.rs.core.Response;
58 public class PackageWrapperTest {
59 private static String resourcePath;
62 MsbAddrConfig.setMsbAddress("http://10.74.44.28:80");
63 HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080");
64 HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/");
66 CatalogAppConfiguration configuration = new CatalogAppConfiguration();
67 Config.setConfigration(configuration);
68 Config.getConfigration().setParserType("zte");
69 ModelParserFactory.getInstance().put(EnumPackageFormat.TOSCA_YAML, new TestYamlModelParser());
72 private static PackageManager manager;
76 * startup db session before class.
80 public static void setUpBeforeClass() throws CatalogResourceException {
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");
90 * create data before test.
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);
102 PackageData packageData = new PackageData();
103 packageData = getPackageData();
104 manager.addPackage(packageData);
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();
117 FormDataContentDisposition fileDetail =
118 FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();
121 resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
122 } catch (URISyntaxException e1) {
123 e1.printStackTrace();
125 final String filename = "NanocellGW.csar";
126 File packageFile = new File(resourcePath + filename);
128 ins = new FileInputStream(packageFile);
129 } catch (FileNotFoundException e2) {
130 e2.printStackTrace();
134 result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
135 } catch (Exception e3) {
136 e3.printStackTrace();
139 assertNotNull(result);
140 assertEquals(200, result.getStatus());
141 assertNotNull(result.getEntity());
144 result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
145 } catch (Exception e4) {
146 e4.printStackTrace();
148 assertEquals(500, result1.getStatus());
151 result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
152 } catch (Exception e5) {
153 e5.printStackTrace();
155 assertEquals(500, result2.getStatus());
159 public void testQueryPackageById() throws Exception {
160 PackageMeta metas = new PackageMeta();
161 metas = getPackageMeta();
163 Response result = PackageWrapper.getInstance().queryPackageById("123456");
164 assertEquals(200, result.getStatus());
165 assertEquals(metas, result.getEntity());
169 public void testQueryPackageByCond() {
170 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
171 metas = getPackageMetaList();
172 System.out.println("Test query package by Id");
174 PackageWrapper.getInstance().queryPackageListByCond("NanocellGW", "ZTE", "V1.0", "false",
176 assertEquals(200, result.getStatus());
177 assertEquals(metas, result.getEntity());
181 public void testUpdatePackageStatus() {
182 System.out.println("Test update package status");
184 PackageWrapper.getInstance().updatePackageStatus("123456", "Enabled", "NotInUse", "true",
185 "onBoarding", "true");
186 assertEquals(200, result.getStatus());
190 public void testGetCsarFileUri() {
191 System.out.println("Test get csar file uri ");
192 CsarFileUriResponse expectResult = new CsarFileUriResponse();
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("\\", "/");
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());
208 public void testDelPackage() {
209 System.out.println("Test delete package ");
210 Response result = PackageWrapper.getInstance().delPackage("123456");
211 assertEquals(204, result.getStatus());
214 } catch (InterruptedException e1) {
215 e1.printStackTrace();
220 * delete data after test.
223 public void tearDown() throws Exception {
224 ArrayList<PackageData> packageList = manager.queryPackageByCsarId("123456");
225 if (packageList != null && packageList.size() != 0) {
226 manager.deletePackage("123456");
230 System.out.println("Tear down");
234 * destory db session after class.
235 * @throws Exception e
238 public static void tearDownAfterClass() {
240 HibernateSession.destory();
241 DaoManager.getInstance().setDaoNull();
242 H2DbServer.shutDown();
243 } catch (Exception e1) {
244 Assert.fail("Exception" + e1.getMessage());
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");
268 private ArrayList<PackageMeta> getPackageMetaList() {
269 PackageMeta meta = new PackageMeta();
270 meta = getPackageMeta();
271 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
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"));
297 private String getDownloadUriHead() {
298 return MsbAddrConfig.getMsbAddress() + "/files/catalog-http";