|
| 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.pulsar.broker.web; |
| 20 | + |
| 21 | +import javax.servlet.ServletContext; |
| 22 | +import javax.ws.rs.core.Context; |
| 23 | +import javax.ws.rs.core.Feature; |
| 24 | +import javax.ws.rs.core.FeatureContext; |
| 25 | +import org.apache.pulsar.broker.PulsarService; |
| 26 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 27 | +import org.glassfish.jersey.server.ResourceConfig; |
| 28 | +import org.glassfish.jersey.servlet.ServletContainer; |
| 29 | +import org.glassfish.jersey.test.DeploymentContext; |
| 30 | +import org.glassfish.jersey.test.JerseyTestNg; |
| 31 | +import org.glassfish.jersey.test.ServletDeploymentContext; |
| 32 | +import org.glassfish.jersey.test.TestProperties; |
| 33 | +import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; |
| 34 | +import org.glassfish.jersey.test.spi.TestContainerException; |
| 35 | +import org.glassfish.jersey.test.spi.TestContainerFactory; |
| 36 | +import org.testng.annotations.AfterClass; |
| 37 | +import org.testng.annotations.BeforeClass; |
| 38 | + |
| 39 | +import static org.mockito.Mockito.doReturn; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | + |
| 42 | +/** |
| 43 | + * A base class for testing subclasses of {@link PulsarWebResource}. |
| 44 | + */ |
| 45 | +public abstract class PulsarWebResourceTest extends JerseyTestNg.ContainerPerClassTest { |
| 46 | + |
| 47 | + protected ServiceConfiguration config; |
| 48 | + protected PulsarService pulsar; |
| 49 | + |
| 50 | + protected PulsarWebResourceTest() { |
| 51 | + config = new ServiceConfiguration(); |
| 52 | + |
| 53 | + pulsar = mock(PulsarService.class); |
| 54 | + doReturn(config).when(pulsar).getConfig(); |
| 55 | + doReturn(config).when(pulsar).getConfiguration(); |
| 56 | + |
| 57 | + set(TestProperties.CONTAINER_PORT, 0); |
| 58 | + } |
| 59 | + |
| 60 | + @BeforeClass(alwaysRun = true) |
| 61 | + @Override |
| 62 | + public void setUp() throws Exception { |
| 63 | + super.setUp(); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterClass(alwaysRun = true) |
| 67 | + @Override |
| 68 | + public void tearDown() throws Exception { |
| 69 | + super.tearDown(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a JAX-RS resource configuration for test purposes. |
| 74 | + */ |
| 75 | + @Override |
| 76 | + protected abstract ResourceConfig configure(); |
| 77 | + |
| 78 | + /** |
| 79 | + * Creates a test container factory with servlet support. |
| 80 | + */ |
| 81 | + @Override |
| 82 | + protected TestContainerFactory getTestContainerFactory() throws TestContainerException { |
| 83 | + return new GrizzlyWebTestContainerFactory(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Configures a deployment context for JAX-RS. |
| 88 | + */ |
| 89 | + @Override |
| 90 | + protected DeploymentContext configureDeployment() { |
| 91 | + ResourceConfig app = configure(); |
| 92 | + app.register(new Feature() { |
| 93 | + @Context |
| 94 | + ServletContext servletContext; |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean configure(FeatureContext context) { |
| 98 | + servletContext.setAttribute(WebService.ATTRIBUTE_PULSAR_NAME, pulsar); |
| 99 | + return true; |
| 100 | + } |
| 101 | + }); |
| 102 | + return ServletDeploymentContext.forServlet(new ServletContainer(app)).build(); |
| 103 | + } |
| 104 | +} |
0 commit comments