b61fde9e511caaf34264eedec57aa42ad9787908
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.commontosca.catalog.db.resource.dao;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Map;
24
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;
38
39 public class PackageDaoTest {
40
41     private static PackageDao packageDao;
42
43     @BeforeClass
44     public static void setUpBeforeClass() throws Exception {
45         H2DbServer.startUp();
46         packageDao = new PackageDao(HibernateSession.init());
47
48     }
49
50     @AfterClass
51     public static void tearDownAfterClass() throws Exception {
52         try {
53             HibernateSession.destory();
54             H2DbServer.shutDown();
55         } catch (Exception e) {
56             Assert.fail("Exception" + e.getMessage());
57         }
58     }
59
60     @Before
61     public void setUp() {
62         PackageData data = new PackageData();
63         data.setCsarId("10001");
64         data.setName("AG");
65         try {
66             packageDao.create(data);
67         } catch (CatalogResourceException e) {
68             Assert.fail("Exception" + e.getMessage());
69         }
70     }
71
72     @After
73     public void tearDown() {
74         PackageData data = new PackageData();
75         data.setCsarId("10001");
76         try {
77             packageDao.delete(data);
78         } catch (CatalogResourceException e) {
79             Assert.fail("Exception" + e.getMessage());
80         }
81     }
82
83     @Test
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>();
88         try {
89             list = (ArrayList<PackageData>) packageDao.query(queryParam);
90         } catch (CatalogResourceException e) {
91             Assert.fail("Exception" + e.getMessage());
92         }
93         Assert.assertTrue(list.size() > 0);
94     }
95
96     @Test
97     public void testUpdatePackage() {
98         PackageData data = new PackageData();
99         data.setSize("20M");
100         try {
101             packageDao.update(data, HqlFactory.getOidFilter(Parameters.csarId.name(), "10001"));
102         } catch (CatalogResourceException e1) {
103             Assert.fail("Exception" + e1.getMessage());
104         }
105         Map<String, String> queryParam = new HashMap<String, String>();
106         queryParam.put(Parameters.csarId.name(), "10001");
107         ArrayList<PackageData> list = new ArrayList<PackageData>();
108         try {
109             list = (ArrayList<PackageData>) packageDao.query(queryParam);
110         } catch (CatalogResourceException e) {
111             Assert.fail("Exception" + e.getMessage());
112         }
113         assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));
114     }
115
116     @Test
117     public void testDeleteByOid() {
118         PackageData data = new PackageData();
119         data.setCsarId("10001");
120         try {
121             packageDao.delete(data);
122         } catch (CatalogResourceException e1) {
123             Assert.fail("Exception" + e1.getMessage());
124         }
125         Map<String, String> queryParam = new HashMap<String, String>();
126         queryParam.put(Parameters.csarId.name(), "10001");
127         ArrayList<PackageData> list = new ArrayList<PackageData>();
128         try {
129             list = (ArrayList<PackageData>) packageDao.query(queryParam);
130         } catch (CatalogResourceException e) {
131             Assert.fail("Exception" + e.getMessage());
132         }
133         assertEquals(list.size(), 0);
134     }
135
136 }