private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext(); int mongoPort = SocketUtils.findAvailableTcpPort(); EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=" + mongoPort); if (configuredVersion != null) { EnvironmentTestUtils.addEnvironment(this.context, "spring.mongodb.embedded.version=" + configuredVersion); } this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class); this.context.refresh(); MongoTemplate mongo = this.context.getBean(MongoTemplate.class); CommandResult buildInfo = mongo.executeCommand("{ buildInfo: 1 }"); assertThat(buildInfo.getString("version")).isEqualTo(expectedVersion); }
public void testFieldNamingStrategy(String strategy, Class<? extends FieldNamingStrategy> expectedType) { this.context = new AnnotationConfigApplicationContext(); if (strategy != null) { EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.field-naming-strategy:" + strategy); } this.context.register(PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); MongoMappingContext mappingContext = this.context .getBean(MongoMappingContext.class); FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils .getField(mappingContext, "fieldNamingStrategy"); assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType); }
private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext(); int mongoPort = SocketUtils.findAvailableTcpPort(); EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=" + mongoPort); if (configuredVersion != null) { EnvironmentTestUtils.addEnvironment(this.context, "spring.mongodb.embedded.version=" + configuredVersion); } this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class); this.context.refresh(); MongoTemplate mongo = this.context.getBean(MongoTemplate.class); CommandResult buildInfo = mongo.executeCommand("{ buildInfo: 1 }"); assertThat(buildInfo.getString("version"), equalTo(expectedVersion)); }
public void testFieldNamingStrategy(String strategy, Class<? extends FieldNamingStrategy> expectedType) { this.context = new AnnotationConfigApplicationContext(); if (strategy != null) { EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.field-naming-strategy:" + strategy); } this.context.register(PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); MongoMappingContext mappingContext = this.context .getBean(MongoMappingContext.class); FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils .getField(mappingContext, "fieldNamingStrategy"); assertEquals(expectedType, fieldNamingStrategy.getClass()); }
@Test public void mongoHealthIndicator() { this.context.register(MongoAutoConfiguration.class, ManagementServerProperties.class, MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(MongoHealthIndicator.class); }
@Test public void notMongoHealthIndicator() { this.context.register(MongoAutoConfiguration.class, ManagementServerProperties.class, MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.mongo.enabled:false", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void combinedHealthIndicator() { this.context.register(MongoAutoConfiguration.class, RedisAutoConfiguration.class, MongoDataAutoConfiguration.class, SolrAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(4); }
@Test public void indicatorExists() { this.context = new AnnotationConfigApplicationContext( PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EndpointAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); assertThat(this.context.getBeanNamesForType(MongoTemplate.class).length) .isEqualTo(1); MongoHealthIndicator healthIndicator = this.context .getBean(MongoHealthIndicator.class); assertThat(healthIndicator).isNotNull(); }
@Test public void mongoSessionStore() { load(Arrays.asList(EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class), "spring.session.store-type=mongo", "spring.data.mongodb.port=0"); validateSessionRepository(MongoOperationsSessionRepository.class); }
@Test public void mongoSessionStoreWithCustomizations() { load(Arrays.asList(EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class), "spring.session.store-type=mongo", "spring.data.mongodb.port=0", "spring.session.mongo.collection-name=foobar"); MongoOperationsSessionRepository repository = validateSessionRepository( MongoOperationsSessionRepository.class); assertThat(new DirectFieldAccessor(repository).getPropertyValue("collectionName")) .isEqualTo("foobar"); }
@Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { List<String> names = new ArrayList<String>(); for (Class<?> type : new Class<?>[] { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class }) { names.add(type.getName()); } return names.toArray(new String[0]); }
@Test public void templateExists() { this.context = new AnnotationConfigApplicationContext( PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); assertThat(this.context.getBeanNamesForType(MongoTemplate.class).length) .isEqualTo(1); }
@Test public void gridFsTemplateExists() { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.gridFsDatabase:grid"); this.context.register(PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBeanNamesForType(GridFsTemplate.class).length) .isEqualTo(1); }
@Test public void customConversions() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(CustomConversionsConfig.class); this.context.register(PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); MongoTemplate template = this.context.getBean(MongoTemplate.class); assertThat(template.getConverter().getConversionService().canConvert(Mongo.class, Boolean.class)).isTrue(); }
@Test public void usesAutoConfigurationPackageToPickUpDocumentTypes() { this.context = new AnnotationConfigApplicationContext(); String cityPackage = City.class.getPackage().getName(); AutoConfigurationPackages.register(this.context, cityPackage); this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); assertDomainTypesDiscovered(this.context.getBean(MongoMappingContext.class), City.class); }
@Test @SuppressWarnings("unchecked") public void entityScanShouldSetInitialEntitySet() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(EntityScanConfig.class, PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); MongoMappingContext mappingContext = this.context .getBean(MongoMappingContext.class); Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils .getField(mappingContext, "initialEntitySet"); assertThat(initialEntitySet).containsOnly(City.class, Country.class); }
private void prepareApplicationContext(Class<?>... configurationClasses) { this.context = new AnnotationConfigApplicationContext(); this.context.register(configurationClasses); this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); }
@Test public void testOrder() throws Exception { TestAutoConfigurationSorter sorter = new TestAutoConfigurationSorter( new CachingMetadataReaderFactory()); Collection<String> classNames = new ArrayList<String>(); classNames.add(MongoAutoConfiguration.class.getName()); classNames.add(DataSourceAutoConfiguration.class.getName()); classNames.add(MongoRepositoriesAutoConfiguration.class.getName()); classNames.add(JpaRepositoriesAutoConfiguration.class.getName()); classNames.add(CloudAutoConfiguration.class.getName()); List<String> ordered = sorter.getInPriorityOrder(classNames); assertThat(ordered.get(0)).isEqualTo(CloudAutoConfiguration.class.getName()); }
@Test public void mongoHealthIndicator() { this.context.register(MongoAutoConfiguration.class, ManagementServerProperties.class, MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertEquals(1, beans.size()); assertEquals(MongoHealthIndicator.class, beans.values().iterator().next().getClass()); }
@Test public void notMongoHealthIndicator() { this.context.register(MongoAutoConfiguration.class, ManagementServerProperties.class, MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.mongo.enabled:false", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertEquals(1, beans.size()); assertEquals(ApplicationHealthIndicator.class, beans.values().iterator().next().getClass()); }
@Test public void combinedHealthIndicator() { this.context.register(MongoAutoConfiguration.class, RedisAutoConfiguration.class, MongoDataAutoConfiguration.class, SolrAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertEquals(4, beans.size()); }
@Test public void indicatorExists() { this.context = new AnnotationConfigApplicationContext( PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EndpointAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); assertEquals(1, this.context.getBeanNamesForType(MongoTemplate.class).length); MongoHealthIndicator healthIndicator = this.context .getBean(MongoHealthIndicator.class); assertNotNull(healthIndicator); }
@Test public void templateExists() { this.context = new AnnotationConfigApplicationContext( PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); assertEquals(1, this.context.getBeanNamesForType(MongoTemplate.class).length); }
@Test public void gridFsTemplateExists() { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.gridFsDatabase:grid"); this.context.register(PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); assertEquals(1, this.context.getBeanNamesForType(GridFsTemplate.class).length); }