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.dao;
18 import static org.junit.Assert.assertEquals;
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.PackageDao;
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;
34 import org.openo.commontosca.catalog.db.util.HqlFactory;
36 import java.util.ArrayList;
37 import java.util.HashMap;
40 public class PackageDaoTest {
42 private static PackageDao packageDao;
45 * setup db session before class.
49 public static void setUpBeforeClass() throws Exception {
51 packageDao = new PackageDao(HibernateSession.init());
56 * shutdown db session before class.
60 public static void tearDownAfterClass() throws Exception {
62 HibernateSession.destory();
63 H2DbServer.shutDown();
64 } catch (Exception e1) {
65 Assert.fail("Exception" + e1.getMessage());
70 * create data before test.
75 PackageData data = new PackageData();
76 data.setCsarId("10001");
79 packageDao.create(data);
80 } catch (CatalogResourceException e1) {
81 Assert.fail("Exception" + e1.getMessage());
86 * delete data after test.
90 public void tearDown() {
91 PackageData data = new PackageData();
92 data.setCsarId("10001");
94 packageDao.delete(data);
95 } catch (CatalogResourceException e1) {
96 Assert.fail("Exception" + e1.getMessage());
101 public void testQueryPackageById() {
102 Map<String, String> queryParam = new HashMap<String, String>();
103 queryParam.put(Parameters.csarId.name(), "10001");
104 ArrayList<PackageData> list = new ArrayList<PackageData>();
106 list = (ArrayList<PackageData>) packageDao.query(queryParam);
107 } catch (CatalogResourceException e1) {
108 Assert.fail("Exception" + e1.getMessage());
110 Assert.assertTrue(list.size() > 0);
114 public void testUpdatePackage() {
115 PackageData data = new PackageData();
118 packageDao.update(data, HqlFactory.getOidFilter(Parameters.csarId.name(), "10001"));
119 } catch (CatalogResourceException e1) {
120 Assert.fail("Exception" + e1.getMessage());
122 Map<String, String> queryParam = new HashMap<String, String>();
123 queryParam.put(Parameters.csarId.name(), "10001");
124 ArrayList<PackageData> list = new ArrayList<PackageData>();
126 list = (ArrayList<PackageData>) packageDao.query(queryParam);
127 } catch (CatalogResourceException e1) {
128 Assert.fail("Exception" + e1.getMessage());
130 assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));
134 public void testDeleteByOid() {
135 PackageData data = new PackageData();
136 data.setCsarId("10001");
138 packageDao.delete(data);
139 } catch (CatalogResourceException e1) {
140 Assert.fail("Exception" + e1.getMessage());
142 Map<String, String> queryParam = new HashMap<String, String>();
143 queryParam.put(Parameters.csarId.name(), "10001");
144 ArrayList<PackageData> list = new ArrayList<PackageData>();
146 list = (ArrayList<PackageData>) packageDao.query(queryParam);
147 } catch (CatalogResourceException e1) {
148 Assert.fail("Exception" + e1.getMessage());
150 assertEquals(list.size(), 0);