Define the VIM manage API.
[aai/esr-server.git] / esr-core / esr-mgr / src / test / java / org / onap / aai / esr / db / resource / SdncManagerTest.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.SdncData;
32 import org.onap.aai.esr.exception.ExtsysException;
33 import org.onap.aai.esr.handle.SdncHandler;
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({SdncHandler.class})
42 public class SdncManagerTest {
43   private SdncHandler handler;
44   private String id = "0000000000000000";
45
46   @BeforeClass
47   public static void setUpBeforeClass() throws Exception {
48     H2DbServer.startUp();
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     SdncData data = new SdncData();
71     data.setName("sdnc");
72     handler = PowerMockito.spy(new SdncHandler());
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   @Ignore
94   @Test
95   public void testAddSdncInstance_validity_false() throws Exception {
96     SdncData data = new SdncData();
97     data.setName("sdnc");
98     PowerMockito.doReturn(false).when(handler, "validity", data);
99     try {
100       handler.add(data);
101     } catch (ExtsysException error) {
102       Assert.assertTrue(true);
103       return;
104     }
105     Assert.fail("not Exception");
106   }
107
108   @Ignore
109   @Test
110   public void testAddSdncInstance_validity_throw_ExtsysException() throws Exception {
111     SdncData data = new SdncData();
112     data.setName("ems2");
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   @Ignore
125   @Test
126   public void testQuerySdncById_exist() {
127     List<SdncData> list = null;
128     try {
129       list = handler.getSdncById(id);
130     } catch (ExtsysException error) {
131       Assert.fail("Exception" + error.getMessage());
132       return;
133     }
134     Assert.assertTrue(list.size() > 0);
135   }
136
137   @Ignore
138   @Test
139   public void testQuerySdncById_not_exist() {
140     List<SdncData> list = null;
141     try {
142       list = handler.getSdncById("100001");
143     } catch (ExtsysException error) {
144       Assert.fail("Exception" + error.getMessage());
145       return;
146     }
147     Assert.assertTrue(list.size() == 0);
148   }
149
150   @Ignore
151   @Test
152   public void testUpdateSdnc() {
153     SdncData data = new SdncData();
154     data.setName("Sdnc_new");
155     try {
156       handler.update(data, id);
157     } catch (ExtsysException error1) {
158       Assert.fail("Exception" + error1.getMessage());
159       return;
160     }
161     List<SdncData> list = null;
162     try {
163       list = handler.getSdncById(id);
164     } catch (ExtsysException error) {
165       Assert.fail("Exception" + error.getMessage());
166       return;
167     }
168     assertTrue(list.size() > 0 && list.get(0).getName().equals("Sdnc_new"));
169   }
170
171 }