Commit 2ecbfec8 authored by Daniel Gerhardt's avatar Daniel Gerhardt

Migrate remaining Spring XML config to Java

parent acf18781
......@@ -23,12 +23,14 @@ import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Assigns a session to the {@link de.thm.arsnova.services.UserSessionService} whenever a user joins a
* session.
*/
@Aspect
@Service
public class UserSessionAspect {
@Autowired
......
......@@ -17,12 +17,16 @@
*/
package de.thm.arsnova.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import de.thm.arsnova.ImageUtils;
import de.thm.arsnova.connector.client.ConnectorClient;
import de.thm.arsnova.connector.client.ConnectorClientImpl;
import de.thm.arsnova.socket.ARSnovaSocket;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
import de.thm.arsnova.web.CacheControlInterceptorHandler;
import de.thm.arsnova.web.CorsFilter;
import de.thm.arsnova.web.DeprecatedApiInterceptorHandler;
import de.thm.arsnova.web.ResponseInterceptorHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
......@@ -30,24 +34,58 @@ import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Loads property file and configures non-security related beans and components.
*
* expose-proxy for AspectJ is needed to access the proxy object via AopContext.currentProxy() in CouchDBDao. It might
* have a negative impact on performance but is needed for caching until a better solution is implemented (e.g. use of
* AspectJ's weaving).
*/
@EnableWebMvc
@ComponentScan({
"de.thm.arsnova.aop",
"de.thm.arsnova.cache",
"de.thm.arsnova.controller",
"de.thm.arsnova.domain",
"de.thm.arsnova.dao",
"de.thm.arsnova.events",
"de.thm.arsnova.security",
"de.thm.arsnova.services",
"de.thm.arsnova.web"})
@Configuration
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableCaching
public class ExtraConfig extends WebMvcConfigurerAdapter {
@EnableWebMvc
@PropertySource(
value = {"classpath:arsnova.properties.example", "file:/etc/arsnova/arsnova.properties"},
ignoreResourceNotFound = true
)
public class AppConfig extends WebMvcConfigurerAdapter {
@Autowired
private Environment env;
......@@ -62,18 +100,102 @@ public class ExtraConfig extends WebMvcConfigurerAdapter {
@Value(value = "${socketio.ssl.jks-file:}") private String socketKeystore;
@Value(value = "${socketio.ssl.jks-password:}") private String socketKeystorePassword;
@Value(value = "${security.cors.origins:}") private String[] corsOrigins;
@Value(value = "${mail.host}") private String mailHost;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(stringMessageConverter());
converters.add(jsonMessageConverter());
//converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.mediaType("json", MediaType.APPLICATION_JSON_UTF8);
configurer.mediaType("xml", MediaType.APPLICATION_XML);
configurer.mediaType("html", MediaType.TEXT_HTML);
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
configurer.favorParameter(true);
configurer.favorPathExtension(false);
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.viewResolver(new InternalResourceViewResolver());
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(cacheControlInterceptorHandler());
registry.addInterceptor(deprecatedApiInterceptorHandler());
registry.addInterceptor(responseInterceptorHandler());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger.json").addResourceLocations("classpath:/");
registry.addResourceHandler("/**").addResourceLocations("/");
}
@Bean
public CacheControlInterceptorHandler cacheControlInterceptorHandler() {
return new CacheControlInterceptorHandler();
}
@Bean
public DeprecatedApiInterceptorHandler deprecatedApiInterceptorHandler() {
return new DeprecatedApiInterceptorHandler();
}
@Bean
public ResponseInterceptorHandler responseInterceptorHandler() {
return new ResponseInterceptorHandler();
}
private static int testPortOffset = 0;
@Bean
public StringHttpMessageConverter stringMessageConverter() {
StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
messageConverter.setSupportedMediaTypes(mediaTypes);
return messageConverter;
}
@Bean
public MappingJackson2HttpMessageConverter jsonMessageConverter() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
builder.defaultViewInclusion(false);
builder.indentOutput(true).simpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
return new MappingJackson2HttpMessageConverter(builder.build());
}
// @Bean
// public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
// final PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
// configurer.setLocations(
// new ClassPathResource("arsnova.properties.example"),
// new FileSystemResource("/etc/arsnova/arsnova.properties")
// );
// configurer.setIgnoreResourceNotFound(true);
// configurer.setIgnoreUnresolvablePlaceholders(false);
//
// return configurer;
// }
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocations(
new ClassPathResource("arsnova.properties.example"),
new FileSystemResource("/etc/arsnova/arsnova.properties")
);
configurer.setIgnoreResourceNotFound(true);
// configurer.setLocations(
// new ClassPathResource("arsnova.properties.example"),
// new FileSystemResource("/etc/arsnova/arsnova.properties")
// );
// configurer.setIgnoreResourceNotFound(true);
configurer.setIgnoreUnresolvablePlaceholders(false);
return configurer;
}
......@@ -115,24 +237,19 @@ public class ExtraConfig extends WebMvcConfigurerAdapter {
return socketServer;
}
@Profile("test")
@Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer")
public ARSnovaSocket socketTestServer() {
final int testSocketPort = 1234 + testPortOffset++ % 10;
final ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer();
socketServer.setHostIp(socketAddress);
socketServer.setPortNumber(socketPort + testSocketPort);
socketServer.setUseSSL(!socketKeystore.isEmpty());
socketServer.setKeystore(socketKeystore);
socketServer.setStorepass(socketKeystorePassword);
return socketServer;
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
@Bean
public JavaMailSenderImpl mailSender() {
final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(mailHost);
return mailSender;
}
@Bean
public ImageUtils imageUtils() {
return new ImageUtils();
......
......@@ -51,8 +51,9 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
......@@ -79,7 +80,8 @@ import java.util.concurrent.ConcurrentLinkedQueue;
* @see <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html">Spring Framework's Cache Abstraction</a>
* @see <a href="https://github.com/thm-projects/arsnova-backend/wiki/Caching">Caching in ARSnova explained</a>
*/
@Component("databaseDao")
@Profile("!test")
@Service("databaseDao")
public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware {
private static final int BULK_PARTITION_SIZE = 500;
......
......@@ -21,13 +21,13 @@ import de.thm.arsnova.events.NovaEvent;
import de.thm.arsnova.events.NovaEventVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* An external Listener is required because otherwise the event methods are not called through a Spring proxy.
* This would result in Spring method annotations not working.
*/
@Component
@Service
public class ARSnovaSocketListener implements ApplicationListener<NovaEvent> {
@Autowired
......
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- ARSnova Servlet Context -->
<context:component-scan base-package="de.thm.arsnova.aop,de.thm.arsnova.controller,de.thm.arsnova.web" />
<mvc:annotation-driven
content-negotiation-manager="mvcContentNegotiationManager" />
<mvc:interceptors>
<bean class="de.thm.arsnova.web.CacheControlInterceptorHandler" />
<bean class="de.thm.arsnova.web.DeprecatedApiInterceptorHandler" />
<bean class="de.thm.arsnova.web.ResponseInterceptorHandler" />
</mvc:interceptors>
<bean id="requestMappingHandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" primary="true"></bean>
<aop:aspectj-autoproxy />
<!-- Enables swgger ui-->
<mvc:resources mapping="swagger.json" location="classpath:/"/>
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:ignoreUnresolvablePlaceholders="false" p:ignoreResourceNotFound="true">
<property name="locations">
<list>
<value>classpath:arsnova.properties.example</value>
<value>file:///etc/arsnova/arsnova.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8" />
</bean>
<bean id="mvcContentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<value>
html=text/html
json=application/json
xml=application/xml
</value>
</property>
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/**" location="/" />
<!-- -->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<!-- -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="prefixJson" value="false" />
</bean>
</list>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd">
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:ignoreUnresolvablePlaceholders="false" p:ignoreResourceNotFound="true">
<property name="locations">
<list>
<value>classpath:arsnova.properties.example</value>
<value>file:///etc/arsnova/arsnova.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8" />
</bean>
<context:component-scan base-package="de.thm.arsnova.dao,de.thm.arsnova.cache,de.thm.arsnova.events,de.thm.arsnova.security,de.thm.arsnova.services,de.thm.arsnova.config,de.thm.arsnova.domain" />
<context:annotation-config />
<task:annotation-driven />
<!--
expose-proxy is needed to access the proxy object via AopContext.currentProxy() in CouchDBDao. It might have a
negative impact on performance but is needed for caching until a better solution is implemented (e.g. use of
AspectJ's weaving).
-->
<aop:aspectj-autoproxy expose-proxy="true">
<aop:include name="userSessionAspect" />
</aop:aspectj-autoproxy>
<bean id="userSessionAspect" class="de.thm.arsnova.aop.UserSessionAspect" />
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
</bean>
<bean id="databaseDao" class="de.thm.arsnova.dao.CouchDBDao" />
<bean id="socketListener" class="de.thm.arsnova.socket.ARSnovaSocketListener" />
</beans>
......@@ -3,10 +3,15 @@
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring-main.xml
de.thm.arsnova.config.AppConfig
de.thm.arsnova.config.SecurityConfig
</param-value>
</context-param>
<context-param>
......@@ -16,12 +21,6 @@
<servlet>
<servlet-name>arsnova</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/arsnova-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
......
......@@ -17,6 +17,9 @@
*/
package de.thm.arsnova;
import de.thm.arsnova.config.AppConfig;
import de.thm.arsnova.config.TestAppConfig;
import de.thm.arsnova.config.TestSecurityConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
......@@ -30,12 +33,7 @@ import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
"file:src/test/resources/test-config.xml",
"file:src/test/resources/test-socketioconfig.xml"
})
@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class, TestSecurityConfig.class})
@ActiveProfiles("test")
public class ImageUtilsTest {
......
......@@ -23,6 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
......@@ -30,14 +31,9 @@ import static org.junit.Assert.assertNull;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
"file:src/test/resources/test-config.xml",
"file:src/test/resources/test-socketioconfig.xml"
})
@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class, TestSecurityConfig.class})
@ActiveProfiles("test")
public class ExtraConfigTest {
public class AppConfigTest extends AbstractJUnit4SpringContextTests {
@Autowired(required = false)
private ConnectorClient connectorClient;
......
package de.thm.arsnova.config;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.dao.StubDatabaseDao;
import de.thm.arsnova.services.StubUserService;
import de.thm.arsnova.socket.ARSnovaSocket;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.SimpleThreadScope;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ComponentScan({
"de.thm.arsnova.aop",
"de.thm.arsnova.cache",
"de.thm.arsnova.controller",
"de.thm.arsnova.domain",
"de.thm.arsnova.dao",
"de.thm.arsnova.events",
"de.thm.arsnova.security",
"de.thm.arsnova.services",
"de.thm.arsnova.web"})
@Configuration
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableCaching
@EnableWebMvc
@PropertySource(
value = {"classpath:arsnova.properties.example", "file:/etc/arsnova/arsnova.properties"},
ignoreResourceNotFound = true
)
@Profile("test")
public class TestAppConfig {
private static int testPortOffset = 0;
@Value("${socketio.bind-address}") private String socketAddress;
@Value("${socketio.port}") private int socketPort;
@Bean
public MockServletContext servletContext() {
return new MockServletContext();
}
@Bean
public CustomScopeConfigurer customScopeConfigurer() {
final CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope("session", new SimpleThreadScope());
return configurer;
}
@Bean
public IDatabaseDao databaseDao() {
return new StubDatabaseDao();
}
@Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer")
public ARSnovaSocket socketTestServer() {
final int testSocketPort = 1234 + testPortOffset++ % 10;
final ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer();
socketServer.setHostIp(socketAddress);
socketServer.setPortNumber(socketPort + testSocketPort);
return socketServer;
}
@Bean
public StubUserService stubUserService() {
return new StubUserService();
}
}
......@@ -17,17 +17,30 @@
*/
package de.thm.arsnova.controller;
import de.thm.arsnova.config.AppConfig;
import de.thm.arsnova.config.TestAppConfig;
import de.thm.arsnova.config.TestSecurityConfig;
import de.thm.arsnova.services.StubUserService;
import org.junit.After;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractControllerTest {
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class, TestSecurityConfig.class})
@ActiveProfiles("test")
public abstract class AbstractControllerTest extends AbstractJUnit4SpringContextTests {
@Autowired protected StubUserService userService;
......
......@@ -21,39 +21,18 @@ import de.thm.arsnova.connector.client.ConnectorClient;
import de.thm.arsnova.services.StubUserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
"file:src/test/resources/test-config.xml",
"file:src/test/resources/test-socketioconfig.xml"
})
@ActiveProfiles("test")
public class CourseControllerTest {
public class CourseControllerTest extends AbstractControllerTest {
private MockMvc mockMvc;
......@@ -69,17 +48,6 @@ public class CourseControllerTest {
@Mock
private ConnectorClient connectorClient;
private void setAuthenticated(final boolean isAuthenticated, final String username) {
if (isAuthenticated) {
final List<GrantedAuthority> ga = new ArrayList<>();
final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga);
SecurityContextHolder.getContext().setAuthentication(token);
userService.setUserAuthenticated(isAuthenticated, username);
} else {
userService.setUserAuthenticated(isAuthenticated);
}
}
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
......
......@@ -20,13 +20,8 @@ package de.thm.arsnova.controller;
import de.thm.arsnova.services.StubUserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
......@@ -34,16 +29,7 @@ import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml",
"file:src/main/webapp/WEB-INF/sprin