1ebb94b77c3894434fa6b4f207c0a3a83e0cdfbc
[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 getValue from 'nfvo-utils/getValue.js';
19 import { actionTypes } from './SoftwareProductValidationConstants.js';
20 import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
21 import { enums, screenTypes } from 'sdc-app/onboarding/OnboardingConstants.js';
22 import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
23 import i18n from 'nfvo-utils/i18n/i18n.js';
24
25 function postVSPCertificationChecks(tests) {
26     const restPrefix = Configuration.get('restPrefix');
27     return RestAPIUtil.post(
28         `${restPrefix}/v1.0/externaltesting/executions`,
29         getValue(tests)
30     );
31 }
32
33 function fetchVspChecks() {
34     const restPrefix = Configuration.get('restPrefix');
35     return RestAPIUtil.fetch(`${restPrefix}/v1.0/externaltesting/testcasetree`);
36 }
37
38 const SoftwareProductValidationActionHelper = {
39     navigateToSoftwareProductValidationResults(
40         dispatch,
41         { softwareProductId, version, status, tests }
42     ) {
43         postVSPCertificationChecks(tests)
44             .then(response => {
45                 dispatch({
46                     type: actionTypes.POST_VSP_TESTS,
47                     vspTestResults: response
48                 });
49                 ScreensHelper.loadScreen(dispatch, {
50                     screen: enums.SCREEN.SOFTWARE_PRODUCT_VALIDATION_RESULTS,
51                     screenType: screenTypes.SOFTWARE_PRODUCT,
52                     props: {
53                         softwareProductId,
54                         version,
55                         status
56                     }
57                 });
58             })
59             .catch(error => {
60                 let errMessage = error.message || error.responseJSON.message;
61                 let title = error.responseJSON
62                     ? error.responseJSON.status
63                     : i18n('Error');
64                 dispatch({
65                     type: modalActionTypes.GLOBAL_MODAL_ERROR,
66                     data: {
67                         title: title,
68                         msg: errMessage,
69                         cancelButtonText: i18n('OK')
70                     }
71                 });
72             });
73     },
74
75     fetchVspChecks(dispatch) {
76         return new Promise((resolve, reject) => {
77             fetchVspChecks()
78                 .then(response => {
79                     dispatch({
80                         type: actionTypes.FETCH_VSP_CHECKS,
81                         vspChecks: response
82                     });
83                     resolve(response);
84                 })
85                 .catch(error => {
86                     reject(error);
87                 });
88         });
89     },
90
91     setActiveTab(dispatch, { activeTab }) {
92         dispatch({
93             type: actionTypes.SET_ACTIVE_TAB,
94             activeTab
95         });
96     },
97
98     onErrorThrown(dispatch, msg) {
99         dispatch({
100             type: modalActionTypes.GLOBAL_MODAL_ERROR,
101             data: {
102                 title: i18n('Error'),
103                 modalComponentName: i18n('Error'),
104                 modalComponentProps: {
105                     onClose: () =>
106                         dispatch({
107                             type: modalActionTypes.GLOBAL_MODAL_CLOSE
108                         })
109                 },
110                 msg: msg,
111                 cancelButtonText: i18n('OK')
112             }
113         });
114     },
115
116     setVspTestsMap(dispatch, map) {
117         dispatch({
118             type: actionTypes.SET_VSP_TESTS_MAP,
119             vspTestsMap: map
120         });
121     },
122
123     setComplianceChecked(dispatch, checked) {
124         dispatch({
125             type: actionTypes.SET_COMPLIANCE_CHECKED,
126             complianceChecked: checked
127         });
128     },
129
130     setCertificationChecked(dispatch, checked) {
131         dispatch({
132             type: actionTypes.SET_CERTIFICATION_CHECKED,
133             certificationChecked: checked
134         });
135     },
136
137     setTestsRequest(dispatch, request, info) {
138         dispatch({
139             type: actionTypes.SET_TESTS_REQUEST,
140             testsRequest: request,
141             generalInfo: info
142         });
143     },
144
145     setGeneralInfo(dispatch, info) {
146         dispatch({
147             type: actionTypes.SET_GENERAL_INFO,
148             generalInfo: info
149         });
150     },
151
152     setIsVspValidationDisabled(dispatch, { isValidationDisabled }) {
153         dispatch({
154             type: actionTypes.SET_VSP_VALIDATION_DISABLED,
155             isValidationDisabled: isValidationDisabled
156         });
157     }
158 };
159
160 export default SoftwareProductValidationActionHelper;