VSP Compliance Check for Compute Flavor
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / validation / inputs / VspValidationInputsView.jsx
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 React, { Component } from 'react';
17 import PropTypes from 'prop-types';
18
19 import i18n from 'nfvo-utils/i18n/i18n.js';
20 import Button from 'sdc-ui/lib/react/Button.js';
21 import GridSection from 'nfvo-components/grid/GridSection.jsx';
22 import GridItem from 'nfvo-components/grid/GridItem.jsx';
23 import Input from 'nfvo-components/input/validation/Input.jsx';
24 import Form from 'nfvo-components/input/validation/Form.jsx';
25
26 class VspInputs extends React.Component {
27     constructor(props) {
28         super(props);
29         this.state = {};
30     }
31
32     shouldComponentUpdate() {
33         return true;
34     }
35
36     changeInputs(e, check, parameterName) {
37         let { testsRequest, generalInfo, setTestsRequest } = this.props;
38         testsRequest[check].parameters[parameterName] = e;
39         generalInfo[check][parameterName] = { isValid: true, errorText: '' };
40         setTestsRequest(testsRequest, generalInfo);
41     }
42
43     renderInputs(check) {
44         let { vspTestsMap, testsRequest, generalInfo } = this.props;
45         return (
46             <div className="div-clear-both">
47                 <GridSection
48                     title={i18n('{title} Inputs :', {
49                         title: vspTestsMap[check].title
50                     })}>
51                     {vspTestsMap[check].parameters.map((parameter, index) => {
52                         if (
53                             parameter.type === 'text' &&
54                             !parameter.metadata.hidden
55                         ) {
56                             return (
57                                 <GridItem key={index}>
58                                     <Input
59                                         data-test-id={
60                                             check +
61                                             '_' +
62                                             parameter.name +
63                                             '_input'
64                                         }
65                                         isRequired={!parameter.isOptional}
66                                         label={parameter.description}
67                                         isValid={
68                                             generalInfo[check][parameter.name]
69                                                 .isValid
70                                         }
71                                         errorText={
72                                             generalInfo[check][parameter.name]
73                                                 .errorText
74                                         }
75                                         type={
76                                             parameter.metadata.choices
77                                                 ? 'select'
78                                                 : 'text'
79                                         }
80                                         value={
81                                             testsRequest[check].parameters[
82                                                 parameter.name
83                                             ] || ''
84                                         }
85                                         onChange={e => {
86                                             this.changeInputs(
87                                                 e.target ? e.target.value : e,
88                                                 check,
89                                                 parameter.name
90                                             );
91                                         }}
92                                         disabled={
93                                             parameter.metadata.disabled || false
94                                         }>
95                                         {parameter.metadata.choices && (
96                                             <option key="placeholder" value="">
97                                                 {i18n('Select...')}
98                                             </option>
99                                         )}
100                                         {parameter.metadata.choices &&
101                                             parameter.metadata.choices.map(
102                                                 selectOption => (
103                                                     <option
104                                                         key={selectOption.key}
105                                                         value={
106                                                             selectOption.key
107                                                         }>
108                                                         {selectOption.label}
109                                                     </option>
110                                                 )
111                                             )}
112                                     </Input>
113                                 </GridItem>
114                             );
115                         }
116                     })}
117                 </GridSection>
118             </div>
119         );
120     }
121
122     render() {
123         let {
124             complianceChecked,
125             vspTestsMap,
126             certificationChecked
127         } = this.props;
128         return (
129             <div>
130                 {complianceChecked.map(complianceCheck => {
131                     if (vspTestsMap[complianceCheck].parameters.length === 0) {
132                         return <div />;
133                     } else {
134                         return this.renderInputs(complianceCheck);
135                     }
136                 })}
137                 {certificationChecked.map(certificateCheck => {
138                     if (vspTestsMap[certificateCheck].parameters.length === 0) {
139                         return <div />;
140                     } else {
141                         return this.renderInputs(certificateCheck);
142                     }
143                 })}
144             </div>
145         );
146     }
147 }
148
149 class VspValidationInputs extends Component {
150     static propTypes = {
151         softwareProductValidation: PropTypes.object
152     };
153
154     constructor(props) {
155         super(props);
156         this.state = {};
157     }
158
159     shouldComponentUpdate() {
160         return true;
161     }
162
163     validateInputs() {
164         let areInputsValid = true;
165         let { softwareProductValidation, setGeneralInfo } = this.props;
166         let generalInfo = softwareProductValidation.generalInfo;
167         Object.keys(softwareProductValidation.testsRequest).forEach(
168             testCaseName => {
169                 let requestParameters =
170                     softwareProductValidation.testsRequest[testCaseName]
171                         .parameters;
172                 let validationParameters =
173                     softwareProductValidation.vspTestsMap[testCaseName]
174                         .parameters;
175                 Object.keys(requestParameters).forEach(parameterName => {
176                     let parameter = validationParameters.find(
177                         o => o.name === parameterName
178                     );
179                     let isParameterValid = true;
180                     let errorText = '';
181                     if (
182                         parameter.type === 'text' &&
183                         parameter.metadata.choices
184                     ) {
185                         if (
186                             !parameter.isOptional &&
187                             !requestParameters[parameterName]
188                         ) {
189                             isParameterValid = false;
190                             errorText = i18n('Field is required');
191                         }
192                     } else if (parameter.type === 'text') {
193                         if (
194                             !parameter.isOptional &&
195                             !requestParameters[parameterName]
196                         ) {
197                             isParameterValid = false;
198                             errorText = i18n('Field is required');
199                         } else if (
200                             (!parameter.isOptional &&
201                                 !requestParameters[parameterName]) ||
202                             (parameter.metadata.maxLength &&
203                                 requestParameters[parameterName].length >
204                                     parseInt(parameter.metadata.maxLength)) ||
205                             (parameter.metadata.minLength &&
206                                 requestParameters[parameterName].length <
207                                     parseInt(parameter.metadata.minLength) &&
208                                 requestParameters[parameterName].length > 0)
209                         ) {
210                             isParameterValid = false;
211                             errorText = i18n(
212                                 'Value Should Be Minimum of {minLength} characters and a Maximum of {maxLength} characters',
213                                 {
214                                     minLength: parameter.metadata.minLength,
215                                     maxLength: parameter.metadata.maxLength
216                                 }
217                             );
218                         }
219                     }
220                     generalInfo[testCaseName][
221                         parameterName
222                     ].isValid = isParameterValid;
223                     generalInfo[testCaseName][
224                         parameterName
225                     ].errorText = errorText;
226                     areInputsValid = areInputsValid && isParameterValid;
227                 });
228             }
229         );
230         if (!areInputsValid) {
231             setGeneralInfo(generalInfo);
232         }
233         return areInputsValid;
234     }
235
236     performVSPTests() {
237         let tests = [];
238         let {
239             version,
240             onTestSubmit,
241             status,
242             softwareProductId,
243             softwareProductValidation
244         } = this.props;
245
246         Object.keys(softwareProductValidation.testsRequest).forEach(key => {
247             tests.push(softwareProductValidation.testsRequest[key]);
248         });
249         if (this.validateInputs()) {
250             onTestSubmit(softwareProductId, version, status, tests);
251         }
252     }
253
254     prepareDataForVspInputs() {
255         let { setTestsRequest } = this.props;
256         let {
257             complianceChecked,
258             certificationChecked,
259             vspTestsMap,
260             testsRequest,
261             generalInfo
262         } = this.props.softwareProductValidation;
263         return {
264             setTestsRequest,
265             complianceChecked,
266             certificationChecked,
267             vspTestsMap,
268             testsRequest,
269             generalInfo
270         };
271     }
272
273     render() {
274         return (
275             <div className="vsp-validation-view">
276                 <Form
277                     hasButtons={false}
278                     formReady={null}
279                     isValid={true}
280                     onSubmit={() => this.performVSPTests()}
281                     isReadOnlyMode={false}>
282                     <VspInputs {...this.prepareDataForVspInputs()} />
283                     <Button
284                         size="default"
285                         data-test-id="proceed-to-validation-results-btn"
286                         disabled={false}
287                         type="submit"
288                         className="proceed-to-validation-monitor-btn">
289                         {i18n('Submit')}
290                     </Button>
291                 </Form>
292             </div>
293         );
294     }
295 }
296
297 export default VspValidationInputs;