Home | History | Annotate | Download | only in stats
      1 /*
      2  * Copyright 2016-17, 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.Duration;
     23 import io.opencensus.stats.Aggregation.Mean;
     24 import io.opencensus.stats.View.AggregationWindow.Cumulative;
     25 import io.opencensus.stats.View.AggregationWindow.Interval;
     26 import io.opencensus.tags.TagKey;
     27 import java.util.Arrays;
     28 import java.util.Collections;
     29 import java.util.List;
     30 import org.junit.Rule;
     31 import org.junit.Test;
     32 import org.junit.rules.ExpectedException;
     33 import org.junit.runner.RunWith;
     34 import org.junit.runners.JUnit4;
     35 
     36 /** Tests for {@link View}. */
     37 @RunWith(JUnit4.class)
     38 public final class ViewTest {
     39 
     40   @Rule public final ExpectedException thrown = ExpectedException.none();
     41 
     42   @Test
     43   public void testConstants() {
     44     assertThat(View.NAME_MAX_LENGTH).isEqualTo(255);
     45   }
     46 
     47   @Test
     48   public void sortTagKeys() {
     49     final View view =
     50         View.create(
     51             NAME,
     52             DESCRIPTION,
     53             MEASURE,
     54             MEAN,
     55             Arrays.asList(
     56                 TagKey.create("ab"), TagKey.create("a"), TagKey.create("A"), TagKey.create("b")));
     57     assertThat(view.getColumns())
     58         .containsExactly(
     59             TagKey.create("A"), TagKey.create("a"), TagKey.create("ab"), TagKey.create("b"))
     60         .inOrder();
     61   }
     62 
     63   @Test
     64   public void testDistributionView() {
     65     final View view = View.create(NAME, DESCRIPTION, MEASURE, MEAN, KEYS);
     66     assertThat(view.getName()).isEqualTo(NAME);
     67     assertThat(view.getDescription()).isEqualTo(DESCRIPTION);
     68     assertThat(view.getMeasure().getName()).isEqualTo(MEASURE.getName());
     69     assertThat(view.getAggregation()).isEqualTo(MEAN);
     70     assertThat(view.getColumns()).containsExactly(BAR, FOO).inOrder();
     71     assertThat(view.getWindow()).isEqualTo(Cumulative.create());
     72   }
     73 
     74   @Test
     75   public void testIntervalView() {
     76     final View view = View.create(NAME, DESCRIPTION, MEASURE, MEAN, KEYS, Interval.create(MINUTE));
     77     assertThat(view.getName()).isEqualTo(NAME);
     78     assertThat(view.getDescription()).isEqualTo(DESCRIPTION);
     79     assertThat(view.getMeasure().getName()).isEqualTo(MEASURE.getName());
     80     assertThat(view.getAggregation()).isEqualTo(MEAN);
     81     assertThat(view.getColumns()).containsExactly(BAR, FOO).inOrder();
     82     assertThat(view.getWindow()).isEqualTo(Interval.create(MINUTE));
     83   }
     84 
     85   @Test
     86   public void testViewEquals() {
     87     new EqualsTester()
     88         .addEqualityGroup(
     89             View.create(NAME, DESCRIPTION, MEASURE, MEAN, KEYS),
     90             View.create(NAME, DESCRIPTION, MEASURE, MEAN, KEYS, Cumulative.create()))
     91         .addEqualityGroup(
     92             View.create(NAME, DESCRIPTION + 2, MEASURE, MEAN, KEYS, Cumulative.create()))
     93         .addEqualityGroup(
     94             View.create(NAME, DESCRIPTION, MEASURE, MEAN, KEYS, Interval.create(MINUTE)),
     95             View.create(NAME, DESCRIPTION, MEASURE, MEAN, KEYS, Interval.create(MINUTE)))
     96         .addEqualityGroup(
     97             View.create(NAME, DESCRIPTION, MEASURE, MEAN, KEYS, Interval.create(TWO_MINUTES)))
     98         .testEquals();
     99   }
    100 
    101   @Test
    102   public void preventDuplicateColumns() {
    103     TagKey key1 = TagKey.create("duplicate");
    104     TagKey key2 = TagKey.create("duplicate");
    105     thrown.expect(IllegalArgumentException.class);
    106     thrown.expectMessage("Columns have duplicate.");
    107     View.create(NAME, DESCRIPTION, MEASURE, MEAN, Arrays.asList(key1, key2));
    108   }
    109 
    110   @Test(expected = NullPointerException.class)
    111   public void preventNullViewName() {
    112     View.create(null, DESCRIPTION, MEASURE, MEAN, KEYS);
    113   }
    114 
    115   @Test
    116   public void preventTooLongViewName() {
    117     char[] chars = new char[View.NAME_MAX_LENGTH + 1];
    118     Arrays.fill(chars, 'a');
    119     String longName = String.valueOf(chars);
    120     thrown.expect(IllegalArgumentException.class);
    121     View.Name.create(longName);
    122   }
    123 
    124   @Test
    125   public void preventNonPrintableViewName() {
    126     thrown.expect(IllegalArgumentException.class);
    127     View.Name.create("\2");
    128   }
    129 
    130   @Test
    131   public void testViewName() {
    132     assertThat(View.Name.create("my name").asString()).isEqualTo("my name");
    133   }
    134 
    135   @Test(expected = NullPointerException.class)
    136   public void preventNullNameString() {
    137     View.Name.create(null);
    138   }
    139 
    140   @Test(expected = IllegalArgumentException.class)
    141   public void preventNegativeIntervalDuration() {
    142     Interval.create(NEG_TEN_SECONDS);
    143   }
    144 
    145   @Test
    146   public void testViewNameEquals() {
    147     new EqualsTester()
    148         .addEqualityGroup(View.Name.create("view-1"), View.Name.create("view-1"))
    149         .addEqualityGroup(View.Name.create("view-2"))
    150         .testEquals();
    151   }
    152 
    153   private static final View.Name NAME = View.Name.create("test-view-name");
    154   private static final String DESCRIPTION = "test-view-name description";
    155   private static final Measure MEASURE =
    156       Measure.MeasureDouble.create("measure", "measure description", "1");
    157   private static final TagKey FOO = TagKey.create("foo");
    158   private static final TagKey BAR = TagKey.create("bar");
    159   private static final List<TagKey> KEYS = Collections.unmodifiableList(Arrays.asList(FOO, BAR));
    160   private static final Mean MEAN = Mean.create();
    161   private static final Duration MINUTE = Duration.create(60, 0);
    162   private static final Duration TWO_MINUTES = Duration.create(120, 0);
    163   private static final Duration NEG_TEN_SECONDS = Duration.create(-10, 0);
    164 }
    165