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