Refresh option in validation result page
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / validationResults / SoftwareProductValidationResultsView.jsx
index 2c2cccd..c5da4f6 100644 (file)
@@ -20,64 +20,48 @@ import { SVGIcon } from 'onap-ui-react';
 import GridSection from 'nfvo-components/grid/GridSection.jsx';
 import GridItem from 'nfvo-components/grid/GridItem.jsx';
 import i18n from 'nfvo-utils/i18n/i18n.js';
-import unCamelCasedString from 'nfvo-utils/unCamelCaseString.js';
-
-const TestResultComponent = ({ tests }) => {
-    return (
-        <div>
-            {tests.map((test, index) => {
-                let name = 'errorCircle';
-                let color = 'warning';
-                if (
-                    test.testResult &&
-                    test.testResult.toLowerCase() === 'pass'
-                ) {
-                    color = 'positive';
-                    name = 'checkCircle';
-                } else if (
-                    test.testResult &&
-                    test.testResult.toLowerCase() === 'fail'
-                ) {
-                    name = 'exclamationTriangleFull';
-                }
-                return (
-                    <li type="none" key={index}>
-                        <SVGIcon
-                            color={color}
-                            name={name}
-                            labelPosition="right"
-                        />
-                        <span className="validation-results-test-result-label">
-                            {test.testName +
-                                ' | ' +
-                                test.testResult +
-                                ' | ' +
-                                test.notes}
-                        </span>
-                    </li>
-                );
-            })}
-        </div>
-    );
-};
 
 class SoftwareProductValidationResultsView extends React.Component {
     static propTypes = {
-        softwareProductValidation: PropTypes.object
+        softwareProductValidation: PropTypes.object,
+        refreshValidationResults: PropTypes.func
     };
 
     constructor(props) {
         super(props);
         this.state = {
             vspId: this.props.softwareProductId,
-            versionNumber: this.props.version.name
+            versionNumber: this.props.version.name,
+            refreshValidationResults: this.props.refreshValidationResults,
+            vspTestResults: this.props.vspTestResults,
+            flatTestsMap: {},
+            generalInfo: {}
         };
     }
+    componentDidMount() {
+        this.configBasicTestData();
+    }
+    componentDidUpdate() {
+        this.updateTestResultToDisplay();
+    }
+
+    prepareDataForCheckboxes(children, ftm) {
+        for (var val of children) {
+            if (val.children) {
+                this.prepareDataForCheckboxes(val.children, ftm);
+            } else if (val.tests) {
+                for (var test of val.tests) {
+                    ftm[test.testCaseName] = test.description;
+                }
+            }
+        }
+        return ftm;
+    }
 
     getTitle(result) {
-        let { vspTestsMap } = this.props.softwareProductValidation;
+        let { flatTestsMap: vspTestsMap } = this.state;
         let title = vspTestsMap[result.testCaseName]
-            ? vspTestsMap[result.testCaseName].title
+            ? vspTestsMap[result.testCaseName].split(/\r?\n/)[0]
             : i18n('Unknown');
         return i18n(
             'Scenario: {scenario} | Title: {title} | Test Case: {testCaseName} | Status: {status}',
@@ -90,19 +74,36 @@ class SoftwareProductValidationResultsView extends React.Component {
         );
     }
 
-    renderJSON(result) {
+    renderJSON(result, indexKey) {
+        if (result.status === 'in-progress') {
+            return this.renderInprogress(i18n('Test is In-progress'), indexKey);
+        } else {
+            return (
+                <li key={indexKey} type="none">
+                    <textarea
+                        disabled={true}
+                        className="validation-results-test-result-json"
+                        value={JSON.stringify(result, null, 2)}
+                    />
+                </li>
+            );
+        }
+    }
+    renderInprogress(result, indexKey) {
         return (
-            <li type="none">
-                <textarea
-                    disabled={true}
-                    className="validation-results-test-result-json"
-                    value={JSON.stringify(result, null, 2)}
+            <li key={indexKey} type="none">
+                <SVGIcon
+                    color="warning"
+                    name="exclamationTriangleLine"
+                    labelPosition="right"
                 />
+                <span className="validation-results-test-result-label">
+                    {result}
+                </span>
             </li>
         );
     }
-
-    renderError(result) {
+    renderError(result, indexKey) {
         if (Array.isArray(result)) {
             return result.map((parameter, index) => {
                 return (
@@ -120,95 +121,60 @@ class SoftwareProductValidationResultsView extends React.Component {
                     </li>
                 );
             });
-        } else {
+        } else if (
+            typeof result === 'string' ||
+            result.hasOwnProperty('code') ||
+            result.hasOwnProperty('advice') ||
+            result.hasOwnProperty('message') ||
+            result.hasOwnProperty('error')
+        ) {
+            result =
+                result instanceof Object && result.error instanceof Object
+                    ? result.error
+                    : result;
             return (
-                <li type="none">
+                <li key={indexKey} type="none">
                     <SVGIcon
                         color="negative"
                         name="errorCircle"
                         labelPosition="right"
                     />
                     <span className="validation-results-test-result-label">
-                        {(result.code || '') +
-                            ' | ' +
-                            (result.advice || result.message)}
+                        {typeof result === 'string'
+                            ? result
+                            : (result.code || '') +
+                              ' | ' +
+                              (result.advice || result.message || result.error)}
                     </span>
                 </li>
             );
-        }
-    }
-
-    renderResults(result) {
-        if (typeof result === 'string' || result instanceof String) {
+        } else {
             return (
-                <div>
-                    <SVGIcon
-                        color="warning"
-                        name="errorCircle"
-                        labelPosition="right"
-                    />
-                    <span className="validation-results-test-result-label">
-                        {result}
-                    </span>
-                </div>
+                <Accordion key={indexKey} defaultExpanded>
+                    {this.renderJSON(result)}
+                </Accordion>
             );
         }
-        return Object.keys(result).map((key, index) => {
-            let title = unCamelCasedString(key);
-            if (
-                typeof result[key] === 'string' ||
-                result[key] instanceof String
-            ) {
-                return (
-                    <Accordion
-                        defaultExpanded
-                        dataTestId={title}
-                        title={title}
-                        key={index}>
-                        {this.renderString(result[key])}
-                    </Accordion>
-                );
-            } else if (Array.isArray(result[key])) {
-                if (result[key].length > 0) {
-                    return (
-                        <Accordion
-                            defaultExpanded
-                            dataTestId={title}
-                            title={title}
-                            key={index}>
-                            <TestResultComponent tests={result[key]} />
-                        </Accordion>
-                    );
-                } else {
-                    return (
-                        <Accordion
-                            defaultExpanded
-                            dataTestId={title}
-                            title={title}
-                            key={index}>
-                            {i18n('{title} results are not available', {
-                                title: title
-                            })}
-                        </Accordion>
-                    );
-                }
-            } else {
-                return (
-                    <Accordion
-                        defaultExpanded
-                        dataTestId={title}
-                        title={title}
-                        key={index}>
-                        {this.renderJSON(result[key])}
-                    </Accordion>
-                );
-            }
-        });
     }
 
-    renderString(result) {
+    renderResults(result, indexKey) {
         return (
-            <li type="none">
+            <li key={indexKey} type="none">
+                <SVGIcon
+                    color="positive"
+                    name="checkCircle"
+                    labelPosition="right"
+                />
+                <span className="validation-results-test-result-label">
+                    {result}
+                </span>
+            </li>
+        );
+    }
+
+    renderString(result, indexKey) {
+        return (
+            <li key={indexKey} type="none">
                 <textarea
                     type="textarea"
                     disabled={true}
@@ -219,92 +185,191 @@ class SoftwareProductValidationResultsView extends React.Component {
         );
     }
 
-    buildSubAccordions(result) {
+    buildSubAccordions(result, indexKey) {
         let results = result.results;
 
         if (!results) {
             return (
                 <Accordion
+                    key={indexKey}
                     defaultExpanded
                     dataTestId="vsp-test-no-results"
                     title={this.getTitle(result)}>
-                    {this.renderJSON(result)}
+                    {this.renderJSON(result, indexKey)}
                 </Accordion>
             );
         } else if (typeof results === 'string' || results instanceof String) {
             return (
                 <Accordion
+                    key={indexKey}
                     defaultExpanded
                     dataTestId="vsp-test-string-results"
                     title={this.getTitle(result)}>
-                    {this.renderString(results)}
+                    {this.renderString(results, indexKey)}
                 </Accordion>
             );
         } else {
             return (
                 <Accordion
+                    key={indexKey}
                     defaultExpanded
                     dataTestId="vsp-test-object-results"
                     title={this.getTitle(result)}>
                     {Object.keys(results).length === 0
                         ? this.renderString(
-                              i18n('{title} results are not available', {
-                                  title: 'Test'
-                              })
+                              i18n(
+                                  '{title} results are not available',
+                                  {
+                                      title: 'Test'
+                                  },
+                                  indexKey
+                              )
                           )
-                        : Object.keys(results).map(key => {
-                              if (key === 'errors' || key === 'error') {
-                                  return this.renderError(results[key]);
-                              } else if (key === 'testResults') {
-                                  return this.renderResults(results[key]);
-                              } else {
-                                  let title = unCamelCasedString(key);
-                                  if (results[key] instanceof Object) {
-                                      return (
-                                          <Accordion
-                                              defaultExpanded
-                                              dataTestId={title}
-                                              title={title}>
-                                              {this.renderJSON(results[key])}
-                                          </Accordion>
-                                      );
-                                  } else {
-                                      return (
-                                          <Accordion
-                                              defaultExpanded
-                                              dataTestId={title}
-                                              title={title}>
-                                              {this.renderString(results[key])}
-                                          </Accordion>
-                                      );
-                                  }
-                              }
-                          })}
+                        : Array.isArray(results)
+                          ? Object.keys(results).map((key, indexKey) => {
+                                if (Object.keys(results[key]).length === 0) {
+                                    return this.renderResults(
+                                        result.testCaseName +
+                                            ' ' +
+                                            i18n('has passed all checks'),
+                                        indexKey
+                                    );
+                                } else {
+                                    return this.renderError(
+                                        results[key],
+                                        indexKey
+                                    );
+                                }
+                            })
+                          : this.renderError(results, indexKey)}
                 </Accordion>
             );
         }
     }
-
+    refreshValidationResult(thisObj) {
+        let { refreshValidationResults } = thisObj.props;
+        var testResultKey = this.props.softwareProductValidationResult
+            .testResultKeys[this.state.vspId + this.state.versionNumber];
+        refreshValidationResults(
+            testResultKey.requestId,
+            testResultKey.endPoints
+        );
+        delete this.props.softwareProductValidation.vspTestResults;
+    }
+    configBasicTestData() {
+        let {
+            softwareProductValidationResult,
+            softwareProductValidation
+        } = this.props;
+        if (
+            softwareProductValidationResult.vspChecks !== undefined &&
+            softwareProductValidationResult.vspChecks.children !== undefined
+        ) {
+            var ftm = this.prepareDataForCheckboxes(
+                this.props.softwareProductValidationResult.vspChecks.children,
+                {}
+            );
+            this.setState({
+                flatTestsMap: ftm
+            });
+        }
+        if (softwareProductValidation.testResultKeys) {
+            if (!this.props.softwareProductValidationResult.testResultKeys) {
+                this.props.softwareProductValidationResult.testResultKeys = {};
+            }
+            this.props.softwareProductValidationResult.testResultKeys[
+                this.state.vspId + this.state.versionNumber
+            ] =
+                softwareProductValidation.testResultKeys;
+            delete this.props.softwareProductValidation.testResultKeys;
+        }
+    }
+    updateTestResultToDisplay() {
+        if (this.props.softwareProductValidation.vspTestResults) {
+            let { updateDisplayTestResultData } = this.props;
+            var testResultToDisplay = this.props.softwareProductValidationResult
+                .testResultToDisplay;
+            testResultToDisplay = testResultToDisplay
+                ? testResultToDisplay
+                : {};
+            testResultToDisplay[
+                this.state.vspId + this.state.versionNumber
+            ] = this.props.softwareProductValidation.vspTestResults;
+            updateDisplayTestResultData(testResultToDisplay);
+            delete this.props.softwareProductValidation.vspTestResults;
+        } else if (this.props.softwareProductValidationResult.vspTestResults) {
+            let { updateDisplayTestResultData } = this.props;
+            var testResultToDisplay = this.props.softwareProductValidationResult
+                .testResultToDisplay
+                ? this.props.softwareProductValidationResult.testResultToDisplay
+                : {};
+            testResultToDisplay[
+                this.state.vspId + this.state.versionNumber
+            ] = this.props.softwareProductValidationResult.vspTestResults;
+            updateDisplayTestResultData(testResultToDisplay);
+            delete this.props.softwareProductValidationResult.vspTestResults;
+        }
+    }
     render() {
-        let results = this.props.softwareProductValidation.vspTestResults || [];
-        if (results.length > 0) {
+        let testResultToDisplay = this.props.softwareProductValidationResult
+            .testResultToDisplay;
+        let results = testResultToDisplay
+            ? testResultToDisplay[this.state.vspId + this.state.versionNumber]
+            : null;
+        if (!results) {
             return (
-                <GridSection title={i18n('Validation Results')}>
-                    <GridItem colSpan={10}>
-                        <Accordion
-                            defaultExpanded
-                            dataTestId="vsp-validation-test-result"
-                            title={i18n('Test Results')}>
-                            {results.map(row => this.buildSubAccordions(row))}
-                        </Accordion>
-                    </GridItem>
+                <GridSection title={i18n('Test Results')}>
+                    <h4>{i18n('No Test Performed')}</h4>
                 </GridSection>
             );
+        } else if (results.length > 0) {
+            return (
+                <div>
+                    <div
+                        onClick={() => this.refreshValidationResult(this)}
+                        data-test-id="vsp-validation-refresh-btn"
+                        className={'vcp-validation-refresh-btn'}>
+                        <SVGIcon
+                            label="Refresh"
+                            labelPosition="left"
+                            color=""
+                            iconClassName="vcp-validation-refresh-icon"
+                            name="versionControllerSync"
+                        />
+                    </div>
+                    <GridSection title={i18n('Test Results')}>
+                        <GridItem colSpan={10}>
+                            <Accordion
+                                defaultExpanded
+                                dataTestId="vsp-validation-test-result"
+                                title={i18n('Test Results')}>
+                                {results.map((row, index) =>
+                                    this.buildSubAccordions(row, index)
+                                )}
+                            </Accordion>
+                        </GridItem>
+                    </GridSection>
+                </div>
+            );
         } else {
             return (
-                <GridSection title={i18n('Validation Results')}>
-                    <h4>{i18n('No Validation Checks Performed')}</h4>
-                </GridSection>
+                <div>
+                    <div
+                        onClick={() => this.refreshValidationResult(this)}
+                        data-test-id="vsp-validation-refresh-btn"
+                        className={'vcp-validation-refresh-btn'}>
+                        <SVGIcon
+                            label="Refresh"
+                            labelPosition="left"
+                            color=""
+                            iconClassName="vcp-validation-refresh-icon"
+                            name="versionControllerSync"
+                        />
+                    </div>
+                    <GridSection title={i18n('Test Results')}>
+                        <h4>{i18n('No Test Result Available')}</h4>
+                    </GridSection>
+                </div>
             );
         }
     }