Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / test / java / org / onap / aai / esr / db / resource / VnfmManagerTest.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 static org.junit.Assert.assertTrue;
19
20 import org.junit.After;
21 import org.junit.AfterClass;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.onap.aai.esr.dao.DaoManager;
28 import org.onap.aai.esr.db.util.H2DbServer;
29 import org.onap.aai.esr.db.util.HibernateSession;
30 import org.onap.aai.esr.entity.db.VnfmData;
31 import org.onap.aai.esr.exception.ExtsysException;
32 import org.onap.aai.esr.handle.VnfmHandler;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36
37
38 import java.util.List;
39
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest({VnfmHandler.class})
42 public class VnfmManagerTest {
43   private VnfmHandler handler;
44   private String id = "0000000000000000";
45
46   @BeforeClass
47   public static void setUpBeforeClass() throws Exception {
48     H2DbServer.startUp();
49   }
50
51   /**
52    * shut down db.
53    */
54   @AfterClass
55   public static void tearDownAfterClass() throws Exception {
56     try {
57       HibernateSession.destory();
58       H2DbServer.shutDown();
59     } catch (Exception error) {
60       Assert.fail("Exception" + error.getMessage());
61     }
62   }
63
64   /**
65    * init db data.
66    */
67   @Before
68   public void setUp() throws Exception {
69     DaoManager.getInstance().setSessionFactory(HibernateSession.init());
70     VnfmData data = new VnfmData();
71     data.setName("vnfm");
72     handler = PowerMockito.spy(new VnfmHandler());
73     PowerMockito.doReturn(true).when(handler, "validity", data);
74     try {
75       id = handler.add(data).getId();
76     } catch (ExtsysException error) {
77       Assert.fail("Exception" + error.getMessage());
78     }
79   }
80
81   /**
82    * clear db data.
83    */
84   @After
85   public void tearDown() {
86     try {
87       handler.delete(id);
88     } catch (ExtsysException error) {
89       Assert.fail("Exception" + error.getMessage());
90     }
91   }
92
93
94   @Test
95   public void testQueryVnfmById_exist() {
96     List<VnfmData> list = null;
97     try {
98       list = handler.getVnfmById(id);
99     } catch (ExtsysException error) {
100       Assert.fail("Exception" + error.getMessage());
101       return;
102     }
103     Assert.assertTrue(list.size() > 0);
104   }
105
106   @Test
107   public void testQueryVnfmById_not_exist() {
108     List<VnfmData> list = null;
109     try {
110       list = handler.getVnfmById("100001");
111     } catch (ExtsysException error) {
112       Assert.fail("Exception" + error.getMessage());
113       return;
114     }
115     Assert.assertTrue(list.size() == 0);
116   }
117
118   @Test
119   public void testUpdateVnfm() {
120     VnfmData data = new VnfmData();
121     data.setName("vnfm_new");
122     try {
123       handler.update(data, id);
124     } catch (ExtsysException error1) {
125       Assert.fail("Exception" + error1.getMessage());
126       return;
127     }
128     List<VnfmData> list = null;
129     try {
130       list = handler.getVnfmById(id);
131     } catch (ExtsysException error) {
132       Assert.fail("Exception" + error.getMessage());
133       return;
134     }
135     assertTrue(list.size() > 0 && list.get(0).getName().equals("vnfm_new"));
136   }
137
138   @Test
139   public void testAddVnfmInstance_validity_false() throws Exception {
140     VnfmData data = new VnfmData();
141     data.setName("Vnfm");
142     PowerMockito.doReturn(false).when(handler, "validity", data);
143     try {
144       handler.add(data);
145     } catch (ExtsysException error) {
146       Assert.assertTrue(true);
147       return;
148     }
149     Assert.fail("not Exception");
150   }
151
152   @Test
153   public void testAddVnfmInstance_validity_throw_ExtsysException() throws Exception {
154     VnfmData data = new VnfmData();
155     data.setName("vnfm2");
156     PowerMockito.doReturn(false).when(handler, "validity", data);
157     PowerMockito.doThrow(new ExtsysException()).when(handler, "validity", data);
158     try {
159       handler.add(data);
160     } catch (ExtsysException error) {
161       Assert.assertTrue(true);
162       return;
163     }
164     Assert.fail("not Exception");
165   }
166 }