feat:Add file transfer function
[usecase-ui/server.git] / server / src / test / java / org / onap / usecaseui / server / service / intent / impl / IntentServiceImplTest.java
1 /*
2  * Copyright (C) 2017 CTC, 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.intent.impl;
17
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;
23
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;
40
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;
46
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({ZipUtil.class})
50 class IntentServiceImplTest {
51     public IntentServiceImplTest(){}
52
53
54     @InjectMocks
55     private IntentServiceImpl intentService;
56
57     @Mock
58     private SessionFactory sessionFactory;
59
60     @Mock
61     private Session session;
62
63     @Before
64     public void before() throws Exception {
65         MemberModifier.field(IntentServiceImpl.class, "sessionFactory").set(intentService , sessionFactory);
66         doReturn(session).when(sessionFactory,"openSession");
67     }
68
69     @Test
70     public void addModelTest() throws Exception {
71         IntentModel model = new IntentModel();
72         model.setId(1);
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");
80
81     }
82
83     @Test
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());
90
91     }
92
93     @Test
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);
100
101     }
102
103     @Test
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");
110
111     }
112     @Test
113     public void activeModel() throws Exception {
114         Transaction tx = Mockito.mock(Transaction.class);
115         doReturn(tx).when(session,"beginTransaction");
116
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);
126
127         Mockito.doNothing().when(tx).commit();
128         assertEquals(intentService.activeModel("1"), null);
129
130     }
131     @Test
132     public void activeModelFileModelIsNullTest() {
133         assertEquals(intentService.activeModelFile(null), null);
134     }
135     @Test
136     public void activeModelFileFilePathIsNullTest() {
137         IntentModel model = new IntentModel();
138         assertEquals(intentService.activeModelFile(model), null);
139     }
140
141
142     @Test
143     public void calcFieldValueValueIsNullTest() {
144         assertEquals(intentService.calcFieldValue(null, null), "");
145     }
146     @Test
147     public void calcFieldValueKeyIsResourceSharingLevelTest() {
148         assertEquals(intentService.calcFieldValue("resourceSharingLevel", "shared"), "shared");
149     }
150     @Test
151     public void calcFieldValueKeyIsUEMobilityLevelNomadicTest() {
152         assertEquals(intentService.calcFieldValue("uEMobilityLevel", "Nomadic"), "nomadic");
153     }
154     @Test
155     public void calcFieldValueKeyIsUEMobilityLevelRestrictedTest() {
156         assertEquals(intentService.calcFieldValue("uEMobilityLevel", "restricted"), "Spatially Restricted Mobility");
157     }
158     @Test
159     public void calcFieldValueKeyIsUEMobilityLevelFullyTest() {
160         assertEquals(intentService.calcFieldValue("uEMobilityLevel", "fully"), "Fully Mobility");
161     }
162     @Test
163     public void calcFieldValueKeyIsCoverageAreaTest() {
164         assertEquals(intentService.calcFieldValue("coverageArea", "zhongguancun"), "Beijing Haidian District Zhongguancun");
165     }
166     @Test
167     public void calcFieldValueKeyIsMaxNumberofUEsTest() {
168         assertEquals(intentService.calcFieldValue("maxNumberofUEs", "5"), "5");
169     }
170     @Test
171     public void calcFieldValueKeyIsExpDataRateDLTest() {
172         assertEquals(intentService.calcFieldValue("expDataRateDL", "1gb"), "1000");
173     }
174     @Test
175     public void calcFieldValueKeyIsLatencyTest() {
176         assertEquals(intentService.calcFieldValue("latency", "1s"), "200");
177     }
178
179
180     @Test
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");
187     }
188 }