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.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Ordering; 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 com.google.common.truth.Subject;
import google.registry.monitoring.metrics.Metric; import google.registry.monitoring.metrics.Metric;
import google.registry.monitoring.metrics.MetricPoint; 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. * <p>For use with the Google <a href="https://google.github.io/truth/">Truth</a> framework.
*/ */
abstract class AbstractMetricSubject< abstract class AbstractMetricSubject<T, S extends AbstractMetricSubject<T, S>>
T, M extends Metric<T>, S extends AbstractMetricSubject<T, M, S>> extends Subject<S, Metric<T>> {
extends Subject<S, M> {
/** And chainer to allow fluent assertions. */ /** 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; private final S subject;
@ -57,7 +56,8 @@ abstract class AbstractMetricSubject<
return new And<>((S) this); 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. * <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) { protected AbstractMetricSubject(FailureMetadata metadata, Metric<T> actual) {
super(strategy, checkNotNull(actual)); super(metadata, checkNotNull(actual));
} }
/** /**
@ -148,9 +148,7 @@ abstract class AbstractMetricSubject<
return andChainer(); 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) { protected And<S> doesNotHaveAnyValueForLabels(String... labels) {
MetricPoint<T> metricPoint = findMetricPointForLabels(ImmutableList.copyOf(labels)); MetricPoint<T> metricPoint = findMetricPointForLabels(ImmutableList.copyOf(labels));
if (metricPoint != null) { if (metricPoint != null) {
@ -197,6 +195,15 @@ abstract class AbstractMetricSubject<
return null; 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. * 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. * return true if the value is not zero, and so on.
*/ */
protected abstract boolean hasDefaultValue(MetricPoint<T> metricPoint); 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.BoundType;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import com.google.common.truth.FailureStrategy; import com.google.common.truth.FailureMetadata;
import com.google.common.truth.SubjectFactory; import com.google.common.truth.Subject;
import google.registry.monitoring.metrics.Distribution; 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 google.registry.monitoring.metrics.MetricPoint;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable; 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: * <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") * .hasAnyValueForLabels("label1", "label2", "label3")
* .and() * .and()
* .hasNoOtherValues(); * .hasNoOtherValues();
* assertThat(myEventMetric) * assertThat(myDistributionMetric)
* .doesNotHaveAnyValueForLabels("label1", "label2"); * .doesNotHaveAnyValueForLabels("label1", "label2");
* </pre> * </pre>
* *
@ -44,28 +44,23 @@ import javax.annotation.Nullable;
* it's difficult to write assertions about expected metric data when any number of empty * 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. * distributions can also be present, so they are screened out for convenience.
*/ */
public final class EventMetricSubject public final class DistributionMetricSubject
extends AbstractMetricSubject<Distribution, EventMetric, EventMetricSubject> { extends AbstractMetricSubject<Distribution, DistributionMetricSubject> {
/** {@link SubjectFactory} for assertions about {@link EventMetric} objects. */ /** {@link Subject.Factory} for assertions about {@link Metric<Distribution>} objects. */
private static final SubjectFactory<EventMetricSubject, EventMetric> private static final Subject.Factory<DistributionMetricSubject, Metric<Distribution>>
SUBJECT_FACTORY = SUBJECT_FACTORY =
new SubjectFactory<EventMetricSubject, EventMetric>() { // The Truth extensibility documentation indicates that the target should be nullable.
// The Truth extensibility documentation indicates that the target should be nullable. (FailureMetadata failureMetadata, @Nullable Metric<Distribution> target) ->
@Override new DistributionMetricSubject(failureMetadata, target);
public EventMetricSubject getSubject(
FailureStrategy failureStrategy, @Nullable EventMetric target) {
return new EventMetricSubject(failureStrategy, target);
}
};
/** Static assertThat({@link EventMetric}) shortcut method. */ /** Static assertThat({@link Metric<Distribution>}) shortcut method. */
public static EventMetricSubject assertThat(@Nullable EventMetric metric) { public static DistributionMetricSubject assertThat(@Nullable Metric<Distribution> metric) {
return assertAbout(SUBJECT_FACTORY).that(metric); return assertAbout(SUBJECT_FACTORY).that(metric);
} }
private EventMetricSubject(FailureStrategy strategy, EventMetric actual) { private DistributionMetricSubject(FailureMetadata metadata, Metric<Distribution> actual) {
super(strategy, 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 static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.FailureStrategy; import com.google.common.truth.FailureMetadata;
import com.google.common.truth.SubjectFactory; import com.google.common.truth.Subject;
import google.registry.monitoring.metrics.IncrementableMetric; import google.registry.monitoring.metrics.Metric;
import google.registry.monitoring.metrics.MetricPoint; import google.registry.monitoring.metrics.MetricPoint;
import javax.annotation.Nullable; 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: * <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") * .hasValueForLabels(5, "label1", "label2", "label3")
* .and() * .and()
* .hasAnyValueForLabels("label1", "label2", "label4") * .hasAnyValueForLabels("label1", "label2", "label4")
* .and() * .and()
* .hasNoOtherValues(); * .hasNoOtherValues();
* assertThat(myIncrementableMetric) * assertThat(myLongMetric)
* .doesNotHaveAnyValueForLabels("label1", "label2"); * .doesNotHaveAnyValueForLabels("label1", "label2");
* </pre> * </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 * 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. * number of zero values can also be present, so they are screened out for convenience.
*/ */
public final class IncrementableMetricSubject public final class LongMetricSubject extends AbstractMetricSubject<Long, LongMetricSubject> {
extends AbstractMetricSubject<Long, IncrementableMetric, IncrementableMetricSubject> {
/** {@link SubjectFactory} for assertions about {@link IncrementableMetric} objects. */ /** {@link Subject.Factory} for assertions about {@link Metric<Long>} objects. */
private static final SubjectFactory<IncrementableMetricSubject, IncrementableMetric> private static final Subject.Factory<LongMetricSubject, Metric<Long>> SUBJECT_FACTORY =
SUBJECT_FACTORY = // The Truth extensibility documentation indicates that the target should be nullable.
new SubjectFactory<IncrementableMetricSubject, IncrementableMetric>() { (FailureMetadata failureMetadata, @Nullable Metric<Long> target) ->
// The Truth extensibility documentation indicates that the target should be nullable. new LongMetricSubject(failureMetadata, target);
@Override
public IncrementableMetricSubject getSubject(
FailureStrategy failureStrategy, @Nullable IncrementableMetric target) {
return new IncrementableMetricSubject(failureStrategy, target);
}
};
/** Static assertThat({@link IncrementableMetric}) shortcut method. */ /** Static assertThat({@link Metric<Long>}) shortcut method. */
public static IncrementableMetricSubject assertThat(@Nullable IncrementableMetric metric) { public static LongMetricSubject assertThat(@Nullable Metric<Long> metric) {
return assertAbout(SUBJECT_FACTORY).that(metric); return assertAbout(SUBJECT_FACTORY).that(metric);
} }
private IncrementableMetricSubject(FailureStrategy strategy, IncrementableMetric actual) { private LongMetricSubject(FailureMetadata metadata, Metric<Long> actual) {
super(strategy, actual); super(metadata, actual);
} }
/** /**
* Asserts that the metric has a given value for the specified label values. This is a convenience * 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. * 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); return hasValueForLabels(Long.valueOf(value), labels);
} }
@ -82,10 +75,4 @@ public final class IncrementableMetricSubject
protected boolean hasDefaultValue(MetricPoint<Long> metricPoint) { protected boolean hasDefaultValue(MetricPoint<Long> metricPoint) {
return metricPoint.value() == 0L; 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.doesPremiumListExist;
import static google.registry.model.registry.label.PremiumListUtils.getPremiumPrice; import static google.registry.model.registry.label.PremiumListUtils.getPremiumPrice;
import static google.registry.model.registry.label.PremiumListUtils.savePremiumListAndEntries; 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.DistributionMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.IncrementableMetricSubject.assertThat; import static google.registry.monitoring.metrics.contrib.LongMetricSubject.assertThat;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.loadPremiumListEntries; import static google.registry.testing.DatastoreHelper.loadPremiumListEntries;
import static google.registry.testing.DatastoreHelper.persistPremiumList; 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.getAllowedNameservers;
import static google.registry.model.registry.label.ReservedList.getReservationTypes; import static google.registry.model.registry.label.ReservedList.getReservationTypes;
import static google.registry.model.registry.label.ReservedList.matchesAnchorTenantReservation; 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.DistributionMetricSubject.assertThat;
import static google.registry.monitoring.metrics.contrib.IncrementableMetricSubject.assertThat; import static google.registry.monitoring.metrics.contrib.LongMetricSubject.assertThat;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistReservedList; import static google.registry.testing.DatastoreHelper.persistReservedList;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;

View file

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

View file

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