|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.bookkeeper.server.http.service; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.times; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | + |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | +import org.apache.bookkeeper.bookie.LedgerStorage; |
| 31 | +import org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorage; |
| 32 | +import org.apache.bookkeeper.conf.ServerConfiguration; |
| 33 | +import org.apache.bookkeeper.http.HttpServer; |
| 34 | +import org.apache.bookkeeper.http.service.HttpServiceRequest; |
| 35 | +import org.apache.bookkeeper.http.service.HttpServiceResponse; |
| 36 | +import org.apache.bookkeeper.proto.BookieServer; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * Unit test for {@link TriggerGCService}. |
| 43 | + */ |
| 44 | +@Slf4j |
| 45 | +public class TriggerGCServiceTest { |
| 46 | + private TriggerGCService service; |
| 47 | + private BookieServer mockBookieServer; |
| 48 | + private LedgerStorage mockLedgerStorage; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setup() { |
| 52 | + this.mockBookieServer = mock(BookieServer.class, RETURNS_DEEP_STUBS); |
| 53 | + this.mockLedgerStorage = mock(DbLedgerStorage.class); |
| 54 | + when(mockBookieServer.getBookie().getLedgerStorage()).thenReturn(mockLedgerStorage); |
| 55 | + when(mockLedgerStorage.isInForceGC()).thenReturn(false); |
| 56 | + when(mockLedgerStorage.isMajorGcSuspended()).thenReturn(false); |
| 57 | + when(mockLedgerStorage.isMinorGcSuspended()).thenReturn(false); |
| 58 | + this.service = new TriggerGCService(new ServerConfiguration(), mockBookieServer); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testHandleRequest() throws Exception { |
| 63 | + |
| 64 | + // test empty put body |
| 65 | + HttpServiceRequest request = new HttpServiceRequest(); |
| 66 | + request.setMethod(HttpServer.Method.PUT); |
| 67 | + HttpServiceResponse resp = service.handle(request); |
| 68 | + assertEquals(HttpServer.StatusCode.OK.getValue(), resp.getStatusCode()); |
| 69 | + assertEquals("\"Triggered GC on BookieServer: " + mockBookieServer.getBookieId() + "\"", |
| 70 | + resp.getBody()); |
| 71 | + |
| 72 | + // test invalid put json body |
| 73 | + request = new HttpServiceRequest(); |
| 74 | + request.setMethod(HttpServer.Method.PUT); |
| 75 | + request.setBody("test"); |
| 76 | + resp = service.handle(request); |
| 77 | + assertEquals(HttpServer.StatusCode.BAD_REQUEST.getValue(), resp.getStatusCode()); |
| 78 | + assertEquals("Failed to handle the request, exception: Failed to deserialize Object from Json string", |
| 79 | + resp.getBody()); |
| 80 | + |
| 81 | + // test forceMajor and forceMinor not set |
| 82 | + request = new HttpServiceRequest(); |
| 83 | + request.setMethod(HttpServer.Method.PUT); |
| 84 | + request.setBody("{\"test\":1}"); |
| 85 | + resp = service.handle(request); |
| 86 | + verify(mockLedgerStorage, times(1)).forceGC(eq(true), eq(true)); |
| 87 | + assertEquals(HttpServer.StatusCode.OK.getValue(), resp.getStatusCode()); |
| 88 | + assertEquals("\"Triggered GC on BookieServer: " + mockBookieServer.getBookieId() + "\"", |
| 89 | + resp.getBody()); |
| 90 | + |
| 91 | + // test forceMajor set, but forceMinor not set |
| 92 | + request = new HttpServiceRequest(); |
| 93 | + request.setMethod(HttpServer.Method.PUT); |
| 94 | + request.setBody("{\"test\":1,\"forceMajor\":true}"); |
| 95 | + resp = service.handle(request); |
| 96 | + verify(mockLedgerStorage, times(2)).forceGC(eq(true), eq(true)); |
| 97 | + assertEquals(HttpServer.StatusCode.OK.getValue(), resp.getStatusCode()); |
| 98 | + assertEquals("\"Triggered GC on BookieServer: " + mockBookieServer.getBookieId() + "\"", |
| 99 | + resp.getBody()); |
| 100 | + |
| 101 | + // test forceMajor set, but forceMinor not set |
| 102 | + request = new HttpServiceRequest(); |
| 103 | + request.setMethod(HttpServer.Method.PUT); |
| 104 | + request.setBody("{\"test\":1,\"forceMajor\":\"true\"}"); |
| 105 | + resp = service.handle(request); |
| 106 | + verify(mockLedgerStorage, times(3)).forceGC(eq(true), eq(true)); |
| 107 | + assertEquals(HttpServer.StatusCode.OK.getValue(), resp.getStatusCode()); |
| 108 | + assertEquals("\"Triggered GC on BookieServer: " + mockBookieServer.getBookieId() + "\"", |
| 109 | + resp.getBody()); |
| 110 | + |
| 111 | + // test forceMajor set to false, and forMinor not set |
| 112 | + request = new HttpServiceRequest(); |
| 113 | + request.setMethod(HttpServer.Method.PUT); |
| 114 | + request.setBody("{\"test\":1,\"forceMajor\":false}"); |
| 115 | + resp = service.handle(request); |
| 116 | + verify(mockLedgerStorage, times(1)).forceGC(eq(false), eq(true)); |
| 117 | + assertEquals(HttpServer.StatusCode.OK.getValue(), resp.getStatusCode()); |
| 118 | + assertEquals("\"Triggered GC on BookieServer: " + mockBookieServer.getBookieId() + "\"", |
| 119 | + resp.getBody()); |
| 120 | + |
| 121 | + // test forceMajor not set and forMinor set |
| 122 | + request = new HttpServiceRequest(); |
| 123 | + request.setMethod(HttpServer.Method.PUT); |
| 124 | + request.setBody("{\"test\":1,\"forceMinor\":true}"); |
| 125 | + resp = service.handle(request); |
| 126 | + verify(mockLedgerStorage, times(4)).forceGC(eq(true), eq(true)); |
| 127 | + assertEquals(HttpServer.StatusCode.OK.getValue(), resp.getStatusCode()); |
| 128 | + assertEquals("\"Triggered GC on BookieServer: " + mockBookieServer.getBookieId() + "\"", |
| 129 | + resp.getBody()); |
| 130 | + |
| 131 | + // test get gc |
| 132 | + request = new HttpServiceRequest(); |
| 133 | + request.setMethod(HttpServer.Method.GET); |
| 134 | + resp = service.handle(request); |
| 135 | + assertEquals(HttpServer.StatusCode.OK.getValue(), resp.getStatusCode()); |
| 136 | + assertEquals("{\n \"is_in_force_gc\" : \"false\"\n}", resp.getBody()); |
| 137 | + |
| 138 | + // test invalid method type |
| 139 | + request = new HttpServiceRequest(); |
| 140 | + request.setMethod(HttpServer.Method.POST); |
| 141 | + resp = service.handle(request); |
| 142 | + assertEquals(HttpServer.StatusCode.METHOD_NOT_ALLOWED.getValue(), resp.getStatusCode()); |
| 143 | + assertEquals("Not allowed method. Should be PUT to trigger GC, Or GET to get Force GC state.", |
| 144 | + resp.getBody()); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments