Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for HibernateMetrics.monitor() with SessionFactory #1727

Merged
merged 1 commit into from Dec 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,12 +23,12 @@
import org.hibernate.stat.Statistics;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.persistence.EntityManagerFactory;
import java.util.concurrent.TimeUnit;

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

/**
Expand All @@ -41,29 +41,46 @@ class HibernateMetricsTest {

private MeterRegistry registry;

private static EntityManagerFactory createEntityManagerFactoryMock(final boolean statsEnabled) {
EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
SessionFactory sf = Mockito.mock(SessionFactory.class);
Statistics stats = Mockito.mock(Statistics.class, invocation -> {
private static EntityManagerFactory createMockEntityManagerFactory(boolean statsEnabled) {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
SessionFactory sf = createMockSessionFactory(statsEnabled);
when(emf.unwrap(SessionFactory.class)).thenReturn(sf);
return emf;
}

private static SessionFactory createMockSessionFactory(boolean statsEnabled) {
SessionFactory sf = mock(SessionFactory.class);
Statistics stats = mock(Statistics.class, invocation -> {
if (invocation.getMethod().getName().equals("isStatisticsEnabled")) {
return statsEnabled;
}
return 42L;
});
when(emf.unwrap(SessionFactory.class)).thenReturn(sf);
when(sf.getStatistics()).thenReturn(stats);
return emf;
return sf;
}

@BeforeEach
void setup() {
registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
}

@SuppressWarnings("deprecation")
@Test
void shouldExposeMetricsWhenStatsEnabled() {
EntityManagerFactory entityManagerFactory = createEntityManagerFactoryMock(true);
void deprecatedMonitorShouldExposeMetricsWhenStatsEnabled() {
EntityManagerFactory entityManagerFactory = createMockEntityManagerFactory(true);
HibernateMetrics.monitor(registry, entityManagerFactory, "entityManagerFactory");
assertThatMonitorShouldExposeMetricsWhenStatsEnabled();
}

@Test
void monitorShouldExposeMetricsWhenStatsEnabled() {
SessionFactory sessionFactory = createMockSessionFactory(true);
HibernateMetrics.monitor(registry, sessionFactory, "sessionFactory");
assertThatMonitorShouldExposeMetricsWhenStatsEnabled();
}

private void assertThatMonitorShouldExposeMetricsWhenStatsEnabled() {
assertThat(registry.get("hibernate.sessions.open").functionCounter().count()).isEqualTo(42.0);
assertThat(registry.get("hibernate.sessions.closed").functionCounter().count()).isEqualTo(42.0);

Expand Down Expand Up @@ -111,11 +128,19 @@ void shouldExposeMetricsWhenStatsEnabled() {
assertThat(registry.get("hibernate.cache.query.puts").functionCounter().count()).isEqualTo(42.0d);
}

@SuppressWarnings("deprecation")
@Test
void shouldNotExposeMetricsWhenStatsNotEnabled() {
EntityManagerFactory entityManagerFactory = createEntityManagerFactoryMock(false);
void deprecatedMonitorShouldNotExposeMetricsWhenStatsNotEnabled() {
EntityManagerFactory entityManagerFactory = createMockEntityManagerFactory(false);
HibernateMetrics.monitor(registry, entityManagerFactory, "entityManagerFactory");
assertThat(registry.find("hibernate.sessions.open").gauge()).isNull();
}

@Test
void monitorShouldNotExposeMetricsWhenStatsNotEnabled() {
SessionFactory sessionFactory = createMockSessionFactory(true);
HibernateMetrics.monitor(registry, sessionFactory, "sessionFactory");
assertThat(registry.find("hibernate.sessions.open").gauge()).isNull();
}

}