|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.http; |
| 19 | + |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import javax.servlet.http.HttpServletRequest; |
| 25 | +import javax.servlet.http.HttpServletResponse; |
| 26 | + |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.PrintWriter; |
| 30 | + |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | +import static org.mockito.Matchers.anyInt; |
| 33 | +import static org.mockito.Matchers.anyString; |
| 34 | +import static org.mockito.Matchers.eq; |
| 35 | +import static org.mockito.Mockito.atLeastOnce; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.never; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + * Test if the {@link IsActiveServlet} returns the right answer if the |
| 44 | + * underlying service is active. |
| 45 | + */ |
| 46 | +public class TestIsActiveServlet { |
| 47 | + |
| 48 | + private IsActiveServlet servlet; |
| 49 | + private HttpServletRequest req; |
| 50 | + private HttpServletResponse resp; |
| 51 | + private ByteArrayOutputStream respOut; |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setUp() throws Exception { |
| 55 | + req = mock(HttpServletRequest.class); |
| 56 | + resp = mock(HttpServletResponse.class); |
| 57 | + respOut = new ByteArrayOutputStream(); |
| 58 | + PrintWriter writer = new PrintWriter(respOut); |
| 59 | + when(resp.getWriter()).thenReturn(writer); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSucceedsOnActive() throws IOException { |
| 64 | + servlet = new IsActiveServlet() { |
| 65 | + @Override |
| 66 | + protected boolean isActive() { |
| 67 | + return true; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + String response = doGet(); |
| 72 | + verify(resp, never()).sendError(anyInt(), anyString()); |
| 73 | + assertEquals(IsActiveServlet.RESPONSE_ACTIVE, response); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testFailsOnInactive() throws IOException { |
| 78 | + servlet = new IsActiveServlet() { |
| 79 | + @Override |
| 80 | + protected boolean isActive() { |
| 81 | + return false; |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + doGet(); |
| 86 | + verify(resp, atLeastOnce()).sendError( |
| 87 | + eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), |
| 88 | + eq(IsActiveServlet.RESPONSE_NOT_ACTIVE)); |
| 89 | + } |
| 90 | + |
| 91 | + private String doGet() throws IOException { |
| 92 | + servlet.doGet(req, resp); |
| 93 | + return new String(respOut.toByteArray(), "UTF-8"); |
| 94 | + } |
| 95 | +} |
0 commit comments