Add Unit Tests for GeodeIndexesHealthIndicator.

This commit is contained in:
John Blum
2018-09-19 17:21:52 -07:00
parent a0476218c1
commit 263379bae9
2 changed files with 141 additions and 1 deletions

View File

@@ -103,7 +103,7 @@ public class GeodeIndexesHealthIndicator extends AbstractGeodeHealthIndicator {
builder.withDetail(indexKey(indexName, "from-clause"), index.getFromClause())
.withDetail(indexKey(indexName, "indexed-expression"), index.getIndexedExpression())
.withDetail(indexKey(indexName, "project-attributes"), index.getProjectionAttributes())
.withDetail(indexKey(indexName, "projection-attributes"), index.getProjectionAttributes())
.withDetail(indexKey(indexName, "region"), Optional.ofNullable(index.getRegion())
.map(Region::getFullPath)
.orElse(""))

View File

@@ -0,0 +1,140 @@
/*
* Copyright 2018 the original author or authors.
*
* 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 org.springframework.geode.boot.actuate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.Map;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.query.Index;
import org.apache.geode.cache.query.IndexStatistics;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.ApplicationContext;
import org.springframework.data.gemfire.IndexType;
import org.springframework.data.gemfire.tests.mock.CacheMockObjects;
import org.springframework.data.gemfire.tests.mock.IndexMockObjects;
/**
* Unit tests for {@link GeodeIndexesHealthIndicator}.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.mockito.junit.MockitoJUnitRunner
* @see org.apache.geode.cache.GemFireCache
* @see org.apache.geode.cache.Region
* @see org.apache.geode.cache.query.Index
* @see org.apache.geode.cache.query.IndexStatistics
* @see org.springframework.boot.actuate.health.Health
* @see org.springframework.boot.actuate.health.HealthIndicator
* @see org.springframework.data.gemfire.tests.mock.CacheMockObjects
* @see org.springframework.data.gemfire.tests.mock.IndexMockObjects
* @see org.springframework.geode.boot.actuate.GeodeIndexesHealthIndicator
* @since 1.0.0
*/
@RunWith(MockitoJUnitRunner.class)
public class GeodeIndexesHealthIndicatorUnitTests {
@Mock
private ApplicationContext applicationContext;
private GeodeIndexesHealthIndicator indexesHealthIndicator;
@Before
public void setup() {
this.indexesHealthIndicator = new GeodeIndexesHealthIndicator(this.applicationContext);
}
@Test
public void healthCheckCapturesDetails() throws Exception {
Region mockRegion = CacheMockObjects.mockRegion("MockRegion", DataPolicy.PARTITION);
IndexStatistics mockIndexStatistics = IndexMockObjects.mockIndexStatistics(226,
100000, 6000, 1024000L, 51515L,
512, 2048L, 4096L);
Index mockIndex = IndexMockObjects.mockIndex("MockIndex", "/Example",
"id", "one, two", mockRegion, mockIndexStatistics,
IndexType.PRIMARY_KEY.getGemfireIndexType());
Map<String, Index> mockIndexBeans = Collections.singletonMap("MockIndex", mockIndex);
when(this.applicationContext.getBeansOfType(eq(Index.class))).thenReturn(mockIndexBeans);
Health.Builder builder = new Health.Builder();
this.indexesHealthIndicator.doHealthCheck(builder);
Health health = builder.build();
assertThat(health).isNotNull();
assertThat(health.getStatus()).isEqualTo(Status.UP);
Map<String, Object> healthDetails = health.getDetails();
assertThat(healthDetails).isNotNull();
assertThat(healthDetails).isNotEmpty();
assertThat(healthDetails).containsEntry("geode.index.count", mockIndexBeans.size());
assertThat(healthDetails).containsEntry("geode.index.MockIndex.from-clause", "/Example");
assertThat(healthDetails).containsEntry("geode.index.MockIndex.indexed-expression", "id");
assertThat(healthDetails).containsEntry("geode.index.MockIndex.projection-attributes", "one, two");
assertThat(healthDetails).containsEntry("geode.index.MockIndex.region", "/MockRegion");
assertThat(healthDetails).containsEntry("geode.index.MockIndex.type",
IndexType.PRIMARY_KEY.getGemfireIndexType().toString());
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.number-of-bucket-indexes", 226);
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.number-of-keys", 100000L);
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.number-of-map-index-keys", 6000L);
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.number-of-values", 1024000L);
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.number-of-updates", 51515L);
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.read-lock-count", 512);
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.total-update-time", 2048L);
assertThat(healthDetails).containsEntry("geode.index.MockIndex.statistics.total-uses", 4096L);
verify(this.applicationContext, times(1)).getBeansOfType(eq(Index.class));
}
@Test
public void healthCheckFailsWhenApplicationContextContainsNoIndexBeans() throws Exception {
GeodeIndexesHealthIndicator healthIndicator = new GeodeIndexesHealthIndicator();
Health.Builder builder = new Health.Builder();
healthIndicator.doHealthCheck(builder);
Health health = builder.build();
assertThat(health).isNotNull();
assertThat(health.getDetails()).isEmpty();
assertThat(health.getStatus()).isEqualTo(Status.UNKNOWN);
}
}