/*
 * Copyright 2017 Sergei Munovarov
 *
 * 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 xyz.randomcode.dropwizard_morphia.health;

import com.codahale.metrics.health.HealthCheck;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import xyz.randomcode.dropwizard_morphia.BaseMongoTest;
import xyz.randomcode.dropwizard_morphia.dummy.DummyEntity;

import static org.assertj.core.api.Assertions.assertThat;


public class MongoHealthCheckTest extends BaseMongoTest {

    private static Datastore datastore;


    @BeforeClass
    public static void setUpAll() throws Exception {
        MongoClient mongoClient = new MongoClient(new MongoClientURI(String.format("mongodb://localhost:%d", port)));
        datastore = new Morphia().map(DummyEntity.class).createDatastore(mongoClient, "test");
    }


    @AfterClass
    public static void tearDownAll() throws Exception {
        datastore.getMongo().close();
    }


    @Test
    public void checkHealthyResult() throws Exception {
        MongoHealthCheck healthCheck = new MongoHealthCheck(datastore);
        HealthCheck.Result result = healthCheck.check();
        assertThat(result.isHealthy()).isTrue();
    }
}

@@ -760,16 +760,51 @@
 entURI;%0A
+import com.mongodb.MongoException;%0A
 import o
@@ -820,24 +820,24 @@
 AfterClass;%0A
-
 import org.j
@@ -1126,16 +1126,95 @@
 rtThat;%0A
+import static org.mockito.Mockito.spy;%0Aimport static org.mockito.Mockito.when;%0A
 %0A%0Apublic
@@ -1908,24 +1908,24 @@
 ck.check();%0A
-
         asse
@@ -1967,10 +1967,439 @@
 ;%0A    %7D%0A
+%0A%0A    @Test%0A    public void checkUnhealthyResult() throws Exception %7B%0A        Datastore spy = spy(datastore);%0A        when(spy.getDB()).thenThrow(new MongoException(%22error%22));%0A%0A        MongoHealthCheck healthCheck = new MongoHealthCheck(spy);%0A        HealthCheck.Result result = healthCheck.check();%0A        assertThat(result.isHealthy()).isFalse();%0A        assertThat(result.getMessage()).isNotEmpty().isEqualTo(%22error%22);%0A    %7D%0A
 %7D%0A

Add health check test