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.dao;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
21 import java.util.ArrayList;
22 import java.util.HashMap;
25 import org.junit.After;
26 import org.junit.AfterClass;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.openo.commontosca.catalog.db.common.Parameters;
32 import org.openo.commontosca.catalog.db.dao.PackageDao;
33 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
34 import org.openo.commontosca.catalog.db.util.HqlFactory;
35 import org.openo.commontosca.catalog.db.entity.PackageData;
36 import org.openo.commontosca.catalog.db.util.H2DbServer;
37 import org.openo.commontosca.catalog.db.util.HibernateSession;
39 public class PackageDaoTest {
41 private static PackageDao packageDao;
44 public static void setUpBeforeClass() throws Exception {
46 packageDao = new PackageDao(HibernateSession.init());
51 public static void tearDownAfterClass() throws Exception {
53 HibernateSession.destory();
54 H2DbServer.shutDown();
55 } catch (Exception e) {
56 Assert.fail("Exception" + e.getMessage());
62 PackageData data = new PackageData();
63 data.setCsarId("10001");
66 packageDao.create(data);
67 } catch (CatalogResourceException e) {
68 Assert.fail("Exception" + e.getMessage());
73 public void tearDown() {
74 PackageData data = new PackageData();
75 data.setCsarId("10001");
77 packageDao.delete(data);
78 } catch (CatalogResourceException e) {
79 Assert.fail("Exception" + e.getMessage());
84 public void testQueryPackageById() {
85 Map<String, String> queryParam = new HashMap<String, String>();
86 queryParam.put(Parameters.csarId.name(), "10001");
87 ArrayList<PackageData> list = new ArrayList<PackageData>();
89 list = (ArrayList<PackageData>) packageDao.query(queryParam);
90 } catch (CatalogResourceException e) {
91 Assert.fail("Exception" + e.getMessage());
93 Assert.assertTrue(list.size() > 0);
97 public void testUpdatePackage() {
98 PackageData data = new PackageData();
101 packageDao.update(data, HqlFactory.getOidFilter(Parameters.csarId.name(), "10001"));
102 } catch (CatalogResourceException e1) {
103 Assert.fail("Exception" + e1.getMessage());
105 Map<String, String> queryParam = new HashMap<String, String>();
106 queryParam.put(Parameters.csarId.name(), "10001");
107 ArrayList<PackageData> list = new ArrayList<PackageData>();
109 list = (ArrayList<PackageData>) packageDao.query(queryParam);
110 } catch (CatalogResourceException e) {
111 Assert.fail("Exception" + e.getMessage());
113 assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));
117 public void testDeleteByOid() {
118 PackageData data = new PackageData();
119 data.setCsarId("10001");
121 packageDao.delete(data);
122 } catch (CatalogResourceException e1) {
123 Assert.fail("Exception" + e1.getMessage());
125 Map<String, String> queryParam = new HashMap<String, String>();
126 queryParam.put(Parameters.csarId.name(), "10001");
127 ArrayList<PackageData> list = new ArrayList<PackageData>();
129 list = (ArrayList<PackageData>) packageDao.query(queryParam);
130 } catch (CatalogResourceException e) {
131 Assert.fail("Exception" + e.getMessage());
133 assertEquals(list.size(), 0);