<commit_before>/**
 * Copyright 2015 Groupon.com
 *
 * 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.arpnetworking.metrics.mad;

import com.arpnetworking.metrics.mad.model.DefaultMetric;
import com.arpnetworking.metrics.mad.model.DefaultRecord;
import com.arpnetworking.tsdcore.model.AggregatedData;
import com.arpnetworking.tsdcore.model.DefaultKey;
import com.arpnetworking.tsdcore.model.Key;
import com.arpnetworking.tsdcore.model.MetricType;
import com.arpnetworking.tsdcore.model.PeriodicData;
import com.arpnetworking.tsdcore.model.Quantity;
import com.arpnetworking.tsdcore.model.Unit;
import com.arpnetworking.tsdcore.sinks.Sink;
import com.arpnetworking.tsdcore.statistics.Statistic;
import com.arpnetworking.tsdcore.statistics.StatisticFactory;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.hamcrest.Matchers;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Period;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.Collections;
import java.util.UUID;
import javax.annotation.Nullable;

/**
 * Tests for the <code>Bucket</code> class.
 *
 * @author <NAME> (ville dot koskela at inscopemetrics dot com)
 */
public class BucketTest {

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        _bucket = new Bucket.Builder()
                .setKey(new DefaultKey(
                        ImmutableMap.of(
                                Key.HOST_DIMENSION_KEY, "MyHost",
                                Key.SERVICE_DIMENSION_KEY, "MyService",
                                Key.CLUSTER_DIMENSION_KEY, "MyCluster")))
                .setSink(_sink)
                .setStart(START)
                .setPeriod(Period.minutes(1))
                .setSpecifiedCounterStatistics(ImmutableSet.of(MIN_STATISTIC))
                .setSpecifiedGaugeStatistics(ImmutableSet.of(MEAN_STATISTIC))
                .setSpecifiedTimerStatistics(ImmutableSet.of(MAX_STATISTIC))
                .setDependentCounterStatistics(ImmutableSet.of())
                .setDependentGaugeStatistics(ImmutableSet.of(COUNT_STATISTIC, SUM_STATISTIC))
                .setDependentTimerStatistics(ImmutableSet.of())
                .setSpecifiedStatistics(_specifiedStatsCache)
                .setDependentStatistics(_dependentStatsCache)
                .build();
    }

    @Test
    public void testCounter() {
        addData("MyCounter", MetricType.COUNTER, ONE, 10);
        addData("MyCounter", MetricType.COUNTER, TWO, 20);
        addData("MyCounter", MetricType.COUNTER, THREE, 30);
        _bucket.close();

        final ArgumentCaptor<PeriodicData> dataCaptor = ArgumentCaptor.forClass(PeriodicData.class);
        Mockito.verify(_sink).recordAggregateData(dataCaptor.capture());

        final ImmutableMultimap<String, AggregatedData> data = dataCaptor.getValue().getData();
        Assert.assertEquals(2, data.size());

        Assert.assertThat(
                data.get("MyCounter"),
                Matchers.containsInAnyOrder(
                        new AggregatedData.Builder()
                                .setIsSpecified(false)
                                .setPopulationSize(3L)
                                .setStatistic(COUNT_STATISTIC)
                                .setValue(THREE)
                                .build(),
                        new AggregatedData.Builder()
                                .setIsSpecified(true)
                                .setPopulationSize(3L)
                                .setStatistic(MIN_STATISTIC)
                                .setValue(ONE)
                                .build()));
    }

    @Test
    public void testGauge() {
        addData("MyGauge", MetricType.GAUGE, TWO, 10);
        addData("MyGauge", MetricType.GAUGE, ONE, 20);
        addData("MyGauge", MetricType.GAUGE, THREE, 30);
        _bucket.close();
<commit_msg>add another test to cover another branch<commit_after>/**
 * Copyright 2015 Groupon.com
 *
 * 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.arpnetworking.metrics.mad;

import com.arpnetworking.metrics.mad.model.DefaultMetric;
import com.arpnetworking.metrics.mad.model.DefaultRecord;
import com.arpnetworking.tsdcore.model.AggregatedData;
import com.arpnetworking.tsdcore.model.DefaultKey;
import com.arpnetworking.tsdcore.model.Key;
import com.arpnetworking.tsdcore.model.MetricType;
import com.arpnetworking.tsdcore.model.PeriodicData;
import com.arpnetworking.tsdcore.model.Quantity;
import com.arpnetworking.tsdcore.model.Unit;
import com.arpnetworking.tsdcore.sinks.Sink;
import com.arpnetworking.tsdcore.statistics.Statistic;
import com.arpnetworking.tsdcore.statistics.StatisticFactory;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.hamcrest.Matchers;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Period;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.Collections;
import java.util.UUID;
import javax.annotation.Nullable;

/**
 * Tests for the <code>Bucket</code> class.
 *
 * @author <NAME> (ville dot koskela at inscopemetrics dot com)
 */
public class BucketTest {

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        _bucket = new Bucket.Builder()
                .setKey(new DefaultKey(
                        ImmutableMap.of(
                                Key.HOST_DIMENSION_KEY, "MyHost",
                                Key.SERVICE_DIMENSION_KEY, "MyService",
                                Key.CLUSTER_DIMENSION_KEY, "MyCluster")))
                .setSink(_sink)
                .setStart(START)
                .setPeriod(Period.minutes(1))
                .setSpecifiedCounterStatistics(ImmutableSet.of(MIN_STATISTIC))
                .setSpecifiedGaugeStatistics(ImmutableSet.of(MEAN_STATISTIC))
                .setSpecifiedTimerStatistics(ImmutableSet.of(MAX_STATISTIC))
                .setDependentCounterStatistics(ImmutableSet.of())
                .setDependentGaugeStatistics(ImmutableSet.of(COUNT_STATISTIC, SUM_STATISTIC))
                .setDependentTimerStatistics(ImmutableSet.of())
                .setSpecifiedStatistics(_specifiedStatsCache)
                .setDependentStatistics(_dependentStatsCache)
                .build();
    }

    @Test
    public void testCounter() {
        addData("MyCounter", MetricType.COUNTER, ONE, 10);
        addData("MyCounter", MetricType.COUNTER, TWO, 20);
        addData("MyCounter", MetricType.COUNTER, THREE, 30);
        _bucket.close();

        final ArgumentCaptor<PeriodicData> dataCaptor = ArgumentCaptor.forClass(PeriodicData.class);
        Mockito.verify(_sink).recordAggregateData(dataCaptor.capture());

        final ImmutableMultimap<String, AggregatedData> data = dataCaptor.getValue().getData();
        Assert.assertEquals(2, data.size());

        Assert.assertThat(
                data.get("MyCounter"),
                Matchers.containsInAnyOrder(
                        new AggregatedData.Builder()
                                .setIsSpecified(false)
                                .setPopulationSize(3L)
                                .setStatistic(COUNT_STATISTIC)
                                .setValue(THREE)
                                .build(),
                        new AggregatedData.Builder()
                                .setIsSpecified(true)
                                .setPopulationSize(3L)
                                .setStatistic(MIN_STATISTIC)
                                .setValue(ONE)
                                .build()));
    }

    @Test
    public void testEmptyCounter() {
        _bucket.add(
                new DefaultRecord.Builder()
                        .setTime(START.plus(Duration.standardSeconds(10)))
                        .setCluster("MyCluster")
                        .setService("MyService")
                        .setHost("MyHost")
                        .setId(UUID.randomUUID().toString())
                        .setMetrics(ImmutableMap.of(
                                "MyCounter",
                                new DefaultMetric.Builder()
                                        .setType(MetricType.COUNTER)
                                        .setValues(Collections.emptyList())
                                        .build()))
                        .build());
        _bucket.close();

        final ArgumentCaptor<PeriodicData> dataCaptor = ArgumentCaptor.forClass(PeriodicData.class);
        Mockito.verify(_sink).recordAggregateData(dataCaptor.capture());

        final ImmutableMultimap<String, AggregatedData> data = dataCaptor.getValue().getData();
        Assert.assertEquals(0, data.size());
    }

    @Test
    public void testGauge() {
        addData("MyGauge", MetricType.GAUGE, TWO, 10);
        addData("MyGauge", MetricType.GAUGE, ONE, 20);
        addData("MyGauge", MetricType.GAUGE, THREE, 30);
        _bucket.close();

{'fraction_non_alphanumeric': 0.09600147411092684, 'fraction_numerical': 0.0046987285793255945, 'mean_word_length': 2.3084425480036574, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '372123', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2939, 'n_words': 559}