Mercurial > hg > thermostat-ng > web-gateway
changeset 145:3eca89ccecb1
Add tests for gateway.server classes
Reviewed-by: aazores
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023042.html
author | Jie Kang <jkang@redhat.com> |
---|---|
date | Wed, 10 May 2017 13:29:20 -0400 |
parents | b7f0912a6827 |
children | 7e1c6071024c |
files | server/src/test/java/com/redhat/thermostat/gateway/server/CoreServerBuilderTest.java server/src/test/java/com/redhat/thermostat/gateway/server/apidoc/SwaggerUiHandlerTest.java server/src/test/java/com/redhat/thermostat/gateway/server/services/CoreServiceBuilderFactoryTest.java server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreServiceTest.java server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilderTest.java |
diffstat | 5 files changed, 467 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/CoreServerBuilderTest.java Wed May 10 13:29:20 2017 -0400 @@ -0,0 +1,96 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.gateway.server; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.junit.Test; + +import com.redhat.thermostat.gateway.common.core.config.Configuration; +import com.redhat.thermostat.gateway.common.core.config.GlobalConfiguration; +import com.redhat.thermostat.gateway.server.services.CoreService; +import com.redhat.thermostat.gateway.server.services.CoreServiceBuilder; + +public class CoreServerBuilderTest { + + @Test + public void testBuild() { + CoreServiceBuilder serviceBuilder = mock(CoreServiceBuilder.class); + List<CoreService> serviceList = new ArrayList<>(); + CoreService service = mock(CoreService.class); + ServletContextHandler servletContextHandler = mock(ServletContextHandler.class); + when(servletContextHandler.getServer()).thenReturn(mock(Server.class)); + when(service.createServletContextHandler(any(Server.class))).thenReturn(servletContextHandler); + serviceList.add(service); + when(serviceBuilder.build()).thenReturn(serviceList); + + Map<String, Object> configMap = new HashMap<>(); + String ip = "127.0.0.1"; + String port = "8080"; + configMap.put(GlobalConfiguration.ConfigurationKey.IP.name(), ip); + configMap.put(GlobalConfiguration.ConfigurationKey.PORT.name(), port); + Configuration configuration = mock(Configuration.class); + when(configuration.asMap()).thenReturn(configMap); + + + CoreServerBuilder builder = new CoreServerBuilder(); + builder.setServerConfiguration(configuration); + builder.setServiceBuilder(serviceBuilder); + + Server server = builder.build(); + + ContextHandlerCollection handler = (ContextHandlerCollection) server.getHandler(); + // Expects 2 handlers, 1 service mocked above and 1 swagger ui handler + assertEquals(2, handler.getHandlers().length); + + ServerConnector connector = (ServerConnector) server.getConnectors()[0]; + assertEquals(connector.getPort(), Integer.parseInt(port)); + assertEquals(connector.getHost(), ip); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/apidoc/SwaggerUiHandlerTest.java Wed May 10 13:29:20 2017 -0400 @@ -0,0 +1,69 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.gateway.server.apidoc; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ResourceHandler; +import org.eclipse.jetty.util.resource.Resource; +import org.junit.Test; + +public class SwaggerUiHandlerTest { + @Test + public void testCreateHandler() throws IOException { + ContextHandler contextHandler = new SwaggerUiHandler().createSwaggerResourceHandler(); + + Handler handler = contextHandler.getHandler(); + assertTrue(handler instanceof ResourceHandler); + ResourceHandler resourceHandler = (ResourceHandler) handler; + + assertFalse(resourceHandler.isDirectoriesListed()); + assertTrue(resourceHandler.getWelcomeFiles().length == 1 && resourceHandler.getWelcomeFiles()[0].equals("index.html")); + + Resource r = resourceHandler.getBaseResource(); + + assertTrue(r.getResource("index.html").getFile().exists()); + assertTrue(r.getResource("swagger-ui.js").getFile().exists()); + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/services/CoreServiceBuilderFactoryTest.java Wed May 10 13:29:20 2017 -0400 @@ -0,0 +1,55 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.gateway.server.services; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import org.junit.Test; + +import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; + +public class CoreServiceBuilderFactoryTest { + @Test + public void testCreateWebArchiveServiceBuilder() { + + CoreServiceBuilderFactory coreServiceBuilderFactory = new CoreServiceBuilderFactory(mock(ConfigurationFactory.class)); + CoreServiceBuilder builder = coreServiceBuilderFactory.createBuilder(CoreServiceBuilderFactory.CoreServiceType.WEB_ARCHIVE); + + assertTrue(builder instanceof WebArchiveServiceBuilder); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreServiceTest.java Wed May 10 13:29:20 2017 -0400 @@ -0,0 +1,170 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.gateway.server.services; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jetty.security.ConstraintSecurityHandler; +import org.eclipse.jetty.security.LoginService; +import org.eclipse.jetty.security.SecurityHandler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.UserIdentity; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.webapp.WebAppContext; +import org.junit.Test; +import org.keycloak.adapters.jetty.KeycloakJettyAuthenticator; + +import com.redhat.thermostat.gateway.common.core.config.Configuration; +import com.redhat.thermostat.gateway.common.core.config.ServiceConfiguration; +import com.redhat.thermostat.gateway.server.auth.basic.BasicLoginService; + +public class WebArchiveCoreServiceTest { + + private final String contextPath = "/test"; + private final String warPath = "/test.war"; + private final String keycloakJson = "{\n" + + " \"realm\": \"thermostat\",\n" + + " \"bearer-only\": true,\n" + + " \"auth-server-url\": \"http://172.17.0.2:8080/auth\",\n" + + " \"ssl-required\": \"external\",\n" + + " \"resource\": \"thermostat-bearer\"\n" + + "}"; + + @Test + public void testBasicService() { + Map<String, Object> configurationMap = new HashMap<>(); + + Configuration configuration = mock(Configuration.class); + when(configuration.asMap()).thenReturn(configurationMap); + + WebArchiveCoreService service = new WebArchiveCoreService(contextPath, warPath, configuration); + + ServletContextHandler servletContextHandler = service.createServletContextHandler(mock(Server.class)); + + assertTrue(servletContextHandler instanceof WebAppContext); + WebAppContext webAppContext = (WebAppContext) servletContextHandler; + + assertEquals(webAppContext.getContextPath(), contextPath); + assertEquals(webAppContext.getWar(), warPath); + } + + @Test + public void testServiceWithKeycloakAuth() { + Map<String, Object> configurationMap = new HashMap<>(); + configurationMap.put(ServiceConfiguration.ConfigurationKey.SECURITY_KEYCLOAK.name(), "true"); + configurationMap.put(ServiceConfiguration.ConfigurationKey.KEYCLOAK_CONFIG.name(), keycloakJson); + Configuration configuration = mock(Configuration.class); + when(configuration.asMap()).thenReturn(configurationMap); + + WebArchiveCoreService service = new WebArchiveCoreService(contextPath, warPath, configuration); + + ServletContextHandler servletContextHandler = service.createServletContextHandler(mock(Server.class)); + + assertTrue(servletContextHandler instanceof WebAppContext); + WebAppContext webAppContext = (WebAppContext) servletContextHandler; + + assertEquals(webAppContext.getContextPath(), contextPath); + assertEquals(webAppContext.getWar(), warPath); + + assertEquals(webAppContext.getInitParameter("org.keycloak.json.adapterConfig"), keycloakJson); + assertTrue(Arrays.asList(webAppContext.getSystemClasses()).contains("org.keycloak.")); + + SecurityHandler securityHandler = webAppContext.getSecurityHandler(); + + assertTrue(securityHandler instanceof ConstraintSecurityHandler); + assertTrue(securityHandler.getAuthenticator() instanceof KeycloakJettyAuthenticator); + assertEquals(securityHandler.getRealmName(), "thermostat"); + } + + + @Test + public void testServiceWithBasicAuth() { + Map<String, Object> configurationMap = new HashMap<>(); + configurationMap.put(ServiceConfiguration.ConfigurationKey.SECURITY_BASIC.name(), "true"); + + Map<String, String> basicUsers = new HashMap<>(); + basicUsers.put("user", "password"); + configurationMap.put(ServiceConfiguration.ConfigurationKey.SECURITY_BASIC_USERS.name(), basicUsers); + + Configuration configuration = mock(Configuration.class); + when(configuration.asMap()).thenReturn(configurationMap); + + WebArchiveCoreService service = new WebArchiveCoreService(contextPath, warPath, configuration); + + ServletContextHandler servletContextHandler = service.createServletContextHandler(mock(Server.class)); + + assertTrue(servletContextHandler instanceof WebAppContext); + WebAppContext webAppContext = (WebAppContext) servletContextHandler; + assertEquals(webAppContext.getContextPath(), contextPath); + assertEquals(webAppContext.getWar(), warPath); + + SecurityHandler securityHandler = webAppContext.getSecurityHandler(); + assertTrue(securityHandler.getLoginService() instanceof BasicLoginService); + LoginService loginService =securityHandler.getLoginService(); + UserIdentity u = loginService.login("user", "password"); + assertTrue(loginService.validate(u)); + } + + @Test + public void testServiceWithWebSockets() { + Map<String, Object> configurationMap = new HashMap<>(); + configurationMap.put(ServiceConfiguration.ConfigurationKey.WEBSOCKETS.name(), "true"); + Configuration configuration = mock(Configuration.class); + when(configuration.asMap()).thenReturn(configurationMap); + + WebArchiveCoreService service = new WebArchiveCoreService(contextPath, warPath, configuration); + + Server server = mock(Server.class); + ServletContextHandler servletContextHandler = service.createServletContextHandler(server); + + assertTrue(servletContextHandler instanceof WebAppContext); + WebAppContext webAppContext = (WebAppContext) servletContextHandler; + + + assertEquals(webAppContext.getContextPath(), contextPath); + assertEquals(webAppContext.getWar(), warPath); + + assertEquals(webAppContext.getServer(), server); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilderTest.java Wed May 10 13:29:20 2017 -0400 @@ -0,0 +1,77 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.gateway.server.services; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +import com.redhat.thermostat.gateway.common.core.config.Configuration; +import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; + +public class WebArchiveServiceBuilderTest { + @Test + public void testBuildWebArchiveCoreServices() { + ConfigurationFactory configurationFactory = mock(ConfigurationFactory.class); + when(configurationFactory.createServiceConfiguration(anyString())).thenReturn(mock(Configuration.class)); + + WebArchiveServiceBuilder serviceBuilder = new WebArchiveServiceBuilder(configurationFactory); + Configuration configuration = mock(Configuration.class); + Map<String, Object> configMap = new HashMap<>(); + configMap.put("/context0", "serviceName0"); + configMap.put("/context1", "serviceName1"); + + when(configuration.asMap()).thenReturn(configMap); + serviceBuilder.setConfiguration(configuration); + + List<CoreService> services = serviceBuilder.build(); + assertEquals(2, services.size()); + + assertTrue(services.get(0) instanceof WebArchiveCoreService); + assertTrue(services.get(1) instanceof WebArchiveCoreService); + } + + +}