5690dfed38894b335991b70089e9b8cb4fbfaf10
[usecase-ui/server.git] /
1 /**
2  * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
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.usecaseui.server.service.impl; 
17
18 import org.junit.Test; 
19 import org.junit.Before; 
20 import org.junit.After;
21 import org.junit.runner.RunWith;
22 import org.onap.usecaseui.server.UsecaseuiServerApplication;
23 import org.onap.usecaseui.server.bean.PerformanceInformation;
24 import org.onap.usecaseui.server.service.impl.PerformanceInformationServiceImpl;
25 import org.onap.usecaseui.server.util.DateUtils;
26 import org.onap.usecaseui.server.util.Page;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.boot.test.context.SpringBootTest;
29 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30 import org.springframework.test.context.web.WebAppConfiguration;
31
32 import java.sql.Timestamp;
33 import java.text.SimpleDateFormat;
34 import java.util.*;
35 import java.io.*;
36 import org.hibernate.Query;
37 import org.hibernate.Session;
38 import org.hibernate.SessionFactory;
39 import org.hibernate.Transaction;
40
41 import mockit.Mock;
42 import mockit.MockUp;
43
44 /** 
45 * PerformanceInformationServiceImpl Tester. 
46
47 * @author <Authors name> 
48 * @since <pre>8, 2018</pre>
49 * @version 1.0 
50 */
51 public class PerformanceInformationServiceImplTest {
52         PerformanceInformationServiceImpl performanceInformationServiceImpl = null;
53         private static final long serialVersionUID = 1L;
54
55         @Before
56         public void before() throws Exception {
57                 performanceInformationServiceImpl = new PerformanceInformationServiceImpl();
58
59                 MockUp<Transaction> mockUpTransaction = new MockUp<Transaction>() {
60                         @Mock
61                         public void commit() {
62                         }
63                 };
64                 MockUp<Query> mockUpQuery = new MockUp<Query>() {
65                 };
66                 new MockUp<Query>() {
67                         @Mock
68                         public Query setString(String name, String value) {
69                                 return mockUpQuery.getMockInstance();
70                         }
71                         @Mock
72                         public Query setDate(String name, Date value) {
73                                 return mockUpQuery.getMockInstance();
74                         }
75                         @Mock
76                         public Query setInteger(String name, int value) {
77                                 return mockUpQuery.getMockInstance();
78                         }
79                         @Mock
80                         public int executeUpdate() {
81                                 return 0;
82                         }
83                         @Mock
84                         public Query setMaxResults(int value) {
85                                 return mockUpQuery.getMockInstance();
86                         }
87                         @Mock
88                         public Query setFirstResult(int firstResult) {
89                                 return mockUpQuery.getMockInstance();
90                         }
91                         @Mock
92                         public Query setParameterList(String name, Object[] values) {
93                                 return mockUpQuery.getMockInstance();
94                         }
95                         @Mock
96                         public List<PerformanceInformation> list() {
97                                 PerformanceInformation pi = new PerformanceInformation();
98                                 return Arrays.asList(pi);
99                         }
100                         @Mock
101                         public Object uniqueResult() {
102                                 return "0";
103                         }
104                 };
105                 MockUp<Session> mockedSession = new MockUp<Session>() {
106                         @Mock
107                         public Query createQuery(String sql) {
108                                 return mockUpQuery.getMockInstance();
109                         }
110                         @Mock
111                         public Transaction beginTransaction() {
112                                 return mockUpTransaction.getMockInstance();
113                         }
114                         @Mock
115                         public Transaction getTransaction() {
116                                 return mockUpTransaction.getMockInstance();
117                         }
118                         @Mock
119                         public Serializable save(Object object) {
120                                 return (Serializable) serialVersionUID;
121                         }
122                         @Mock
123                         public void flush() {
124                         }
125                         @Mock
126                         public void update(Object object) {
127                         }
128                 };
129                 new MockUp<SessionFactory>() {
130                         @Mock
131                         public Session openSession() {
132                                 return mockedSession.getMockInstance();
133                         }
134                 };
135                 new MockUp<PerformanceInformationServiceImpl>() {
136                         @Mock
137                         private Session getSession() {
138                                 return mockedSession.getMockInstance();
139                         }
140                 };
141         }
142
143         @After
144         public void after() throws Exception {
145         }
146
147         @Test
148         public void testSavePerformanceInformation() throws Exception {
149                 PerformanceInformation pi = null;
150                 performanceInformationServiceImpl.savePerformanceInformation(pi);
151         }
152
153         @Test
154         public void testUpdatePerformanceInformation() throws Exception {
155                 PerformanceInformation pi = null;
156                 performanceInformationServiceImpl.updatePerformanceInformation(pi);
157         }
158
159         @Test
160         public void testGetAllCount() throws Exception {
161                 new MockUp<Query>() {
162                         @Mock
163                         public Object uniqueResult() {
164                                 return "1";
165                         }
166                 };
167                 PerformanceInformation pi = new PerformanceInformation();
168                 pi.setName("");
169                 pi.setValue("");
170                 pi.setEventId("");
171                 pi.setCreateTime(DateUtils.now());
172                 pi.setUpdateTime(DateUtils.now());
173                 performanceInformationServiceImpl.getAllCount(pi, 1, 1);
174         }
175
176         @Test
177         public void testQueryPerformanceInformation() throws Exception {
178                 PerformanceInformation pi = new PerformanceInformation();
179                 pi.setName("");
180                 pi.setValue("");
181                 pi.setEventId("");
182                 pi.setCreateTime(DateUtils.now());
183                 pi.setUpdateTime(DateUtils.now());
184                 performanceInformationServiceImpl.queryPerformanceInformation(pi, 1, 1);
185         }
186
187         @Test
188         public void testQueryId() throws Exception {
189                 String[] id = {"1", "2", "3"};
190                 performanceInformationServiceImpl.queryId(id);
191         }
192
193         @Test
194         public void testQueryDateBetween() throws Exception {
195                 performanceInformationServiceImpl.queryDateBetween("eventId", DateUtils.now(), DateUtils.now());
196                 performanceInformationServiceImpl.queryDateBetween("resourceId", "name", "startTime", "endTime");
197         }
198
199         @Test
200         public void testQueryDataBetweenSum() throws Exception {
201                 performanceInformationServiceImpl.queryDataBetweenSum("eventId", "name", DateUtils.now(), DateUtils.now());
202         }
203
204         @Test(expected = Exception.class)
205         public void testSavePerformanceInformationException() throws Exception {
206                 new MockUp<PerformanceInformationServiceImpl>() {
207                         @Mock
208                         private Session getSession() throws Exception {
209                                 throw new Exception();
210                         }
211                 };
212                 PerformanceInformation pi = new PerformanceInformation();
213                 performanceInformationServiceImpl.savePerformanceInformation(pi);
214         }
215
216         @Test(expected = Exception.class)
217         public void testUpdatePerformanceInformationException() throws Exception {
218                 new MockUp<PerformanceInformationServiceImpl>() {
219                         @Mock
220                         private Session getSession() throws Exception {
221                                 throw new Exception();
222                         }
223                 };
224                 PerformanceInformation pi = new PerformanceInformation();
225                 performanceInformationServiceImpl.updatePerformanceInformation(pi);
226         }
227
228         @Test(expected = Exception.class)
229         public void testGetAllCountException() throws Exception {
230                 new MockUp<PerformanceInformationServiceImpl>() {
231                         @Mock
232                         private Session getSession() throws Exception {
233                                 throw new Exception();
234                         }
235                 };
236                 PerformanceInformation pi = new PerformanceInformation();
237                 performanceInformationServiceImpl.getAllCount(pi, 1, 1);
238         }
239
240         @Test(expected = Exception.class)
241         public void testQueryPerformanceInformationException() throws Exception {
242                 new MockUp<PerformanceInformationServiceImpl>() {
243                         @Mock
244                         private Session getSession() throws Exception {
245                                 throw new Exception();
246                         }
247                 };
248                 PerformanceInformation pi = new PerformanceInformation();
249                 performanceInformationServiceImpl.queryPerformanceInformation(pi, 1, 1);
250         }
251
252         @Test(expected = Exception.class)
253         public void testQueryIdException() throws Exception {
254                 new MockUp<PerformanceInformationServiceImpl>() {
255                         @Mock
256                         private Session getSession() throws Exception {
257                                 throw new Exception();
258                         }
259                 };
260                 String[] id = {"1", "2", "3"};
261                 performanceInformationServiceImpl.queryId(id);
262         }
263
264         @Test(expected = Exception.class)
265         public void testQueryDateBetweenException() throws Exception {
266                 new MockUp<PerformanceInformationServiceImpl>() {
267                         @Mock
268                         private Session getSession() throws Exception {
269                                 throw new Exception();
270                         }
271                 };
272                 performanceInformationServiceImpl.queryDateBetween("eventId", DateUtils.now(), DateUtils.now());
273                 performanceInformationServiceImpl.queryDateBetween("resourceId", "name", "startTime", "endTime");
274         }
275
276         @Test(expected = Exception.class)
277         public void testQueryDataBetweenSumException() throws Exception {
278                 new MockUp<PerformanceInformationServiceImpl>() {
279                         @Mock
280                         private Session getSession() throws Exception {
281                                 throw new Exception();
282                         }
283                 };
284                 performanceInformationServiceImpl.queryDataBetweenSum("eventId", "name", DateUtils.now(), DateUtils.now());
285         }
286 }