aboutsummaryrefslogtreecommitdiff
path: root/api/src/test/java/io/opencensus/stats/NoopViewManagerTest.java
blob: 44c7626f9d2b7f7ba145ee6292db689854b7d02f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.stats;

import static com.google.common.truth.Truth.assertThat;

import io.opencensus.common.Duration;
import io.opencensus.common.Timestamp;
import io.opencensus.stats.Aggregation.Sum;
import io.opencensus.stats.Measure.MeasureDouble;
import io.opencensus.stats.View.AggregationWindow.Cumulative;
import io.opencensus.stats.View.AggregationWindow.Interval;
import io.opencensus.stats.View.Name;
import io.opencensus.stats.ViewData.AggregationWindowData.CumulativeData;
import io.opencensus.stats.ViewData.AggregationWindowData.IntervalData;
import io.opencensus.tags.TagKey;
import java.util.Arrays;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link NoopStats#newNoopViewManager}. */
@RunWith(JUnit4.class)
public final class NoopViewManagerTest {
  private static final MeasureDouble MEASURE =
      Measure.MeasureDouble.create("my measure", "description", "s");
  private static final TagKey KEY = TagKey.create("KEY");
  private static final Name VIEW_NAME = Name.create("my view");
  private static final String VIEW_DESCRIPTION = "view description";
  private static final Sum AGGREGATION = Sum.create();
  private static final Cumulative CUMULATIVE = Cumulative.create();
  private static final Duration TEN_SECONDS = Duration.create(10, 0);
  private static final Interval INTERVAL = Interval.create(TEN_SECONDS);

  @Rule public final ExpectedException thrown = ExpectedException.none();

  @Test
  public void noopViewManager_RegisterView_DisallowRegisteringDifferentViewWithSameName() {
    final View view1 =
        View.create(
            VIEW_NAME, "description 1", MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
    final View view2 =
        View.create(
            VIEW_NAME, "description 2", MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
    ViewManager viewManager = NoopStats.newNoopViewManager();
    viewManager.registerView(view1);

    try {
      thrown.expect(IllegalArgumentException.class);
      thrown.expectMessage("A different view with the same name already exists.");
      viewManager.registerView(view2);
    } finally {
      assertThat(viewManager.getView(VIEW_NAME).getView()).isEqualTo(view1);
    }
  }

  @Test
  public void noopViewManager_RegisterView_AllowRegisteringSameViewTwice() {
    View view =
        View.create(
            VIEW_NAME, VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
    ViewManager viewManager = NoopStats.newNoopViewManager();
    viewManager.registerView(view);
    viewManager.registerView(view);
  }

  @Test
  public void noopViewManager_RegisterView_DisallowNull() {
    ViewManager viewManager = NoopStats.newNoopViewManager();
    thrown.expect(NullPointerException.class);
    viewManager.registerView(null);
  }

  @Test
  public void noopViewManager_GetView_GettingNonExistentViewReturnsNull() {
    ViewManager viewManager = NoopStats.newNoopViewManager();
    assertThat(viewManager.getView(VIEW_NAME)).isNull();
  }

  @Test
  public void noopViewManager_GetView_Cumulative() {
    View view =
        View.create(
            VIEW_NAME, VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY), CUMULATIVE);
    ViewManager viewManager = NoopStats.newNoopViewManager();
    viewManager.registerView(view);

    ViewData viewData = viewManager.getView(VIEW_NAME);
    assertThat(viewData.getView()).isEqualTo(view);
    assertThat(viewData.getAggregationMap()).isEmpty();
    assertThat(viewData.getStart()).isEqualTo(Timestamp.create(0, 0));
    assertThat(viewData.getEnd()).isEqualTo(Timestamp.create(0, 0));
    assertThat(viewData.getWindowData())
        .isEqualTo(CumulativeData.create(Timestamp.create(0, 0), Timestamp.create(0, 0)));
  }

  @Test
  public void noopViewManager_GetView_Interval() {
    View view =
        View.create(
            VIEW_NAME, VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY), INTERVAL);
    ViewManager viewManager = NoopStats.newNoopViewManager();
    viewManager.registerView(view);

    ViewData viewData = viewManager.getView(VIEW_NAME);
    assertThat(viewData.getView()).isEqualTo(view);
    assertThat(viewData.getAggregationMap()).isEmpty();
    assertThat(viewData.getWindowData()).isEqualTo(IntervalData.create(Timestamp.create(0, 0)));
  }

  @Test
  public void noopViewManager_GetView_DisallowNull() {
    ViewManager viewManager = NoopStats.newNoopViewManager();
    thrown.expect(NullPointerException.class);
    viewManager.getView(null);
  }

  @Test
  public void getAllExportedViews() {
    ViewManager viewManager = NoopStats.newNoopViewManager();
    assertThat(viewManager.getAllExportedViews()).isEmpty();
    View cumulativeView1 =
        View.create(
            View.Name.create("View 1"),
            VIEW_DESCRIPTION,
            MEASURE,
            AGGREGATION,
            Arrays.asList(KEY),
            CUMULATIVE);
    View cumulativeView2 =
        View.create(
            View.Name.create("View 2"),
            VIEW_DESCRIPTION,
            MEASURE,
            AGGREGATION,
            Arrays.asList(KEY),
            CUMULATIVE);
    View intervalView =
        View.create(
            View.Name.create("View 3"),
            VIEW_DESCRIPTION,
            MEASURE,
            AGGREGATION,
            Arrays.asList(KEY),
            INTERVAL);
    viewManager.registerView(cumulativeView1);
    viewManager.registerView(cumulativeView2);
    viewManager.registerView(intervalView);

    // Only cumulative views should be exported.
    assertThat(viewManager.getAllExportedViews()).containsExactly(cumulativeView1, cumulativeView2);
  }

  @Test
  public void getAllExportedViews_ResultIsUnmodifiable() {
    ViewManager viewManager = NoopStats.newNoopViewManager();
    View view1 =
        View.create(
            View.Name.create("View 1"), VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY));
    viewManager.registerView(view1);
    Set<View> exported = viewManager.getAllExportedViews();

    View view2 =
        View.create(
            View.Name.create("View 2"), VIEW_DESCRIPTION, MEASURE, AGGREGATION, Arrays.asList(KEY));
    thrown.expect(UnsupportedOperationException.class);
    exported.add(view2);
  }
}