b6cc1d5bdc4c84a5f79529dc752412a61b66744c
[sdc.git] /
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     buildSubAccordions(result) {
78         if (result.status && result.status.toLowerCase() === 'completed') {
79             if (!result.results.testResults) {
80                 return (
81                     <div
82                         title={i18n('Scenario: {scenario} | Status: {status}', {
83                             scenario: result.scenario,
84                             status: result.status
85                         })}>
86                         <SVGIcon
87                             color="negative"
88                             name="errorCircle"
89                             labelPosition="right"
90                         />
91                         <span className="validation-results-test-result-label">
92                             {i18n('{title} results are not available', {
93                                 title: result.scenario
94                             })}
95                         </span>
96                     </div>
97                 );
98             }
99             return (
100                 <Accordion
101                     dataTestId="vsp-validation-test-result-success"
102                     title={i18n('Scenario: {scenario} | Status: {status}', {
103                         scenario: result.scenario,
104                         status: result.status
105                     })}>
106                     {Object.keys(result.results.testResults).map(
107                         (key, index) => {
108                             let title = unCamelCasedString(key);
109                             if (result.results.testResults[key].length > 0) {
110                                 return (
111                                     <Accordion
112                                         dataTestId={title}
113                                         title={title}
114                                         key={index}>
115                                         <TestResultComponent
116                                             tests={
117                                                 result.results.testResults[key]
118                                             }
119                                         />
120                                     </Accordion>
121                                 );
122                             } else {
123                                 return (
124                                     <div>
125                                         {i18n(
126                                             '{title} results are not available',
127                                             {
128                                                 title: title
129                                             }
130                                         )}
131                                     </div>
132                                 );
133                             }
134                         }
135                     )}
136                 </Accordion>
137             );
138         } else if (
139             result.status &&
140             result.status.toLowerCase() === 'failed' &&
141             result.results.errors
142         ) {
143             return (
144                 <Accordion
145                     dataTestId="vsp-validation-test-result-success"
146                     title={i18n('Scenario: {scenario} | Status: {status}', {
147                         scenario: result.scenario,
148                         status: result.status
149                     })}>
150                     {result.results.errors.map((element, index) => {
151                         return (
152                             <li type="none" key={index}>
153                                 <SVGIcon
154                                     color="negative"
155                                     name="errorCircle"
156                                     labelPosition="right"
157                                 />
158                                 <span className="validation-results-test-result-label">
159                                     {element.reason + ' | ' + element.advice}
160                                 </span>
161                             </li>
162                         );
163                     })}
164                 </Accordion>
165             );
166         } else if (result.message || result.httpStatus) {
167             return (
168                 <div>
169                     <SVGIcon
170                         color="negative"
171                         name="errorCircle"
172                         labelPosition="right"
173                     />
174                     <span className="validation-results-test-result-label">
175                         {result.message + ' | ' + result.httpStatus}
176                     </span>
177                 </div>
178             );
179         }
180     }
181
182     render() {
183         let results = this.props.softwareProductValidation.vspTestResults || [];
184         if (results.length > 0) {
185             return (
186                 <GridSection title={i18n('Validation Results')}>
187                     <GridItem colSpan={10}>
188                         <Accordion
189                             defaultExpanded
190                             dataTestId="vsp-validation-test-result"
191                             title={i18n('Test Results')}>
192                             {results.map(row => this.buildSubAccordions(row))}
193                         </Accordion>
194                     </GridItem>
195                 </GridSection>
196             );
197         } else {
198             return (
199                 <GridSection title={i18n('Validation Results')}>
200                     <h4>{i18n('No Validation Checks Performed')}</h4>
201                 </GridSection>
202             );
203         }
204     }
205 }
206
207 export default SoftwareProductValidationResultsView;