View Javadoc

1   package com.atlassian.vcache.internal.test;
2   
3   import com.atlassian.vcache.internal.LongMetric;
4   import com.atlassian.vcache.internal.MetricLabel;
5   import org.hamcrest.Description;
6   import org.hamcrest.Factory;
7   import org.hamcrest.Matcher;
8   import org.hamcrest.TypeSafeDiagnosingMatcher;
9   
10  import java.util.EnumMap;
11  import java.util.Optional;
12  
13  import static java.util.Objects.requireNonNull;
14  
15  public class CacheMetricsMatcher extends TypeSafeDiagnosingMatcher<EnumMap<MetricLabel, ? extends LongMetric>> {
16      private final MetricLabel label;
17      private final Optional<Matcher<Integer>> sizeMatcher;
18      private final Matcher<Long> countMatcher;
19      private final Matcher<Long> totalMatcher;
20  
21      private CacheMetricsMatcher(MetricLabel label,
22                                  Matcher<Long> countMatcher,
23                                  Matcher<Long> totalMatcher) {
24          this.label = label;
25          this.sizeMatcher = Optional.empty();
26          this.countMatcher = requireNonNull(countMatcher);
27          this.totalMatcher = requireNonNull(totalMatcher);
28      }
29  
30      private CacheMetricsMatcher(Matcher<Integer> sizeMatcher) {
31          this.label = null;
32          this.sizeMatcher = Optional.of(sizeMatcher);
33          this.countMatcher = null;
34          this.totalMatcher = null;
35      }
36  
37      @Override
38      public void describeTo(Description description) {
39          if (sizeMatcher.isPresent()) {
40              description.appendText("Number of metrics ").appendDescriptionOf(sizeMatcher.get());
41          } else {
42              description.appendText("Contains metric ").appendValue(label);
43              description.appendText(" with sampleCount that ").appendDescriptionOf(countMatcher);
44              description.appendText(" with samplesTotal that ").appendDescriptionOf(totalMatcher);
45          }
46      }
47  
48      @Override
49      protected boolean matchesSafely(EnumMap<MetricLabel, ? extends LongMetric> metricsMap, Description description) {
50          if (sizeMatcher.isPresent()) {
51              description.appendText("Metrics has ")
52                      .appendValue(metricsMap.size())
53                      .appendText(" entries: ")
54                      .appendValue(metricsMap.keySet());
55          } else if (metricsMap.containsKey(label)) {
56              description.appendText("Metrics does not match ").appendValue(metricsMap.get(label));
57          } else {
58              description.appendText("Missing label ")
59                      .appendValue(label)
60                      .appendText(" had ")
61                      .appendValue(metricsMap.keySet());
62          }
63  
64          if (sizeMatcher.isPresent()) {
65              return sizeMatcher.get().matches(metricsMap.size());
66          }
67  
68          return metricsMap.containsKey(label) &&
69                  countMatcher.matches(metricsMap.get(label).getSampleCount()) &&
70                  totalMatcher.matches(metricsMap.get(label).getSamplesTotal());
71      }
72  
73      @Factory
74      public static Matcher<EnumMap<MetricLabel, ? extends LongMetric>> hasMetric(
75              MetricLabel label,
76              Matcher<Long> countMatcher,
77              Matcher<Long> totalMatcher) {
78          return new CacheMetricsMatcher(label, countMatcher, totalMatcher);
79      }
80  
81      @Factory
82      public static Matcher<EnumMap<MetricLabel, ? extends LongMetric>> hasSize(Matcher<Integer> sizeMatcher) {
83          return new CacheMetricsMatcher(sizeMatcher);
84      }
85  }