Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import org.hamcrest.Description; |
| 4 | import org.hamcrest.Factory; |
| 5 | import org.hamcrest.Matcher; |
| 6 | import org.hamcrest.TypeSafeMatcher; |
| 7 | import org.restlet.data.Status; |
| 8 | import org.restlet.resource.ClientResource; |
| 9 | |
| 10 | /** |
| 11 | * Hamcrest Matcher to determine if a Restlet ClientResource object's status |
| 12 | * matches a given status. |
| 13 | */ |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 14 | public final class ClientResourceStatusMatcher extends TypeSafeMatcher<ClientResource> { |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 15 | private Status status; |
| 16 | |
| 17 | /** |
| 18 | * Hide constructor with no arguments. |
| 19 | */ |
| 20 | @SuppressWarnings("unused") |
| 21 | private ClientResourceStatusMatcher() { } |
| 22 | |
| 23 | /** |
| 24 | * Create a matcher for the given status object. |
| 25 | * |
| 26 | * @param newStatus status to be matched against |
| 27 | */ |
| 28 | private ClientResourceStatusMatcher(final Status newStatus) { |
| 29 | status = newStatus; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Match a Client resource. Compares the status of the given client against |
| 34 | * the expected Status that was given when the object was created. |
| 35 | * |
| 36 | * @param client the client to perform the match against |
| 37 | * @return true if the Status of the given client matches the Status that |
| 38 | * was given when the Matcher was created, false otherwise |
| 39 | */ |
| 40 | @Override |
| 41 | public boolean matchesSafely(ClientResource client) { |
| 42 | return status.equals(client.getStatus()); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Generate a Hamcrest description for the 'to' object of this Matcher. |
| 47 | * |
| 48 | * @param description Description object to add the textual description to |
| 49 | */ |
| 50 | @Override |
| 51 | public void describeTo(Description description) { |
| 52 | description.appendText("status '") |
| 53 | .appendText(status.getDescription()) |
| 54 | .appendText("'"); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Describe why a mismatch was generated for the given client. |
| 59 | * |
| 60 | * @param client the client that was detected as a mismatch |
| 61 | * @param mismatchDescription Description object to add the textual |
| 62 | * description to |
| 63 | */ |
| 64 | @Override |
| 65 | public void describeMismatchSafely(ClientResource client, |
| 66 | Description mismatchDescription) { |
| 67 | mismatchDescription.appendText(" was '") |
| 68 | .appendText(client.getStatus().getDescription()) |
| 69 | .appendText("'"); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Factory method to create a status Matcher for an ClientResource. |
| 74 | * |
| 75 | * @param status HTTP status code that the client status is matched against. |
| 76 | * @return Matcher object |
| 77 | */ |
| 78 | @Factory |
| 79 | public static Matcher<ClientResource> hasStatusOf(final Status status) { |
| 80 | return new ClientResourceStatusMatcher(status); |
| 81 | } |
| 82 | } |