View Javadoc

1   package com.atlassian.jira.rest.client.test.matchers;
2   
3   import com.atlassian.jira.rest.client.api.RestClientException;
4   import com.atlassian.jira.rest.client.api.domain.util.ErrorCollection;
5   import com.google.common.collect.ImmutableList;
6   import org.hamcrest.BaseMatcher;
7   import org.hamcrest.Description;
8   import org.hamcrest.Matcher;
9   import org.hamcrest.Matchers;
10  
11  /**
12   * Matchers for RestClientException
13   *
14   * @since v2.0
15   */
16  public class RestClientExceptionMatchers {
17  
18      public static Matcher<RestClientException> rceWithSingleError(final Integer statusCode, final String expectedErrorMessage) {
19          return new BaseMatcher<RestClientException>() {
20  
21              @Override
22              public boolean matches(final Object item) {
23                  if (item instanceof RestClientException) {
24                      final RestClientException ex = (RestClientException) item;
25                      final Matcher<Iterable<? extends String>> errorMessageMatcher = Matchers
26                              .contains(expectedErrorMessage);
27                      return ex.getStatusCode().get().equals(statusCode)
28                              && ex.getErrorCollections().size() == 1
29                              && errorMessageMatcher.matches(ex.getErrorCollections().iterator().next().getErrorMessages());
30  
31                  }
32                  return false;
33              }
34  
35              @Override
36              public void describeTo(final Description description) {
37                  final ErrorCollection expectedErrorCollection = ErrorCollection.builder()
38                          .errorMessage(expectedErrorMessage).status(statusCode).build();
39  
40                  final RestClientException expectedException = new RestClientException(
41                          ImmutableList.of(expectedErrorCollection), statusCode);
42  
43                  description.appendText("<" + expectedException.toString() + ">");
44              }
45          };
46      }
47  }