com.linuxbox.enkive.teststats.StatsDayGrainTest.java Source code

Java tutorial

Introduction

Here is the source code for com.linuxbox.enkive.teststats.StatsDayGrainTest.java

Source

/*******************************************************************************
 * Copyright 2013 The Linux Box Corporation.
 * 
 * This file is part of Enkive CE (Community Edition).
 * Enkive CE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * 
 * Enkive CE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public
 * License along with Enkive CE. If not, see
 * <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.linuxbox.enkive.teststats;

import static com.linuxbox.enkive.statistics.StatsConstants.STAT_TIMESTAMP;
import static com.linuxbox.enkive.statistics.consolidation.ConsolidationConstants.CONSOLIDATION_AVG;
import static com.linuxbox.enkive.statistics.consolidation.ConsolidationConstants.CONSOLIDATION_MAX;
import static com.linuxbox.enkive.statistics.consolidation.ConsolidationConstants.CONSOLIDATION_MIN;
import static org.junit.Assert.assertTrue;

import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.junit.BeforeClass;
import org.junit.Test;

import com.linuxbox.enkive.statistics.consolidation.DayConsolidator;
import com.linuxbox.enkive.statistics.consolidation.HourConsolidator;
import com.linuxbox.enkive.statistics.gathering.GathererAttributes;
import com.linuxbox.enkive.statistics.gathering.GathererException;
import com.linuxbox.enkive.statistics.services.StatsClient;
import com.mongodb.DBCollection;

public class StatsDayGrainTest {
    private static StatsClient client;
    private static DayConsolidator grain;
    private static DBCollection coll;
    private static long dataCount;

    @BeforeClass
    public static void setUp() throws ParseException, GathererException {
        coll = TestHelper.GetTestCollection();
        client = TestHelper.BuildClient();
        grain = new DayConsolidator(client);

        List<Map<String, Object>> stats = (new HourConsolidator(client)).consolidateData();
        Map<String, Object> timeMap = new HashMap<String, Object>();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR, 0);
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                cal.add(Calendar.DATE, -1);
            }
            timeMap.put(CONSOLIDATION_MAX, cal.getTime());
            timeMap.put(CONSOLIDATION_MIN, cal.getTime());
            for (Map<String, Object> data : stats) {
                data.put(STAT_TIMESTAMP, timeMap);
            }
            client.storeData(stats);
        }
        dataCount = coll.count();
    }

    @Test
    public void correctQueryTest() {
        for (GathererAttributes attribute : client.getAttributes()) {
            String name = attribute.getName();
            List<Map<String, Object>> data = grain.gathererFilter(name).get(0);
            int size = 0;
            if (data != null) {
                size = data.size();
            }
            assertTrue("the query did not return the correct number of objects: 5 vs. " + size, size == 5);
        }
    }

    @Test
    public void noDeletedDataTest() {
        long count = coll.count();
        assertTrue(
                "data was deleted by consolidatation: dataCount before: " + dataCount + "dataCount now: " + count,
                count >= dataCount);
    }

    @Test
    public void consolidationMethods() {
        List<Map<String, Object>> consolidatedData = grain.consolidateData();
        assertTrue("the consolidated data is null", consolidatedData != null);
        String methods[] = { CONSOLIDATION_AVG, CONSOLIDATION_MAX, CONSOLIDATION_MIN };
        DescriptiveStatistics statsMaker = new DescriptiveStatistics();
        statsMaker.addValue(111);
        statsMaker.addValue(11);
        statsMaker.addValue(1);
        Map<String, Object> statData = new HashMap<String, Object>();
        for (String method : methods) {
            grain.methodMapBuilder(method, statsMaker, statData);
        }
        assertTrue("methodMapBuilder returned null", statData != null);
    }

}