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.db.resource;
19 import static org.junit.Assert.assertTrue;
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.db.common.Parameters;
29 import org.openo.commontosca.catalog.db.dao.DaoManager;
30 import org.openo.commontosca.catalog.db.entity.PackageData;
31 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
32 import org.openo.commontosca.catalog.db.util.H2DbServer;
33 import org.openo.commontosca.catalog.db.util.HibernateSession;
35 import java.util.ArrayList;
36 import java.util.HashMap;
40 public class PackageManagerTest {
41 private static PackageManager manager;
44 * startup db session before class.
48 public static void setUpBeforeClass() throws Exception {
50 DaoManager.getInstance().setSessionFactory(HibernateSession.init());
51 manager = PackageManager.getInstance();
55 * destory db session after class.
59 public static void tearDownAfterClass() throws Exception {
61 HibernateSession.destory();
62 DaoManager.getInstance().setPackageDao(null);
63 H2DbServer.shutDown();
64 } catch (Exception e1) {
65 Assert.fail("Exception" + e1.getMessage());
70 * create data before test.
74 PackageData data = new PackageData();
75 data.setCsarId("10001");
77 data.setVersion("v1.0");
78 data.setProvider("ZTE");
80 manager.addPackage(data);
81 } catch (CatalogResourceException e1) {
82 Assert.fail("Exception" + e1.getMessage());
87 * delete data after test.
90 public void tearDown() {
92 manager.deletePackage("10001");
93 } catch (CatalogResourceException e1) {
94 Assert.fail("Exception" + e1.getMessage());
99 public void testAddPackageRepeat() {
100 PackageData data = new PackageData();
101 data.setCsarId("10001");
103 data.setVersion("v1.0");
104 data.setProvider("ZTE");
106 manager.addPackage(data);
107 Assert.fail("no exception");
108 } catch (CatalogResourceException e1) {
109 Assert.assertTrue(true);
115 public void testQueryPackageByCsarId_exist() {
116 ArrayList<PackageData> list = new ArrayList<PackageData>();
118 list = manager.queryPackageByCsarId("10001");
119 } catch (CatalogResourceException e1) {
120 Assert.fail("Exception" + e1.getMessage());
122 Assert.assertTrue(list.size() > 0);
126 public void testQueryPackageByCsarId_not_exist() {
127 ArrayList<PackageData> list = new ArrayList<PackageData>();
129 list = manager.queryPackageByCsarId("10002");
130 } catch (CatalogResourceException e1) {
131 Assert.fail("Exception" + e1.getMessage());
133 Assert.assertTrue(list.size() == 0);
137 public void testQueryPackage_exist() {
139 ArrayList<PackageData> list = new ArrayList<PackageData>();
141 list = manager.queryPackage("AG", "ZTE", "v1.0", null, null);
142 } catch (CatalogResourceException e1) {
143 Assert.fail("Exception" + e1.getMessage());
145 Assert.assertTrue(list.size() > 0);
150 public void testQueryPackage_not_exist() {
152 ArrayList<PackageData> list = new ArrayList<PackageData>();
154 list = manager.queryPackage("AG", "ZTE", "v2.0", null, null);
155 } catch (CatalogResourceException e1) {
156 Assert.fail("Exception" + e1.getMessage());
158 Assert.assertTrue(list.size() == 0);
163 public void testUpdatePackage() {
164 PackageData data = new PackageData();
167 manager.updatePackage(data, "10001");
168 } catch (CatalogResourceException e1) {
169 Assert.fail("Exception" + e1.getMessage());
171 Map<String, String> queryParam = new HashMap<String, String>();
172 queryParam.put(Parameters.csarId.name(), "10001");
173 ArrayList<PackageData> list = new ArrayList<PackageData>();
175 list = manager.queryPackageByCsarId("10001");
176 } catch (CatalogResourceException e1) {
177 Assert.fail("Exception" + e1.getMessage());
179 assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));