|
| 1 | +package com.github.m0nk3y2k4.thetvdb.testutils.assertj; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
| 8 | +import com.github.m0nk3y2k4.thetvdb.api.exception.APIException; |
| 9 | +import com.github.m0nk3y2k4.thetvdb.api.model.APIResponse; |
| 10 | +import com.github.m0nk3y2k4.thetvdb.testutils.json.JSONTestUtil; |
| 11 | +import com.github.m0nk3y2k4.thetvdb.testutils.parameterized.TestTheTVDBAPICall; |
| 12 | +import org.assertj.core.api.AbstractAssert; |
| 13 | +import org.mockserver.client.MockServerClient; |
| 14 | +import org.mockserver.model.HttpRequest; |
| 15 | +import org.mockserver.verify.VerificationTimes; |
| 16 | + |
| 17 | +/** |
| 18 | + * Special assertion for {@link TestTheTVDBAPICall} objects, wrapping some API route invocation |
| 19 | + * <p><br> |
| 20 | + * Supports expectation matching for API routes, regardless of the actual API layout that is used. For wrapped non-void routes a |
| 21 | + * {@link JSONTestUtil.JsonResource} object is expected for matching. The assert will automatically try to determine the layout used |
| 22 | + * by the API route and will perform the matching operation accordingly. |
| 23 | + * <pre>{@code |
| 24 | + * TheTVDBApi api = TheTVDBApiFactory.createApi("Some APIKey"); |
| 25 | + * |
| 26 | + * // Each assertion invokes the given (non-void) API route and matches the returned value with the given expectation (with automatic conversion) |
| 27 | + * |
| 28 | + * // TheTVDBApi layout: expectation automatically converted to ACTORS.getDTO().getData() |
| 29 | + * TestTheTVDBAPICallAssert.assertThat(route(() -> api.getActors(45), "Returning List<Actors>")) |
| 30 | + * .matchesExpectation(JsonResource.ACTORS); |
| 31 | + * |
| 32 | + * // Extended layout: expectation automatically converted to ACTORS.getDTO() |
| 33 | + * TestTheTVDBAPICallAssert.assertThat(route(() -> api.extended().getActors(45), "Returning APIResponse<List<Actor>>")) |
| 34 | + * .matchesExpectation(JsonResource.ACTORS); |
| 35 | + * |
| 36 | + * // JSON layout: expectation automatically converted to ACTORS.getJson() |
| 37 | + * TestTheTVDBAPICallAssert.assertThat(route(() -> api.json().getActors(45), "Returning JsonNode")) |
| 38 | + * .matchesExpectation(JsonResource.ACTORS); |
| 39 | + * }</pre> |
| 40 | + * Unfortunately void API routes do not return any object which could be used for matching an expectation. However, you may let the assert |
| 41 | + * verify that a specific resource has been invoked on the mock server by passing a corresponding HttpRequest object as expectation. For |
| 42 | + * this to work the assert must get in contact with the mock server running in the background. The server can be announced by setting a |
| 43 | + * mock server reference to the assert first. |
| 44 | + * <pre>{@code |
| 45 | + * @Test |
| 46 | + * void voidApiRouteTest(MockServerClient client) throws Exception { // Client can be injected when using the JUnit5 HttpsMockServerExtension |
| 47 | + * TheTVDBApi api = TheTVDBApiFactory.createApi("Some APIKey"); |
| 48 | + * |
| 49 | + * // Make the mock server known to the assert with "usingMockServer(client)" |
| 50 | + * TestTheTVDBAPICallAssert.assertThat(route(() -> api.login(), "Void route not returning any object")) |
| 51 | + * .usingMockServer(client).matchesExpectation(HttpRequest.request("/api/login").withMethod("GET")); |
| 52 | + * } |
| 53 | + * } </pre> |
| 54 | + * @param <T> type of the wrapped routes actual return value |
| 55 | + */ |
| 56 | +public class TestTheTVDBAPICallAssert<T> extends AbstractAssert<TestTheTVDBAPICallAssert<T>, TestTheTVDBAPICall<T>> { |
| 57 | + |
| 58 | + /** Reference to the mock server (for verifying resource invocations of void API routes). Has to be set via #usingMockServer first. */ |
| 59 | + private MockServerClient client; |
| 60 | + |
| 61 | + private TestTheTVDBAPICallAssert(TestTheTVDBAPICall<T> actual) { |
| 62 | + super(actual, TestTheTVDBAPICallAssert.class); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new instance of TestTheTVDBAPICallAssert |
| 67 | + * |
| 68 | + * @param actual The actual value |
| 69 | + * |
| 70 | + * @param <T> type of the wrapped routes actual return value |
| 71 | + * |
| 72 | + * @return The created assertion object |
| 73 | + */ |
| 74 | + public static <T> TestTheTVDBAPICallAssert<T> assertThat(TestTheTVDBAPICall<T> actual) { |
| 75 | + return new TestTheTVDBAPICallAssert<>(actual); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Use the given client to verify requests have been received by the mock server. Has to be called before matching a |
| 80 | + * HttpRequest expectation. |
| 81 | + * |
| 82 | + * @param client Client reference used for working with the mock server |
| 83 | + * |
| 84 | + * @return This assertion object |
| 85 | + */ |
| 86 | + public TestTheTVDBAPICallAssert<T> usingMockServer(MockServerClient client) { |
| 87 | + this.client = client; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Invokes the actual API call and matches the given expectation which must be either |
| 93 | + * <ul> |
| 94 | + * <li>A {@link JSONTestUtil.JsonResource} object for non-void API routes<br> |
| 95 | + * The routes actual return value will be compared with the given object using automatic conversion</li> |
| 96 | + * <li>A {@link HttpRequest} object for void API routes<br> |
| 97 | + * After the route has been invoked the mock server will be asked to verify that a request matching the given |
| 98 | + * object has been received exactly once</li> |
| 99 | + * </ul> |
| 100 | + * |
| 101 | + * @param expected The expected JsonResource or the HttpRequest to be verified |
| 102 | + * |
| 103 | + * @throws IOException If an exception occurred while auto-converting a JsonResource object into it's JsonNode representation |
| 104 | + * @throws APIException If an exception occurred while invoking the actual API call of this assertion |
| 105 | + */ |
| 106 | + public void matchesExpectation(Object expected) throws IOException, APIException { |
| 107 | + isNotNull(); |
| 108 | + |
| 109 | + if (isVoidCallInvocation()) { |
| 110 | + verifyMockServerRouteInvoked((HttpRequest)expected); |
| 111 | + } else { |
| 112 | + matchesJsonResourceExpectation((JSONTestUtil.JsonResource)expected); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Checks whether the actual API call represents a void API route |
| 118 | + * |
| 119 | + * @return True if the API call is an instance of {@link TestTheTVDBAPICall.Void} |
| 120 | + */ |
| 121 | + private boolean isVoidCallInvocation() { |
| 122 | + return actual instanceof TestTheTVDBAPICall.Void; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Invokes the actual API call and verifies that the given HttpRequest has been received exactly once by the mock server |
| 127 | + * |
| 128 | + * @param request The HttpRequest to be verified it has been invoked once |
| 129 | + * |
| 130 | + * @throws APIException If an exception occurred while invoking the actual API call of this assertion |
| 131 | + */ |
| 132 | + private void verifyMockServerRouteInvoked(HttpRequest request) throws APIException { |
| 133 | + if (client == null) { |
| 134 | + failWithMessage("Cannot verify HTTP request expectation due to missing mock server client. " |
| 135 | + + "Please provide a valid mock server client via TestTheTVDBAPICallAssert#usingMockServer(client)"); |
| 136 | + } |
| 137 | + |
| 138 | + actual.invoke(); // Ignore return value as it is always "null" for void methods |
| 139 | + |
| 140 | + client.verify(request, VerificationTimes.once()); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Invokes the actual API call and verifies that its return value matches the given JSON resource. The JSON resource may |
| 145 | + * be auto-converted based on the current API call layout before comparing the values. |
| 146 | + * |
| 147 | + * @param resource JSON resource which is expected to be returned by the invocation of the actual API call |
| 148 | + * |
| 149 | + * @throws IOException If an exception occurred while auto-converting a JsonResource object into it's JsonNode representation |
| 150 | + * @throws APIException If an exception occurred while invoking the actual API call of this assertion |
| 151 | + */ |
| 152 | + private void matchesJsonResourceExpectation(JSONTestUtil.JsonResource resource) throws IOException, APIException { |
| 153 | + T result = actual.invoke(); |
| 154 | + Object expected = buildExpectation(result, resource); |
| 155 | + |
| 156 | + if (!Objects.equals(result, expected)) { |
| 157 | + failWithActualExpectedAndMessage(result, expected, "Expected to be equal"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Auto-converts the given JSON resource to a representation matching the layout used by the API call. The layout will be determined |
| 163 | + * heuristically by analyzing the routes actual return value and compare it to the type of values typically returned by a specific layout. |
| 164 | + * |
| 165 | + * @param result The value returned by invoking the actual API call |
| 166 | + * @param resource JSON resource representing the value expected to be returned by the API call |
| 167 | + * |
| 168 | + * @return Representation of the given resource matching the used layout. This can either be a data object, an APIResponse DTO or a JSON representation. |
| 169 | + * |
| 170 | + * @throws IOException If an exception occurred while auto-converting a JsonResource object into it's JsonNode representation |
| 171 | + */ |
| 172 | + private Object buildExpectation(T result, JSONTestUtil.JsonResource resource) throws IOException { |
| 173 | + if (usingExtendedLayout(result)) { |
| 174 | + // Invocation of some (non-void) TheTVDBApi.Extended layout route -> These routes always return an APIResponse<DTO> object |
| 175 | + return resource.getDTO(); |
| 176 | + } else if (usingJsonLayout(result)) { |
| 177 | + // Invocation of some (non-void) TheTVDBApi.JSON layout route -> These routes always return a JsonNode object |
| 178 | + return resource.getJson(); |
| 179 | + } else { |
| 180 | + // Invocation of some (non-void) TheTVDBApi layout route -> These routes always return the actual content payload of the APIResponse<DTO> |
| 181 | + return resource.getDTO().getData(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Checks whether the given object is a typical return value for the {@link com.github.m0nk3y2k4.thetvdb.api.TheTVDBApi.Extended} layout |
| 187 | + * |
| 188 | + * @param result The value to check |
| 189 | + * |
| 190 | + * @return True if the given value represents a class that is typically returned by the invocation of Extended layout API routes |
| 191 | + */ |
| 192 | + private boolean usingExtendedLayout(T result) { |
| 193 | + return Optional.ofNullable(result).map(Object::getClass).map(APIResponse.class::isAssignableFrom).orElse(false); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Checks whether the given object is a typical return value for the {@link com.github.m0nk3y2k4.thetvdb.api.TheTVDBApi.JSON} layout |
| 198 | + * |
| 199 | + * @param result The value to check |
| 200 | + * |
| 201 | + * @return True if the given value represents a class that is typically returned by the invocation of JSON layout API routes |
| 202 | + */ |
| 203 | + private boolean usingJsonLayout(T result) { |
| 204 | + return Optional.ofNullable(result).map(Object::getClass).map(JsonNode.class::isAssignableFrom).orElse(false); |
| 205 | + } |
| 206 | +} |
0 commit comments