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.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.List;
24 import org.hibernate.query.Query;
25 import org.hibernate.Session;
26 import org.hibernate.SessionFactory;
27 import org.hibernate.Transaction;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.onap.usecaseui.server.bean.intent.IntentModel;
35 import org.onap.usecaseui.server.util.ZipUtil;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.api.support.membermodification.MemberModifier;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 import static org.junit.Assert.*;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.anyString;
44 import static org.powermock.api.mockito.PowerMockito.doReturn;
45 import static org.powermock.api.mockito.PowerMockito.when;
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({ZipUtil.class})
50 class IntentServiceImplTest {
51 public IntentServiceImplTest(){}
55 private IntentServiceImpl intentService;
58 private SessionFactory sessionFactory;
61 private Session session;
64 public void before() throws Exception {
65 MemberModifier.field(IntentServiceImpl.class, "sessionFactory").set(intentService , sessionFactory);
66 doReturn(session).when(sessionFactory,"openSession");
70 public void addModelTest() throws Exception {
71 IntentModel model = new IntentModel();
73 Transaction tx = Mockito.mock(Transaction.class);
74 doReturn(tx).when(session,"beginTransaction");
75 Serializable save = Mockito.mock(Serializable.class);
76 Mockito.when(session.save(model)).thenReturn(save);
77 Mockito.doNothing().when(tx).commit();
78 Mockito.doNothing().when(session).flush();
79 assertEquals(intentService.addModel(model), "1");
84 public void listModelsTest() {
85 Query query = Mockito.mock(Query.class);
86 when(session.createQuery(anyString())).thenReturn(query);
87 List<IntentModel> list = new ArrayList<>();
88 when(query.list()).thenReturn(list);
89 assertTrue(intentService.listModels().isEmpty());
94 public void getModel() {
95 Query query = Mockito.mock(Query.class);
96 when(session.createQuery(anyString())).thenReturn(query);
97 when(query.setParameter("modelId", "1")).thenReturn(query);
98 when(query.uniqueResult()).thenReturn(null);
99 assertEquals(intentService.getModel("1"), null);
104 public void deleteModel() throws Exception {
105 Transaction tx = Mockito.mock(Transaction.class);
106 doReturn(tx).when(session,"beginTransaction");
107 Mockito.doNothing().when(session).delete(any());
108 Mockito.doNothing().when(tx).commit();
109 assertEquals(intentService.deleteModel("1"), "1");
113 public void activeModel() throws Exception {
114 Transaction tx = Mockito.mock(Transaction.class);
115 doReturn(tx).when(session,"beginTransaction");
117 Query query = Mockito.mock(Query.class);
118 when(session.createQuery(anyString())).thenReturn(query);
119 List<IntentModel> list = new ArrayList<>();
120 IntentModel intentModel = new IntentModel();
121 intentModel.setActive(1);
122 list.add(intentModel);
123 when(query.list()).thenReturn(list);
124 Serializable save = Mockito.mock(Serializable.class);
125 Mockito.when(session.save(any())).thenReturn(save);
127 Mockito.doNothing().when(tx).commit();
128 assertEquals(intentService.activeModel("1"), null);
132 public void activeModelFileModelIsNullTest() {
133 assertEquals(intentService.activeModelFile(null), null);
136 public void activeModelFileFilePathIsNullTest() {
137 IntentModel model = new IntentModel();
138 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 Haidian District Zhongguancun");
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 calcFieldValueKeyIsLatencyTest() {
176 assertEquals(intentService.calcFieldValue("latency", "1s"), "200");
181 public void formatValueForResourcesSharingLevelTest() throws InvocationTargetException, IllegalAccessException {
182 String value = "shared";
183 IntentServiceImpl spy = PowerMockito.spy(intentService);
184 Method method = PowerMockito.method(IntentServiceImpl.class, "formatValueForResourcesSharingLevel", String.class);//如果多个参数,逗号分隔,然后写参数类型.class
185 Object result = method.invoke(spy, value);
186 assertEquals(result, "shared");