Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / test / java / org / onap / aai / esr / db / resource / CommonManagerTest.java
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.onap.aai.esr.db.resource;
17
18 import org.junit.After;
19 import org.junit.AfterClass;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.onap.aai.esr.dao.DaoManager;
26 import org.onap.aai.esr.db.util.H2DbServer;
27 import org.onap.aai.esr.db.util.HibernateSession;
28 import org.onap.aai.esr.entity.db.BaseData;
29 import org.onap.aai.esr.entity.db.EmsData;
30 import org.onap.aai.esr.entity.db.VnfmData;
31 import org.onap.aai.esr.exception.ExtsysException;
32 import org.onap.aai.esr.handle.CommonHandler;
33 import org.onap.aai.esr.handle.EmsHandler;
34 import org.onap.aai.esr.handle.VnfmHandler;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 import java.util.HashMap;
40 import java.util.List;
41
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest({EmsHandler.class, VnfmHandler.class})
44 public class CommonManagerTest {
45   private CommonHandler handler = new CommonHandler();
46   private EmsHandler emshandler;
47   private VnfmHandler vnfHandler;
48   private HashMap<String, String> idMap = new HashMap<String, String>();
49
50   @BeforeClass
51   public static void setUpBeforeClass() throws Exception {
52     H2DbServer.startUp();
53
54   }
55
56   /**
57    * shut down db.
58    */
59   @AfterClass
60   public static void tearDownAfterClass() throws Exception {
61     try {
62       HibernateSession.destory();
63       H2DbServer.shutDown();
64     } catch (Exception error) {
65       Assert.fail("Exception" + error.getMessage());
66     }
67   }
68
69   /**
70    * init db data.
71    */
72   @Before
73   public void setUp() throws Exception {
74     DaoManager.getInstance().setSessionFactory(HibernateSession.init());
75     try {
76       EmsData data = new EmsData();
77       data.setName("ems");
78       emshandler = PowerMockito.spy(new EmsHandler());
79       PowerMockito.doReturn(true).when(emshandler, "validity", data);
80       idMap.put("ems", emshandler.add(data).getId());
81       VnfmData vnfm = new VnfmData();
82       vnfm.setName("VNFM");
83       vnfHandler = PowerMockito.spy(new VnfmHandler());
84       PowerMockito.doReturn(true).when(vnfHandler, "validity", vnfm);
85       idMap.put("vnfm", vnfHandler.add(vnfm).getId());
86     } catch (ExtsysException error) {
87       Assert.fail("Exception" + error.getMessage());
88     }
89   }
90
91   /**
92    * clear db data.
93    */
94   @After
95   public void tearDown() {
96     try {
97       java.util.Iterator<String> it = idMap.keySet().iterator();
98       while (it.hasNext()) {
99         String key = it.next();
100         if ("ems".equals(key)) {
101           emshandler.delete(idMap.get(key));
102         } else {
103           vnfHandler.delete(idMap.get(key));
104         }
105       }
106
107     } catch (ExtsysException error) {
108       Assert.fail("Exception" + error.getMessage());
109     }
110   }
111
112   @Test
113   public void testQueryEmsById_exist() {
114     List<BaseData> emslist = null;
115     List<BaseData> vnfmlist = null;
116     try {
117       emslist = handler.getInstanceById(idMap.get("ems"));
118       vnfmlist = handler.getInstanceById(idMap.get("vnfm"));
119     } catch (ExtsysException error) {
120       Assert.fail("Exception" + error.getMessage());
121       return;
122     }
123     Assert.assertTrue(emslist.size() > 0 && vnfmlist.size() > 0);
124   }
125
126   @Test
127   public void testQueryEmsById_not_exist() {
128     List<BaseData> list = null;
129     try {
130       list = handler.getInstanceById("123456");
131     } catch (ExtsysException error) {
132       Assert.fail("Exception" + error.getMessage());
133       return;
134     }
135     Assert.assertTrue(list.size() == 0);
136   }
137
138 }