1 /* 2 * Copyright 2017, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.stats; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.testing.EqualsTester; 22 import io.opencensus.common.Function; 23 import io.opencensus.common.Functions; 24 import io.opencensus.common.Timestamp; 25 import io.opencensus.stats.AggregationData.CountData; 26 import io.opencensus.stats.AggregationData.DistributionData; 27 import io.opencensus.stats.AggregationData.DistributionData.Exemplar; 28 import io.opencensus.stats.AggregationData.LastValueDataDouble; 29 import io.opencensus.stats.AggregationData.LastValueDataLong; 30 import io.opencensus.stats.AggregationData.MeanData; 31 import io.opencensus.stats.AggregationData.SumDataDouble; 32 import io.opencensus.stats.AggregationData.SumDataLong; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.Collections; 36 import java.util.List; 37 import java.util.Map; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.rules.ExpectedException; 41 import org.junit.runner.RunWith; 42 import org.junit.runners.JUnit4; 43 44 /** Unit tests for {@link io.opencensus.stats.AggregationData}. */ 45 @RunWith(JUnit4.class) 46 public class AggregationDataTest { 47 48 private static final double TOLERANCE = 1e-6; 49 private static final Timestamp TIMESTAMP_1 = Timestamp.create(1, 0); 50 private static final Timestamp TIMESTAMP_2 = Timestamp.create(2, 0); 51 private static final Map<String, String> ATTACHMENTS = Collections.singletonMap("key", "value"); 52 53 @Rule public ExpectedException thrown = ExpectedException.none(); 54 55 @Test testCreateDistributionData()56 public void testCreateDistributionData() { 57 DistributionData distributionData = 58 DistributionData.create(7.7, 10, 1.1, 9.9, 32.2, Arrays.asList(4L, 1L, 5L)); 59 assertThat(distributionData.getMean()).isWithin(TOLERANCE).of(7.7); 60 assertThat(distributionData.getCount()).isEqualTo(10); 61 assertThat(distributionData.getMin()).isWithin(TOLERANCE).of(1.1); 62 assertThat(distributionData.getMax()).isWithin(TOLERANCE).of(9.9); 63 assertThat(distributionData.getSumOfSquaredDeviations()).isWithin(TOLERANCE).of(32.2); 64 assertThat(distributionData.getBucketCounts()).containsExactly(4L, 1L, 5L).inOrder(); 65 } 66 67 @Test testCreateDistributionDataWithExemplar()68 public void testCreateDistributionDataWithExemplar() { 69 Exemplar exemplar1 = Exemplar.create(4, TIMESTAMP_2, ATTACHMENTS); 70 Exemplar exemplar2 = Exemplar.create(1, TIMESTAMP_1, ATTACHMENTS); 71 DistributionData distributionData = 72 DistributionData.create( 73 7.7, 10, 1.1, 9.9, 32.2, Arrays.asList(4L, 1L), Arrays.asList(exemplar1, exemplar2)); 74 assertThat(distributionData.getExemplars()).containsExactly(exemplar1, exemplar2).inOrder(); 75 } 76 77 @Test testExemplar()78 public void testExemplar() { 79 Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP_1, ATTACHMENTS); 80 assertThat(exemplar.getValue()).isEqualTo(15.0); 81 assertThat(exemplar.getTimestamp()).isEqualTo(TIMESTAMP_1); 82 assertThat(exemplar.getAttachments()).isEqualTo(ATTACHMENTS); 83 } 84 85 @Test testExemplar_PreventNullAttachments()86 public void testExemplar_PreventNullAttachments() { 87 thrown.expect(NullPointerException.class); 88 thrown.expectMessage("attachments"); 89 Exemplar.create(15, TIMESTAMP_1, null); 90 } 91 92 @Test testExemplar_PreventNullAttachmentKey()93 public void testExemplar_PreventNullAttachmentKey() { 94 Map<String, String> attachments = Collections.singletonMap(null, "value"); 95 thrown.expect(NullPointerException.class); 96 thrown.expectMessage("key of attachment"); 97 Exemplar.create(15, TIMESTAMP_1, attachments); 98 } 99 100 @Test testExemplar_PreventNullAttachmentValue()101 public void testExemplar_PreventNullAttachmentValue() { 102 Map<String, String> attachments = Collections.singletonMap("key", null); 103 thrown.expect(NullPointerException.class); 104 thrown.expectMessage("value of attachment"); 105 Exemplar.create(15, TIMESTAMP_1, attachments); 106 } 107 108 @Test preventNullBucketCountList()109 public void preventNullBucketCountList() { 110 thrown.expect(NullPointerException.class); 111 thrown.expectMessage("bucketCounts"); 112 DistributionData.create(1, 1, 1, 1, 0, null); 113 } 114 115 @Test preventNullBucket()116 public void preventNullBucket() { 117 thrown.expect(NullPointerException.class); 118 thrown.expectMessage("bucket"); 119 DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 1L, null)); 120 } 121 122 @Test preventNullExemplarList()123 public void preventNullExemplarList() { 124 thrown.expect(NullPointerException.class); 125 thrown.expectMessage("exemplar list should not be null."); 126 DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 1L, 1L), null); 127 } 128 129 @Test preventNullExemplar()130 public void preventNullExemplar() { 131 thrown.expect(NullPointerException.class); 132 thrown.expectMessage("exemplar"); 133 DistributionData.create( 134 1, 1, 1, 1, 0, Arrays.asList(0L, 1L, 1L), Collections.<Exemplar>singletonList(null)); 135 } 136 137 @Test preventMinIsGreaterThanMax()138 public void preventMinIsGreaterThanMax() { 139 thrown.expect(IllegalArgumentException.class); 140 thrown.expectMessage("max should be greater or equal to min."); 141 DistributionData.create(1, 1, 10, 1, 0, Arrays.asList(0L, 1L, 0L)); 142 } 143 144 @Test testEquals()145 public void testEquals() { 146 new EqualsTester() 147 .addEqualityGroup(SumDataDouble.create(10.0), SumDataDouble.create(10.0)) 148 .addEqualityGroup(SumDataDouble.create(20.0), SumDataDouble.create(20.0)) 149 .addEqualityGroup(SumDataLong.create(20), SumDataLong.create(20)) 150 .addEqualityGroup(CountData.create(40), CountData.create(40)) 151 .addEqualityGroup(CountData.create(80), CountData.create(80)) 152 .addEqualityGroup( 153 DistributionData.create(10, 10, 1, 1, 0, Arrays.asList(0L, 10L, 0L)), 154 DistributionData.create(10, 10, 1, 1, 0, Arrays.asList(0L, 10L, 0L))) 155 .addEqualityGroup(DistributionData.create(10, 10, 1, 1, 0, Arrays.asList(0L, 10L, 100L))) 156 .addEqualityGroup(DistributionData.create(110, 10, 1, 1, 0, Arrays.asList(0L, 10L, 0L))) 157 .addEqualityGroup(DistributionData.create(10, 110, 1, 1, 0, Arrays.asList(0L, 10L, 0L))) 158 .addEqualityGroup(DistributionData.create(10, 10, -1, 1, 0, Arrays.asList(0L, 10L, 0L))) 159 .addEqualityGroup(DistributionData.create(10, 10, 1, 5, 0, Arrays.asList(0L, 10L, 0L))) 160 .addEqualityGroup(DistributionData.create(10, 10, 1, 1, 55.5, Arrays.asList(0L, 10L, 0L))) 161 .addEqualityGroup(MeanData.create(5.0, 1), MeanData.create(5.0, 1)) 162 .addEqualityGroup(MeanData.create(-5.0, 1), MeanData.create(-5.0, 1)) 163 .addEqualityGroup(LastValueDataDouble.create(20.0), LastValueDataDouble.create(20.0)) 164 .addEqualityGroup(LastValueDataLong.create(20), LastValueDataLong.create(20)) 165 .testEquals(); 166 } 167 168 @Test testMatchAndGet()169 public void testMatchAndGet() { 170 List<AggregationData> aggregations = 171 Arrays.asList( 172 SumDataDouble.create(10.0), 173 SumDataLong.create(100000000), 174 CountData.create(40), 175 DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 10L, 0L)), 176 LastValueDataDouble.create(20.0), 177 LastValueDataLong.create(200000000L)); 178 179 final List<Object> actual = new ArrayList<Object>(); 180 for (AggregationData aggregation : aggregations) { 181 aggregation.match( 182 new Function<SumDataDouble, Void>() { 183 @Override 184 public Void apply(SumDataDouble arg) { 185 actual.add(arg.getSum()); 186 return null; 187 } 188 }, 189 new Function<SumDataLong, Void>() { 190 @Override 191 public Void apply(SumDataLong arg) { 192 actual.add(arg.getSum()); 193 return null; 194 } 195 }, 196 new Function<CountData, Void>() { 197 @Override 198 public Void apply(CountData arg) { 199 actual.add(arg.getCount()); 200 return null; 201 } 202 }, 203 new Function<DistributionData, Void>() { 204 @Override 205 public Void apply(DistributionData arg) { 206 actual.add(arg.getBucketCounts()); 207 return null; 208 } 209 }, 210 new Function<LastValueDataDouble, Void>() { 211 @Override 212 public Void apply(LastValueDataDouble arg) { 213 actual.add(arg.getLastValue()); 214 return null; 215 } 216 }, 217 new Function<LastValueDataLong, Void>() { 218 @Override 219 public Void apply(LastValueDataLong arg) { 220 actual.add(arg.getLastValue()); 221 return null; 222 } 223 }, 224 Functions.<Void>throwIllegalArgumentException()); 225 } 226 227 assertThat(actual) 228 .containsExactly(10.0, 100000000L, 40L, Arrays.asList(0L, 10L, 0L), 20.0, 200000000L) 229 .inOrder(); 230 } 231 } 232