Define the VIM manage API.
[aai/esr-server.git] / esr-core / esr-mgr / src / test / java / org / onap / aai / esr / db / resource / EmsManagerTest.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.EmsData;
32 import org.onap.aai.esr.exception.ExtsysException;
33 import org.onap.aai.esr.handle.EmsHandler;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
37
38 import java.util.List;
39
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest({EmsHandler.class})
42 public class EmsManagerTest {
43   private EmsHandler handler;
44   private static 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     EmsData data = new EmsData();
72     data.setName("ems");
73     handler = PowerMockito.spy(new EmsHandler());
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   @Test
95   @Ignore
96   public void testQueryEmsById_exist() {
97     List<EmsData> list = null;
98     try {
99       list = handler.getEmsById(id);
100     } catch (ExtsysException error) {
101       Assert.fail("Exception" + error.getMessage());
102       return;
103     }
104     Assert.assertTrue(list.size() > 0);
105   }
106
107   @Ignore
108   @Test
109   public void testAddEmsInstance_validity_false() throws Exception {
110     EmsData data = new EmsData();
111     data.setName("ems2");
112     PowerMockito.doReturn(false).when(handler, "validity", data);
113     try {
114       handler.add(data);
115     } catch (ExtsysException error) {
116       Assert.assertTrue(true);
117       return;
118     }
119     Assert.fail("not Exception");
120   }
121
122   @Ignore
123   @Test
124   public void testAddEmsInstance_validity_throw_ExtsysException() throws Exception {
125     EmsData data = new EmsData();
126     data.setName("ems2");
127     PowerMockito.doReturn(false).when(handler, "validity", data);
128     PowerMockito.doThrow(new ExtsysException()).when(handler, "validity", data);
129     try {
130       handler.add(data);
131     } catch (ExtsysException error) {
132       Assert.assertTrue(true);
133       return;
134     }
135     Assert.fail("not Exception");
136   }
137
138   @Ignore
139   @Test
140   public void testQueryEmsById_not_exist() {
141     List<EmsData> list = null;
142     try {
143       list = handler.getEmsById("123456");
144     } catch (ExtsysException error) {
145       Assert.fail("Exception" + error.getMessage());
146       return;
147     }
148     Assert.assertTrue(list.size() == 0);
149   }
150
151   @Ignore
152   @Test
153   public void testUpdateEms() {
154     EmsData data = new EmsData();
155     data.setName("ems_new");
156     try {
157       handler.update(data, id);
158     } catch (ExtsysException error1) {
159       Assert.fail("Exception" + error1.getMessage());
160       return;
161     }
162     List<EmsData> list = null;
163     try {
164       list = handler.getEmsById(id);
165     } catch (ExtsysException error) {
166       Assert.fail("Exception" + error.getMessage());
167       return;
168     }
169     assertTrue(list.size() > 0 && list.get(0).getName().equals("ems_new"));
170   }
171
172 }