From 33321d2e53b492eb7a2d4ca928dac2dd13e62f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vedran=20Pavi=C4=87?= Date: Mon, 2 May 2016 22:11:54 +0200 Subject: [PATCH] Polish MongoHttpSessionConfigurationTests This improves test coverage and makes the MongoHttpSessionConfigurationTests more consistent with analogous tests for other repositories. Fixes gh-503 --- .../MongoHttpSessionConfigurationTest.java | 159 ------------- .../MongoHttpSessionConfigurationTests.java | 216 ++++++++++++++++++ 2 files changed, 216 insertions(+), 159 deletions(-) delete mode 100644 spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java create mode 100644 spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTests.java diff --git a/spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java b/spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java deleted file mode 100644 index af802ed..0000000 --- a/spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * 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 org.springframework.session.data.mongo.config.annotation.web.http; - -import java.net.UnknownHostException; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.core.IndexOperations; -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.session.data.mongo.MongoOperationsSessionRepository; -import org.springframework.test.util.ReflectionTestUtils; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.BDDMockito.given; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; - -/** - * @author Eddú Meléndez - */ -public class MongoHttpSessionConfigurationTest { - - private AnnotationConfigApplicationContext context; - - @Before - public void before() { - this.context = new AnnotationConfigApplicationContext(); - } - - @After - public void after() { - if (this.context != null) { - this.context.close(); - } - } - - @Test - public void defaultCollectionName() { - registerAndRefresh(DefaultConfiguration.class); - MongoHttpSessionConfiguration session = this.context - .getBean(MongoHttpSessionConfiguration.class); - assertThat(session).isNotNull(); - assertThat(ReflectionTestUtils.getField(session, "collectionName")).isEqualTo( - "sessions"); - MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class); - assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo( - "sessions"); - } - - @Test - public void customCollectionName() { - registerAndRefresh(CustomCollectionNameConfiguration.class); - MongoHttpSessionConfiguration session = this.context - .getBean(MongoHttpSessionConfiguration.class); - assertThat(session).isNotNull(); - assertThat(ReflectionTestUtils.getField(session, "collectionName")).isEqualTo( - "testSessions"); - - MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class); - assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo( - "testSessions"); - } - - @Test - public void setCustomCollectionName() { - registerAndRefresh(CustomConfiguration.class, CustomCollectionNameSetConfiguration.class); - MongoHttpSessionConfiguration session = this.context - .getBean(MongoHttpSessionConfiguration.class); - assertThat(session).isNotNull(); - assertThat(ReflectionTestUtils.getField(session, "collectionName")).isEqualTo( - "customSession"); - MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class); - assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo( - "customSession"); - } - - @Test - public void customMaxInactiveIntervalInSeconds() { - registerAndRefresh(CustomConfiguration.class, CustomMaxInactiveIntervalInSecondsSetConfiguration.class); - MongoHttpSessionConfiguration session = this.context - .getBean(MongoHttpSessionConfiguration.class); - assertThat(session).isNotNull(); - assertThat(ReflectionTestUtils.getField(session, "maxInactiveIntervalInSeconds")).isEqualTo( - 10); - MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class); - assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")).isEqualTo( - 10); - } - - private void registerAndRefresh(Class... annotatedClasses) { - this.context.register(annotatedClasses); - this.context.refresh(); - } - - static class BaseConfiguration { - - @Bean - public MongoOperations mongoOperations() throws UnknownHostException { - MongoOperations mongoOperations = mock(MongoOperations.class); - IndexOperations indexOperations = mock(IndexOperations.class); - given(mongoOperations.indexOps(anyString())).willReturn(indexOperations); - return mongoOperations; - } - - } - - @Configuration - @EnableMongoHttpSession(collectionName = "testSessions") - static class CustomCollectionNameConfiguration extends BaseConfiguration { - } - - @Configuration - @EnableMongoHttpSession - static class DefaultConfiguration extends BaseConfiguration { - } - - @Configuration - static class CustomCollectionNameSetConfiguration extends MongoHttpSessionConfiguration { - - CustomCollectionNameSetConfiguration() { - setCollectionName("customSession"); - } - - } - - @Configuration - static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends MongoHttpSessionConfiguration { - - CustomMaxInactiveIntervalInSecondsSetConfiguration() { - setMaxInactiveIntervalInSeconds(10); - } - - } - - @Configuration - static class CustomConfiguration extends BaseConfiguration { - - } - -} diff --git a/spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTests.java b/spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTests.java new file mode 100644 index 0000000..b58b9b8 --- /dev/null +++ b/spring-session/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTests.java @@ -0,0 +1,216 @@ +/* + * Copyright 2014-2016 the original author or authors. + * + * 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 org.springframework.session.data.mongo.config.annotation.web.http; + +import java.net.UnknownHostException; + +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import org.springframework.beans.factory.UnsatisfiedDependencyException; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.mongodb.core.IndexOperations; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.session.data.mongo.AbstractMongoSessionConverter; +import org.springframework.session.data.mongo.MongoOperationsSessionRepository; +import org.springframework.test.util.ReflectionTestUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link MongoHttpSessionConfiguration}. + * + * @author Eddú Meléndez + * @author Vedran Pavic + */ +public class MongoHttpSessionConfigurationTests { + + private static final String COLLECTION_NAME = "testSessions"; + + private static final int MAX_INACTIVE_INTERVAL_IN_SECONDS = 600; + + @Rule + public final ExpectedException thrown = ExpectedException.none(); + + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + + @After + public void after() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void noMongoOperationsConfiguration() { + this.thrown.expect(UnsatisfiedDependencyException.class); + this.thrown.expectMessage("mongoSessionRepository"); + + registerAndRefresh(EmptyConfiguration.class); + } + + @Test + public void defaultConfiguration() { + registerAndRefresh(DefaultConfiguration.class); + + assertThat(this.context.getBean(MongoOperationsSessionRepository.class)) + .isNotNull(); + } + + @Test + public void customCollectionName() { + registerAndRefresh(CustomCollectionNameConfiguration.class); + + MongoOperationsSessionRepository repository = this.context + .getBean(MongoOperationsSessionRepository.class); + assertThat(repository).isNotNull(); + assertThat(ReflectionTestUtils.getField(repository, "collectionName")) + .isEqualTo(COLLECTION_NAME); + } + + @Test + public void setCustomCollectionName() { + registerAndRefresh(CustomCollectionNameSetConfiguration.class); + + MongoHttpSessionConfiguration session = this.context + .getBean(MongoHttpSessionConfiguration.class); + assertThat(session).isNotNull(); + assertThat(ReflectionTestUtils.getField(session, "collectionName")) + .isEqualTo(COLLECTION_NAME); + } + + @Test + public void customMaxInactiveIntervalInSeconds() { + registerAndRefresh(CustomMaxInactiveIntervalInSecondsConfiguration.class); + + MongoOperationsSessionRepository repository = this.context + .getBean(MongoOperationsSessionRepository.class); + assertThat(repository).isNotNull(); + assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")) + .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + } + + @Test + public void setCustomMaxInactiveIntervalInSeconds() { + registerAndRefresh(CustomMaxInactiveIntervalInSecondsSetConfiguration.class); + + MongoOperationsSessionRepository repository = this.context + .getBean(MongoOperationsSessionRepository.class); + assertThat(repository).isNotNull(); + assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")) + .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + } + + @Test + public void setCustomSessionConverterConfiguration() { + registerAndRefresh(CustomSessionConverterConfiguration.class); + + MongoOperationsSessionRepository repository = this.context + .getBean(MongoOperationsSessionRepository.class); + AbstractMongoSessionConverter mongoSessionConverter = this.context + .getBean(AbstractMongoSessionConverter.class); + assertThat(repository).isNotNull(); + assertThat(mongoSessionConverter).isNotNull(); + assertThat(ReflectionTestUtils.getField(repository, "mongoSessionConverter")) + .isEqualTo(mongoSessionConverter); + } + + private void registerAndRefresh(Class... annotatedClasses) { + this.context.register(annotatedClasses); + this.context.refresh(); + } + + @Configuration + @EnableMongoHttpSession + static class EmptyConfiguration { + + } + + static class BaseConfiguration { + + @Bean + public MongoOperations mongoOperations() throws UnknownHostException { + MongoOperations mongoOperations = mock(MongoOperations.class); + IndexOperations indexOperations = mock(IndexOperations.class); + given(mongoOperations.indexOps(anyString())).willReturn(indexOperations); + return mongoOperations; + } + + } + + @Configuration + @EnableMongoHttpSession + static class DefaultConfiguration extends BaseConfiguration { + + } + + @Configuration + static class MongoConfiguration extends BaseConfiguration { + + } + + @Configuration + @EnableMongoHttpSession(collectionName = COLLECTION_NAME) + static class CustomCollectionNameConfiguration extends BaseConfiguration { + + } + + @Configuration + @Import(MongoConfiguration.class) + static class CustomCollectionNameSetConfiguration extends MongoHttpSessionConfiguration { + + CustomCollectionNameSetConfiguration() { + setCollectionName(COLLECTION_NAME); + } + + } + + @Configuration + @EnableMongoHttpSession(maxInactiveIntervalInSeconds = MAX_INACTIVE_INTERVAL_IN_SECONDS) + static class CustomMaxInactiveIntervalInSecondsConfiguration extends BaseConfiguration { + + } + + @Configuration + @Import(MongoConfiguration.class) + static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends MongoHttpSessionConfiguration { + + CustomMaxInactiveIntervalInSecondsSetConfiguration() { + setMaxInactiveIntervalInSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS); + } + + } + + @Configuration + @Import(MongoConfiguration.class) + static class CustomSessionConverterConfiguration extends MongoHttpSessionConfiguration { + + @Bean + public AbstractMongoSessionConverter mongoSessionConverter() { + return mock(AbstractMongoSessionConverter.class); + } + + } + +}