View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
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  
17  package dev.aherscu.qa.jgiven.reporter;
18  
19  import static dev.aherscu.qa.testing.utils.FileUtilsExtensions.*;
20  
21  import java.io.*;
22  
23  import org.apache.commons.io.*;
24  import org.testng.xml.*;
25  
26  import com.google.gson.*;
27  import com.tngtech.jgiven.report.json.*;
28  import com.tngtech.jgiven.report.model.*;
29  
30  import lombok.*;
31  import lombok.experimental.*;
32  
33  /**
34   * All in one report.
35   */
36  @SuperBuilder(toBuilder = true)
37  @NoArgsConstructor(force = true)
38  @ToString(callSuper = true)
39  public class QaJGivenReporter
40      extends AbstractQaJgivenReporter<CompleteReportModel, QaJGivenReporter> {
41      public static final String DEFAULT_TEMPLATE_RESOURCE =
42          "/qa-jgiven-reporter.html";
43  
44      public final String        productName;
45      public final String        productVersion;
46      public final String        testDocumentId;
47      public final String        testDocumentRev;
48      public final String        specDocumentId;
49      public final String        specDocumentRev;
50      public final String        planDocumentId;
51      public final String        planDocumentRev;
52      public final String        traceabilityDocumentId;
53      public final String        traceabilityDocumentRev;
54  
55      /**
56       * Builds a new reporter configured with additional TestNG XML suite
57       * parameters. Currently, only {@code templateResource} is recognized.
58       *
59       * @param xmlSuite
60       *            TestNG XML suite
61       * @return reporter configured
62       */
63      @Override
64      protected QaJGivenReporter with(final XmlSuite xmlSuite) {
65          return ((QaJGivenReporter) super.with(xmlSuite))
66              .toBuilder()
67              .templateResource(templateResourceParamFrom(xmlSuite,
68                  DEFAULT_TEMPLATE_RESOURCE))
69              .build();
70      }
71  
72      /**
73       * Generates a report including all test classes by aggregating all JGiven
74       * generated JSON files.
75       */
76      @Override
77      @SneakyThrows
78      public void generate() {
79          val targetReportFile = new File(outputDirectory,
80              // FIXME Warning:(80, 13) Argument
81              // 'FilenameUtils.getName(templateResource)' might be null
82              FilenameUtils.getName(templateResource));
83          // FIXME should supply a reportModelFile (a .json file)
84          val aggregatedReportModel = reportModel(targetReportFile)
85              .toBuilder()
86              .jgivenReport(
87                  new ReportModelReader(
88                      QaJGivenReportConfig.builder()
89                          .sourceDir(sourceDirectory)
90                          .targetDir(outputDirectory)
91                          .build())
92                      .readDirectory())
93              .screenshotScale(screenshotScale)
94              .datePattern(datePattern)
95              .testDocumentId(testDocumentId)
96              .testDocumentRev(testDocumentRev)
97              .specDocumentId(specDocumentId)
98              .specDocumentRev(specDocumentRev)
99              .planDocumentId(planDocumentId)
100             .planDocumentRev(planDocumentRev)
101             .traceabilityDocumentId(traceabilityDocumentId)
102             .traceabilityDocumentRev(traceabilityDocumentRev)
103             .productName(productName)
104             .productVersion(productVersion)
105             .build();
106 
107         if (debug) {
108             try (val debugReportWriter = fileWriter(
109                 new File(outputDirectory, "debug-report.json"))) {
110                 new GsonBuilder()
111                     .setPrettyPrinting()
112                     .create()
113                     .toJson(aggregatedReportModel, debugReportWriter);
114             }
115         }
116 
117         try (val reportWriter = fileWriter(targetReportFile)) {
118             template()
119                 .execute(aggregatedReportModel, reportWriter);
120         }
121 
122         // FIXME should work only with html reports
123         // if (pdf) {
124         // renderToPDF(
125         // reportFile(reportModelFile, ".html"),
126         // reportFile(reportModelFile, ".pdf")
127         // .getAbsolutePath());
128         // }
129     }
130 
131 }