/** * Copyright 2014 Lockheed Martin Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package streamflow.server.resource; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Scopes; import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.GuiceServletContextListener; import com.sun.jersey.guice.JerseyServletModule; import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; import com.sun.jersey.test.framework.JerseyTest; import com.sun.jersey.test.framework.WebAppDescriptor; import com.sun.jersey.test.framework.spi.container.TestContainerFactory; import com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory; import javax.ws.rs.core.MediaType; import streamflow.model.config.StreamflowConfig; import streamflow.model.test.IntegrationTest; import static org.junit.Assert.*; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) @Category(IntegrationTest.class) public class ConfigurationResourceTest extends JerseyTest { public static StreamflowConfig streamflowConfig = new StreamflowConfig(); public ConfigurationResourceTest() { super(new WebAppDescriptor.Builder() .contextListenerClass(ConfigurationWebConfig.class) .filterClass(GuiceFilter.class) .build()); } @Test public void getAppConfiguration() { StreamflowConfig responseConfig = resource().path("/api/config/app") .accept(MediaType.APPLICATION_JSON).get(StreamflowConfig.class); assertEquals("Response configuration should match the mocked configuration", streamflowConfig, responseConfig); } @Override public TestContainerFactory getTestContainerFactory() { return new GrizzlyWebTestContainerFactory(); } public static class ConfigurationWebConfig extends GuiceServletContextListener { @Override protected Injector getInjector() { return Guice.createInjector(new JerseyServletModule() { @Override protected void configureServlets() { bind(StreamflowConfig.class).toInstance(streamflowConfig); bind(ConfigurationResource.class); serve("/api/*").with(GuiceContainer.class); // hook Jackson into Jersey as the POJO <-> JSON mapper bind(JacksonJsonProvider.class).in(Scopes.SINGLETON); } }); } } }