Define the VIM manage API.
[aai/esr-server.git] / esr-core / esr-mgr / src / test / java / org / onap / aai / esr / db / resource / VimManagerTest.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.VimData;
32 import org.onap.aai.esr.exception.ExtsysException;
33 import org.onap.aai.esr.handle.VimHandler;
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({VimHandler.class})
43 public class VimManagerTest {
44   private VimHandler handler;
45   private String id = "0000000000000000";
46
47   @BeforeClass
48   public static void setUpBeforeClass() throws Exception {
49     H2DbServer.startUp();
50
51   }
52
53   /**
54    * shut down db.
55    */
56   @AfterClass
57   public static void tearDownAfterClass() throws Exception {
58     try {
59       HibernateSession.destory();
60       H2DbServer.shutDown();
61     } catch (Exception error) {
62       Assert.fail("Exception" + error.getMessage());
63     }
64   }
65
66   /**
67    * init db data.
68    */
69   @Before
70   public void setUp() throws Exception {
71     DaoManager.getInstance().setSessionFactory(HibernateSession.init());
72     VimData data = new VimData();
73     handler = PowerMockito.spy(new VimHandler());
74     PowerMockito.doReturn(true).when(handler, "validity", data);
75     data.setId("10000");
76     data.setName("vim");
77     try {
78       id = handler.add(data).getId();
79     } catch (ExtsysException error) {
80       Assert.fail("Exception" + error.getMessage());
81     }
82   }
83
84   /**
85    * clear db data.
86    */
87   @After
88   public void tearDown() {
89     try {
90       handler.delete(id);
91     } catch (ExtsysException error) {
92       Assert.fail("Exception" + error.getMessage());
93     }
94   }
95
96   @Ignore
97   @Test
98   public void testAddVimInstance_validity_false() throws Exception {
99     VimData data = new VimData();
100     data.setName("Vim");
101     PowerMockito.doReturn(false).when(handler, "validity", data);
102     try {
103       handler.add(data);
104     } catch (ExtsysException error) {
105       Assert.assertTrue(true);
106       return;
107     }
108     Assert.fail("not Exception");
109   }
110
111   @Ignore
112   @Test
113   public void testAddVimInstance_validity_throw_ExtsysException() throws Exception {
114     VimData data = new VimData();
115     data.setName("vim2");
116     PowerMockito.doReturn(false).when(handler, "validity", data);
117     PowerMockito.doThrow(new ExtsysException()).when(handler, "validity", data);
118     try {
119       handler.add(data);
120     } catch (ExtsysException error) {
121       Assert.assertTrue(true);
122       return;
123     }
124     Assert.fail("not Exception");
125   }
126
127   @Ignore
128   @Test
129   public void testQueryVimById_exist() {
130     List<VimData> list = null;
131     try {
132       list = handler.getVimById(id);
133     } catch (ExtsysException error) {
134       Assert.fail("Exception" + error.getMessage());
135       return;
136     }
137     Assert.assertTrue(list.size() > 0);
138   }
139
140   @Ignore
141   @Test
142   public void testQueryVimById_not_exist() {
143     List<VimData> list = null;
144     try {
145       list = handler.getVimById("100001");
146     } catch (ExtsysException error) {
147       Assert.fail("Exception" + error.getMessage());
148       return;
149     }
150     Assert.assertTrue(list.size() == 0);
151   }
152
153   @Ignore
154   @Test
155   public void testUpdateVim() {
156     VimData data = new VimData();
157     data.setName("vim_new");
158     try {
159       handler.update(data, id);
160     } catch (ExtsysException error1) {
161       Assert.fail("Exception" + error1.getMessage());
162       return;
163     }
164     List<VimData> list = null;
165     try {
166       list = handler.getVimById(id);
167     } catch (ExtsysException error) {
168       Assert.fail("Exception" + error.getMessage());
169       return;
170     }
171     assertTrue(list.size() > 0 && list.get(0).getName().equals("vim_new"));
172   }
173
174 }