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  import static org.apache.commons.io.FilenameUtils.*;
21  import static org.xhtmlrenderer.simple.PDFRenderer.*;
22  
23  import org.testng.xml.*;
24  
25  import com.tngtech.jgiven.report.json.*;
26  import com.tngtech.jgiven.report.model.*;
27  
28  import lombok.*;
29  import lombok.experimental.*;
30  import lombok.extern.slf4j.*;
31  
32  /**
33   * Per test class reporter.
34   */
35  @SuperBuilder(toBuilder = true)
36  @NoArgsConstructor(force = true)
37  @Slf4j
38  @ToString(callSuper = true)
39  public class QaJGivenPerClassReporter
40      extends
41      AbstractQaJgivenReporter<ReportModel, QaJGivenPerClassReporter> {
42  
43      public static final String DEFAULT_TEMPLATE_RESOURCE =
44          "/qa-jgiven-perclass-reporter.html";
45  
46      /**
47       * Builds a new reporter configured with additional TestNG XML suite
48       * parameters. Currently, only {@code templateResource} is recognized.
49       *
50       * @param xmlSuite
51       *            TestNG XML suite
52       * @return reporter configured
53       */
54      @Override
55      protected QaJGivenPerClassReporter with(final XmlSuite xmlSuite) {
56          return ((QaJGivenPerClassReporter) super.with(xmlSuite))
57              .toBuilder()
58              .templateResource(templateResourceParamFrom(xmlSuite,
59                  DEFAULT_TEMPLATE_RESOURCE))
60              .build();
61      }
62  
63      /**
64       * Generates a report for each test class. It is assumed that JGiven
65       * generates a JSON file for each test class.
66       */
67      @Override
68      @SneakyThrows
69      public void generate() {
70          for (val reportModelFile : listJGivenReports()) {
71  
72              log.debug("reading " + reportModelFile);
73              // TODO make it read .mustache files and drop the extension here
74              val targetReportFile = reportFile(reportModelFile,
75                  EXTENSION_SEPARATOR_STR + getExtension(templateResource));
76              try (val reportWriter = fileWriter(targetReportFile)) {
77                  template()
78                      .execute(reportModel(targetReportFile)
79                          .toBuilder()
80                          .jgivenReport(new ScenarioJsonReader()
81                              .apply(reportModelFile))
82                          .screenshotScale(screenshotScale)
83                          .datePattern(datePattern)
84                          .build(),
85                          reportWriter);
86              }
87  
88              // FIXME should work only with html reports
89              if (pdf) {
90                  renderToPDF(
91                      reportFile(reportModelFile, ".html"),
92                      reportFile(reportModelFile, ".pdf")
93                          .getAbsolutePath());
94              }
95          }
96      }
97  }