|
| 1 | +package moe.yuuta.server.api; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.mockito.Mockito; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Locale; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import io.vertx.core.AsyncResult; |
| 14 | +import io.vertx.core.Handler; |
| 15 | +import io.vertx.core.Vertx; |
| 16 | +import io.vertx.core.http.HttpClientRequest; |
| 17 | +import io.vertx.ext.unit.Async; |
| 18 | +import io.vertx.ext.unit.TestContext; |
| 19 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 20 | +import io.vertx.ext.web.client.HttpResponse; |
| 21 | +import moe.yuuta.common.Constants; |
| 22 | +import moe.yuuta.server.mipush.Message; |
| 23 | +import moe.yuuta.server.mipush.MiPushApi; |
| 24 | +import moe.yuuta.server.mipush.SendMessageResponse; |
| 25 | + |
| 26 | +import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST; |
| 27 | +import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR; |
| 28 | + |
| 29 | +@RunWith(VertxUnitRunner.class) |
| 30 | +public class PushRequestVerifyTest { |
| 31 | + private Vertx vertx; |
| 32 | + private ApiHandler apiHandler; |
| 33 | + private ApiVerticle apiVerticle; // We won't build a mocked RoutingContext |
| 34 | + private PushRequest normalRequest; |
| 35 | + private volatile boolean nextApiCallShouldOK; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp (TestContext testContext) { |
| 39 | + vertx = Vertx.vertx(); |
| 40 | + apiHandler = Mockito.spy(ApiHandler.apiHandler(vertx)); |
| 41 | + Mockito.when(apiHandler.getMiPushApi()).thenReturn(new MiPushApi(null) { |
| 42 | + @Override |
| 43 | + public void pushOnceToId(Message message, String[] regIds, Map<String, String> customExtras, Handler<AsyncResult<HttpResponse<SendMessageResponse>>> handler) { |
| 44 | + if (!nextApiCallShouldOK) |
| 45 | + testContext.fail("Unaccepted call"); |
| 46 | + else |
| 47 | + handler.handle(new AsyncResult<HttpResponse<SendMessageResponse>>() { |
| 48 | + @Override |
| 49 | + public HttpResponse<SendMessageResponse> result() { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Throwable cause() { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean succeeded() { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean failed() { |
| 65 | + return false; |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + }); |
| 70 | + apiVerticle = Mockito.spy(new ApiVerticle()); |
| 71 | + Mockito.when(apiVerticle.getApiHandler()).thenReturn(apiHandler); |
| 72 | + vertx.deployVerticle(apiVerticle, testContext.asyncAssertSuccess()); |
| 73 | + } |
| 74 | + |
| 75 | + private void restoreRequest () { |
| 76 | + normalRequest = new PushRequest(); |
| 77 | + normalRequest.setDelayMs(0); |
| 78 | + normalRequest.setRegistrationId("abc"); |
| 79 | + normalRequest.setDisplay(Constants.DISPLAY_ALL); |
| 80 | + normalRequest.setExtras(null); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void shouldPassCorrectPushRequest (TestContext testContext) { |
| 85 | + Async async = testContext.async(); |
| 86 | + // Should pass |
| 87 | + nextApiCallShouldOK = true; |
| 88 | + restoreRequest(); |
| 89 | + vertx.createHttpClient().post(8080, "localhost", ApiVerticle.ROUTE_TEST, httpClientResponse -> { |
| 90 | + testContext.assertEquals(INTERNAL_SERVER_ERROR.code(), httpClientResponse.statusCode()); |
| 91 | + nextApiCallShouldOK = false; |
| 92 | + async.countDown(); |
| 93 | + }).putHeader("Content-Type", "application/json") |
| 94 | + .putHeader("Accept-Language", Locale.ENGLISH.toString()) |
| 95 | + .putHeader(Constants.HEADER_PRODUCT, "123") |
| 96 | + .setChunked(true) |
| 97 | + .end(ApiUtils.tryObjectToJson(normalRequest)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test(timeout = 2000) |
| 101 | + public void shouldRefuseBadPushRequest (TestContext testContext) { |
| 102 | + Async async = testContext.async(6); |
| 103 | + // Missing request test |
| 104 | + send(testContext, async, false, true /* Keep the variable amount always 1 */, false); |
| 105 | + |
| 106 | + // Missing product test |
| 107 | + restoreRequest(); |
| 108 | + send(testContext, async, true, false, false); |
| 109 | + |
| 110 | + // Incorrect delay test |
| 111 | + restoreRequest(); |
| 112 | + normalRequest.setDelayMs(-100); |
| 113 | + send(testContext, async, true, true, false); |
| 114 | + restoreRequest(); |
| 115 | + normalRequest.setDelayMs(Constants.PUSH_DELAY_MS_MAX + 100); |
| 116 | + send(testContext, async, true, true, false); |
| 117 | + |
| 118 | + // Incorrect delay test |
| 119 | + restoreRequest(); |
| 120 | + normalRequest.setDisplay(-100); |
| 121 | + send(testContext, async, true, true, false); |
| 122 | + |
| 123 | + // Null registration ID test |
| 124 | + restoreRequest(); |
| 125 | + normalRequest.setRegistrationId(null); |
| 126 | + send(testContext, async, true, true, false); |
| 127 | + |
| 128 | + // Too many extras test |
| 129 | + Map<String, String> strings = new HashMap<>(20); |
| 130 | + for (int i = 0; i < 20; i ++) |
| 131 | + strings.put("K" + i, "V" + i); |
| 132 | + restoreRequest(); |
| 133 | + normalRequest.setExtras(strings); |
| 134 | + send(testContext, async, true, true, true); |
| 135 | + } |
| 136 | + |
| 137 | + private void send (TestContext testContext, Async async, boolean addProduct, boolean addRequest, boolean complete) { |
| 138 | + HttpClientRequest request = vertx.createHttpClient().post(8080, "localhost", ApiVerticle.ROUTE_TEST, httpClientResponse -> { |
| 139 | + testContext.assertEquals(BAD_REQUEST.code(), httpClientResponse.statusCode()); |
| 140 | + async.countDown(); |
| 141 | + if (complete) async.complete(); |
| 142 | + }).putHeader("Content-Type", "application/json") |
| 143 | + .putHeader("Accept-Language", Locale.ENGLISH.toString()) |
| 144 | + .setChunked(true); |
| 145 | + if (addProduct) { |
| 146 | + request.putHeader(Constants.HEADER_PRODUCT, "123"); |
| 147 | + } |
| 148 | + if (addRequest) request.end(ApiUtils.tryObjectToJson(normalRequest)); |
| 149 | + else request.end(); |
| 150 | + } |
| 151 | + |
| 152 | + @After |
| 153 | + public void tearDown (TestContext testContext) { |
| 154 | + vertx.close(testContext.asyncAssertSuccess()); |
| 155 | + } |
| 156 | +} |
0 commit comments