From 60f2ce85b2aac9d9c7f92330aeb6bf52ff241fb4 Mon Sep 17 00:00:00 2001 From: jasper linschoten Date: Wed, 3 Jan 2024 17:23:12 +0100 Subject: [PATCH 1/2] reduce code smells --- .../oss/boat/AbstractGenerateMojo.java | 31 ++--- .../java/com/backbase/oss/boat/DiffMojo.java | 5 +- .../oss/boat/diff/BatchOpenApiDiff.java | 30 +++-- .../backbase/oss/boat/radio/RadioMojo.java | 119 ++++++++---------- 4 files changed, 92 insertions(+), 93 deletions(-) diff --git a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/AbstractGenerateMojo.java b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/AbstractGenerateMojo.java index 92f9e5f1e..703529a0c 100644 --- a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/AbstractGenerateMojo.java +++ b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/AbstractGenerateMojo.java @@ -1,12 +1,13 @@ package com.backbase.oss.boat; +import lombok.extern.slf4j.Slf4j; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; + import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import lombok.extern.slf4j.Slf4j; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; @Slf4j public abstract class AbstractGenerateMojo extends GenerateMojo { @@ -16,25 +17,27 @@ public abstract class AbstractGenerateMojo extends GenerateMojo { "ServerVariable.java", "StringUtil.java", "Authentication.java", "HttpBasicAuth.java", "HttpBearerAuth.java", "ApiKeyAuth.java", "JavaTimeFormatter.java" ); + private static final String FALSE = "false"; + private static final String TRUE = "true"; public void execute(String generatorName, String library, boolean isEmbedded, boolean reactive, boolean generateSupportingFiles) throws MojoExecutionException, MojoFailureException { Map options = new HashMap<>(); options.put("library", library); - options.put("java8", "true"); + options.put("java8", TRUE); options.put("dateLibrary", "java8"); options.put("reactive", Boolean.toString(reactive)); - options.put("performBeanValidation", "true"); - options.put("skipDefaultInterface", "true"); - options.put("interfaceOnly", "true"); - options.put("useTags", "true"); - options.put("useBeanValidation", "true"); - options.put("useClassLevelBeanValidation", "false"); - options.put("useOptional", "false"); - options.put("useJakartaEe", "true"); - options.put("useSpringBoot3", "true"); - options.put("containerDefaultToNull", "false"); + options.put("performBeanValidation", TRUE); + options.put("skipDefaultInterface", TRUE); + options.put("interfaceOnly", TRUE); + options.put("useTags", TRUE); + options.put("useBeanValidation", TRUE); + options.put("useClassLevelBeanValidation", FALSE); + options.put("useOptional", FALSE); + options.put("useJakartaEe", TRUE); + options.put("useSpringBoot3", TRUE); + options.put("containerDefaultToNull", FALSE); this.generatorName = generatorName; this.generateSupportingFiles = generateSupportingFiles; diff --git a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/DiffMojo.java b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/DiffMojo.java index 5c2b476be..2595f3f0c 100644 --- a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/DiffMojo.java +++ b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/DiffMojo.java @@ -80,12 +80,13 @@ public void execute() throws MojoExecutionException { } - public static String renderChangedOpenApi(Render render, ChangedOpenApi changedOpenApi) { + public static String renderChangedOpenApi( + Render render, ChangedOpenApi changedOpenApi) throws MojoExecutionException { try (OutputStream outputStream = new ByteArrayOutputStream()) { render.render(changedOpenApi, new OutputStreamWriter(outputStream)); return outputStream.toString(); } catch (IOException e) { - throw new RuntimeException(e); + throw new MojoExecutionException(e); } } diff --git a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/diff/BatchOpenApiDiff.java b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/diff/BatchOpenApiDiff.java index 90e58bb5f..fac759c6b 100644 --- a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/diff/BatchOpenApiDiff.java +++ b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/diff/BatchOpenApiDiff.java @@ -9,6 +9,7 @@ import lombok.experimental.UtilityClass; import org.apache.commons.lang3.tuple.Pair; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.apache.maven.plugin.MojoExecutionException; import org.openapitools.openapidiff.core.compare.OpenApiDiff; import org.openapitools.openapidiff.core.compare.OpenApiDiffOptions; import org.openapitools.openapidiff.core.model.ChangedOpenApi; @@ -114,26 +115,31 @@ private static void writeChangelogInOpenAPI(Path openApiFilePath, String changel Files.write(openApiFilePath, SerializerUtils.toYamlString(diffedApi).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE); } - private static String renderChangeLog(List changeLog) { + private static String renderChangeLog(List changeLog) throws MojoExecutionException{ StringBuilder markDown = new StringBuilder(); markDown.append("# Changelog\n"); - changeLog.forEach(diff -> { - markDown.append("## ") + for (ChangedOpenApi changedOpenApi : changeLog) { + markDown.append(generateDiffReport(changedOpenApi)); + } + return markDown.toString(); + } + + private static String generateDiffReport(ChangedOpenApi diff) throws MojoExecutionException { + StringBuilder markDown = new StringBuilder(); + markDown.append("## ") .append(diff.getOldSpecOpenApi().getInfo().getVersion()) .append(" - ") .append(diff.getNewSpecOpenApi().getInfo().getVersion()) .append("\n"); - if (!diff.isDifferent()) { - markDown.append("No Changes\n"); - } else { - if (diff.isIncompatible()) { - markDown.append("**Note:** API has incompatible changes!!\n"); - } - String changes = DiffMojo.renderChangedOpenApi(markdownRender, diff); - markDown.append(changes); + if (!diff.isDifferent()) { + markDown.append("No Changes\n"); + } else { + if (diff.isIncompatible()) { + markDown.append("**Note:** API has incompatible changes!!\n"); } - }); + markDown.append(DiffMojo.renderChangedOpenApi(markdownRender, diff)); + } return markDown.toString(); } diff --git a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java index a2e082d50..7816a341d 100644 --- a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java +++ b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java @@ -9,12 +9,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; import feign.auth.BasicAuthRequestInterceptor; -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Stream; import lombok.Getter; import lombok.Setter; import lombok.SneakyThrows; @@ -28,6 +22,12 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.List; +import java.util.stream.Stream; + import static java.lang.String.format; /** @@ -140,77 +140,66 @@ public class RadioMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { - BasicAuthRequestInterceptor basicAuthRequestInterceptor = null; - - ObjectMapper objectMapper = JsonMapper.builder() - .findAndAddModules() - .build(); - - if (StringUtils.isNotEmpty(boatBayUsername) && StringUtils.isNotEmpty(boatBayPassword)) { - getLog().info("Basic Authentication set for username " + boatBayUsername); - basicAuthRequestInterceptor = new BasicAuthRequestInterceptor(boatBayUsername, boatBayPassword); - } else { - getLog().info("No Authentication set"); - } - - List allSpecs = new ArrayList<>(); - - for (SpecConfig spec : specs) { - allSpecs.add(mapToUploadSpec(spec)); - } - ApiClient apiClient = new ApiClient().setBasePath(boatBayUrl); - if (basicAuthRequestInterceptor != null) { - apiClient.addAuthorization("Basic Auth", basicAuthRequestInterceptor); - } - + applyAuth(apiClient); BoatMavenPluginApi api = apiClient.buildClient(BoatMavenPluginApi.class); UploadRequestBody uploadRequestBody = UploadRequestBody.builder() - .groupId(groupId).artifactId(artifactId).version(version).specs(allSpecs).build(); + .groupId(groupId).artifactId(artifactId).version(version).build(); + for (SpecConfig spec : specs) { + uploadRequestBody.getSpecs().add(mapToUploadSpec(spec)); + } - List reports =null; try { - reports = api.uploadSpec(portalKey, sourceKey, uploadRequestBody); - }catch (Exception e){ + List reports = api.uploadSpec(portalKey, sourceKey, uploadRequestBody); + writeReportFile(reports); + + if (failOnBreakingChange) { + boolean doesSpecsHaveBreakingChanges = reports.stream() + .anyMatch(report -> report.getSpec().getChanges().equals(Changes.BREAKING)); + if (doesSpecsHaveBreakingChanges) + throw new MojoFailureException("Specs have Breaking Changes. Check full report."); + } + + if (failOnLintViolation) { + boolean doesSpecsHaveMustViolations = reports.stream() + .anyMatch(report -> report.getViolations().stream() + .anyMatch(violation -> violation.getSeverity().equals(Severity.MUST))); + if (doesSpecsHaveMustViolations) + throw new MojoFailureException("Specs have Must Violations. Check full report."); + } + } catch (Exception e){ getLog().error("BoatBay error :: " + e.getMessage()); - if(failOnBoatBayErrorResponse) + if (failOnBoatBayErrorResponse) { throw new MojoFailureException("BoatBay error", e); + } } + } - // Process Result - if(reports!=null) { - try { - File outputFile = new File(getOutput(), "radioOutput.json"); - objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputFile, reports); - // Log summary of report - reports.forEach(report -> { - getLog().info(format("Spec %s summary :", report.getSpec().getKey())); - getLog().info(format("Changes are %s ", report.getSpec().getChanges())); - getLog().info("Number of Violations:" + report.getViolations().size()); - }); - // Log link to reports - getLog().info("UPLOAD TO BOAT-BAY SUCCESSFUL, check the full report: " + outputFile.getCanonicalPath()); - - if (failOnBreakingChange) { - boolean doesSpecsHaveBreakingChanges = reports.stream() - .anyMatch(report -> report.getSpec().getChanges().equals(Changes.BREAKING)); - if (doesSpecsHaveBreakingChanges) - throw new MojoFailureException("Specs have Breaking Changes. Check full report."); - } + private void writeReportFile(List reports) throws IOException { + ObjectMapper objectMapper = JsonMapper.builder() + .findAndAddModules() + .build(); + File outputFile = new File(getOutput(), "radioOutput.json"); + objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputFile, reports); + // Log summary of report + reports.forEach(report -> { + getLog().info(format("Spec %s summary :", report.getSpec().getKey())); + getLog().info(format("Changes are %s ", report.getSpec().getChanges())); + getLog().info("Number of Violations:" + report.getViolations().size()); + }); + // Log link to reports + getLog().info("UPLOAD TO BOAT-BAY SUCCESSFUL, check the full report: " + outputFile.getCanonicalPath()); + } - if (failOnLintViolation) { - boolean doesSpecsHaveMustViolations = reports.stream() - .anyMatch(report -> report.getViolations().stream() - .anyMatch(violation -> violation.getSeverity().equals(Severity.MUST))); - if (doesSpecsHaveMustViolations) - throw new MojoFailureException("Specs have Must Violations. Check full report."); - } - } catch (IOException e) { - throw new MojoFailureException("Failed to write output", e); - } + private void applyAuth(ApiClient apiClient) { + if (StringUtils.isNotEmpty(boatBayUsername) && StringUtils.isNotEmpty(boatBayPassword)) { + getLog().info("Basic Authentication set for username " + boatBayUsername); + apiClient.addAuthorization( + "Basic Auth", new BasicAuthRequestInterceptor(boatBayUsername, boatBayPassword)); + } else { + getLog().info("No Authentication set"); } - } private UploadSpec mapToUploadSpec(SpecConfig spec) throws MojoExecutionException { From a4085fa0eea402e51e2ca13aad474da884011fde Mon Sep 17 00:00:00 2001 From: jasper linschoten Date: Thu, 4 Jan 2024 14:27:29 +0100 Subject: [PATCH 2/2] fix tests A better fix --- .../backbase/oss/boat/radio/RadioMojo.java | 18 ++-- .../oss/boat/radio/RadioMojoTests.java | 82 +++++++++---------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java index 7816a341d..f23599ebb 100644 --- a/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java +++ b/boat-maven-plugin/src/main/java/com/backbase/oss/boat/radio/RadioMojo.java @@ -6,6 +6,7 @@ import com.backbase.oss.boat.bay.client.model.*; import com.backbase.oss.boat.loader.OpenAPILoader; import com.backbase.oss.boat.loader.OpenAPILoaderException; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; import feign.auth.BasicAuthRequestInterceptor; @@ -25,6 +26,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; @@ -145,7 +147,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { BoatMavenPluginApi api = apiClient.buildClient(BoatMavenPluginApi.class); UploadRequestBody uploadRequestBody = UploadRequestBody.builder() - .groupId(groupId).artifactId(artifactId).version(version).build(); + .groupId(groupId).artifactId(artifactId).version(version).specs(new ArrayList<>()).build(); for (SpecConfig spec : specs) { uploadRequestBody.getSpecs().add(mapToUploadSpec(spec)); } @@ -156,19 +158,23 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (failOnBreakingChange) { boolean doesSpecsHaveBreakingChanges = reports.stream() - .anyMatch(report -> report.getSpec().getChanges().equals(Changes.BREAKING)); - if (doesSpecsHaveBreakingChanges) + .anyMatch(report -> report.getSpec().getChanges() == Changes.BREAKING); + if (doesSpecsHaveBreakingChanges) { throw new MojoFailureException("Specs have Breaking Changes. Check full report."); + } } if (failOnLintViolation) { boolean doesSpecsHaveMustViolations = reports.stream() .anyMatch(report -> report.getViolations().stream() - .anyMatch(violation -> violation.getSeverity().equals(Severity.MUST))); - if (doesSpecsHaveMustViolations) + .anyMatch(violation -> violation.getSeverity() == Severity.MUST)); + if (doesSpecsHaveMustViolations) { throw new MojoFailureException("Specs have Must Violations. Check full report."); + } } - } catch (Exception e){ + } catch (IOException e) { + throw new MojoExecutionException(e); + } catch (RuntimeException e){ getLog().error("BoatBay error :: " + e.getMessage()); if (failOnBoatBayErrorResponse) { throw new MojoFailureException("BoatBay error", e); diff --git a/boat-maven-plugin/src/test/java/com/backbase/oss/boat/radio/RadioMojoTests.java b/boat-maven-plugin/src/test/java/com/backbase/oss/boat/radio/RadioMojoTests.java index 110aa35cb..2b84379b0 100644 --- a/boat-maven-plugin/src/test/java/com/backbase/oss/boat/radio/RadioMojoTests.java +++ b/boat-maven-plugin/src/test/java/com/backbase/oss/boat/radio/RadioMojoTests.java @@ -2,12 +2,6 @@ import com.backbase.oss.boat.bay.client.model.*; import com.fasterxml.jackson.databind.ObjectMapper; -import feign.auth.BasicAuthRequestInterceptor; -import java.io.File; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import okhttp3.mockwebserver.Dispatcher; @@ -20,8 +14,16 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.io.File; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; + +import static com.backbase.oss.boat.bay.client.model.Changes.BREAKING; +import static com.backbase.oss.boat.bay.client.model.Changes.COMPATIBLE; +import static com.backbase.oss.boat.bay.client.model.Severity.HINT; +import static com.backbase.oss.boat.bay.client.model.Severity.MUST; import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.assertEquals; @Slf4j class RadioMojoTests { @@ -51,7 +53,6 @@ void test_all_valid_inputs() { final String fileNameRequest = "one-client-*-v1.yaml"; final String fileNameResolved = "one-client-api-v1.yaml"; - final BigDecimal reportId = BigDecimal.valueOf(10); final String reportGrade = "A"; SpecConfig specConfig = new SpecConfig(); @@ -87,7 +88,8 @@ public MockResponse dispatch(RecordedRequest request) { uploadSpec.getOpenApi().length() > 0 ) { log.info(uploadSpec.getOpenApi()); - List result = getSampleBoatLintReports(specKey, reportId, reportGrade,Changes.COMPATIBLE,Severity.HINT); + List result = buildSampleBoatLintReports( + specKey, reportGrade, COMPATIBLE, HINT,"test_all_valid_inputs"); return new MockResponse().setResponseCode(200).setBody(objectMapper.writeValueAsString(result)); } else { return new MockResponse().setResponseCode(400); @@ -108,7 +110,7 @@ public MockResponse dispatch(RecordedRequest request) { List result = Arrays.asList(objectMapper.readValue(output, BoatLintReport[].class)); assertEquals(1, result.size()); - assertEquals(reportId, result.get(0).getId()); + assertEquals(BigDecimal.TEN, result.get(0).getId()); assertEquals(reportGrade, result.get(0).getGrade()); } @@ -126,7 +128,6 @@ void test_empty_specKeyAndName() { final String fileNameRequest = "one-client-*-v2.yaml"; final String fileNameResolved = "one-client-api-v2.yaml"; - final BigDecimal reportId = BigDecimal.valueOf(20); final String reportGrade = "B"; SpecConfig specConfig = new SpecConfig(); @@ -162,7 +163,8 @@ public MockResponse dispatch(RecordedRequest request) { uploadSpec.getOpenApi().length() > 0 ) { log.info(uploadSpec.getOpenApi()); - List result = getSampleBoatLintReports(expectedDefaultKey, reportId, reportGrade,Changes.COMPATIBLE,Severity.HINT); + List result = buildSampleBoatLintReports( + expectedDefaultKey, reportGrade, COMPATIBLE, HINT, "test_empty_specKeyAndName"); return new MockResponse().setResponseCode(200).setBody(objectMapper.writeValueAsString(result)); } else { return new MockResponse().setResponseCode(400); @@ -183,7 +185,7 @@ public MockResponse dispatch(RecordedRequest request) { List result = Arrays.asList(objectMapper.readValue(output, BoatLintReport[].class)); assertEquals(1, result.size()); - assertEquals(reportId, result.get(0).getId()); + assertEquals(BigDecimal.TEN, result.get(0).getId()); assertEquals(reportGrade, result.get(0).getGrade()); } @@ -225,7 +227,7 @@ public MockResponse dispatch(RecordedRequest request) { mockBackEnd.setDispatcher(dispatcher); - assertThrows(MojoExecutionException.class, () -> mojo.execute()); + assertThrows(MojoExecutionException.class, mojo::execute); } @@ -266,7 +268,7 @@ public MockResponse dispatch(RecordedRequest request) { mockBackEnd.setDispatcher(dispatcher); - assertThrows(MojoExecutionException.class, () -> mojo.execute()); + assertThrows(MojoExecutionException.class, mojo::execute); } @@ -307,7 +309,7 @@ public MockResponse dispatch(RecordedRequest request) { mockBackEnd.setDispatcher(dispatcher); - assertThrows(MojoExecutionException.class, () -> mojo.execute()); + assertThrows(MojoExecutionException.class, mojo::execute); } @@ -339,7 +341,6 @@ void test_invalid_parent_folder() { public MockResponse dispatch(RecordedRequest request) { switch (request.getPath()) { case "/api/boat/portals/" + portalKey + "/boat-maven-plugin/" + sourceKey + "/upload": - return new MockResponse().setResponseCode(200); } return new MockResponse().setResponseCode(404); @@ -348,7 +349,7 @@ public MockResponse dispatch(RecordedRequest request) { mockBackEnd.setDispatcher(dispatcher); - Exception exception = assertThrows(MojoExecutionException.class, () -> mojo.execute()); + Exception exception = assertThrows(MojoExecutionException.class, mojo::execute); assertTrue(exception.getMessage().startsWith("Invalid parent spec folder")); @@ -367,7 +368,6 @@ void test_auth() { final String validName = "one-client-api-v1.yaml"; final String username = "admin"; final String password = "admin"; - final BigDecimal reportId = BigDecimal.valueOf(10); final String reportGrade = "A"; SpecConfig specConfig = new SpecConfig(); @@ -392,7 +392,8 @@ public MockResponse dispatch(RecordedRequest request) { case "/api/boat/portals/" + portalKey + "/boat-maven-plugin/" + sourceKey + "/upload": if(request.getHeader("Authorization")!=null && request.getHeader("Authorization").length()>0){ - List result = getSampleBoatLintReports(specKey, reportId, reportGrade,Changes.COMPATIBLE,Severity.HINT); + List result = buildSampleBoatLintReports( + specKey, reportGrade, COMPATIBLE, HINT, "test_auth"); return new MockResponse().setResponseCode(200).setBody(objectMapper.writeValueAsString(result)); } return new MockResponse().setResponseCode(401); @@ -421,7 +422,6 @@ void test_build_fail_on_breaking_changes() { final String artifactId = "pet-store-bom"; final String specKey = "spec-key"; final String version = "2021.09"; - final BigDecimal reportId = BigDecimal.valueOf(10); final String reportGrade = "A"; final String validName = "one-client-api-v1.yaml"; @@ -443,7 +443,8 @@ void test_build_fail_on_breaking_changes() { public MockResponse dispatch(RecordedRequest request) { switch (request.getPath()) { case "/api/boat/portals/" + portalKey + "/boat-maven-plugin/" + sourceKey + "/upload": - List result = getSampleBoatLintReports(specKey, reportId, reportGrade,Changes.BREAKING,Severity.HINT); + List result = buildSampleBoatLintReports( + specKey, reportGrade, BREAKING, HINT, "test_build_fail_on_breaking_changes"); return new MockResponse().setResponseCode(200).setBody(objectMapper.writeValueAsString(result)); } return new MockResponse().setResponseCode(404); @@ -460,7 +461,7 @@ public MockResponse dispatch(RecordedRequest request) { // Build will fail if failOnBreakingChange is true mojo.setFailOnBreakingChange(true); - Exception exception = assertThrows(MojoFailureException.class, () -> mojo.execute()); + Exception exception = assertThrows(MojoFailureException.class, mojo::execute); assertTrue(exception.getMessage().startsWith("Specs have Breaking Changes")); } @@ -475,7 +476,6 @@ void test_build_fail_on_must_violation() { final String artifactId = "pet-store-bom"; final String specKey = "spec-key"; final String version = "2021.09"; - final BigDecimal reportId = BigDecimal.valueOf(10); final String reportGrade = "A"; final String validName = "one-client-api-v1.yaml"; @@ -497,7 +497,8 @@ void test_build_fail_on_must_violation() { public MockResponse dispatch(RecordedRequest request) { switch (request.getPath()) { case "/api/boat/portals/" + portalKey + "/boat-maven-plugin/" + sourceKey + "/upload": - List result = getSampleBoatLintReports(specKey, reportId, reportGrade,Changes.COMPATIBLE,Severity.MUST); + List result = buildSampleBoatLintReports( + specKey, reportGrade, COMPATIBLE, MUST, "test_build_fail_on_must_violation"); return new MockResponse().setResponseCode(200).setBody(objectMapper.writeValueAsString(result)); } return new MockResponse().setResponseCode(404); @@ -514,7 +515,7 @@ public MockResponse dispatch(RecordedRequest request) { // Build will fail if failOnLintViolation is true mojo.setFailOnLintViolation(true); - Exception exception = assertThrows(MojoFailureException.class, () -> mojo.execute()); + Exception exception = assertThrows(MojoFailureException.class, mojo::execute); assertTrue(exception.getMessage().startsWith("Specs have Must Violations")); } @@ -527,10 +528,7 @@ void test_when_boat_bay_is_unavailable_and_failOnBoatBayError_is_true() { final String sourceKey = "pet-store-bom"; final String groupId = "com.backbase.boat.samples"; final String artifactId = "pet-store-bom"; - final String specKey = "spec-key"; final String version = "2021.09"; - final BigDecimal reportId = BigDecimal.valueOf(10); - final String reportGrade = "A"; final String validName = "one-client-api-v1.yaml"; SpecConfig specConfig = new SpecConfig(); @@ -562,17 +560,17 @@ public MockResponse dispatch(RecordedRequest request) { mockBackEnd.setDispatcher(dispatcher); - // Build will fail, when failOnBoatBayErrorResponse is true (Defualt) - Exception exception = assertThrows(MojoFailureException.class, () -> mojo.execute()); + // Build will fail, when failOnBoatBayErrorResponse is true (Default) + Exception exception = assertThrows(MojoFailureException.class, mojo::execute); assertTrue(exception.getMessage().startsWith("BoatBay error")); // Build will not fail if failOnBoatBayErrorResponse is false mojo.setFailOnBoatBayErrorResponse(false); //No Exception is thrown - assertDoesNotThrow(() -> mojo.execute()); + assertDoesNotThrow(mojo::execute); File output = new File(mojo.getRadioOutput(), "radioOutput.json"); //But No output file present - assertTrue(!output.exists()); + assertFalse(output.exists()); } @@ -581,17 +579,19 @@ private String getFile(String glob) { return (new File("src/test/resources").getAbsolutePath() + glob); } + @NotNull - private List getSampleBoatLintReports(String expectedDefaultKey, BigDecimal reportId, String reportGrade, - Changes typeOfChange, Severity sampleSeverityInResponse) { + private List buildSampleBoatLintReports(String expectedDefaultKey, String reportGrade, + Changes typeOfChange, Severity sampleSeverityInResponse, + String description) { BoatLintReport boatLintReport = new BoatLintReport(); - boatLintReport.setId(reportId); + boatLintReport.setId(BigDecimal.TEN); boatLintReport.setGrade(reportGrade); - boatLintReport.violations(List.of(BoatViolation.builder().severity(sampleSeverityInResponse).build())); - boatLintReport.setSpec(BoatSpec.builder().key(expectedDefaultKey).changes(typeOfChange).build()); - List result = new ArrayList<>(); - result.add(boatLintReport); - return result; + boatLintReport.violations( + List.of(BoatViolation.builder().severity(sampleSeverityInResponse).build())); + boatLintReport.setSpec( + BoatSpec.builder().key(expectedDefaultKey).description(description).changes(typeOfChange).build()); + return List.of(boatLintReport); } } \ No newline at end of file