2f27676ef533b8e107b055e1d87d41faac8f2e8a
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 ZTE Corporation.
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
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;
35
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 public class PackageDaoTest {
41
42   private static PackageDao packageDao;
43
44   /**
45    * setup db session before class.
46    * @throws Exception e
47    */
48   @BeforeClass
49   public static void setUpBeforeClass() throws Exception {
50     H2DbServer.startUp();
51     packageDao = new PackageDao(HibernateSession.init());
52
53   }
54
55   /**
56    * shutdown db session before class.
57    * @throws Exception e
58    */
59   @AfterClass
60   public static void tearDownAfterClass() throws Exception {
61     try {
62       HibernateSession.destory();
63       H2DbServer.shutDown();
64     } catch (Exception e1) {
65       Assert.fail("Exception" + e1.getMessage());
66     }
67   }
68
69   /**
70    * create data before test.
71    * @throws Exception e
72    */
73   @Before
74   public void setUp() {
75     PackageData data = new PackageData();
76     data.setCsarId("10001");
77     data.setName("AG");
78     try {
79       packageDao.create(data);
80     } catch (CatalogResourceException e1) {
81       Assert.fail("Exception" + e1.getMessage());
82     }
83   }
84
85   /**
86    * delete data after test.
87    * @throws Exception e
88    */
89   @After
90   public void tearDown() {
91     PackageData data = new PackageData();
92     data.setCsarId("10001");
93     try {
94       packageDao.delete(data);
95     } catch (CatalogResourceException e1) {
96       Assert.fail("Exception" + e1.getMessage());
97     }
98   }
99
100   @Test
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>();
105     try {
106       list = (ArrayList<PackageData>) packageDao.query(queryParam);
107     } catch (CatalogResourceException e1) {
108       Assert.fail("Exception" + e1.getMessage());
109     }
110     Assert.assertTrue(list.size() > 0);
111   }
112
113   @Test
114   public void testUpdatePackage() {
115     PackageData data = new PackageData();
116     data.setSize("20M");
117     try {
118       packageDao.update(data, HqlFactory.getOidFilter(Parameters.csarId.name(), "10001"));
119     } catch (CatalogResourceException e1) {
120       Assert.fail("Exception" + e1.getMessage());
121     }
122     Map<String, String> queryParam = new HashMap<String, String>();
123     queryParam.put(Parameters.csarId.name(), "10001");
124     ArrayList<PackageData> list = new ArrayList<PackageData>();
125     try {
126       list = (ArrayList<PackageData>) packageDao.query(queryParam);
127     } catch (CatalogResourceException e1) {
128       Assert.fail("Exception" + e1.getMessage());
129     }
130     assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));
131   }
132
133   @Test
134   public void testDeleteByOid() {
135     PackageData data = new PackageData();
136     data.setCsarId("10001");
137     try {
138       packageDao.delete(data);
139     } catch (CatalogResourceException e1) {
140       Assert.fail("Exception" + e1.getMessage());
141     }
142     Map<String, String> queryParam = new HashMap<String, String>();
143     queryParam.put(Parameters.csarId.name(), "10001");
144     ArrayList<PackageData> list = new ArrayList<PackageData>();
145     try {
146       list = (ArrayList<PackageData>) packageDao.query(queryParam);
147     } catch (CatalogResourceException e1) {
148       Assert.fail("Exception" + e1.getMessage());
149     }
150     assertEquals(list.size(), 0);
151   }
152
153 }