Refector metrics truth subject

The concrete implementation of a Metric is not of importance when asserting on the values it contains. Therefore this CL removes Metric<T> as a type parameter of AbstractMetricSubject. As a result the two implementations of the abstract subject can be used on any Metric<Long> and Metric<Distribution>, respectively.

Also migrate to Subject.Factory from deprecated SubjectFactory.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171012012
This commit is contained in:
jianglai 2017-10-04 08:23:05 -07:00 committed by Ben McIlwain
parent a9ecccf672
commit 9d8e48cf24
7 changed files with 60 additions and 74 deletions

View file

@ -21,7 +21,7 @@ import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import google.registry.monitoring.metrics.Metric;
import google.registry.monitoring.metrics.MetricPoint;
@ -34,12 +34,11 @@ import javax.annotation.Nullable;
*
* <p>For use with the Google <a href="https://google.github.io/truth/">Truth</a> framework.
*/
abstract class AbstractMetricSubject<
T, M extends Metric<T>, S extends AbstractMetricSubject<T, M, S>>
extends Subject<S, M> {
abstract class AbstractMetricSubject<T, S extends AbstractMetricSubject<T, S>>
extends Subject<S, Metric<T>> {
/** And chainer to allow fluent assertions. */
public static class And<S extends AbstractMetricSubject<?, ?, S>> {
public static class And<S extends AbstractMetricSubject<?, S>> {
private final S subject;
@ -57,7 +56,8 @@ abstract class AbstractMetricSubject<
return new And<>((S) this);
}
/** List of label value tuples about which an assertion has been made so far.
/**
* List of label value tuples about which an assertion has been made so far.
*
* <p>Used to track what tuples have been seen, in order to support hasNoOtherValues() assertions.
*/
@ -77,8 +77,8 @@ abstract class AbstractMetricSubject<
}
};
protected AbstractMetricSubject(FailureStrategy strategy, M actual) {
super(strategy, checkNotNull(actual));
protected AbstractMetricSubject(FailureMetadata metadata, Metric<T> actual) {
super(metadata, checkNotNull(actual));
}
/**
@ -148,9 +148,7 @@ abstract class AbstractMetricSubject<
return andChainer();
}
/**
* Asserts that the metric does not have a (non-default) value for the specified label values.
*/
/** Asserts that the metric does not have a (non-default) value for the specified label values. */
protected And<S> doesNotHaveAnyValueForLabels(String... labels) {
MetricPoint<T> metricPoint = findMetricPointForLabels(ImmutableList.copyOf(labels));
if (metricPoint != null) {
@ -197,6 +195,15 @@ abstract class AbstractMetricSubject<
return null;
}
/**
* Returns a string representation of a metric point value, for use in error messages.
*
* <p>Subclass can override this method if the string needs extra processing.
*/
protected String getMessageRepresentation(T value) {
return String.valueOf(value);
}
/**
* Returns true if the metric point has a non-default value.
*
@ -204,7 +211,4 @@ abstract class AbstractMetricSubject<
* return true if the value is not zero, and so on.
*/
protected abstract boolean hasDefaultValue(MetricPoint<T> metricPoint);
/** Returns a string representation of a metric point value, for use in error messages. */
protected abstract String getMessageRepresentation(T value);
}

View file

@ -18,24 +18,24 @@ import static com.google.common.truth.Truth.assertAbout;
import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.SubjectFactory;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import google.registry.monitoring.metrics.Distribution;
import google.registry.monitoring.metrics.EventMetric;
import google.registry.monitoring.metrics.Metric;
import google.registry.monitoring.metrics.MetricPoint;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Truth subject for the {@link EventMetric} class.
* Truth subject for the {@link Metric<Distribution>} class.
*
* <p>For use with the Google <a href="https://google.github.io/truth/">Truth</a> framework. Usage:
*
* <pre> assertThat(myEventMetric)
* <pre> assertThat(myDistributionMetric)
* .hasAnyValueForLabels("label1", "label2", "label3")
* .and()
* .hasNoOtherValues();
* assertThat(myEventMetric)
* assertThat(myDistributionMetric)
* .doesNotHaveAnyValueForLabels("label1", "label2");
* </pre>
*
@ -44,28 +44,23 @@ import javax.annotation.Nullable;
* it's difficult to write assertions about expected metric data when any number of empty
* distributions can also be present, so they are screened out for convenience.
*/
public final class EventMetricSubject
extends AbstractMetricSubject<Distribution, EventMetric, EventMetricSubject> {
public final class DistributionMetricSubject
extends AbstractMetricSubject<Distribution, DistributionMetricSubject> {
/** {@link SubjectFactory} for assertions about {@link EventMetric} objects. */
private static final SubjectFactory<EventMetricSubject, EventMetric>
/** {@link Subject.Factory} for assertions about {@link Metric<Distribution>} objects. */
private static final Subject.Factory<DistributionMetricSubject, Metric<Distribution>>
SUBJECT_FACTORY =
new SubjectFactory<EventMetricSubject, EventMetric>() {
// The Truth extensibility documentation indicates that the target should be nullable.
@Override
public EventMetricSubject getSubject(
FailureStrategy failureStrategy, @Nullable EventMetric target) {
return new EventMetricSubject(failureStrategy, target);
}
};
// The Truth extensibility documentation indicates that the target should be nullable.
(FailureMetadata failureMetadata, @Nullable Metric<Distribution> target) ->
new DistributionMetricSubject(failureMetadata, target);
/** Static assertThat({@link EventMetric}) shortcut method. */
public static EventMetricSubject assertThat(@Nullable EventMetric metric) {
/** Static assertThat({@link Metric<Distribution>}) shortcut method. */
public static DistributionMetricSubject assertThat(@Nullable Metric<Distribution> metric) {
return assertAbout(SUBJECT_FACTORY).that(metric);
}
private EventMetricSubject(FailureStrategy strategy, EventMetric actual) {
super(strategy, actual);
private DistributionMetricSubject(FailureMetadata metadata, Metric<Distribution> actual) {
super(metadata, actual);
}
/**

View file

@ -16,24 +16,24 @@ package google.registry.monitoring.metrics.contrib;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.SubjectFactory;
import google.registry.monitoring.metrics.IncrementableMetric;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import google.registry.monitoring.metrics.Metric;
import google.registry.monitoring.metrics.MetricPoint;
import javax.annotation.Nullable;
/**
* Truth subject for the {@link IncrementableMetric} class.
* Truth subject for the {@link Metric<Long>} class.
*
* <p>For use with the Google <a href="https://google.github.io/truth/">Truth</a> framework. Usage:
*
* <pre> assertThat(myIncrementableMetric)
* <pre> assertThat(myLongMetric)
* .hasValueForLabels(5, "label1", "label2", "label3")
* .and()
* .hasAnyValueForLabels("label1", "label2", "label4")
* .and()
* .hasNoOtherValues();
* assertThat(myIncrementableMetric)
* assertThat(myLongMetric)
* .doesNotHaveAnyValueForLabels("label1", "label2");
* </pre>
*
@ -42,35 +42,28 @@ import javax.annotation.Nullable;
* after they are reset. But it's difficult to write assertions about expected metric data when any
* number of zero values can also be present, so they are screened out for convenience.
*/
public final class IncrementableMetricSubject
extends AbstractMetricSubject<Long, IncrementableMetric, IncrementableMetricSubject> {
public final class LongMetricSubject extends AbstractMetricSubject<Long, LongMetricSubject> {
/** {@link SubjectFactory} for assertions about {@link IncrementableMetric} objects. */
private static final SubjectFactory<IncrementableMetricSubject, IncrementableMetric>
SUBJECT_FACTORY =
new SubjectFactory<IncrementableMetricSubject, IncrementableMetric>() {
// The Truth extensibility documentation indicates that the target should be nullable.
@Override
public IncrementableMetricSubject getSubject(
FailureStrategy failureStrategy, @Nullable IncrementableMetric target) {
return new IncrementableMetricSubject(failureStrategy, target);
}
};
/** {@link Subject.Factory} for assertions about {@link Metric<Long>} objects. */
private static final Subject.Factory<LongMetricSubject, Metric<Long>> SUBJECT_FACTORY =
// The Truth extensibility documentation indicates that the target should be nullable.
(FailureMetadata failureMetadata, @Nullable Metric<Long> target) ->
new LongMetricSubject(failureMetadata, target);
/** Static assertThat({@link IncrementableMetric}) shortcut method. */
public static IncrementableMetricSubject assertThat(@Nullable IncrementableMetric metric) {
/** Static assertThat({@link Metric<Long>}) shortcut method. */
public static LongMetricSubject assertThat(@Nullable Metric<Long> metric) {
return assertAbout(SUBJECT_FACTORY).that(metric);
}
private IncrementableMetricSubject(FailureStrategy strategy, IncrementableMetric actual) {
super(strategy, actual);
private LongMetricSubject(FailureMetadata metadata, Metric<Long> actual) {
super(metadata, actual);
}
/**
* Asserts that the metric has a given value for the specified label values. This is a convenience
* method that takes a long instead of a Long, for ease of use.
*/
public And<IncrementableMetricSubject> hasValueForLabels(long value, String... labels) {
public And<LongMetricSubject> hasValueForLabels(long value, String... labels) {
return hasValueForLabels(Long.valueOf(value), labels);
}
@ -82,10 +75,4 @@ public final class IncrementableMetricSubject
protected boolean hasDefaultValue(MetricPoint<Long> metricPoint) {
return metricPoint.value() == 0L;
}
/** Returns an appropriate string representation of a metric value for use in error messages. */
@Override
protected String getMessageRepresentation(Long value) {
return String.valueOf(value);
}
}

View file

@ -27,8 +27,8 @@ import static google.registry.model.registry.label.PremiumListUtils.deletePremiu
import static google.registry.model.registry.label.PremiumListUtils.doesPremiumListExist;
import static google.registry.model.registry.label.PremiumListUtils.getPremiumPrice;
import static google.registry.model.registry.label.PremiumListUtils.savePremiumListAndEntries;
import static google.registry.monitoring.metrics.contrib.EventMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.IncrementableMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.DistributionMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.LongMetricSubject.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.loadPremiumListEntries;
import static google.registry.testing.DatastoreHelper.persistPremiumList;

View file

@ -28,8 +28,8 @@ import static google.registry.model.registry.label.ReservationType.RESERVED_FOR_
import static google.registry.model.registry.label.ReservedList.getAllowedNameservers;
import static google.registry.model.registry.label.ReservedList.getReservationTypes;
import static google.registry.model.registry.label.ReservedList.matchesAnchorTenantReservation;
import static google.registry.monitoring.metrics.contrib.EventMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.IncrementableMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.DistributionMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.LongMetricSubject.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistReservedList;
import static google.registry.testing.DatastoreHelper.persistResource;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics.contrib;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.contrib.EventMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.DistributionMetricSubject.assertThat;
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableSet;
@ -28,7 +28,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class EventMetricSubjectTest {
public class DistributionMetricSubjectTest {
private static final ImmutableSet<LabelDescriptor> LABEL_DESCRIPTORS =
ImmutableSet.of(

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics.contrib;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.contrib.IncrementableMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.LongMetricSubject.assertThat;
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableSet;
@ -28,7 +28,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class IncrementableMetricSubjectTest {
public class LongMetricSubjectTest {
private static final ImmutableSet<LabelDescriptor> LABEL_DESCRIPTORS =
ImmutableSet.of(