Refresh option in validation result page
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / validationResults / SoftwareProductValidationResultsView.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 from 'react';
17 import PropTypes from 'prop-types';
18 import { Accordion } from 'onap-ui-react';
19 import { SVGIcon } from 'onap-ui-react';
20 import GridSection from 'nfvo-components/grid/GridSection.jsx';
21 import GridItem from 'nfvo-components/grid/GridItem.jsx';
22 import i18n from 'nfvo-utils/i18n/i18n.js';
23
24 class SoftwareProductValidationResultsView extends React.Component {
25     static propTypes = {
26         softwareProductValidation: PropTypes.object,
27         refreshValidationResults: PropTypes.func
28     };
29
30     constructor(props) {
31         super(props);
32         this.state = {
33             vspId: this.props.softwareProductId,
34             versionNumber: this.props.version.name,
35             refreshValidationResults: this.props.refreshValidationResults,
36             vspTestResults: this.props.vspTestResults,
37             flatTestsMap: {},
38             generalInfo: {}
39         };
40     }
41     componentDidMount() {
42         this.configBasicTestData();
43     }
44     componentDidUpdate() {
45         this.updateTestResultToDisplay();
46     }
47
48     prepareDataForCheckboxes(children, ftm) {
49         for (var val of children) {
50             if (val.children) {
51                 this.prepareDataForCheckboxes(val.children, ftm);
52             } else if (val.tests) {
53                 for (var test of val.tests) {
54                     ftm[test.testCaseName] = test.description;
55                 }
56             }
57         }
58         return ftm;
59     }
60
61     getTitle(result) {
62         let { flatTestsMap: vspTestsMap } = this.state;
63         let title = vspTestsMap[result.testCaseName]
64             ? vspTestsMap[result.testCaseName].split(/\r?\n/)[0]
65             : i18n('Unknown');
66         return i18n(
67             'Scenario: {scenario} | Title: {title} | Test Case: {testCaseName} | Status: {status}',
68             {
69                 scenario: result.scenario || i18n('Unknown'),
70                 status: result.status || i18n('Unknown'),
71                 testCaseName: result.testCaseName || i18n('Unknown'),
72                 title: title
73             }
74         );
75     }
76
77     renderJSON(result, indexKey) {
78         if (result.status === 'in-progress') {
79             return this.renderInprogress(i18n('Test is In-progress'), indexKey);
80         } else {
81             return (
82                 <li key={indexKey} type="none">
83                     <textarea
84                         disabled={true}
85                         className="validation-results-test-result-json"
86                         value={JSON.stringify(result, null, 2)}
87                     />
88                 </li>
89             );
90         }
91     }
92     renderInprogress(result, indexKey) {
93         return (
94             <li key={indexKey} type="none">
95                 <SVGIcon
96                     color="warning"
97                     name="exclamationTriangleLine"
98                     labelPosition="right"
99                 />
100                 <span className="validation-results-test-result-label">
101                     {result}
102                 </span>
103             </li>
104         );
105     }
106     renderError(result, indexKey) {
107         if (Array.isArray(result)) {
108             return result.map((parameter, index) => {
109                 return (
110                     <li type="none" key={index}>
111                         <SVGIcon
112                             color="negative"
113                             name="errorCircle"
114                             labelPosition="right"
115                         />
116                         <span className="validation-results-test-result-label">
117                             {(parameter.code || '') +
118                                 ' | ' +
119                                 (parameter.advice || parameter.message)}
120                         </span>
121                     </li>
122                 );
123             });
124         } else if (
125             typeof result === 'string' ||
126             result.hasOwnProperty('code') ||
127             result.hasOwnProperty('advice') ||
128             result.hasOwnProperty('message') ||
129             result.hasOwnProperty('error')
130         ) {
131             result =
132                 result instanceof Object && result.error instanceof Object
133                     ? result.error
134                     : result;
135             return (
136                 <li key={indexKey} type="none">
137                     <SVGIcon
138                         color="negative"
139                         name="errorCircle"
140                         labelPosition="right"
141                     />
142                     <span className="validation-results-test-result-label">
143                         {typeof result === 'string'
144                             ? result
145                             : (result.code || '') +
146                               ' | ' +
147                               (result.advice || result.message || result.error)}
148                     </span>
149                 </li>
150             );
151         } else {
152             return (
153                 <Accordion key={indexKey} defaultExpanded>
154                     {this.renderJSON(result)}
155                 </Accordion>
156             );
157         }
158     }
159
160     renderResults(result, indexKey) {
161         return (
162             <li key={indexKey} type="none">
163                 <SVGIcon
164                     color="positive"
165                     name="checkCircle"
166                     labelPosition="right"
167                 />
168                 <span className="validation-results-test-result-label">
169                     {result}
170                 </span>
171             </li>
172         );
173     }
174
175     renderString(result, indexKey) {
176         return (
177             <li key={indexKey} type="none">
178                 <textarea
179                     type="textarea"
180                     disabled={true}
181                     className="validation-results-test-result-string"
182                     value={result}
183                 />
184             </li>
185         );
186     }
187
188     buildSubAccordions(result, indexKey) {
189         let results = result.results;
190
191         if (!results) {
192             return (
193                 <Accordion
194                     key={indexKey}
195                     defaultExpanded
196                     dataTestId="vsp-test-no-results"
197                     title={this.getTitle(result)}>
198                     {this.renderJSON(result, indexKey)}
199                 </Accordion>
200             );
201         } else if (typeof results === 'string' || results instanceof String) {
202             return (
203                 <Accordion
204                     key={indexKey}
205                     defaultExpanded
206                     dataTestId="vsp-test-string-results"
207                     title={this.getTitle(result)}>
208                     {this.renderString(results, indexKey)}
209                 </Accordion>
210             );
211         } else {
212             return (
213                 <Accordion
214                     key={indexKey}
215                     defaultExpanded
216                     dataTestId="vsp-test-object-results"
217                     title={this.getTitle(result)}>
218                     {Object.keys(results).length === 0
219                         ? this.renderString(
220                               i18n(
221                                   '{title} results are not available',
222                                   {
223                                       title: 'Test'
224                                   },
225                                   indexKey
226                               )
227                           )
228                         : Array.isArray(results)
229                           ? Object.keys(results).map((key, indexKey) => {
230                                 if (Object.keys(results[key]).length === 0) {
231                                     return this.renderResults(
232                                         result.testCaseName +
233                                             ' ' +
234                                             i18n('has passed all checks'),
235                                         indexKey
236                                     );
237                                 } else {
238                                     return this.renderError(
239                                         results[key],
240                                         indexKey
241                                     );
242                                 }
243                             })
244                           : this.renderError(results, indexKey)}
245                 </Accordion>
246             );
247         }
248     }
249     refreshValidationResult(thisObj) {
250         let { refreshValidationResults } = thisObj.props;
251         var testResultKey = this.props.softwareProductValidationResult
252             .testResultKeys[this.state.vspId + this.state.versionNumber];
253         refreshValidationResults(
254             testResultKey.requestId,
255             testResultKey.endPoints
256         );
257         delete this.props.softwareProductValidation.vspTestResults;
258     }
259     configBasicTestData() {
260         let {
261             softwareProductValidationResult,
262             softwareProductValidation
263         } = this.props;
264         if (
265             softwareProductValidationResult.vspChecks !== undefined &&
266             softwareProductValidationResult.vspChecks.children !== undefined
267         ) {
268             var ftm = this.prepareDataForCheckboxes(
269                 this.props.softwareProductValidationResult.vspChecks.children,
270                 {}
271             );
272             this.setState({
273                 flatTestsMap: ftm
274             });
275         }
276         if (softwareProductValidation.testResultKeys) {
277             if (!this.props.softwareProductValidationResult.testResultKeys) {
278                 this.props.softwareProductValidationResult.testResultKeys = {};
279             }
280             this.props.softwareProductValidationResult.testResultKeys[
281                 this.state.vspId + this.state.versionNumber
282             ] =
283                 softwareProductValidation.testResultKeys;
284             delete this.props.softwareProductValidation.testResultKeys;
285         }
286     }
287     updateTestResultToDisplay() {
288         if (this.props.softwareProductValidation.vspTestResults) {
289             let { updateDisplayTestResultData } = this.props;
290             var testResultToDisplay = this.props.softwareProductValidationResult
291                 .testResultToDisplay;
292             testResultToDisplay = testResultToDisplay
293                 ? testResultToDisplay
294                 : {};
295             testResultToDisplay[
296                 this.state.vspId + this.state.versionNumber
297             ] = this.props.softwareProductValidation.vspTestResults;
298             updateDisplayTestResultData(testResultToDisplay);
299             delete this.props.softwareProductValidation.vspTestResults;
300         } else if (this.props.softwareProductValidationResult.vspTestResults) {
301             let { updateDisplayTestResultData } = this.props;
302             var testResultToDisplay = this.props.softwareProductValidationResult
303                 .testResultToDisplay
304                 ? this.props.softwareProductValidationResult.testResultToDisplay
305                 : {};
306             testResultToDisplay[
307                 this.state.vspId + this.state.versionNumber
308             ] = this.props.softwareProductValidationResult.vspTestResults;
309             updateDisplayTestResultData(testResultToDisplay);
310             delete this.props.softwareProductValidationResult.vspTestResults;
311         }
312     }
313     render() {
314         let testResultToDisplay = this.props.softwareProductValidationResult
315             .testResultToDisplay;
316         let results = testResultToDisplay
317             ? testResultToDisplay[this.state.vspId + this.state.versionNumber]
318             : null;
319         if (!results) {
320             return (
321                 <GridSection title={i18n('Test Results')}>
322                     <h4>{i18n('No Test Performed')}</h4>
323                 </GridSection>
324             );
325         } else if (results.length > 0) {
326             return (
327                 <div>
328                     <div
329                         onClick={() => this.refreshValidationResult(this)}
330                         data-test-id="vsp-validation-refresh-btn"
331                         className={'vcp-validation-refresh-btn'}>
332                         <SVGIcon
333                             label="Refresh"
334                             labelPosition="left"
335                             color=""
336                             iconClassName="vcp-validation-refresh-icon"
337                             name="versionControllerSync"
338                         />
339                     </div>
340                     <GridSection title={i18n('Test Results')}>
341                         <GridItem colSpan={10}>
342                             <Accordion
343                                 defaultExpanded
344                                 dataTestId="vsp-validation-test-result"
345                                 title={i18n('Test Results')}>
346                                 {results.map((row, index) =>
347                                     this.buildSubAccordions(row, index)
348                                 )}
349                             </Accordion>
350                         </GridItem>
351                     </GridSection>
352                 </div>
353             );
354         } else {
355             return (
356                 <div>
357                     <div
358                         onClick={() => this.refreshValidationResult(this)}
359                         data-test-id="vsp-validation-refresh-btn"
360                         className={'vcp-validation-refresh-btn'}>
361                         <SVGIcon
362                             label="Refresh"
363                             labelPosition="left"
364                             color=""
365                             iconClassName="vcp-validation-refresh-icon"
366                             name="versionControllerSync"
367                         />
368                     </div>
369                     <GridSection title={i18n('Test Results')}>
370                         <h4>{i18n('No Test Result Available')}</h4>
371                     </GridSection>
372                 </div>
373             );
374         }
375     }
376 }
377
378 export default SoftwareProductValidationResultsView;