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.dao;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
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.Test;
29 import org.openo.commontosca.catalog.db.common.Parameters;
30 import org.openo.commontosca.catalog.db.dao.PackageDao;
31 import org.openo.commontosca.catalog.db.entity.PackageData;
32 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
33 import org.openo.commontosca.catalog.db.util.H2DbServer;
34 import org.openo.commontosca.catalog.db.util.HibernateSession;
35 import org.openo.commontosca.catalog.db.util.HqlFactory;
37 import java.util.ArrayList;
38 import java.util.HashMap;
41 public class PackageDaoTest {
43 private static PackageDao packageDao;
46 * setup db session before class.
50 public static void setUpBeforeClass() throws Exception {
52 packageDao = new PackageDao(HibernateSession.init());
57 * shutdown db session before class.
61 public static void tearDownAfterClass() throws Exception {
63 HibernateSession.destory();
64 H2DbServer.shutDown();
65 } catch (Exception e1) {
66 Assert.fail("Exception" + e1.getMessage());
71 * create data before test.
76 PackageData data = new PackageData();
77 data.setCsarId("10001");
80 packageDao.create(data);
81 } catch (CatalogResourceException e1) {
82 Assert.fail("Exception" + e1.getMessage());
87 * delete data after test.
91 public void tearDown() {
92 PackageData data = new PackageData();
93 data.setCsarId("10001");
95 packageDao.delete(data);
96 } catch (CatalogResourceException e1) {
97 Assert.fail("Exception" + e1.getMessage());
102 public void testQueryPackageById() {
103 Map<String, String> queryParam = new HashMap<String, String>();
104 queryParam.put(Parameters.csarId.name(), "10001");
105 ArrayList<PackageData> list = new ArrayList<PackageData>();
107 list = (ArrayList<PackageData>) packageDao.query(queryParam);
108 } catch (CatalogResourceException e1) {
109 Assert.fail("Exception" + e1.getMessage());
111 Assert.assertTrue(list.size() > 0);
115 public void testUpdatePackage() {
116 PackageData data = new PackageData();
119 packageDao.update(data, HqlFactory.getOidFilter(Parameters.csarId.name(), "10001"));
120 } catch (CatalogResourceException e1) {
121 Assert.fail("Exception" + e1.getMessage());
123 Map<String, String> queryParam = new HashMap<String, String>();
124 queryParam.put(Parameters.csarId.name(), "10001");
125 ArrayList<PackageData> list = new ArrayList<PackageData>();
127 list = (ArrayList<PackageData>) packageDao.query(queryParam);
128 } catch (CatalogResourceException e1) {
129 Assert.fail("Exception" + e1.getMessage());
131 assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));
135 public void testDeleteByOid() {
136 PackageData data = new PackageData();
137 data.setCsarId("10001");
139 packageDao.delete(data);
140 } catch (CatalogResourceException e1) {
141 Assert.fail("Exception" + e1.getMessage());
143 Map<String, String> queryParam = new HashMap<String, String>();
144 queryParam.put(Parameters.csarId.name(), "10001");
145 ArrayList<PackageData> list = new ArrayList<PackageData>();
147 list = (ArrayList<PackageData>) packageDao.query(queryParam);
148 } catch (CatalogResourceException e1) {
149 Assert.fail("Exception" + e1.getMessage());
151 assertEquals(list.size(), 0);