24e9e0d30556f0878223e03cbeaf2f1dbdd70ce3
[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.onap.usecaseui.server.bean.PerformanceInformation;
22 import org.onap.usecaseui.server.service.impl.PerformanceInformationServiceImpl;
23 import org.onap.usecaseui.server.util.DateUtils;
24
25 import java.util.*;
26 import java.io.*;
27 import org.hibernate.Query;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.hibernate.Transaction;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34
35 /** 
36 * PerformanceInformationServiceImpl Tester. 
37
38 * @author <Authors name> 
39 * @since <pre>8, 2018</pre>
40 * @version 1.0 
41 */
42 public class PerformanceInformationServiceImplTest {
43         PerformanceInformationServiceImpl performanceInformationServiceImpl = null;
44         private static final long serialVersionUID = 1L;
45
46         @Before
47         public void before() throws Exception {
48                 performanceInformationServiceImpl = new PerformanceInformationServiceImpl();
49
50                 MockUp<Transaction> mockUpTransaction = new MockUp<Transaction>() {
51                         @Mock
52                         public void commit() {
53                         }
54                 };
55                 MockUp<Query> mockUpQuery = new MockUp<Query>() {
56                 };
57                 new MockUp<Query>() {
58                         @Mock
59                         public Query setString(String name, String value) {
60                                 return mockUpQuery.getMockInstance();
61                         }
62                         @Mock
63                         public Query setDate(String name, Date value) {
64                                 return mockUpQuery.getMockInstance();
65                         }
66                         @Mock
67                         public Query setInteger(String name, int value) {
68                                 return mockUpQuery.getMockInstance();
69                         }
70                         @Mock
71                         public int executeUpdate() {
72                                 return 0;
73                         }
74                         @Mock
75                         public Query setMaxResults(int value) {
76                                 return mockUpQuery.getMockInstance();
77                         }
78                         @Mock
79                         public Query setFirstResult(int firstResult) {
80                                 return mockUpQuery.getMockInstance();
81                         }
82                         @Mock
83                         public Query setParameterList(String name, Object[] values) {
84                                 return mockUpQuery.getMockInstance();
85                         }
86                         @Mock
87                         public List<PerformanceInformation> list() {
88                                 PerformanceInformation pi = new PerformanceInformation();
89                                 return Arrays.asList(pi);
90                         }
91                         @Mock
92                         public Object uniqueResult() {
93                                 return "0";
94                         }
95                 };
96                 MockUp<Session> mockedSession = new MockUp<Session>() {
97                         @Mock
98                         public Query createQuery(String sql) {
99                                 return mockUpQuery.getMockInstance();
100                         }
101                         @Mock
102                         public Transaction beginTransaction() {
103                                 return mockUpTransaction.getMockInstance();
104                         }
105                         @Mock
106                         public Transaction getTransaction() {
107                                 return mockUpTransaction.getMockInstance();
108                         }
109                         @Mock
110                         public Serializable save(Object object) {
111                                 return (Serializable) serialVersionUID;
112                         }
113                         @Mock
114                         public void flush() {
115                         }
116                         @Mock
117                         public void update(Object object) {
118                         }
119                 };
120                 new MockUp<SessionFactory>() {
121                         @Mock
122                         public Session openSession() {
123                                 return mockedSession.getMockInstance();
124                         }
125                 };
126                 new MockUp<PerformanceInformationServiceImpl>() {
127                         @Mock
128                         private Session getSession() {
129                                 return mockedSession.getMockInstance();
130                         }
131                 };
132         }
133
134         @After
135         public void after() throws Exception {
136         }
137
138         @Test
139         public void testSavePerformanceInformation() throws Exception {
140                 try {
141                         PerformanceInformation pi = null;
142                         performanceInformationServiceImpl.savePerformanceInformation(pi);
143                 } catch (Exception e) {
144                         e.printStackTrace();
145                 }
146         }
147
148         @Test
149         public void testUpdatePerformanceInformation() throws Exception {
150                 try {
151                         PerformanceInformation pi = null;
152                         performanceInformationServiceImpl.updatePerformanceInformation(pi);
153                 } catch (Exception e) {
154                         e.printStackTrace();
155                 }
156         }
157
158         @Test
159         public void testGetAllCount() throws Exception {
160                 new MockUp<Query>() {
161                         @Mock
162                         public Object uniqueResult() {
163                                 return "1";
164                         }
165                 };
166                 try {
167                         PerformanceInformation pi = new PerformanceInformation();
168                         pi.setName("");
169                         pi.setValue("");
170                         pi.setSourceId("");
171                         pi.setStartEpochMicrosec("");;
172                         pi.setLastEpochMicroSec("");;
173                         performanceInformationServiceImpl.getAllCount(pi, 1, 1);
174                 } catch (Exception e) {
175                         e.printStackTrace();
176                 }
177         }
178
179         @Test
180         public void testQueryPerformanceInformation() throws Exception {
181                 try {
182                         PerformanceInformation pi = new PerformanceInformation();
183                         pi.setName("");
184                         pi.setValue("");
185                         pi.setSourceId("");
186                         pi.setStartEpochMicrosec("");;
187                         pi.setLastEpochMicroSec("");;
188                         performanceInformationServiceImpl.queryPerformanceInformation(pi, 1, 1);
189                 } catch (Exception e) {
190                         e.printStackTrace();
191                 }
192         }
193
194         @Test
195         public void testQueryId() throws Exception {
196                 try {
197                         String[] id = {"1", "2", "3"};
198                         performanceInformationServiceImpl.queryId(id);
199                 } catch (Exception e) {
200                         e.printStackTrace();
201                 }
202         }
203
204         @Test
205         public void testQueryDateBetween() throws Exception {
206                 try {
207                         performanceInformationServiceImpl.queryDateBetween("eventId", DateUtils.now(), DateUtils.now());
208                         performanceInformationServiceImpl.queryDateBetween("resourceId", "name", "startTime", "endTime");
209                 } catch (Exception e) {
210                         e.printStackTrace();
211                 }
212         }
213
214         @Test
215         public void testQueryDataBetweenSum() throws Exception {
216                 try {
217                         performanceInformationServiceImpl.queryDataBetweenSum("eventId", "name", DateUtils.now(), DateUtils.now());
218                 } catch (Exception e) {
219                         e.printStackTrace();
220                 }
221         }
222 }