summaryrefslogtreecommitdiffstats
path: root/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/taskmgr/TaskManagerTest.java
blob: 921a8e084eba2e793613b504296c9a9be991ceb7 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (c) 2018 Intel Corporation Intellectual Property
 *
 * 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.vnfsdk.functest.taskmgr;

import io.dropwizard.testing.junit.DAOTestRule;
import mockit.Mock;
import mockit.MockUp;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.onap.vnfsdk.functest.db.TaskMgrCaseTblDAO;
import org.onap.vnfsdk.functest.db.TaskMgrTaskTblDAO;
import org.onap.vnfsdk.functest.externalservice.entity.OperationStatus;
import org.onap.vnfsdk.functest.externalservice.entity.OperationStatusHandler;
import org.onap.vnfsdk.functest.models.CaseRecord;
import org.onap.vnfsdk.functest.models.TaskRecord;
import org.onap.vnfsdk.functest.scriptmgr.ScriptManager;
import org.onap.vnfsdk.functest.util.RestResponseUtil;

import javax.ws.rs.core.Response;
import java.lang.reflect.Method;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class TaskManagerTest {

    @Rule
    public DAOTestRule daoTestRule = DAOTestRule.newBuilder()
            .addEntityClass(TaskRecord.class)
            .addEntityClass(CaseRecord.class)
            .build();
    private TaskManager taskManager;
    private ScriptManager scriptManager;
    private TaskMgrTaskTblDAO taskMgrTaskTblDAO;
    private TaskMgrCaseTblDAO taskMgrCaseTblDAO;
    private Response response = null;
    private TaskManager.RequestBody requestBody;
    private String packageID = "1234567890";
    private String taskID = "c0a1a373-8635-484d-bc6c-307a606cb8a1";
    private String envID = "0034b3f3-caea-4138-b186-e260a0cc509c";
    private String uploadID = "3dbf7978-0e5d-4fa8-ade7-bf6450b54a9b";
    private String operID = "3dbf7978-0e5d-4fa8-ade7-bf6450b54a9b";
    private String funcID = "";
    private String status = "CREATED";
    private String operFinished = "True";
    private String operResult = "SUCCESS";
    private String operResultMessage = "";
    private String testID = "NOT CREATED";
    private String testResult = "NULL";
    private String testDescription = "";

    @Before
    public void setUp() throws Exception {
        taskMgrTaskTblDAO = new TaskMgrTaskTblDAO(daoTestRule.getSessionFactory());
        taskMgrCaseTblDAO = new TaskMgrCaseTblDAO(daoTestRule.getSessionFactory());
        scriptManager = new ScriptManager(taskMgrTaskTblDAO, taskMgrCaseTblDAO);
        taskManager = new TaskManager(taskMgrTaskTblDAO, taskMgrCaseTblDAO, scriptManager);
        requestBody = new TaskManager.RequestBody();
        requestBody.setPackageID("1234567890");
    }

    @Test
    public void testStartOnboardTestingPackageIDAbsentInDB() {
        new MockUp<ScriptManager>() {
            @Mock
            public UUID uploadFuncTestPackage(UUID taskID, UUID envID, String url) {
                return UUID.randomUUID();
            }
        };

        try {
            response = taskManager.startOnboardTesting(requestBody);
            assertNotNull(response);
            assertEquals(201, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testStartOnboardTestingPackageIDAlreadyPresentInDB() {
        try {
            daoTestRule.inTransaction(() -> taskMgrTaskTblDAO.saveOrUpdate(new TaskRecord(packageID, taskID, envID, uploadID, operID, funcID, status, operFinished, operResult, operResultMessage)));
            response = taskManager.startOnboardTesting(requestBody);
            assertNotNull(response);
            assertEquals(500, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testQueryTestStatusPresentInDB() {
        try {
            daoTestRule.inTransaction(() -> taskMgrTaskTblDAO.saveOrUpdate(new TaskRecord(packageID, taskID, envID, uploadID, operID, funcID, status, operFinished, operResult, operResultMessage)));
            response = taskManager.queryTestStatus(taskID);
            assertNotNull(response);
            assertEquals(200, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testQueryTestStatusPresentInOperationStatus() {
        try {
            daoTestRule.inTransaction(() -> taskMgrTaskTblDAO.saveOrUpdate(new TaskRecord(packageID, taskID, envID, uploadID, operID, funcID, status, operFinished, operResult, operResultMessage)));
            UUID operID = UUID.fromString(taskMgrTaskTblDAO.findByTaskID(taskID).get(0).getOperID());
            OperationStatus operStatus = new OperationStatus();
            operStatus.setoResultCode(OperationStatus.operResultCode.SUCCESS);
            operStatus.setOperResultMessage("Execute function test finished");
            operStatus.setOperFinished(true);
            OperationStatusHandler.getInstance().setOperStatusMap(operID, operStatus);

            response = taskManager.queryTestStatus(taskID);
            assertNotNull(response);
            assertEquals(200, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testQueryTestStatusAbsent() {
        try {
            response = taskManager.queryTestStatus(taskID);
            assertNotNull(response);
            assertEquals(404, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCollectTaskResultCreated() {
        try {
            testID = "INTEL";
            daoTestRule.inTransaction(() -> taskMgrCaseTblDAO.saveOrUpdate(new CaseRecord(taskID, funcID, testID, testResult, testDescription)));
            response = taskManager.collectTaskResult(taskID);
            assertNotNull(response);
            assertEquals(200, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCollectTaskResultUncreated() {
        new MockUp<ScriptManager>() {
            @Mock
            public Response downloadResults(UUID taskID) {
                return RestResponseUtil.getSuccessResponse(null);
            }
        };
        try {
            daoTestRule.inTransaction(() -> taskMgrCaseTblDAO.saveOrUpdate(new CaseRecord(taskID, funcID, testID, testResult, testDescription)));
            response = taskManager.collectTaskResult(taskID);
            assertNotNull(response);
            assertEquals(200, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCollectTaskResultAbsent() {
        try {
            response = taskManager.collectTaskResult(taskID);
            assertNotNull(response);
            assertEquals(404, response.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testInitOnboardTesting() {
        TaskRecord taskRecord = new TaskRecord();
        CaseRecord caseRecord = new CaseRecord();

        try {
            TaskManager taskManagerObj = new TaskManager(taskMgrTaskTblDAO, taskMgrCaseTblDAO, scriptManager);
            Method m = taskManagerObj.getClass().getDeclaredMethod("initOnboardTesting", new Class[]{TaskRecord.class, CaseRecord.class, String.class});
            m.setAccessible(true);
            m.invoke(taskManagerObj, taskRecord, caseRecord, packageID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testLoadEnvConfigurations() {
        try {
            TaskManager loadEnvConfigurationsObj = new TaskManager(taskMgrTaskTblDAO, taskMgrCaseTblDAO, scriptManager);
            Method m = loadEnvConfigurationsObj.getClass().getDeclaredMethod("loadEnvConfigurations");
            m.setAccessible(true);
            m.invoke(loadEnvConfigurationsObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}