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;
48 import java.io.FileInputStream;
49 import java.io.FileNotFoundException;
50 import java.io.InputStream;
51 import java.net.URISyntaxException;
52 import java.util.ArrayList;
54 import javax.ws.rs.core.Response;
56 public class PackageWrapperTest {
57 private static String resourcePath;
60 MsbAddrConfig.setMsbAddress("http://127.0.0.1:80");
64 HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080");
68 HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/");
71 private static PackageManager manager;
75 * startup db session before class.
79 public static void setUpBeforeClass() throws CatalogResourceException {
81 DaoManager.getInstance().setSessionFactory(HibernateSession.init());
82 manager = PackageManager.getInstance();
83 CatalogAppConfiguration configuration = new CatalogAppConfiguration();
84 Config.setConfigration(configuration);
85 System.out.println("Set up before class");
89 * create data before test.
92 public void setUp() throws Exception {
93 ArrayList<PackageData> packageList = manager.queryPackage(null, null, null, null, null);
94 if (packageList != null && packageList.size() > 0) {
95 for (int i = 0; i < packageList.size(); i++) {
96 String packageOid = packageList.get(i).getCsarId();
97 manager.deletePackage(packageOid);
101 PackageData packageData = new PackageData();
102 packageData = getPackageData();
103 manager.addPackage(packageData);
108 public void testUploadPackage() throws Exception {
109 InputStream ins = null;
110 Response result = null;
111 Response result1 = null;
112 Response result2 = null;
113 // PackageData packageData = new PackageData();
114 // packageData = getPackageData();
116 FormDataContentDisposition fileDetail =
117 FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();
120 resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
121 } catch (URISyntaxException e1) {
122 e1.printStackTrace();
124 final String filename = "NanocellGW.csar";
125 File packageFile = new File(resourcePath + filename);
127 ins = new FileInputStream(packageFile);
128 } catch (FileNotFoundException e2) {
129 e2.printStackTrace();
133 result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
134 } catch (Exception e3) {
135 e3.printStackTrace();
138 assertEquals(200, result.getStatus());
139 assertNotNull(result.getEntity());
142 result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
143 } catch (Exception e4) {
144 e4.printStackTrace();
146 assertEquals(500, result1.getStatus());
149 result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
150 } catch (Exception e5) {
151 e5.printStackTrace();
153 assertEquals(500, result2.getStatus());
157 public void testQueryPackageById() throws Exception {
158 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
159 metas = getPackageMetaList();
161 Response result = PackageWrapper.getInstance().queryPackageById("123456");
162 assertEquals(200, result.getStatus());
163 assertEquals(metas, result.getEntity());
167 public void testQueryPackageByCond() {
168 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
169 metas = getPackageMetaList();
170 System.out.println("Test query package by Id");
172 PackageWrapper.getInstance().queryPackageListByCond("NanocellGW", "ZTE", "V1.0", "false",
174 assertEquals(200, result.getStatus());
175 assertEquals(metas, result.getEntity());
179 public void testUpdatePackageStatus() {
180 System.out.println("Test update package status");
182 PackageWrapper.getInstance().updatePackageStatus("123456", "Enabled", "NotInUse", "true",
183 "onBoarding", "true");
184 assertEquals(200, result.getStatus());
188 public void testGetCsarFileUri() {
189 System.out.println("Test get csar file uri ");
190 CsarFileUriResponse expectResult = new CsarFileUriResponse();
192 MsbAddrConfig.getMsbAddress() + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
194 HttpServerPathConfig.getHttpServerPath()
195 + "NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
196 File srcDir = new File(localUri);
197 String localPath = srcDir.getAbsolutePath().replace("\\", "/");
199 expectResult.setDownloadUri(csarFileUri);
200 expectResult.setLocalPath(localPath);
201 Response result = PackageWrapper.getInstance().getCsarFileUri("123456", "/images/segw.img");
202 assertEquals(200, result.getStatus());
203 assertEquals(expectResult, result.getEntity());
207 public void testDelPackage() {
208 System.out.println("Test delete package ");
209 Response result = PackageWrapper.getInstance().delPackage("123456");
210 assertEquals(204, result.getStatus());
213 } catch (InterruptedException e1) {
214 e1.printStackTrace();
219 * delete data after test.
222 public void tearDown() throws Exception {
223 ArrayList<PackageData> packageList = manager.queryPackageByCsarId("123456");
224 if (packageList != null && packageList.size() != 0) {
225 manager.deletePackage("123456");
229 System.out.println("Tear down");
233 * destory db session after class.
234 * @throws Exception e
237 public static void tearDownAfterClass() {
239 HibernateSession.destory();
240 DaoManager.getInstance().setDaoNull();
241 H2DbServer.shutDown();
242 } catch (Exception e1) {
243 Assert.fail("Exception" + e1.getMessage());
247 private PackageData getPackageData() {
248 PackageData packageData = new PackageData();
249 packageData.setCsarId("123456");
250 packageData.setCreateTime("2016-06-29 03:33:15");
251 packageData.setDeletionPending("false");
252 packageData.setDownloadUri("/NSAR/ZTE/NanocellGW/v1.0/");
253 packageData.setFormat("yml");
254 packageData.setModifyTime("2016-06-29 03:33:15");
255 packageData.setName("NanocellGW");
256 packageData.setOnBoardState("non-onBoarded");
257 packageData.setOperationalState("Disabled");
258 packageData.setProvider("ZTE");
259 packageData.setSize("0.93M");
260 packageData.setType("NSAR");
261 packageData.setUsageState("InUse");
262 packageData.setVersion("V1.0");
263 packageData.setProcessState("normal");
267 private ArrayList<PackageMeta> getPackageMetaList() {
268 PackageMeta meta = new PackageMeta();
269 meta.setCreateTime("2016-06-29 03:33:15");
270 meta.setCsarId("123456");
271 meta.setDeletionPending(false);
272 meta.setDownloadUri(MsbAddrConfig.getMsbAddress()
273 + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW.csar");
274 meta.setFormat("yml");
275 meta.setModifyTime("2016-06-29 03:33:15");
276 meta.setName("NanocellGW");
277 meta.setOperationalState(EnumOperationalState.valueOf("Disabled"));
278 meta.setProvider("ZTE");
279 meta.setSize("0.93M");
280 meta.setType("NSAR");
281 meta.setUsageState(EnumUsageState.valueOf("InUse"));
282 meta.setVersion("V1.0");
283 meta.setOnBoardState(EnumOnboardState.nonOnBoarded.getValue());
284 meta.setProcessState(EnumProcessState.valueOf("normal"));
285 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();