4181512d880fd07223e746eb1e84780edeb70898
[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.wrapper;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20
21 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
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.CatalogAppConfiguration;
29 import org.openo.commontosca.catalog.common.Config;
30 import org.openo.commontosca.catalog.common.HttpServerAddrConfig;
31 import org.openo.commontosca.catalog.common.HttpServerPathConfig;
32 import org.openo.commontosca.catalog.common.MsbAddrConfig;
33 import org.openo.commontosca.catalog.db.dao.DaoManager;
34 import org.openo.commontosca.catalog.db.entity.PackageData;
35 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
36 import org.openo.commontosca.catalog.db.resource.PackageManager;
37 import org.openo.commontosca.catalog.db.util.H2DbServer;
38 import org.openo.commontosca.catalog.db.util.HibernateSession;
39 import org.openo.commontosca.catalog.entity.EnumOnboardState;
40 import org.openo.commontosca.catalog.entity.EnumOperationalState;
41 import org.openo.commontosca.catalog.entity.EnumProcessState;
42 import org.openo.commontosca.catalog.entity.EnumUsageState;
43 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
44 import org.openo.commontosca.catalog.entity.response.PackageMeta;
45 import org.openo.commontosca.catalog.model.parser.EnumPackageFormat;
46 import org.openo.commontosca.catalog.model.parser.ModelParserFactory;
47 import org.openo.commontosca.catalog.model.parser.yaml.zte.ToscaYamlModelParser;
48
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.FileNotFoundException;
52 import java.io.InputStream;
53 import java.net.URISyntaxException;
54 import java.util.ArrayList;
55
56 import javax.ws.rs.core.Response;
57
58 public class PackageWrapperTest {
59   private static String resourcePath;
60   
61   static {
62     MsbAddrConfig.setMsbAddress("http://10.74.44.28:80");
63     HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080");
64     HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/");
65     
66     CatalogAppConfiguration configuration = new CatalogAppConfiguration();
67     Config.setConfigration(configuration);
68     Config.getConfigration().setParserType("zte");
69     ModelParserFactory.getInstance().put(EnumPackageFormat.TOSCA_YAML, new TestYamlModelParser());
70   }
71   
72   private static PackageManager manager;
73
74
75   /**
76    * startup db session before class.
77    * @throws Exception e
78    */
79   @BeforeClass
80   public static void setUpBeforeClass() throws CatalogResourceException {
81     H2DbServer.startUp();
82     DaoManager.getInstance().setSessionFactory(HibernateSession.init());
83     manager = PackageManager.getInstance();
84     CatalogAppConfiguration configuration = new CatalogAppConfiguration();
85     Config.setConfigration(configuration);
86     System.out.println("Set up before class");
87   }
88
89   /**
90    * create data before test.
91    */
92   @Before
93   public void setUp() throws Exception {
94     ArrayList<PackageData> packageList = manager.queryPackage(null, null, null, null, null);
95     if (packageList != null && packageList.size() > 0) {
96       for (int i = 0; i < packageList.size(); i++) {
97         String packageOid = packageList.get(i).getCsarId();
98         manager.deletePackage(packageOid);
99       }
100     }
101     ArrayList<PackageData> packageList2 = manager.queryPackage(null, null, null, null, null);
102     if (packageList2 != null && packageList2.size() > 0) {
103       for (int j = 0; j < packageList2.size(); j++) {
104         if (packageList2.get(j).getCsarId().equals("123456")) {
105           manager.updatePackage(getPackageData(), "123456");
106         } else {
107           PackageData packageData = new PackageData();
108           packageData = getPackageData();
109           manager.addPackage(packageData);
110         }
111       }
112     } else {
113       PackageData packageData = new PackageData();
114       packageData = getPackageData();
115       manager.addPackage(packageData);
116     }
117   }
118
119   // @Ignore
120   @Test
121   public void testUploadPackage() throws Exception {
122     InputStream ins = null;
123     Response result = null;
124     Response result1 = null;
125     Response result2 = null;
126     // PackageData packageData = new PackageData();
127     // packageData = getPackageData();
128
129     FormDataContentDisposition fileDetail =
130         FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();
131
132     try {
133       resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
134     } catch (URISyntaxException e1) {
135       e1.printStackTrace();
136     }
137     final String filename = "NanocellGW.csar";
138     File packageFile = new File(resourcePath + filename);
139     try {
140       ins = new FileInputStream(packageFile);
141     } catch (FileNotFoundException e2) {
142       e2.printStackTrace();
143     }
144     if (ins != null) {
145       try {
146         result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
147       } catch (Exception e3) {
148         e3.printStackTrace();
149       }
150     }
151     assertNotNull(result);
152     assertEquals(200, result.getStatus());
153     assertNotNull(result.getEntity());
154
155     try {
156       result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
157     } catch (Exception e4) {
158       e4.printStackTrace();
159     }
160     assertEquals(500, result1.getStatus());
161
162     try {
163       result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
164     } catch (Exception e5) {
165       e5.printStackTrace();
166     }
167     assertEquals(500, result2.getStatus());
168   }
169
170   @Test
171   public void testQueryPackageById() throws Exception {
172     PackageMeta metas = new PackageMeta();
173     metas = getPackageMeta();
174
175     Response result = PackageWrapper.getInstance().queryPackageById("123456");
176     assertEquals(200, result.getStatus());
177     //assertEquals(metas, result.getEntity());
178   }
179
180   @Test
181   public void testQueryPackageByCond() {
182     ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
183     metas = getPackageMetaList();
184     System.out.println("Test query package by Id");
185     Response result =
186         PackageWrapper.getInstance().queryPackageListByCond("NanocellGW", "ZTE", "V1.0", "false",
187             "NSAR");
188     assertEquals(200, result.getStatus());
189     //assertEquals(metas, result.getEntity());
190   }
191
192   @Test
193   public void testUpdatePackageStatus() {
194     System.out.println("Test update package status");
195     Response result =
196         PackageWrapper.getInstance().updatePackageStatus("123456", "Enabled", "NotInUse", "true",
197             "onBoarding", "true");
198     assertEquals(200, result.getStatus());
199   }
200
201   @Test
202   public void testGetCsarFileUri() {
203     System.out.println("Test get csar file uri ");
204     CsarFileUriResponse expectResult = new CsarFileUriResponse();
205     String csarFileUri =
206         getDownloadUriHead() + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
207     String localUri = HttpServerPathConfig.getHttpServerPath()
208         + "NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
209     File srcDir = new File(localUri);
210     String localPath = srcDir.getAbsolutePath().replace("\\", "/");
211
212     expectResult.setDownloadUri(csarFileUri);
213     expectResult.setLocalPath(localPath);
214     Response result = PackageWrapper.getInstance().getCsarFileUri("123456", "/images/segw.img");
215     assertEquals(200, result.getStatus());
216     //assertEquals(expectResult, result.getEntity());
217   }
218
219   // @Test
220   // public void testDelPackage() {
221   // System.out.println("Test delete package ");
222   // Response result = PackageWrapper.getInstance().delPackage("123456");
223   // assertEquals(204, result.getStatus());
224   // try {
225   // Thread.sleep(5000);
226   // } catch (InterruptedException e1) {
227   // e1.printStackTrace();
228   // }
229   // }
230
231   /**
232    * delete data after test.
233    */
234   @After
235   public void tearDown() throws Exception {
236     ArrayList<PackageData> packageList = manager.queryPackageByCsarId("123456");
237     if (packageList != null && packageList.size() != 0) {
238       manager.deletePackage("123456");
239     } else {
240       return;
241     }
242     System.out.println("Tear down");
243   }
244
245   /**
246    * destory db session after class.
247    * @throws Exception e
248    */
249   @AfterClass
250   public static void tearDownAfterClass() {
251     try {
252       HibernateSession.destory();
253       DaoManager.getInstance().setDaoNull();
254       H2DbServer.shutDown();
255     } catch (Exception e1) {
256       Assert.fail("Exception" + e1.getMessage());
257     }
258   }
259
260   private PackageData getPackageData() {
261     PackageData packageData = new PackageData();
262     packageData.setCsarId("123456");
263     packageData.setCreateTime("2016-06-29 03:33:15");
264     packageData.setDeletionPending("false");
265     packageData.setDownloadUri("/NSAR/ZTE/NanocellGW/v1.0/");
266     packageData.setFormat("yml");
267     packageData.setModifyTime("2016-06-29 03:33:15");
268     packageData.setName("NanocellGW");
269     packageData.setOnBoardState("non-onBoarded");
270     packageData.setOperationalState("Disabled");
271     packageData.setProvider("ZTE");
272     packageData.setSize("0.93M");
273     packageData.setType("NSAR");
274     packageData.setUsageState("InUse");
275     packageData.setVersion("V1.0");
276     packageData.setProcessState("normal");
277     return packageData;
278   }
279
280   private ArrayList<PackageMeta> getPackageMetaList() {
281     PackageMeta meta = new PackageMeta();
282     meta = getPackageMeta();
283     ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
284     metas.add(meta);
285     return metas;
286   }
287   
288   private PackageMeta getPackageMeta() {
289     PackageMeta meta = new PackageMeta();
290     meta.setCreateTime("2016-06-29 03:33:15");
291     meta.setCsarId("123456");
292     meta.setDeletionPending(false);
293     meta.setDownloadUri(getDownloadUriHead() 
294         + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW.csar");
295     meta.setFormat("yml");
296     meta.setModifyTime("2016-06-29 03:33:15");
297     meta.setName("NanocellGW");
298     meta.setOperationalState(EnumOperationalState.valueOf("Disabled"));
299     meta.setProvider("ZTE");
300     meta.setSize("0.93M");
301     meta.setType("NSAR");
302     meta.setUsageState(EnumUsageState.valueOf("InUse"));
303     meta.setVersion("V1.0");
304     meta.setOnBoardState(EnumOnboardState.nonOnBoarded.getValue());
305     meta.setProcessState(EnumProcessState.valueOf("normal"));
306     return meta;
307   }
308   
309   private String getDownloadUriHead() {
310     return MsbAddrConfig.getMsbAddress() + "/files/catalog-http";
311   }
312 }