|
| 1 | +package io.jenkins.plugins.util; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.apache.commons.text.StringEscapeUtils; |
| 7 | +import org.assertj.core.api.AbstractAssert; |
| 8 | + |
| 9 | +import hudson.util.FormValidation; |
| 10 | +import hudson.util.FormValidation.Kind; |
| 11 | + |
| 12 | +/** |
| 13 | + * Assertions for {@link FormValidation} instances. |
| 14 | + * |
| 15 | + * @author Ullrich Hafner |
| 16 | + */ |
| 17 | +@SuppressWarnings({"NonBooleanMethodNameMayNotStartWithQuestion", "PMD.LinguisticNaming"}) |
| 18 | +public class FormValidationAssert extends AbstractAssert<FormValidationAssert, FormValidation> { |
| 19 | + private static final String EXPECTED_BUT_WAS_MESSAGE = "%nExpecting %s of:%n <%s>%nto be:%n <%s>%nbut was:%n <%s>."; |
| 20 | + |
| 21 | + /** |
| 22 | + * Creates a new {@link FormValidationAssert} to make assertions on an actual {@link FormValidation}. |
| 23 | + * |
| 24 | + * @param actual |
| 25 | + * the {@link FormValidation} we want to make assertions on |
| 26 | + */ |
| 27 | + public FormValidationAssert(final FormValidation actual) { |
| 28 | + super(actual, FormValidationAssert.class); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * An entry point for {@link FormValidationAssert} to follow AssertJ standard {@code assertThat()}. With a static |
| 33 | + * import, one can write directly {@code assertThat(myIssues)} and get a specific assertion with code completion. |
| 34 | + * |
| 35 | + * @param actual |
| 36 | + * the issues we want to make assertions on |
| 37 | + * |
| 38 | + * @return a new {@link FormValidationAssert} |
| 39 | + */ |
| 40 | + public static FormValidationAssert assertThat(final FormValidation actual) { |
| 41 | + return new FormValidationAssert(actual); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Verifies that the kind of the {@link FormValidation} is {@link Kind#ERROR}. |
| 46 | + * |
| 47 | + * @return this assertion object. |
| 48 | + * @throws AssertionError |
| 49 | + * if the kind of the {@link FormValidation} is not {@link Kind#ERROR}. |
| 50 | + */ |
| 51 | + public FormValidationAssert isError() { |
| 52 | + isNotNull(); |
| 53 | + |
| 54 | + if (!Objects.equals(actual.kind, Kind.ERROR)) { |
| 55 | + failWithMessage(EXPECTED_BUT_WAS_MESSAGE, "kind", actual, "ERROR", "not an ERROR"); |
| 56 | + } |
| 57 | + |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Verifies that the kind of the {@link FormValidation} is {@link Kind#OK}. |
| 63 | + * |
| 64 | + * @return this assertion object. |
| 65 | + * @throws AssertionError |
| 66 | + * if the kind of the {@link FormValidation} is not {@link Kind#OK}. |
| 67 | + */ |
| 68 | + public FormValidationAssert isOk() { |
| 69 | + isNotNull(); |
| 70 | + |
| 71 | + if (!Objects.equals(actual.kind, Kind.OK)) { |
| 72 | + failWithMessage(EXPECTED_BUT_WAS_MESSAGE, "kind", actual, "OK", "not OK"); |
| 73 | + } |
| 74 | + |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Verifies that the message of the {@link FormValidation} equals to the expected message. |
| 80 | + * |
| 81 | + * @param expectedMessage |
| 82 | + * the expected message of the validation result |
| 83 | + * |
| 84 | + * @return this assertion object. |
| 85 | + * @throws AssertionError |
| 86 | + * if the message of the {@link FormValidation} is not equal to the expected message |
| 87 | + */ |
| 88 | + public FormValidationAssert hasMessage(final String expectedMessage) { |
| 89 | + isNotNull(); |
| 90 | + |
| 91 | + String actualMessage = StringEscapeUtils.unescapeHtml4(actual.getMessage()); |
| 92 | + if (!Objects.equals(actualMessage, expectedMessage)) { |
| 93 | + failWithMessage(EXPECTED_BUT_WAS_MESSAGE, "message", |
| 94 | + StringEscapeUtils.unescapeHtml4(actual.toString()), expectedMessage, actualMessage); |
| 95 | + } |
| 96 | + |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Verifies that the message of the {@link FormValidation} contains the expected message text. |
| 102 | + * |
| 103 | + * @param expectedMessagePart |
| 104 | + * a part of the expected message of the validation result |
| 105 | + * |
| 106 | + * @return this assertion object. |
| 107 | + * @throws AssertionError |
| 108 | + * if the message of the {@link FormValidation} contains not the expected message part |
| 109 | + */ |
| 110 | + public FormValidationAssert hasMessageContaining(final String expectedMessagePart) { |
| 111 | + isNotNull(); |
| 112 | + |
| 113 | + String actualMessage = StringEscapeUtils.unescapeHtml4(actual.getMessage()); |
| 114 | + if (!StringUtils.contains(actualMessage, expectedMessagePart)) { |
| 115 | + failWithMessage("%nExpecting %s of:%n <%s>%nto contain:%n <%s>%nbut was:%n <%s>.", "message", |
| 116 | + StringEscapeUtils.unescapeHtml4(actual.toString()), expectedMessagePart, actualMessage); |
| 117 | + } |
| 118 | + |
| 119 | + return this; |
| 120 | + } |
| 121 | +} |
0 commit comments