Skip to content

Commit

Permalink
Merge pull request #272 from pagopa/release-uat
Browse files Browse the repository at this point in the history
fix: promote to Main
  • Loading branch information
stedelia authored Oct 18, 2024
2 parents f2e07fd + 18ea060 commit 6da1201
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package it.gov.pagopa.common.config;

import org.bson.Document;
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

public class CustomReactiveMongoHealthIndicator extends AbstractReactiveHealthIndicator {
private final ReactiveMongoTemplate reactiveMongoTemplate;
public CustomReactiveMongoHealthIndicator(ReactiveMongoTemplate reactiveMongoTemplate) {
super("Mongo health check failed");
Assert.notNull(reactiveMongoTemplate, "ReactiveMongoTemplate must not be null");
this.reactiveMongoTemplate = reactiveMongoTemplate;
}

@Override
protected Mono<Health> doHealthCheck(Health.Builder builder) {
Mono<Document> buildInfo = this.reactiveMongoTemplate.executeCommand("{ isMaster: 1 }");
return buildInfo.map(document -> up(builder, document));
}

private Health up(Health.Builder builder, Document document) {
return builder.up().withDetail("maxWireVersion", document.getInteger("maxWireVersion")).build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package it.gov.pagopa.common.mongo.config;

import it.gov.pagopa.common.config.CustomReactiveMongoHealthIndicator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;

@Configuration
public class MongoHealthConfig {
@Bean
public CustomReactiveMongoHealthIndicator customMongoHealthIndicator(ReactiveMongoTemplate mongoTemplate) {
return new CustomReactiveMongoHealthIndicator(mongoTemplate);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package it.gov.pagopa.common.config;

import com.mongodb.MongoException;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;


class CustomReactiveMongoHealthIndicatorTest {
@Test
void testMongoIsUp() {
Document buildInfo = mock(Document.class);
given(buildInfo.getInteger("maxWireVersion")).willReturn(10);
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
given(reactiveMongoTemplate.executeCommand("{ isMaster: 1 }")).willReturn(Mono.just(buildInfo));
CustomReactiveMongoHealthIndicator mongoReactiveHealthIndicator = new CustomReactiveMongoHealthIndicator(
reactiveMongoTemplate);
Mono<Health> health = mongoReactiveHealthIndicator.health();
StepVerifier.create(health).consumeNextWith(h -> {
assertThat(h.getStatus()).isEqualTo(Status.UP);
assertThat(h.getDetails()).containsOnlyKeys("maxWireVersion");
assertThat(h.getDetails()).containsEntry("maxWireVersion", 10);
}).expectComplete().verify(Duration.ofSeconds(30));
}

@Test
void testMongoIsDown() {
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
given(reactiveMongoTemplate.executeCommand("{ isMaster: 1 }")).willThrow(new MongoException("Connection failed"));
CustomReactiveMongoHealthIndicator mongoReactiveHealthIndicator = new CustomReactiveMongoHealthIndicator(
reactiveMongoTemplate);
Mono<Health> health = mongoReactiveHealthIndicator.health();
StepVerifier.create(health).consumeNextWith(h -> {
assertThat(h.getStatus()).isEqualTo(Status.DOWN);
assertThat(h.getDetails()).containsOnlyKeys("error");
assertThat(h.getDetails()).containsEntry("error", MongoException.class.getName() + ": Connection failed");
}).expectComplete().verify(Duration.ofSeconds(30));
}

}

This file was deleted.

0 comments on commit 6da1201

Please sign in to comment.