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