VSP Compliance Check for Compute Flavor
[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 'sdc-ui/lib/react/Accordion.js';
19 import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';
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 import unCamelCasedString from 'nfvo-utils/unCamelCaseString.js';
24
25 const TestResultComponent = ({ tests }) => {
26     return (
27         <div>
28             {tests.map((test, index) => {
29                 let name = 'errorCircle';
30                 let color = 'warning';
31                 if (
32                     test.testResult &&
33                     test.testResult.toLowerCase() === 'pass'
34                 ) {
35                     color = 'positive';
36                     name = 'checkCircle';
37                 } else if (
38                     test.testResult &&
39                     test.testResult.toLowerCase() === 'fail'
40                 ) {
41                     name = 'exclamationTriangleFull';
42                 }
43                 return (
44                     <li type="none" key={index}>
45                         <SVGIcon
46                             color={color}
47                             name={name}
48                             labelPosition="right"
49                         />
50                         <span className="validation-results-test-result-label">
51                             {test.testName +
52                                 ' | ' +
53                                 test.testResult +
54                                 ' | ' +
55                                 test.notes}
56                         </span>
57                     </li>
58                 );
59             })}
60         </div>
61     );
62 };
63
64 class SoftwareProductValidationResultsView extends React.Component {
65     static propTypes = {
66         softwareProductValidation: PropTypes.object
67     };
68
69     constructor(props) {
70         super(props);
71         this.state = {
72             vspId: this.props.softwareProductId,
73             versionNumber: this.props.version.name
74         };
75     }
76
77     getTitle(result) {
78         let { vspTestsMap } = this.props.softwareProductValidation;
79         let title = vspTestsMap[result.testCaseName]
80             ? vspTestsMap[result.testCaseName].title
81             : i18n('Unknown');
82         return i18n(
83             'Scenario: {scenario} | Title: {title} | Test Case: {testCaseName} | Status: {status}',
84             {
85                 scenario: result.scenario || i18n('Unknown'),
86                 status: result.status || i18n('Unknown'),
87                 testCaseName: result.testCaseName || i18n('Unknown'),
88                 title: title
89             }
90         );
91     }
92
93     renderJSON(result) {
94         return (
95             <li type="none">
96                 <textarea
97                     disabled={true}
98                     className="validation-results-test-result-json"
99                     value={JSON.stringify(result, null, 2)}
100                 />
101             </li>
102         );
103     }
104
105     renderError(result) {
106         if (Array.isArray(result)) {
107             return result.map((parameter, index) => {
108                 return (
109                     <li type="none" key={index}>
110                         <SVGIcon
111                             color="negative"
112                             name="errorCircle"
113                             labelPosition="right"
114                         />
115                         <span className="validation-results-test-result-label">
116                             {(parameter.code || '') +
117                                 ' | ' +
118                                 (parameter.advice || parameter.message)}
119                         </span>
120                     </li>
121                 );
122             });
123         } else {
124             return (
125                 <li type="none">
126                     <SVGIcon
127                         color="negative"
128                         name="errorCircle"
129                         labelPosition="right"
130                     />
131                     <span className="validation-results-test-result-label">
132                         {(result.code || '') +
133                             ' | ' +
134                             (result.advice || result.message)}
135                     </span>
136                 </li>
137             );
138         }
139     }
140
141     renderResults(result) {
142         if (typeof result === 'string' || result instanceof String) {
143             return (
144                 <div>
145                     <SVGIcon
146                         color="warning"
147                         name="errorCircle"
148                         labelPosition="right"
149                     />
150                     <span className="validation-results-test-result-label">
151                         {result}
152                     </span>
153                 </div>
154             );
155         }
156         return Object.keys(result).map((key, index) => {
157             let title = unCamelCasedString(key);
158             if (
159                 typeof result[key] === 'string' ||
160                 result[key] instanceof String
161             ) {
162                 return (
163                     <Accordion
164                         defaultExpanded
165                         dataTestId={title}
166                         title={title}
167                         key={index}>
168                         {this.renderString(result[key])}
169                     </Accordion>
170                 );
171             } else if (Array.isArray(result[key])) {
172                 if (result[key].length > 0) {
173                     return (
174                         <Accordion
175                             defaultExpanded
176                             dataTestId={title}
177                             title={title}
178                             key={index}>
179                             <TestResultComponent tests={result[key]} />
180                         </Accordion>
181                     );
182                 } else {
183                     return (
184                         <Accordion
185                             defaultExpanded
186                             dataTestId={title}
187                             title={title}
188                             key={index}>
189                             {i18n('{title} results are not available', {
190                                 title: title
191                             })}
192                         </Accordion>
193                     );
194                 }
195             } else {
196                 return (
197                     <Accordion
198                         defaultExpanded
199                         dataTestId={title}
200                         title={title}
201                         key={index}>
202                         {this.renderJSON(result[key])}
203                     </Accordion>
204                 );
205             }
206         });
207     }
208
209     renderString(result) {
210         return (
211             <li type="none">
212                 <textarea
213                     type="textarea"
214                     disabled={true}
215                     className="validation-results-test-result-string"
216                     value={result}
217                 />
218             </li>
219         );
220     }
221
222     buildSubAccordions(result) {
223         let results = result.results;
224
225         if (!results) {
226             return (
227                 <Accordion
228                     defaultExpanded
229                     dataTestId="vsp-test-no-results"
230                     title={this.getTitle(result)}>
231                     {this.renderJSON(result)}
232                 </Accordion>
233             );
234         } else if (typeof results === 'string' || results instanceof String) {
235             return (
236                 <Accordion
237                     defaultExpanded
238                     dataTestId="vsp-test-string-results"
239                     title={this.getTitle(result)}>
240                     {this.renderString(results)}
241                 </Accordion>
242             );
243         } else {
244             return (
245                 <Accordion
246                     defaultExpanded
247                     dataTestId="vsp-test-object-results"
248                     title={this.getTitle(result)}>
249                     {Object.keys(results).length === 0
250                         ? this.renderString(
251                               i18n('{title} results are not available', {
252                                   title: 'Test'
253                               })
254                           )
255                         : Object.keys(results).map(key => {
256                               if (key === 'errors' || key === 'error') {
257                                   return this.renderError(results[key]);
258                               } else if (key === 'testResults') {
259                                   return this.renderResults(results[key]);
260                               } else {
261                                   let title = unCamelCasedString(key);
262                                   if (results[key] instanceof Object) {
263                                       return (
264                                           <Accordion
265                                               defaultExpanded
266                                               dataTestId={title}
267                                               title={title}>
268                                               {this.renderJSON(results[key])}
269                                           </Accordion>
270                                       );
271                                   } else {
272                                       return (
273                                           <Accordion
274                                               defaultExpanded
275                                               dataTestId={title}
276                                               title={title}>
277                                               {this.renderString(results[key])}
278                                           </Accordion>
279                                       );
280                                   }
281                               }
282                           })}
283                 </Accordion>
284             );
285         }
286     }
287
288     render() {
289         let results = this.props.softwareProductValidation.vspTestResults || [];
290         if (results.length > 0) {
291             return (
292                 <GridSection title={i18n('Validation Results')}>
293                     <GridItem colSpan={10}>
294                         <Accordion
295                             defaultExpanded
296                             dataTestId="vsp-validation-test-result"
297                             title={i18n('Test Results')}>
298                             {results.map(row => this.buildSubAccordions(row))}
299                         </Accordion>
300                     </GridItem>
301                 </GridSection>
302             );
303         } else {
304             return (
305                 <GridSection title={i18n('Validation Results')}>
306                     <h4>{i18n('No Validation Checks Performed')}</h4>
307                 </GridSection>
308             );
309         }
310     }
311 }
312
313 export default SoftwareProductValidationResultsView;