2 * Copyright 2016 [ZTE] and others.
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.
17 package org.openo.commontosca.catalog.wrapper;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
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;
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;
57 import javax.ws.rs.core.Response;
59 public class PackageWrapperTest {
60 private static String resourcePath;
63 MsbAddrConfig.setMsbAddress("http://10.74.44.28:80");
64 HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080");
65 HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/");
67 CatalogAppConfiguration configuration = new CatalogAppConfiguration();
68 Config.setConfigration(configuration);
69 Config.getConfigration().setParserType("zte");
70 ModelParserFactory.getInstance().put(EnumPackageFormat.TOSCA_YAML, new TestYamlModelParser());
73 private static PackageManager manager;
77 * startup db session before class.
81 public static void setUpBeforeClass() throws CatalogResourceException {
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");
91 * create data before test.
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);
103 PackageData packageData = new PackageData();
104 packageData = getPackageData();
105 manager.addPackage(packageData);
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();
118 FormDataContentDisposition fileDetail =
119 FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();
122 resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
123 } catch (URISyntaxException e1) {
124 e1.printStackTrace();
126 final String filename = "NanocellGW.csar";
127 File packageFile = new File(resourcePath + filename);
129 ins = new FileInputStream(packageFile);
130 } catch (FileNotFoundException e2) {
131 e2.printStackTrace();
135 result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
136 } catch (Exception e3) {
137 e3.printStackTrace();
140 assertNotNull(result);
141 assertEquals(200, result.getStatus());
142 assertNotNull(result.getEntity());
145 result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
146 } catch (Exception e4) {
147 e4.printStackTrace();
149 assertEquals(500, result1.getStatus());
152 result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
153 } catch (Exception e5) {
154 e5.printStackTrace();
156 assertEquals(500, result2.getStatus());
160 public void testQueryPackageById() throws Exception {
161 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
162 metas = getPackageMetaList();
164 Response result = PackageWrapper.getInstance().queryPackageById("123456");
165 assertEquals(200, result.getStatus());
166 assertEquals(metas, result.getEntity());
170 public void testQueryPackageByCond() {
171 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
172 metas = getPackageMetaList();
173 System.out.println("Test query package by Id");
175 PackageWrapper.getInstance().queryPackageListByCond("NanocellGW", "ZTE", "V1.0", "false",
177 assertEquals(200, result.getStatus());
178 assertEquals(metas, result.getEntity());
182 public void testUpdatePackageStatus() {
183 System.out.println("Test update package status");
185 PackageWrapper.getInstance().updatePackageStatus("123456", "Enabled", "NotInUse", "true",
186 "onBoarding", "true");
187 assertEquals(200, result.getStatus());
191 public void testGetCsarFileUri() {
192 System.out.println("Test get csar file uri ");
193 CsarFileUriResponse expectResult = new CsarFileUriResponse();
195 MsbAddrConfig.getMsbAddress() + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
197 HttpServerPathConfig.getHttpServerPath()
198 + "NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
199 File srcDir = new File(localUri);
200 String localPath = srcDir.getAbsolutePath().replace("\\", "/");
202 expectResult.setDownloadUri(csarFileUri);
203 expectResult.setLocalPath(localPath);
204 Response result = PackageWrapper.getInstance().getCsarFileUri("123456", "/images/segw.img");
205 assertEquals(200, result.getStatus());
206 assertEquals(expectResult, result.getEntity());
210 public void testDelPackage() {
211 System.out.println("Test delete package ");
212 Response result = PackageWrapper.getInstance().delPackage("123456");
213 assertEquals(204, result.getStatus());
216 } catch (InterruptedException e1) {
217 e1.printStackTrace();
222 * delete data after test.
225 public void tearDown() throws Exception {
226 ArrayList<PackageData> packageList = manager.queryPackageByCsarId("123456");
227 if (packageList != null && packageList.size() != 0) {
228 manager.deletePackage("123456");
232 System.out.println("Tear down");
236 * destory db session after class.
237 * @throws Exception e
240 public static void tearDownAfterClass() {
242 HibernateSession.destory();
243 DaoManager.getInstance().setDaoNull();
244 H2DbServer.shutDown();
245 } catch (Exception e1) {
246 Assert.fail("Exception" + e1.getMessage());
250 private PackageData getPackageData() {
251 PackageData packageData = new PackageData();
252 packageData.setCsarId("123456");
253 packageData.setCreateTime("2016-06-29 03:33:15");
254 packageData.setDeletionPending("false");
255 packageData.setDownloadUri("/NSAR/ZTE/NanocellGW/v1.0/");
256 packageData.setFormat("yml");
257 packageData.setModifyTime("2016-06-29 03:33:15");
258 packageData.setName("NanocellGW");
259 packageData.setOnBoardState("non-onBoarded");
260 packageData.setOperationalState("Disabled");
261 packageData.setProvider("ZTE");
262 packageData.setSize("0.93M");
263 packageData.setType("NSAR");
264 packageData.setUsageState("InUse");
265 packageData.setVersion("V1.0");
266 packageData.setProcessState("normal");
270 private ArrayList<PackageMeta> getPackageMetaList() {
271 PackageMeta meta = new PackageMeta();
272 meta.setCreateTime("2016-06-29 03:33:15");
273 meta.setCsarId("123456");
274 meta.setDeletionPending(false);
275 meta.setDownloadUri(MsbAddrConfig.getMsbAddress()
276 + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW.csar");
277 meta.setFormat("yml");
278 meta.setModifyTime("2016-06-29 03:33:15");
279 meta.setName("NanocellGW");
280 meta.setOperationalState(EnumOperationalState.valueOf("Disabled"));
281 meta.setProvider("ZTE");
282 meta.setSize("0.93M");
283 meta.setType("NSAR");
284 meta.setUsageState(EnumUsageState.valueOf("InUse"));
285 meta.setVersion("V1.0");
286 meta.setOnBoardState(EnumOnboardState.nonOnBoarded.getValue());
287 meta.setProcessState(EnumProcessState.valueOf("normal"));
288 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();