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