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.db.resource;
18 import static org.junit.Assert.assertTrue;
21 import org.junit.After;
22 import org.junit.AfterClass;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.openo.commontosca.catalog.db.common.Parameters;
28 import org.openo.commontosca.catalog.db.dao.DaoManager;
29 import org.openo.commontosca.catalog.db.entity.PackageData;
30 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
31 import org.openo.commontosca.catalog.db.util.H2DbServer;
32 import org.openo.commontosca.catalog.db.util.HibernateSession;
34 import java.util.ArrayList;
35 import java.util.HashMap;
39 public class PackageManagerTest {
40 private static PackageManager manager;
43 * startup db session before class.
47 public static void setUpBeforeClass() throws Exception {
49 DaoManager.getInstance().setSessionFactory(HibernateSession.init());
50 manager = PackageManager.getInstance();
54 * destory db session after class.
58 public static void tearDownAfterClass() throws Exception {
60 HibernateSession.destory();
61 DaoManager.getInstance().setPackageDao(null);
62 H2DbServer.shutDown();
63 } catch (Exception e1) {
64 Assert.fail("Exception" + e1.getMessage());
69 * create data before test.
73 PackageData data = new PackageData();
74 data.setCsarId("10001");
76 data.setVersion("v1.0");
77 data.setProvider("ZTE");
79 manager.addPackage(data);
80 } catch (CatalogResourceException e1) {
81 Assert.fail("Exception" + e1.getMessage());
86 * delete data after test.
89 public void tearDown() {
91 manager.deletePackage("10001");
92 } catch (CatalogResourceException e1) {
93 Assert.fail("Exception" + e1.getMessage());
98 public void testAddPackageRepeat() {
99 PackageData data = new PackageData();
100 data.setCsarId("10001");
102 data.setVersion("v1.0");
103 data.setProvider("ZTE");
105 manager.addPackage(data);
106 Assert.fail("no exception");
107 } catch (CatalogResourceException e1) {
108 Assert.assertTrue(true);
114 public void testQueryPackageByCsarId_exist() {
115 ArrayList<PackageData> list = new ArrayList<PackageData>();
117 list = manager.queryPackageByCsarId("10001");
118 } catch (CatalogResourceException e1) {
119 Assert.fail("Exception" + e1.getMessage());
121 Assert.assertTrue(list.size() > 0);
125 public void testQueryPackageByCsarId_not_exist() {
126 ArrayList<PackageData> list = new ArrayList<PackageData>();
128 list = manager.queryPackageByCsarId("10002");
129 } catch (CatalogResourceException e1) {
130 Assert.fail("Exception" + e1.getMessage());
132 Assert.assertTrue(list.size() == 0);
136 public void testQueryPackage_exist() {
138 ArrayList<PackageData> list = new ArrayList<PackageData>();
140 list = manager.queryPackage("AG", "ZTE", "v1.0", null, null);
141 } catch (CatalogResourceException e1) {
142 Assert.fail("Exception" + e1.getMessage());
144 Assert.assertTrue(list.size() > 0);
149 public void testQueryPackage_not_exist() {
151 ArrayList<PackageData> list = new ArrayList<PackageData>();
153 list = manager.queryPackage("AG", "ZTE", "v2.0", null, null);
154 } catch (CatalogResourceException e1) {
155 Assert.fail("Exception" + e1.getMessage());
157 Assert.assertTrue(list.size() == 0);
162 public void testUpdatePackage() {
163 PackageData data = new PackageData();
166 manager.updatePackage(data, "10001");
167 } catch (CatalogResourceException e1) {
168 Assert.fail("Exception" + e1.getMessage());
170 Map<String, String> queryParam = new HashMap<String, String>();
171 queryParam.put(Parameters.csarId.name(), "10001");
172 ArrayList<PackageData> list = new ArrayList<PackageData>();
174 list = manager.queryPackageByCsarId("10001");
175 } catch (CatalogResourceException e1) {
176 Assert.fail("Exception" + e1.getMessage());
178 assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));