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