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.Functions;
23 import io.opencensus.stats.Aggregation.Count;
24 import io.opencensus.stats.Aggregation.Distribution;
25 import io.opencensus.stats.Aggregation.LastValue;
26 import io.opencensus.stats.Aggregation.Mean;
27 import io.opencensus.stats.Aggregation.Sum;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 /** Unit tests for {@link io.opencensus.stats.Aggregation}. */
38 @RunWith(JUnit4.class)
39 public class AggregationTest {
40 
41   @Rule public ExpectedException thrown = ExpectedException.none();
42 
43   @Test
testCreateDistribution()44   public void testCreateDistribution() {
45     BucketBoundaries bucketBoundaries = BucketBoundaries.create(Arrays.asList(0.1, 2.2, 33.3));
46     Distribution distribution = Distribution.create(bucketBoundaries);
47     assertThat(distribution.getBucketBoundaries()).isEqualTo(bucketBoundaries);
48   }
49 
50   @Test
testNullBucketBoundaries()51   public void testNullBucketBoundaries() {
52     thrown.expect(NullPointerException.class);
53     thrown.expectMessage("bucketBoundaries");
54     Distribution.create(null);
55   }
56 
57   @Test
testEquals()58   public void testEquals() {
59     new EqualsTester()
60         .addEqualityGroup(Sum.create(), Sum.create())
61         .addEqualityGroup(Count.create(), Count.create())
62         .addEqualityGroup(
63             Distribution.create(BucketBoundaries.create(Arrays.asList(-10.0, 1.0, 5.0))),
64             Distribution.create(BucketBoundaries.create(Arrays.asList(-10.0, 1.0, 5.0))))
65         .addEqualityGroup(
66             Distribution.create(BucketBoundaries.create(Arrays.asList(0.0, 1.0, 5.0))),
67             Distribution.create(BucketBoundaries.create(Arrays.asList(0.0, 1.0, 5.0))))
68         .addEqualityGroup(Mean.create(), Mean.create())
69         .addEqualityGroup(LastValue.create(), LastValue.create())
70         .testEquals();
71   }
72 
73   @Test
testMatch()74   public void testMatch() {
75     List<Aggregation> aggregations =
76         Arrays.asList(
77             Sum.create(),
78             Count.create(),
79             Mean.create(),
80             Distribution.create(BucketBoundaries.create(Arrays.asList(-10.0, 1.0, 5.0))),
81             LastValue.create());
82 
83     List<String> actual = new ArrayList<String>();
84     for (Aggregation aggregation : aggregations) {
85       actual.add(
86           aggregation.match(
87               Functions.returnConstant("SUM"),
88               Functions.returnConstant("COUNT"),
89               Functions.returnConstant("DISTRIBUTION"),
90               Functions.returnConstant("LASTVALUE"),
91               Functions.returnConstant("UNKNOWN")));
92     }
93 
94     assertThat(actual)
95         .isEqualTo(Arrays.asList("SUM", "COUNT", "UNKNOWN", "DISTRIBUTION", "LASTVALUE"));
96   }
97 }
98