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