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