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