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.Ignore;
29 import org.junit.Test;
30 import org.openo.commontosca.catalog.CatalogAppConfiguration;
31 import org.openo.commontosca.catalog.common.Config;
32 import org.openo.commontosca.catalog.common.HttpServerAddrConfig;
33 import org.openo.commontosca.catalog.common.HttpServerPathConfig;
34 import org.openo.commontosca.catalog.common.MsbAddrConfig;
35 import org.openo.commontosca.catalog.db.dao.DaoManager;
36 import org.openo.commontosca.catalog.db.entity.PackageData;
37 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
38 import org.openo.commontosca.catalog.db.resource.PackageManager;
39 import org.openo.commontosca.catalog.db.util.H2DbServer;
40 import org.openo.commontosca.catalog.db.util.HibernateSession;
41 import org.openo.commontosca.catalog.entity.EnumOnboardState;
42 import org.openo.commontosca.catalog.entity.EnumOperationalState;
43 import org.openo.commontosca.catalog.entity.EnumProcessState;
44 import org.openo.commontosca.catalog.entity.EnumUsageState;
45 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
46 import org.openo.commontosca.catalog.entity.response.PackageMeta;
47 import org.openo.commontosca.catalog.filemanage.entity.FileLink;
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://127.0.0.1:80");
66 HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080");
70 HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/");
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 // PowerMockito.mockStatic(PackageManager.class);
104 // PowerMockito.when(manager.addPackage(packageData)).thenReturn(packageData);
105 // PowerMock.replayAll();
106 PackageData packageData = new PackageData();
107 packageData = getPackageData();
108 manager.addPackage(packageData);
113 public void testUploadPackage() throws Exception {
114 InputStream ins = null;
115 Response result = null;
116 Response result1 = null;
117 Response result2 = null;
118 // PackageData packageData = new PackageData();
119 // packageData = getPackageData();
121 FormDataContentDisposition fileDetail =
122 FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();
125 resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
126 } catch (URISyntaxException e1) {
127 e1.printStackTrace();
129 final String filename = "NanocellGW.csar";
130 File packageFile = new File(resourcePath + filename);
132 ins = new FileInputStream(packageFile);
133 } catch (FileNotFoundException e2) {
134 e2.printStackTrace();
138 result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
139 } catch (Exception e3) {
140 e3.printStackTrace();
143 assertEquals(200, result.getStatus());
144 assertNotNull(result.getEntity());
147 result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
148 } catch (Exception e4) {
149 e4.printStackTrace();
151 assertEquals(500, result1.getStatus());
154 result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
155 } catch (Exception e5) {
156 e5.printStackTrace();
158 assertEquals(500, result2.getStatus());
162 public void testQueryPackageById() throws Exception {
163 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
164 metas = getPackageMetaList();
165 // PackageManager packageManager = mock(PackageManager.class);
166 // when(packageManager.queryPackageByCsarId(anyString())).thenThrow(new Exception());
167 // PowerMockito.whenNew(PackageManager.class).withNoArguments().thenReturn(packageManager);
168 // PowerMockito.mockStatic(PackageManager.class);
169 // PowerMockito.when(PackageManager.getInstance()).thenReturn(manager);
170 // PowerMock.replayAll();
171 // when(manager.queryPackageByCsarId(anyString())).thenThrow(new Exception());
172 // PowerMockito.whenNew(PackageManager.class).withNoArguments().thenReturn(manager);
174 Response result = PackageWrapper.getInstance().queryPackageById("1");
175 assertEquals(200, result.getStatus());
176 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("1", "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 MsbAddrConfig.getMsbAddress() + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
208 HttpServerPathConfig.getHttpServerPath()
209 + "NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
210 File srcDir = new File(localUri);
211 String localPath = srcDir.getAbsolutePath().replace("\\", "/");
213 expectResult.setDownloadUri(csarFileUri);
214 expectResult.setLocalPath(localPath);
215 Response result = PackageWrapper.getInstance().getCsarFileUri("1", "/images/segw.img");
216 assertEquals(200, result.getStatus());
217 assertEquals(expectResult, result.getEntity());
222 public void testGetPlansUri() {
223 System.out.println("Test get csar plans uri ");
224 ArrayList<FileLink> expectResult = new ArrayList<FileLink>();
225 FileLink fileLink1 = new FileLink();
226 fileLink1.setFileName("init.zip");
227 fileLink1.setDownloadUri(MsbAddrConfig.getMsbAddress()
228 + "/NSAR/ZTE/NanocellGW/v1.0/Plans/init.zip");
229 expectResult.add(fileLink1);
230 FileLink fileLink2 = new FileLink();
231 fileLink2.setFileName("delete.zip");
232 fileLink2.setDownloadUri(MsbAddrConfig.getMsbAddress()
233 + "/NSAR/ZTE/NanocellGW/v1.0/Plans/delete.zip");
234 expectResult.add(fileLink2);
235 Response result = PackageWrapper.getInstance().getCsarPlansUri("1");
236 assertEquals(200, result.getStatus());
237 assertEquals(expectResult, result.getEntity());
241 public void testDelPackage() {
242 System.out.println("Test delete package ");
243 Response result = PackageWrapper.getInstance().delPackage("1");
244 assertEquals(204, result.getStatus());
247 } catch (InterruptedException e1) {
248 e1.printStackTrace();
253 * delete data after test.
256 public void tearDown() throws Exception {
257 ArrayList<PackageData> packageList = manager.queryPackageByCsarId("1");
258 if (packageList != null && packageList.size() != 0) {
259 manager.deletePackage("1");
263 System.out.println("Tear down");
267 * destory db session after class.
268 * @throws Exception e
271 public static void tearDownAfterClass() {
273 HibernateSession.destory();
274 DaoManager.getInstance().setDaoNull();
275 H2DbServer.shutDown();
276 } catch (Exception e1) {
277 Assert.fail("Exception" + e1.getMessage());
281 private PackageData getPackageData() {
282 PackageData packageData = new PackageData();
283 packageData.setCsarId("1");
284 packageData.setCreateTime("2016-06-29 03:33:15");
285 packageData.setDeletionPending("false");
286 packageData.setDownloadUri("/NSAR/ZTE/NanocellGW/v1.0/");
287 packageData.setFormat("yml");
288 packageData.setModifyTime("2016-06-29 03:33:15");
289 packageData.setName("NanocellGW");
290 packageData.setOnBoardState("non-onBoarded");
291 packageData.setOperationalState("Disabled");
292 packageData.setProvider("ZTE");
293 packageData.setSize("0.93M");
294 packageData.setType("NSAR");
295 packageData.setUsageState("InUse");
296 packageData.setVersion("V1.0");
297 packageData.setProcessState("normal");
301 private ArrayList<PackageData> getPackageDataList() {
302 ArrayList<PackageData> packageDataList = new ArrayList<PackageData>();
303 PackageData packageData = new PackageData();
304 packageData = getPackageData();
305 packageDataList.add(packageData);
306 return packageDataList;
309 private ArrayList<PackageMeta> getPackageMetaList() {
310 PackageMeta meta = new PackageMeta();
311 meta.setCreateTime("2016-06-29 03:33:15");
313 meta.setDeletionPending(false);
314 meta.setDownloadUri(MsbAddrConfig.getMsbAddress()
315 + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW.csar");
316 meta.setFormat("yml");
317 meta.setModifyTime("2016-06-29 03:33:15");
318 meta.setName("NanocellGW");
319 meta.setOperationalState(EnumOperationalState.valueOf("Disabled"));
320 meta.setProvider("ZTE");
321 meta.setSize("0.93M");
322 meta.setType("NSAR");
323 meta.setUsageState(EnumUsageState.valueOf("InUse"));
324 meta.setVersion("V1.0");
325 meta.setOnBoardState(EnumOnboardState.nonOnBoarded.getValue());
326 meta.setProcessState(EnumProcessState.valueOf("normal"));
327 ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();