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);
101 ArrayList<PackageData> packageList2 = manager.queryPackage(null, null, null, null, null);
102 if (packageList2 != null && packageList2.size() > 0) {
103 for (int j = 0; j < packageList2.size(); j++) {
104 if (packageList2.get(j).getCsarId().equals("123456")) {
105 manager.updatePackage(getPackageData(), "123456");
107 PackageData packageData = new PackageData();
108 packageData = getPackageData();
109 manager.addPackage(packageData);
113 PackageData packageData = new PackageData();
114 packageData = getPackageData();
115 manager.addPackage(packageData);
121 public void testUploadPackage() throws Exception {
122 InputStream ins = null;
123 Response result = null;
124 Response result1 = null;
125 Response result2 = null;
126 // PackageData packageData = new PackageData();
127 // packageData = getPackageData();
129 FormDataContentDisposition fileDetail =
130 FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();
133 resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
134 } catch (URISyntaxException e1) {
135 e1.printStackTrace();
137 final String filename = "NanocellGW.csar";
138 File packageFile = new File(resourcePath + filename);
140 ins = new FileInputStream(packageFile);
141 } catch (FileNotFoundException e2) {
142 e2.printStackTrace();
146 result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
147 } catch (Exception e3) {
148 e3.printStackTrace();
151 assertNotNull(result);
152 assertEquals(200, result.getStatus());
153 assertNotNull(result.getEntity());
156 result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
157 } catch (Exception e4) {
158 e4.printStackTrace();
160 assertEquals(500, result1.getStatus());
163 result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
164 } catch (Exception e5) {
165 e5.printStackTrace();
167 assertEquals(500, result2.getStatus());
171 public void testQueryPackageById() throws Exception {
172 PackageMeta metas = new PackageMeta();
173 metas = getPackageMeta();
175 Response result = PackageWrapper.getInstance().queryPackageById("123456");
176 assertEquals(200, result.getStatus());
177 //assertEquals(metas, result.getEntity());
181 public void testQueryPackageByCond() {
182 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
183 metas = getPackageMetaList();
184 System.out.println("Test query package by Id");
186 PackageWrapper.getInstance().queryPackageListByCond("NanocellGW", "ZTE", "V1.0", "false",
188 assertEquals(200, result.getStatus());
189 //assertEquals(metas, result.getEntity());
193 public void testUpdatePackageStatus() {
194 System.out.println("Test update package status");
196 PackageWrapper.getInstance().updatePackageStatus("123456", "Enabled", "NotInUse", "true",
197 "onBoarding", "true");
198 assertEquals(200, result.getStatus());
202 public void testGetCsarFileUri() {
203 System.out.println("Test get csar file uri ");
204 CsarFileUriResponse expectResult = new CsarFileUriResponse();
206 getDownloadUriHead() + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
207 String localUri = HttpServerPathConfig.getHttpServerPath()
208 + "NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
209 File srcDir = new File(localUri);
210 String localPath = srcDir.getAbsolutePath().replace("\\", "/");
212 expectResult.setDownloadUri(csarFileUri);
213 expectResult.setLocalPath(localPath);
214 Response result = PackageWrapper.getInstance().getCsarFileUri("123456", "/images/segw.img");
215 assertEquals(200, result.getStatus());
216 //assertEquals(expectResult, result.getEntity());
220 // public void testDelPackage() {
221 // System.out.println("Test delete package ");
222 // Response result = PackageWrapper.getInstance().delPackage("123456");
223 // assertEquals(204, result.getStatus());
225 // Thread.sleep(5000);
226 // } catch (InterruptedException e1) {
227 // e1.printStackTrace();
232 * delete data after test.
235 public void tearDown() throws Exception {
236 ArrayList<PackageData> packageList = manager.queryPackageByCsarId("123456");
237 if (packageList != null && packageList.size() != 0) {
238 manager.deletePackage("123456");
242 System.out.println("Tear down");
246 * destory db session after class.
247 * @throws Exception e
250 public static void tearDownAfterClass() {
252 HibernateSession.destory();
253 DaoManager.getInstance().setDaoNull();
254 H2DbServer.shutDown();
255 } catch (Exception e1) {
256 Assert.fail("Exception" + e1.getMessage());
260 private PackageData getPackageData() {
261 PackageData packageData = new PackageData();
262 packageData.setCsarId("123456");
263 packageData.setCreateTime("2016-06-29 03:33:15");
264 packageData.setDeletionPending("false");
265 packageData.setDownloadUri("/NSAR/ZTE/NanocellGW/v1.0/");
266 packageData.setFormat("yml");
267 packageData.setModifyTime("2016-06-29 03:33:15");
268 packageData.setName("NanocellGW");
269 packageData.setOnBoardState("non-onBoarded");
270 packageData.setOperationalState("Disabled");
271 packageData.setProvider("ZTE");
272 packageData.setSize("0.93M");
273 packageData.setType("NSAR");
274 packageData.setUsageState("InUse");
275 packageData.setVersion("V1.0");
276 packageData.setProcessState("normal");
280 private ArrayList<PackageMeta> getPackageMetaList() {
281 PackageMeta meta = new PackageMeta();
282 meta = getPackageMeta();
283 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
288 private PackageMeta getPackageMeta() {
289 PackageMeta meta = new PackageMeta();
290 meta.setCreateTime("2016-06-29 03:33:15");
291 meta.setCsarId("123456");
292 meta.setDeletionPending(false);
293 meta.setDownloadUri(getDownloadUriHead()
294 + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW.csar");
295 meta.setFormat("yml");
296 meta.setModifyTime("2016-06-29 03:33:15");
297 meta.setName("NanocellGW");
298 meta.setOperationalState(EnumOperationalState.valueOf("Disabled"));
299 meta.setProvider("ZTE");
300 meta.setSize("0.93M");
301 meta.setType("NSAR");
302 meta.setUsageState(EnumUsageState.valueOf("InUse"));
303 meta.setVersion("V1.0");
304 meta.setOnBoardState(EnumOnboardState.nonOnBoarded.getValue());
305 meta.setProcessState(EnumProcessState.valueOf("normal"));
309 private String getDownloadUriHead() {
310 return MsbAddrConfig.getMsbAddress() + "/files/catalog-http";