Search in sources :

Example 1 with MongoTemplate

use of org.springframework.data.mongodb.core.MongoTemplate in project spring-boot by spring-projects.

the class MongoDataAutoConfigurationTests method customConversions.

@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();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Test(org.junit.Test)

Example 2 with MongoTemplate

use of org.springframework.data.mongodb.core.MongoTemplate in project spring-boot by spring-projects.

the class MongoHealthIndicatorTests method mongoIsUp.

@Test
public void mongoIsUp() throws Exception {
    Document commandResult = mock(Document.class);
    given(commandResult.getString("version")).willReturn("2.6.4");
    MongoTemplate mongoTemplate = mock(MongoTemplate.class);
    given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn(commandResult);
    MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
    Health health = healthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertThat(health.getDetails().get("version")).isEqualTo("2.6.4");
    verify(commandResult).getString("version");
    verify(mongoTemplate).executeCommand("{ buildInfo: 1 }");
}
Also used : Document(org.bson.Document) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Test(org.junit.Test)

Example 3 with MongoTemplate

use of org.springframework.data.mongodb.core.MongoTemplate in project spring-boot by spring-projects.

the class EmbeddedMongoAutoConfigurationTests method assertVersionConfiguration.

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);
    Document buildInfo = mongo.executeCommand("{ buildInfo: 1 }");
    assertThat(buildInfo.getString("version")).isEqualTo(expectedVersion);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Document(org.bson.Document)

Example 4 with MongoTemplate

use of org.springframework.data.mongodb.core.MongoTemplate in project spring-boot by spring-projects.

the class MongoHealthIndicatorTests method mongoIsDown.

@Test
public void mongoIsDown() throws Exception {
    MongoTemplate mongoTemplate = mock(MongoTemplate.class);
    given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow(new MongoException("Connection failed"));
    MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
    Health health = healthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat((String) health.getDetails().get("error")).contains("Connection failed");
    verify(mongoTemplate).executeCommand("{ buildInfo: 1 }");
}
Also used : MongoException(com.mongodb.MongoException) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Test(org.junit.Test)

Aggregations

MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)4 Test (org.junit.Test)3 Document (org.bson.Document)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 MongoException (com.mongodb.MongoException)1