diff --git a/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java b/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java index 93aa2b4884..4d38c2ed75 100644 --- a/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java +++ b/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java @@ -97,9 +97,9 @@ private static boolean containsNewline(final String expression) { public boolean isValid() { DescriptorImpl d = new DescriptorImpl(getJenkinsFacade()); - return d.doCheckScript(script).kind == Kind.OK - && d.doCheckRegexp(regexp).kind == Kind.OK - && d.doCheckName(name).kind == Kind.OK; + return d.checkScript(script).kind == Kind.OK + && d.checkRegexp(regexp).kind == Kind.OK + && d.checkName(name).kind == Kind.OK; } public String getId() { @@ -180,22 +180,22 @@ public int hashCode() { * * @return a new parser instance * @throws IllegalArgumentException - * if this parsers configuration is not valid + * if this parser configuration is not valid */ public IssueParser createParser() { DescriptorImpl descriptor = new DescriptorImpl(getJenkinsFacade()); - FormValidation nameCheck = descriptor.doCheckName(name); + FormValidation nameCheck = descriptor.checkName(name); if (nameCheck.kind == Kind.ERROR) { throw new IllegalArgumentException("Name is not valid: " + nameCheck.getMessage()); } - FormValidation scriptCheck = descriptor.doCheckScript(script); + FormValidation scriptCheck = descriptor.checkScript(script); if (scriptCheck.kind == Kind.ERROR) { throw new IllegalArgumentException("Script is not valid: " + scriptCheck.getMessage()); } - FormValidation regexpCheck = descriptor.doCheckRegexp(regexp); + FormValidation regexpCheck = descriptor.checkRegexp(regexp); if (regexpCheck.kind == Kind.ERROR) { throw new IllegalArgumentException("RegExp is not valid: " + regexpCheck.getMessage()); } @@ -280,11 +280,10 @@ public FormValidation doCheckName(@AncestorInPath final BuildableItem project, return FormValidation.ok(); } - return doCheckName(name); + return checkName(name); } - @VisibleForTesting - FormValidation doCheckName(final String name) { + FormValidation checkName(final String name) { if (StringUtils.isBlank(name)) { return FormValidation.error(Messages.GroovyParser_Error_Name_isEmpty()); } @@ -308,11 +307,10 @@ public FormValidation doCheckRegexp(@AncestorInPath final BuildableItem project, return FormValidation.ok(); } - return doCheckRegexp(regexp); + return checkRegexp(regexp); } - @VisibleForTesting - FormValidation doCheckRegexp(final String regexp) { + FormValidation checkRegexp(final String regexp) { try { if (StringUtils.isBlank(regexp)) { return FormValidation.error(Messages.GroovyParser_Error_Regexp_isEmpty()); @@ -347,11 +345,10 @@ public FormValidation doCheckScript(@AncestorInPath final BuildableItem project, if (!jenkinsFacade.hasPermission(Jenkins.ADMINISTER)) { return NO_RUN_SCRIPT_PERMISSION_WARNING; } - return doCheckScript(script); + return checkScript(script); } - @VisibleForTesting - FormValidation doCheckScript(final String script) { + FormValidation checkScript(final String script) { try { if (StringUtils.isBlank(script)) { return FormValidation.error(Messages.GroovyParser_Error_Script_isEmpty()); @@ -393,11 +390,10 @@ public FormValidation doCheckExample(@AncestorInPath final BuildableItem project if (!jenkinsFacade.hasPermission(Jenkins.ADMINISTER)) { return NO_RUN_SCRIPT_PERMISSION_WARNING; } - return doCheckExample(example, regexp, script); + return checkExample(example, regexp, script); } - @VisibleForTesting - FormValidation doCheckExample(final String example, final String regexp, final String script) { + FormValidation checkExample(final String example, final String regexp, final String script) { if (StringUtils.isNotBlank(example) && StringUtils.isNotBlank(regexp) && StringUtils.isNotBlank(script)) { FormValidation response = parseExample(script, example, regexp, containsNewline(regexp)); if (example.length() <= MAX_EXAMPLE_SIZE) { diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/core/filter/RegexpFilterTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/core/filter/RegexpFilterTest.java index 9cf40d2b54..13f8ab4b1c 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/core/filter/RegexpFilterTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/core/filter/RegexpFilterTest.java @@ -7,7 +7,7 @@ import edu.hm.hafner.analysis.Report; import edu.hm.hafner.analysis.Report.IssueFilterBuilder; -import hudson.model.AbstractProject; +import hudson.model.BuildableItem; import hudson.model.Item; import io.jenkins.plugins.analysis.core.filter.IncludeType.DescriptorImpl; @@ -48,7 +48,7 @@ void issue54035() { @Test void shouldValidatePattern() { JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class); - when(jenkinsFacade.hasPermission(Item.CONFIGURE, (AbstractProject) null)).thenReturn(true); + when(jenkinsFacade.hasPermission(Item.CONFIGURE, (BuildableItem) null)).thenReturn(true); RegexpFilterDescriptor descriptor = new DescriptorImpl(jenkinsFacade); assertThat(descriptor.doCheckPattern(null, null)).isOk().hasMessage(Messages.pattern_blank()); diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/ToolSelectionDescriptorTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/ToolSelectionDescriptorTest.java index bb8cc77051..394a50a303 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/ToolSelectionDescriptorTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/ToolSelectionDescriptorTest.java @@ -8,7 +8,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import hudson.model.AbstractProject; +import hudson.model.BuildableItem; import hudson.model.Item; import hudson.model.Job; import hudson.util.ComboBoxModel; @@ -64,7 +64,7 @@ private ToolSelectionDescriptor createDescriptor(final String[] elements, final JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class); when(jenkinsFacade.getAllJobs()).thenReturn(Lists.list(job)); ToolSelectionDescriptor.setJenkinsFacade(jenkinsFacade); - when(jenkinsFacade.hasPermission(Item.CONFIGURE, (AbstractProject) null)).thenReturn(hasPermission); + when(jenkinsFacade.hasPermission(Item.CONFIGURE, (BuildableItem) null)).thenReturn(hasPermission); return toolSelectionDescriptor; } } diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/core/util/QualityGateTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/core/util/QualityGateTest.java index 3b835d9d5e..0aab4017bc 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/core/util/QualityGateTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/core/util/QualityGateTest.java @@ -4,7 +4,7 @@ import edu.hm.hafner.util.SerializableTest; -import hudson.model.AbstractProject; +import hudson.model.BuildableItem; import hudson.model.Item; import io.jenkins.plugins.analysis.core.util.QualityGate.QualityGateDescriptor; @@ -24,7 +24,7 @@ class QualityGateTest extends SerializableTest { @Test void shouldValidateThreshold() { JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class); - when(jenkinsFacade.hasPermission(Item.CONFIGURE, (AbstractProject) null)).thenReturn(true); + when(jenkinsFacade.hasPermission(Item.CONFIGURE, (BuildableItem) null)).thenReturn(true); QualityGateDescriptor descriptor = new QualityGateDescriptor(jenkinsFacade); diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/RegisteredParserTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/RegisteredParserTest.java index c390ac0f37..93527a42cf 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/RegisteredParserTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/RegisteredParserTest.java @@ -7,8 +7,7 @@ import edu.hm.hafner.analysis.parser.checkstyle.CheckStyleParser; -import hudson.model.AbstractProject; -import hudson.model.Item; +import jenkins.model.Jenkins; import io.jenkins.plugins.analysis.warnings.RegisteredParser.Descriptor; import io.jenkins.plugins.util.JenkinsFacade; @@ -73,14 +72,13 @@ class DescriptorTest { @Test void shouldPopulateListOfParsers() { JenkinsFacade jenkins = mock(JenkinsFacade.class); - AbstractProject job = mock(AbstractProject.class); - when(jenkins.hasPermission(Item.CONFIGURE, job)).thenReturn(true); + when(jenkins.hasPermission(Jenkins.READ)).thenReturn(true); Descriptor descriptor = new Descriptor(jenkins); assertThat(descriptor.getId()).isEqualTo(Descriptor.ANALYSIS_MODEL_ID); assertThat(descriptor.doFillAnalysisModelIdItems()).extracting(o -> o.value).first().isEqualTo("acu-cobol"); - when(jenkins.hasPermission(Item.CONFIGURE, job)).thenReturn(false); + when(jenkins.hasPermission(Jenkins.READ)).thenReturn(false); assertThat(descriptor.doFillAnalysisModelIdItems()).isEmpty(); } } diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParserTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParserTest.java index 8cec7d5940..c34981aada 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParserTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParserTest.java @@ -71,7 +71,7 @@ private void matchMultiLine(final String multiLineRegexp) throws IOException { assertThat(parser.hasMultiLineSupport()).as("Wrong multi line support guess").isTrue(); DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckExample(textToMatch, multiLineRegexp, script)).isOk(); + assertThat(descriptor.checkExample(textToMatch, multiLineRegexp, script)).isOk(); IssueParser instance = parser.createParser(); Run run = mock(Run.class); @@ -112,8 +112,6 @@ void shouldThrowExceptionDueToMissingName() { assertThat(groovyParser.isValid()).isFalse(); assertThatIllegalArgumentException().isThrownBy(groovyParser::createParser) .withMessageContaining("Name is not valid"); - - verifyThatValidationIsSkippedIfUserHasNoPermissions(groovyParser); } @Test @@ -122,8 +120,6 @@ void shouldThrowExceptionDueToBrokenScript() { assertThat(groovyParser.isValid()).isFalse(); assertThatIllegalArgumentException().isThrownBy(groovyParser::createParser) .withMessageContaining("Script is not valid"); - - verifyThatValidationIsSkippedIfUserHasNoPermissions(groovyParser); } @Test @@ -132,15 +128,6 @@ void shouldThrowExceptionDueToBrokenRegExp() { assertThat(groovyParser.isValid()).isFalse(); assertThatIllegalArgumentException().isThrownBy(groovyParser::createParser) .withMessageContaining("RegExp is not valid"); - - groovyParser.setJenkinsFacade(mock(JenkinsFacade.class)); - assertThat(groovyParser.isValid()).isTrue(); - } - - private void verifyThatValidationIsSkippedIfUserHasNoPermissions(final GroovyParser groovyParser) { - groovyParser.setJenkinsFacade(mock(JenkinsFacade.class)); - assertThat(groovyParser.isValid()).isTrue(); - assertThatNoException().isThrownBy(groovyParser::createParser); } @Test @@ -164,39 +151,39 @@ void shouldDetectSingleLineRegularExpression() { void shouldAcceptOnlyNonEmptyStringsAsName() { DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckName(null)).isError(); - assertThat(descriptor.doCheckName(StringUtils.EMPTY)).isError(); - assertThat(descriptor.doCheckName("Java Parser 2")).isOk(); + assertThat(descriptor.checkName(null)).isError(); + assertThat(descriptor.checkName(StringUtils.EMPTY)).isError(); + assertThat(descriptor.checkName("Java Parser 2")).isOk(); } @Test void shouldRejectInvalidRegularExpressions() { DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckRegexp(null)).isError(); - assertThat(descriptor.doCheckRegexp(StringUtils.EMPTY)).isError(); - assertThat(descriptor.doCheckRegexp("one brace (")).isError(); - assertThat(descriptor.doCheckRegexp("backslash \\")).isError(); + assertThat(descriptor.checkRegexp(null)).isError(); + assertThat(descriptor.checkRegexp(StringUtils.EMPTY)).isError(); + assertThat(descriptor.checkRegexp("one brace (")).isError(); + assertThat(descriptor.checkRegexp("backslash \\")).isError(); - assertThat(descriptor.doCheckRegexp("^.*[a-z]")).isOk(); + assertThat(descriptor.checkRegexp("^.*[a-z]")).isOk(); } @Test void shouldRejectInvalidScripts() { DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckScript(null)).isError(); - assertThat(descriptor.doCheckScript(StringUtils.EMPTY)).isError(); - assertThat(descriptor.doCheckScript("Hello World")).isError(); + assertThat(descriptor.checkScript(null)).isError(); + assertThat(descriptor.checkScript(StringUtils.EMPTY)).isError(); + assertThat(descriptor.checkScript("Hello World")).isError(); - assertThat(descriptor.doCheckScript(toString("parser.groovy"))).isOk(); + assertThat(descriptor.checkScript(toString("parser.groovy"))).isOk(); } @Test void shouldFindOneIssueWithValidScriptAndRegularExpression() { DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckExample(SINGLE_LINE_EXAMPLE, SINGLE_LINE_REGEXP, + assertThat(descriptor.checkExample(SINGLE_LINE_EXAMPLE, SINGLE_LINE_REGEXP, toString("parser.groovy"))).isOk(); } @@ -204,7 +191,7 @@ void shouldFindOneIssueWithValidScriptAndRegularExpression() { void shouldReportErrorWhenNoMatchesAreFoundInExample() { DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckExample("this is a warning message", SINGLE_LINE_REGEXP, + assertThat(descriptor.checkExample("this is a warning message", SINGLE_LINE_REGEXP, toString("parser.groovy"))).isError(); } @@ -212,7 +199,7 @@ void shouldReportErrorWhenNoMatchesAreFoundInExample() { void shouldReportErrorWhenRegularExpressionHasIllegalMatchAccess() { DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckExample(SINGLE_LINE_EXAMPLE, "^\\s*(.*):(\\d+):(.*)$", + assertThat(descriptor.checkExample(SINGLE_LINE_EXAMPLE, "^\\s*(.*):(\\d+):(.*)$", toString("parser.groovy"))).isError(); } @@ -220,7 +207,7 @@ void shouldReportErrorWhenRegularExpressionHasIllegalMatchAccess() { void shouldAcceptMultiLineRegularExpression() { DescriptorImpl descriptor = createDescriptor(); - assertThat(descriptor.doCheckExample(MULTI_LINE_EXAMPLE, MULTI_LINE_REGEXP, + assertThat(descriptor.checkExample(MULTI_LINE_EXAMPLE, MULTI_LINE_REGEXP, toString("multiline.groovy"))).isOk(); }