2 * Copyright (C) 2017 CTC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.service.intent.impl;
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.hibernate.query.Query;
23 import org.hibernate.Session;
24 import org.hibernate.SessionFactory;
25 import org.hibernate.Transaction;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.onap.usecaseui.server.bean.intent.IntentModel;
34 import org.onap.usecaseui.server.constant.IntentConstant;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.api.support.membermodification.MemberModifier;
38 import static org.junit.Assert.*;
39 import static org.mockito.ArgumentMatchers.*;
40 import static org.powermock.api.mockito.PowerMockito.*;
43 @RunWith(MockitoJUnitRunner.class)
44 public class IntentServiceImplTest {
45 public IntentServiceImplTest(){}
49 private IntentServiceImpl intentService;
52 private SessionFactory sessionFactory;
55 private Session session;
58 public void before() throws Exception {
59 MemberModifier.field(IntentServiceImpl.class, "sessionFactory").set(intentService , sessionFactory);
60 when(sessionFactory.openSession()).thenReturn(session);
64 public void addModelTest() throws Exception {
65 IntentModel model = new IntentModel();
67 Transaction tx = Mockito.mock(Transaction.class);
68 when(session.beginTransaction()).thenReturn(tx);
69 Serializable save = Mockito.mock(Serializable.class);
70 Mockito.when(session.save(model)).thenReturn(save);
71 Mockito.doNothing().when(tx).commit();
72 Mockito.doNothing().when(session).flush();
73 assertEquals(intentService.addModel(model), "1");
78 public void listModelsTest() {
79 Query query = Mockito.mock(Query.class);
80 when(session.createQuery(anyString())).thenReturn(query);
81 List<IntentModel> list = new ArrayList<>();
82 when(query.list()).thenReturn(list);
83 assertTrue(intentService.listModels().isEmpty());
88 public void getModel() {
89 Query query = Mockito.mock(Query.class);
90 when(session.createQuery(anyString())).thenReturn(query);
91 when(query.setParameter("modelId", 1)).thenReturn(query);
92 when(query.uniqueResult()).thenReturn(null);
93 assertEquals(intentService.getModel("1"), null);
98 public void deleteModel() throws Exception {
99 Transaction tx = Mockito.mock(Transaction.class);
100 when(session.beginTransaction()).thenReturn(tx);
101 Mockito.doNothing().when(session).delete(any());
102 Mockito.doNothing().when(tx).commit();
103 assertEquals(intentService.deleteModel("1"), "1");
107 public void activeModel() throws Exception {
108 Transaction tx = Mockito.mock(Transaction.class);
109 when(session.beginTransaction()).thenReturn(tx);
111 Query query = Mockito.mock(Query.class);
112 when(session.createQuery("from IntentModel where active=1")).thenReturn(query);
113 List<IntentModel> list = new ArrayList<>();
114 IntentModel intentModel = new IntentModel();
115 intentModel.setActive(1);
116 list.add(intentModel);
117 when(query.list()).thenReturn(list);
119 Query query2 = Mockito.mock(Query.class);
120 when(session.createQuery("from IntentModel where id = :modelId")).thenReturn(query2);
121 when(query2.setParameter("modelId",1)).thenReturn(query2);
122 IntentModel intentModel2 = new IntentModel();
123 intentModel2.setActive(0);
124 when(query2.uniqueResult()).thenReturn(intentModel2);
125 Serializable save = Mockito.mock(Serializable.class);
126 Mockito.when(session.save(any())).thenReturn(save);
128 Mockito.doNothing().when(tx).commit();
129 assertEquals(intentService.activeModel("1"), intentModel2);
133 public void activeModelFileModelIsNullTest() {
134 assertEquals(intentService.activeModelFile(null), null);
137 public void activeModelFileFilePathIsNullTest() {
138 IntentModel model = new IntentModel();
139 assertEquals(intentService.activeModelFile(model), null);
143 public void calcFieldValueValueIsNullTest() {
144 assertEquals(intentService.calcFieldValue(null, null), "");
147 public void calcFieldValueKeyIsResourceSharingLevelTest() {
148 assertEquals(intentService.calcFieldValue("resourceSharingLevel", "shared"), "shared");
151 public void calcFieldValueKeyIsUEMobilityLevelNomadicTest() {
152 assertEquals(intentService.calcFieldValue("uEMobilityLevel", "Nomadic"), "nomadic");
155 public void calcFieldValueKeyIsUEMobilityLevelRestrictedTest() {
156 assertEquals(intentService.calcFieldValue("uEMobilityLevel", "restricted"), "Spatially Restricted Mobility");
159 public void calcFieldValueKeyIsUEMobilityLevelFullyTest() {
160 assertEquals(intentService.calcFieldValue("uEMobilityLevel", "fully"), "Fully Mobility");
163 public void calcFieldValueKeyIsCoverageAreaTest() {
164 assertEquals(intentService.calcFieldValue("coverageArea", "zhongguancun"), "Beijing;Beijing;Haidian District;Zhongguancun Street");
167 public void calcFieldValueKeyIsMaxNumberofUEsTest() {
168 assertEquals(intentService.calcFieldValue("maxNumberofUEs", "5"), "5");
171 public void calcFieldValueKeyIsExpDataRateDLTest() {
172 assertEquals(intentService.calcFieldValue("expDataRateDL", "1gb"), "1000");
175 public void calcFieldValueKeyIsExpDataRateDLMBTest() {
176 assertEquals(intentService.calcFieldValue("expDataRateDL", "1mbpss"), "100");
179 public void calcFieldValueKeyIsExpDataRateULTest() {
180 assertEquals(intentService.calcFieldValue("expDataRateUL", "1gb"), "1000");
183 public void calcFieldValueKeyIsExpDataRateULMBTest() {
184 assertEquals(intentService.calcFieldValue("expDataRateUL", "1mbpss"), "100");
187 public void calcFieldValueKeyIsLatencyTest() {
188 assertEquals(intentService.calcFieldValue("latency", "1s"), "200");
191 public void calcFieldValueKeyIsLatencyDefaultTest() {
192 assertEquals(intentService.calcFieldValue("latency", "default"), "200");
195 public void calcFieldValueKeyIsLatencyLowTest() {
196 assertEquals(intentService.calcFieldValue("latency", "low"), "10");
199 public void calcFieldValueKeyIsLatencyOtherTest() {
200 assertEquals(intentService.calcFieldValue("latency", "1min"), "200");
204 public void getActiveModelTypeTest() {
205 IntentModel intentModel = new IntentModel();
206 intentModel.setModelType("ccvpn");
207 Query query = PowerMockito.mock(Query.class);
208 when(session.createQuery(anyString())).thenReturn(query);
209 when(query.uniqueResult()).thenReturn(intentModel);
210 assertEquals(intentService.getActiveModelType(), "ccvpn");
213 public void getActiveModelTypeThrowErrorTest() {
214 assertEquals(intentService.getActiveModelType(), null);
217 public void getModelTypeByIntentTextCCVPNTest() {
218 assertEquals(intentService.getModelTypeByIntentText("Cloud"), IntentConstant.MODEL_TYPE_CCVPN);
221 public void getModelTypeByIntentText5GSTest() {
222 assertEquals(intentService.getModelTypeByIntentText("5gs"), IntentConstant.MODEL_TYPE_5GS);
225 public void activeModelByTypeTest() {
226 Transaction tx = PowerMockito.mock(Transaction.class);
227 when(session.beginTransaction()).thenReturn(tx);
228 Query query = PowerMockito.mock(Query.class);
229 when(session.createQuery("from IntentModel where active=1")).thenReturn(query);
230 IntentModel intentModel = new IntentModel();
231 intentModel.setActive(1);
232 List<IntentModel> list = new ArrayList<>();
233 list.add(intentModel);
234 when(query.list()).thenReturn(list);
235 Serializable save = PowerMockito.mock(Serializable.class);
236 when(session.save(intentModel)).thenReturn(save);
238 Query query1 = PowerMockito.mock(Query.class);
239 when(session.createQuery("from IntentModel where modelType = :modelType order by createTime desc")).thenReturn(query1);
240 when(query1.setParameter("modelType", 1)).thenReturn(query1);
241 List<IntentModel> list1 = new ArrayList<>();
242 IntentModel intentModel1 = new IntentModel();
243 intentModel1.setActive(0);
244 list1.add(intentModel1);
245 when(query1.list()).thenReturn(list1);
246 when(session.save(intentModel1)).thenReturn(save);
247 doNothing().when(tx).commit();
249 IntentServiceImpl spy = spy(intentService);
250 doReturn("fileName").when(spy).activeModelFile(intentModel1);
251 doReturn("OK").when(spy).load(anyString());
254 assertEquals(spy.activeModelByType(IntentConstant.MODEL_TYPE_CCVPN), intentModel1);