/*
 * Copyright 2017, Google Inc.
 * 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 com.google.instrumentation.stats;

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

import com.google.instrumentation.common.Clock;
import com.google.instrumentation.internal.TestClock;
import com.google.instrumentation.stats.MutableView.MutableDistributionView;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link MeasurementDescriptorToViewMap}. */
@RunWith(JUnit4.class)
public class MeasurementDescriptorToViewMapTest {
  private final MeasurementDescriptorToViewMap measurementDescriptorToViewMap =
      new MeasurementDescriptorToViewMap();

  @Test
  public void testPutAndGetView() {
    Clock clock = TestClock.create();
    MutableView expected =
        MutableDistributionView.create(
            RpcViewConstants.RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, clock.now());
    measurementDescriptorToViewMap.putView(
        RpcMeasurementConstants.RPC_CLIENT_ROUNDTRIP_LATENCY.getMeasurementDescriptorName(),
        expected);
    View actual =
        measurementDescriptorToViewMap.getView(
            RpcViewConstants.RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, clock);
    assertThat(actual.getViewDescriptor()).isEqualTo(expected.getViewDescriptor());
  }
}

@@ -684,55 +684,246 @@
 at;%0A
-%0Aimport com.google.instrumentation.common.Clock
+import static com.google.instrumentation.stats.RpcViewConstants.RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW;%0Aimport static org.junit.Assert.fail;%0A%0Aimport com.google.instrumentation.common.Function;%0Aimport com.google.instrumentation.common.Timestamp
 ;%0Aim
@@ -1051,16 +1051,138 @@
 onView;%0A
+import com.google.instrumentation.stats.View.DistributionView;%0Aimport com.google.instrumentation.stats.View.IntervalView;%0A
 import o
@@ -1566,16 +1566,20 @@
 ) %7B%0A    
+Test
 Clock cl
@@ -1605,152 +1605,31 @@
 ate(
-);%0A    MutableView expected =%0A        MutableDistributionView.create(%0A            RpcViewConstants.RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, clock.now(
+Timestamp.create(10, 20
 ));%0A
@@ -1777,238 +1777,947 @@
     
-expected);%0A    View actual =%0A        measurementDescriptorToViewMap.getView(%0A            RpcViewConstants.RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, clock);%0A    assertThat(actual.getViewDescriptor()).isEqualTo(expected.getViewDescriptor()
+MutableDistributionView.create(RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, clock.now()));%0A    clock.setTime(Timestamp.create(30, 40));%0A    View actual = measurementDescriptorToViewMap.getView(RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, clock);%0A    actual.match(%0A        new Function%3CView.DistributionView, Void%3E() %7B%0A          @Override%0A          public Void apply(DistributionView view) %7B%0A            assertThat(view.getViewDescriptor()).isEqualTo(RPC_CLIENT_ROUNDTRIP_LATENCY_VIEW);%0A            assertThat(view.getStart()).isEqualTo(Timestamp.create(10, 20));%0A            assertThat(view.getEnd()).isEqualTo(Timestamp.create(30, 40));%0A            assertThat(view.getDistributionAggregations()).isEmpty();%0A            return null;%0A          %7D%0A        %7D,%0A        new Function%3CView.IntervalView, Void%3E() %7B%0A          @Override%0A          public Void apply(IntervalView view) %7B%0A            fail(%22Wrong view type.%22);%0A            return null;%0A          %7D%0A        %7D
 );%0A 

Check view start and end times in MeasurementDescriptorToViewMapTest.