aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java
blob: a80574d54e1dffd18e27daf5e915422dbea32aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2017 CTC, Inc. and others. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.usecaseui.server.controller;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.onap.usecaseui.server.bean.HttpResponseResult;
import org.onap.usecaseui.server.bean.intent.IntentModel;
import org.onap.usecaseui.server.service.intent.IntentService;
import org.onap.usecaseui.server.util.HttpUtil;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({HttpUtil.class})
class IntentControllerTest {

    public IntentControllerTest(){}

    @InjectMocks
    private IntentController intentController;

    @Mock
    private IntentService intentService;

    @Test
    public void activeModelTest() {
        IntentModel model = new IntentModel();
        String path = "path";
        String modelId = "1";
        when(intentService.activeModel(anyString())).thenReturn(model);
        when(intentService.activeModelFile(model)).thenReturn(path);

        HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
        PowerMockito.mockStatic(HttpUtil.class);
        Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock);
        when(mock.getResultContent()).thenReturn("{'Status':'Success'}");

        assertEquals(intentController.activeModel(modelId), "1");
    }

    @Test
    public void deleteModelTest() throws Exception {
        String modelId = "1";
        IntentModel model = new IntentModel();
        model.setModelName("filename.zip");
        when(intentService.getModel(anyString())).thenReturn(model);
        when(intentService.deleteModel(anyString())).thenReturn("1");

        File file=PowerMockito.mock(File.class);
        PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(file);
        PowerMockito.when(file.exists()).thenReturn(true);
        PowerMockito.when(file.delete()).thenReturn(true);

        assertEquals(intentController.deleteModel(modelId), "1");

    }

    @Test
    public void predictTest() throws ParseException {
        Map<String,Object> body = new HashMap<>();
        body.put("text", "text");
        String respContent = "";
        HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
        PowerMockito.mockStatic(HttpUtil.class);
        Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock);
        when(mock.getResultContent()).thenReturn("{'Region':'chengnan'}");
        when(intentService.calcFieldValue(anyString(), anyString())).thenReturn("Beijing Changping District Chengnan Street");
        String predict = intentController.predict(body);
        JSONObject jsonObject = JSON.parseObject(predict);

        assertEquals(jsonObject.getString("coverageArea"), "Beijing Changping District Chengnan Street");
    }

    @Test
    public void tranlateFieldNameTest() throws InvocationTargetException, IllegalAccessException {
        String key = "Region";
        IntentController spy = PowerMockito.spy(intentController);
        Method method = PowerMockito.method(IntentController.class, "tranlateFieldName", String.class);//如果多个参数,逗号分隔,然后写参数类型.class
        Object result = method.invoke(spy, key);
        assertEquals(result, "coverageArea");
    }
}