Refresh option in validation result page
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / validation / SoftwareProductValidationActionHelper.js
1 /**
2  * Copyright (c) 2019 Vodafone Group
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 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes } from './SoftwareProductValidationConstants.js';
19 import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
20 import { enums, screenTypes } from 'sdc-app/onboarding/OnboardingConstants.js';
21 import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
22 import i18n from 'nfvo-utils/i18n/i18n.js';
23
24 function createCertificationFormData(tests) {
25     var formData = new FormData();
26     var testData = [];
27     for (var test of tests) {
28         if (test.files) {
29             for (var file of test.files) {
30                 formData.append('files', file.file, file.name);
31             }
32         }
33         delete test.files;
34         testData.push(test);
35     }
36     formData.append('testdata', JSON.stringify(testData));
37
38     return formData;
39 }
40 function postVSPCertificationChecks(
41     tests,
42     version,
43     softwareProductId,
44     requestId
45 ) {
46     const restPrefix = Configuration.get('restPrefix');
47     var id = version.id;
48     var formData = createCertificationFormData(tests);
49     return RestAPIUtil.post(
50         `${restPrefix}/v1.0/externaltesting/executions?vspId=${softwareProductId}&vspVersionId=${id}&requestId=${requestId}`,
51         formData
52     );
53 }
54
55 function fetchVspChecks() {
56     const restPrefix = Configuration.get('restPrefix');
57     return RestAPIUtil.fetch(`${restPrefix}/v1.0/externaltesting/testcasetree`);
58 }
59
60 function extractEndPoint(tests) {
61     return [...new Set(tests.map(test => test.endpoint))];
62 }
63 const SoftwareProductValidationActionHelper = {
64     navigateToSoftwareProductValidationResults(
65         dispatch,
66         { softwareProductId, version, status, tests, requestId }
67     ) {
68         return new Promise((resolve, reject) => {
69             postVSPCertificationChecks(
70                 tests,
71                 version,
72                 softwareProductId,
73                 requestId
74             )
75                 .then(response => {
76                     var testResultKeys = {};
77                     testResultKeys.endPoints = extractEndPoint(tests);
78                     testResultKeys.requestId = requestId;
79                     dispatch({
80                         type: actionTypes.POST_VSP_TESTS,
81                         vspTestResults: response,
82                         testResultKeys: testResultKeys
83                     });
84                     ScreensHelper.loadScreen(dispatch, {
85                         screen:
86                             enums.SCREEN.SOFTWARE_PRODUCT_VALIDATION_RESULTS,
87                         screenType: screenTypes.SOFTWARE_PRODUCT,
88                         props: {
89                             softwareProductId,
90                             version,
91                             status
92                         }
93                     });
94                     resolve(response);
95                 })
96                 .catch(error => {
97                     let errMessage =
98                         error.message || error.responseJSON.message;
99                     let title = error.responseJSON
100                         ? error.responseJSON.status
101                         : i18n('Error');
102                     dispatch({
103                         type: modalActionTypes.GLOBAL_MODAL_ERROR,
104                         data: {
105                             title: title,
106                             msg: errMessage,
107                             cancelButtonText: i18n('OK')
108                         }
109                     });
110                     reject(error);
111                 });
112         });
113     },
114
115     fetchVspChecks(dispatch) {
116         return new Promise((resolve, reject) => {
117             fetchVspChecks()
118                 .then(response => {
119                     dispatch({
120                         type: actionTypes.FETCH_VSP_CHECKS,
121                         vspChecks: response
122                     });
123                     resolve(response);
124                 })
125                 .catch(error => {
126                     reject(error);
127                 });
128         });
129     },
130
131     setActiveTab(dispatch, { activeTab }) {
132         dispatch({
133             type: actionTypes.SET_ACTIVE_TAB,
134             activeTab
135         });
136     },
137
138     onErrorThrown(dispatch, msg) {
139         dispatch({
140             type: modalActionTypes.GLOBAL_MODAL_ERROR,
141             data: {
142                 title: i18n('Error'),
143                 modalComponentName: i18n('Error'),
144                 modalComponentProps: {
145                     onClose: () =>
146                         dispatch({
147                             type: modalActionTypes.GLOBAL_MODAL_CLOSE
148                         })
149                 },
150                 msg: msg,
151                 cancelButtonText: i18n('OK')
152             }
153         });
154     },
155
156     setVspTestsMap(dispatch, map) {
157         dispatch({
158             type: actionTypes.SET_VSP_TESTS_MAP,
159             vspTestsMap: map
160         });
161     },
162
163     setComplianceChecked(dispatch, checked) {
164         dispatch({
165             type: actionTypes.SET_COMPLIANCE_CHECKED,
166             complianceChecked: checked
167         });
168     },
169
170     setCertificationChecked(dispatch, checked) {
171         dispatch({
172             type: actionTypes.SET_CERTIFICATION_CHECKED,
173             certificationChecked: checked
174         });
175     },
176
177     setTestsRequest(dispatch, request, info) {
178         dispatch({
179             type: actionTypes.SET_TESTS_REQUEST,
180             testsRequest: request,
181             generalInfo: info
182         });
183     },
184
185     setGeneralInfo(dispatch, info) {
186         dispatch({
187             type: actionTypes.SET_GENERAL_INFO,
188             generalInfo: info
189         });
190     },
191
192     setIsVspValidationDisabled(dispatch, { isValidationDisabled }) {
193         dispatch({
194             type: actionTypes.SET_VSP_VALIDATION_DISABLED,
195             isValidationDisabled: isValidationDisabled
196         });
197     }
198 };
199
200 export default SoftwareProductValidationActionHelper;