32ac3198c5d846aada02a60bb9250c75b5894904
[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;
17
18 import static org.junit.Assert.assertTrue;
19
20
21 import org.junit.After;
22 import org.junit.AfterClass;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.openo.commontosca.catalog.db.common.Parameters;
28 import org.openo.commontosca.catalog.db.dao.DaoManager;
29 import org.openo.commontosca.catalog.db.entity.PackageData;
30 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
31 import org.openo.commontosca.catalog.db.util.H2DbServer;
32 import org.openo.commontosca.catalog.db.util.HibernateSession;
33
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.Map;
37
38
39 public class PackageManagerTest {
40   private static PackageManager manager;
41
42   /**
43    * startup db session before class.
44    * @throws Exception e
45    */
46   @BeforeClass
47   public static void setUpBeforeClass() throws Exception {
48     H2DbServer.startUp();
49     DaoManager.getInstance().setSessionFactory(HibernateSession.init());
50     manager = PackageManager.getInstance();
51   }
52
53   /**
54    * destory db session after class.
55    * @throws Exception e
56    */
57   @AfterClass
58   public static void tearDownAfterClass() throws Exception {
59     try {
60       HibernateSession.destory();
61       DaoManager.getInstance().setPackageDao(null);
62       H2DbServer.shutDown();
63     } catch (Exception e1) {
64       Assert.fail("Exception" + e1.getMessage());
65     }
66   }
67
68   /**
69    * create data before test.
70    */
71   @Before
72   public void setUp() {
73     PackageData data = new PackageData();
74     data.setCsarId("10001");
75     data.setName("AG");
76     data.setVersion("v1.0");
77     data.setProvider("ZTE");
78     try {
79       manager.addPackage(data);
80     } catch (CatalogResourceException e1) {
81       Assert.fail("Exception" + e1.getMessage());
82     }
83   }
84
85   /**
86    * delete data after test.
87    */
88   @After
89   public void tearDown() {
90     try {
91       manager.deletePackage("10001");
92     } catch (CatalogResourceException e1) {
93       Assert.fail("Exception" + e1.getMessage());
94     }
95   }
96
97   @Test
98   public void testAddPackageRepeat() {
99     PackageData data = new PackageData();
100     data.setCsarId("10001");
101     data.setName("AG");
102     data.setVersion("v1.0");
103     data.setProvider("ZTE");
104     try {
105       manager.addPackage(data);
106       Assert.fail("no exception");
107     } catch (CatalogResourceException e1) {
108       Assert.assertTrue(true);
109     }
110
111   }
112
113   @Test
114   public void testQueryPackageByCsarId_exist() {
115     ArrayList<PackageData> list = new ArrayList<PackageData>();
116     try {
117       list = manager.queryPackageByCsarId("10001");
118     } catch (CatalogResourceException e1) {
119       Assert.fail("Exception" + e1.getMessage());
120     }
121     Assert.assertTrue(list.size() > 0);
122   }
123
124   @Test
125   public void testQueryPackageByCsarId_not_exist() {
126     ArrayList<PackageData> list = new ArrayList<PackageData>();
127     try {
128       list = manager.queryPackageByCsarId("10002");
129     } catch (CatalogResourceException e1) {
130       Assert.fail("Exception" + e1.getMessage());
131     }
132     Assert.assertTrue(list.size() == 0);
133   }
134
135   @Test
136   public void testQueryPackage_exist() {
137
138     ArrayList<PackageData> list = new ArrayList<PackageData>();
139     try {
140       list = manager.queryPackage("AG", "ZTE", "v1.0", null, null);
141     } catch (CatalogResourceException e1) {
142       Assert.fail("Exception" + e1.getMessage());
143     }
144     Assert.assertTrue(list.size() > 0);
145
146   }
147
148   @Test
149   public void testQueryPackage_not_exist() {
150
151     ArrayList<PackageData> list = new ArrayList<PackageData>();
152     try {
153       list = manager.queryPackage("AG", "ZTE", "v2.0", null, null);
154     } catch (CatalogResourceException e1) {
155       Assert.fail("Exception" + e1.getMessage());
156     }
157     Assert.assertTrue(list.size() == 0);
158
159   }
160
161   @Test
162   public void testUpdatePackage() {
163     PackageData data = new PackageData();
164     data.setSize("20M");
165     try {
166       manager.updatePackage(data, "10001");
167     } catch (CatalogResourceException e1) {
168       Assert.fail("Exception" + e1.getMessage());
169     }
170     Map<String, String> queryParam = new HashMap<String, String>();
171     queryParam.put(Parameters.csarId.name(), "10001");
172     ArrayList<PackageData> list = new ArrayList<PackageData>();
173     try {
174       list = manager.queryPackageByCsarId("10001");
175     } catch (CatalogResourceException e1) {
176       Assert.fail("Exception" + e1.getMessage());
177     }
178     assertTrue(list.size() > 0 && list.get(0).getSize().equals("20M"));
179   }
180
181 }