/*
 * Copyright 2016 camunda services GmbH.
 *
 * 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 org.camunda.bpm.engine.test.api.mgmt.metrics;

import java.util.Date;
import java.util.List;
import org.camunda.bpm.engine.impl.metrics.MetricsRegistry;
import org.camunda.bpm.engine.impl.util.ClockUtil;
import org.camunda.bpm.engine.management.MetricIntervalValue;
import static org.camunda.bpm.engine.management.Metrics.ACTIVTY_INSTANCE_START;
import org.junit.Assert;
import org.junit.Test;

/**
 * Represents a test suite for the metrics interval query to check if the
 * timestamps are read in a correct time zone.
 *
 * This was a problem before the column MILLISECONDS_ was added.
 *
 * @author Christopher Zell <christopher.zell@camunda.com>
 */
public class MetricsIntervalTimezoneTest extends AbstractMetricsIntervalTest {

  @Test
  public void testTimestampIsInCorrectTimezone() {
    List<MetricIntervalValue> metrics = managementService.createMetricsQuery().limit(1).interval();
    Assert.assertTrue(metrics.get(0).getTimestamp().getTime() < firstInterval.plusMinutes(3 * DEFAULT_INTERVAL).getMillis());

    Date currentTime = new Date();
    MetricsRegistry metricsRegistry = processEngineConfiguration.getMetricsRegistry();
    ClockUtil.setCurrentTime(currentTime);
    metricsRegistry.markOccurrence(ACTIVTY_INSTANCE_START, 1);
    processEngineConfiguration.getDbMetricsReporter().reportNow();
    List<MetricIntervalValue> m2 = managementService.createMetricsQuery().limit(1).interval();
    Assert.assertTrue(m2.get(0).getTimestamp().getTime() < currentTime.getTime());
  }
}

@@ -1401,16 +1401,186 @@
 one() %7B%0A
+    //given generated metric data started at DEFAULT_INTERVAL ends at 3 * DEFAULT_INTERVAL%0A%0A    //when metric query is executed (hint last interval is returned as first)%0A
     List
@@ -1675,134 +1675,483 @@
 ();%0A
+%0A
     
-Assert.assertTrue(metrics.get(0).getTimestamp().getTime() %3C firstInterval.plusMinutes(3 * DEFAULT_INTERVAL).getMillis());%0A
+//then metric interval time should be less than FIRST_INTERVAL + 3 * DEFAULT_INTERVAL%0A    long metricIntervalTime = metrics.get(0).getTimestamp().getTime();%0A    Assert.assertTrue(metricIntervalTime %3C firstInterval.plusMinutes(3 * DEFAULT_INTERVAL).getMillis());%0A    //and larger than first interval time, if not than we have a timezone problem%0A    Assert.assertTrue(metricIntervalTime %3E firstInterval.getMillis());%0A%0A    //when current time is used and metric is reported
 %0A   
@@ -2441,16 +2441,84 @@
 rtNow();
+%0A%0A    //then current time should be larger than metric interval time
 %0A    Lis

add doc on test case