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.
16 package org.openo.commontosca.catalog.db.resource;
18 import static org.junit.Assert.assertTrue;
20 import java.util.ArrayList;
21 import java.util.HashMap;
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.openo.commontosca.catalog.db.common.Parameters;
31 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
32 import org.openo.commontosca.catalog.db.dao.DaoManager;
33 import org.openo.commontosca.catalog.db.entity.PackageData;
34 import org.openo.commontosca.catalog.db.util.H2DbServer;
35 import org.openo.commontosca.catalog.db.util.HibernateSession;
37 public class PackageManagerTest {
38 private static PackageManager manager;
41 public static void setUpBeforeClass() throws Exception {
43 DaoManager.getInstance().setSessionFactory(HibernateSession.init());
44 manager = PackageManager.getInstance();
48 public static void tearDownAfterClass() throws Exception {
50 HibernateSession.destory();
51 DaoManager.getInstance().setPackageDao(null);
52 H2DbServer.shutDown();
53 } catch (Exception e) {
54 Assert.fail("Exception" + e.getMessage());
60 PackageData data = new PackageData();
61 data.setCsarId("10001");
63 data.setVersion("v1.0");
64 data.setProvider("ZTE");
66 manager.addPackage(data);
67 } catch (CatalogResourceException e) {
68 Assert.fail("Exception" + e.getMessage());
73 public void tearDown() {
75 manager.deletePackage("10001");
76 } catch (CatalogResourceException e) {
77 Assert.fail("Exception" + e.getMessage());
82 public void testAddPackageRepeat() {
83 PackageData data = new PackageData();
84 data.setCsarId("10001");
86 data.setVersion("v1.0");
87 data.setProvider("ZTE");
89 manager.addPackage(data);
90 Assert.fail("no exception");
91 } catch (CatalogResourceException e) {
92 Assert.assertTrue(true);
98 public void testQueryPackageByCsarId_exist() {
99 ArrayList<PackageData> list = new ArrayList<PackageData>();
101 list = manager.queryPackageByCsarId("10001");
102 } catch (CatalogResourceException e) {
103 Assert.fail("Exception" + e.getMessage());
105 Assert.assertTrue(list.size() > 0);
109 public void testQueryPackageByCsarId_not_exist() {
110 ArrayList<PackageData> list = new ArrayList<PackageData>();
112 list = manager.queryPackageByCsarId("10002");
113 } catch (CatalogResourceException e) {
114 Assert.fail("Exception" + e.getMessage());
116 Assert.assertTrue(list.size() == 0);
120 public void testQueryPackage_exist() {
122 ArrayList<PackageData> list = new ArrayList<PackageData>();
124 list = manager.queryPackage("AG", "ZTE", "v1.0", null, null);
125 } catch (CatalogResourceException e) {
126 Assert.fail("Exception" + e.getMessage());
128 Assert.assertTrue(list.size() > 0);
133 public void testQueryPackage_not_exist() {
135 ArrayList<PackageData> list = new ArrayList<PackageData>();
137 list = manager.queryPackage("AG", "ZTE", "v2.0", null, null);
138 } catch (CatalogResourceException e) {
139 Assert.fail("Exception" + e.getMessage());
141 Assert.assertTrue(list.size() == 0);
146 public void testUpdatePackage() {
147 PackageData data = new PackageData();
150 manager.updatePackage(data, "10001");
151 } catch (CatalogResourceException e1) {
152 Assert.fail("Exception" + e1.getMessage());
154 Map<String, String> queryParam = new HashMap<String, String>();
155 queryParam.put(Parameters.csarId.name(), "10001");
156 ArrayList<PackageData> list = new ArrayList<PackageData>();
158 list = manager.queryPackageByCsarId("10001");
159 } catch (CatalogResourceException e) {
160 Assert.fail("Exception" + e.getMessage());
162 assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));