org.opentestsystem.shared.monitoringalerting.service.MetricServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.shared.monitoringalerting.service.MetricServiceTest.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.shared.monitoringalerting.service;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.beanutils.BeanUtils;
import org.joda.time.DateTime;
import org.junit.Test;
import org.opentestsystem.shared.monitoringalerting.AbstractPersistenceEmbeddedTest;
import org.opentestsystem.shared.monitoringalerting.domain.CategoryType;
import org.opentestsystem.shared.monitoringalerting.domain.MAException;
import org.opentestsystem.shared.monitoringalerting.domain.Metric;
import org.opentestsystem.shared.monitoringalerting.domain.search.MetricSearchRequest;
import org.opentestsystem.shared.monitoringalerting.gateway.MetricService;
import org.opentestsystem.shared.search.domain.SearchResponse;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.AvoidCatchingGenericException",
        "PMD.JUnitTestContainsTooManyAsserts", "PMD.TooManyMethods" })
public class MetricServiceTest extends AbstractPersistenceEmbeddedTest {

    @Autowired
    private MetricService metricService;

    private static final String CURRENT_PAGE = "currentPage";
    private static final String ALT_KEY_SERVER = "alternateKey.server";
    private static final String ALT_KEY_NODE = "alternateKey.node";
    private static final String COOL = "cool";
    private static final String ONE_HUNDRED = "100";
    private static final String INCORRECT_FILTER = "search did not filter objects correctly";
    private static final String METRIC_VALUE = "metricValue";
    private static final String WRONG_NUM_RESULTS = "wrong number of search results";
    private static final String WRONG_RESP_COUNT = "wrong response count";
    private static final String WRONG_RES_COUNT = "wrong number of results count";

    private Map<String, String[]> getTestPagingParams(final String pageSize) {
        Map<String, String[]> requestMap = new HashMap<String, String[]>();
        requestMap.put("pageSize", new String[] { pageSize });
        requestMap.put(CURRENT_PAGE, new String[] { "0" });
        requestMap.put("sortDir", new String[] { "asc" });
        requestMap.put("sortKey", new String[] { "server" });
        return requestMap;
    }

    @Test
    public void testStringFilters()
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        List<String> keys = Arrays.asList(
                new String[] { ALT_KEY_SERVER, ALT_KEY_NODE, "severity", "component", "message", "metricName" });

        for (String filter : keys) {
            createMetrics(10, filter, COOL);
            createMetrics(10, filter, "bogus");

            Map<String, String[]> requestMap = getTestPagingParams(ONE_HUNDRED);
            requestMap.put(filter, new String[] { COOL });

            MetricSearchRequest searchRequest = new MetricSearchRequest(requestMap);

            SearchResponse<Metric> response = metricService.searchMetrics(searchRequest);

            assertEquals("wrong response count for filter " + filter, 10, response.getReturnCount());
            assertEquals("wrong number of results count " + filter, 10, response.getSearchResults().size());
            for (Metric result : response.getSearchResults()) {
                assertEquals(INCORRECT_FILTER, COOL, BeanUtils.getProperty(result, filter));
            }

        }
    }

    @Test
    public void testNumericFilters()
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        List<String> keys = Arrays.asList(new String[] { METRIC_VALUE });
        for (String filter : keys) {
            createMetrics(12, METRIC_VALUE, 10);
            createMetrics(10, METRIC_VALUE, 20);

            Map<String, String[]> requestMap = getTestPagingParams(ONE_HUNDRED);
            requestMap.put(METRIC_VALUE, new String[] { "10" });

            MetricSearchRequest searchRequest = new MetricSearchRequest(requestMap);

            SearchResponse<Metric> response = metricService.searchMetrics(searchRequest);

            assertEquals("wrong response count for filter " + filter, 12, response.getReturnCount());
            assertEquals("wrong number of results count " + filter, 12, response.getSearchResults().size());
            for (Metric result : response.getSearchResults()) {
                assertEquals(INCORRECT_FILTER, "10", BeanUtils.getProperty(result, filter));
            }
        }

    }

    @Test
    public void testDateFilters() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

        Metric metric = FACTORY.manufacturePojo(Metric.class);
        DateTime feb1513 = new DateTime("2013-02-15T15:40:38.213-06:00");
        for (int integer = 1; integer <= 10; integer++) {
            metric.setId(null);
            DateTime newDate = feb1513.withYear(2013 + integer);
            metric.setInsertTimestamp(newDate);
            mongoTemplate.save(metric);
        }

        DateTime startDate = new DateTime("2015-02-15T15:40:38.213-06:00");
        DateTime endDate = new DateTime("2020-02-15T15:40:38.213-06:00");

        Map<String, String[]> requestMap = getTestPagingParams(ONE_HUNDRED);
        requestMap.put("insertTimestampGreaterThan", new String[] { "2015-02-15T15:40:38.213-06:00" });
        requestMap.put("insertTimestampLessThan", new String[] { "2020-02-15T15:40:38.213-06:00" });

        MetricSearchRequest searchRequest = new MetricSearchRequest(requestMap);

        SearchResponse<Metric> response = metricService.searchMetrics(searchRequest);

        assertEquals(WRONG_NUM_RESULTS, 4, response.getSearchResults().size());
        assertEquals(WRONG_NUM_RESULTS, 4, response.getReturnCount());

        requestMap.put("insertTimestampMillisGreaterThan", new String[] { Long.toString(startDate.getMillis()) });
        requestMap.put("insertTimestampMillisLessThan", new String[] { Long.toString(endDate.getMillis()) });

        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);
        assertEquals(WRONG_NUM_RESULTS, 4, response.getSearchResults().size());
        assertEquals(WRONG_NUM_RESULTS, 4, response.getReturnCount());
    }

    @Test
    public void testMultipleFilters() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(ALT_KEY_NODE, COOL);
        params.put(ALT_KEY_SERVER, "serverA");

        Map<String, Object> params2 = new HashMap<String, Object>();
        params2.put(ALT_KEY_NODE, "uncool");
        params2.put(ALT_KEY_SERVER, "serverB");

        Map<String, Object> params3 = new HashMap<String, Object>();
        params3.put(ALT_KEY_NODE, COOL);
        params3.put(ALT_KEY_SERVER, "serverC");

        createMetrics(10, params);
        createMetrics(10, params2);
        createMetrics(10, params3);

        Map<String, String[]> requestMap = getTestPagingParams(ONE_HUNDRED);
        requestMap.put(ALT_KEY_NODE, new String[] { COOL });
        requestMap.put(ALT_KEY_SERVER, new String[] { "serverc" });

        MetricSearchRequest searchRequest = new MetricSearchRequest(requestMap);

        SearchResponse<Metric> response = metricService.searchMetrics(searchRequest);

        assertEquals(WRONG_RESP_COUNT, 10, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 10, response.getSearchResults().size());
        for (Metric result : response.getSearchResults()) {
            assertEquals(INCORRECT_FILTER, COOL, result.getNode());
            assertEquals(INCORRECT_FILTER, "serverc", result.getServer());
        }

        requestMap.remove(ALT_KEY_SERVER);
        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);

        assertEquals(WRONG_RESP_COUNT, 20, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 20, response.getSearchResults().size());
        for (Metric result : response.getSearchResults()) {
            assertEquals(INCORRECT_FILTER, COOL, result.getNode());
        }

        requestMap.put(ALT_KEY_NODE, new String[] { COOL });
        requestMap.put(ALT_KEY_SERVER, new String[] { "serverb" });
        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);

        assertEquals(WRONG_RESP_COUNT, 0, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 0, response.getSearchResults().size());

    }

    @Test
    public void testPagingFeatures() {
        HashMap<String, Metric> page1Map = new HashMap<String, Metric>();
        HashMap<String, Metric> page2Map = new HashMap<String, Metric>();
        HashMap<String, Metric> page3Map = new HashMap<String, Metric>();
        HashMap<String, Metric> page4Map = new HashMap<String, Metric>();

        createMetrics(10, ALT_KEY_NODE, COOL);
        createMetrics(10, ALT_KEY_NODE, "uncool");

        Map<String, String[]> requestMap = getTestPagingParams("3");
        requestMap.put(ALT_KEY_NODE, new String[] { COOL });

        MetricSearchRequest searchRequest = new MetricSearchRequest(requestMap);

        SearchResponse<Metric> response = metricService.searchMetrics(searchRequest);

        assertEquals(WRONG_RESP_COUNT, 3, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 3, response.getSearchResults().size());

        for (Metric result : response.getSearchResults()) {
            page1Map.put(result.getId(), result);
        }

        requestMap.put(CURRENT_PAGE, new String[] { "1" });
        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);
        assertEquals(WRONG_RESP_COUNT, 3, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 3, response.getSearchResults().size());

        for (Metric result : response.getSearchResults()) {
            page2Map.put(result.getId(), result);
        }

        requestMap.put(CURRENT_PAGE, new String[] { "2" });
        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);
        assertEquals(WRONG_RESP_COUNT, 3, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 3, response.getSearchResults().size());

        for (Metric result : response.getSearchResults()) {
            page3Map.put(result.getId(), result);
        }

        requestMap.put(CURRENT_PAGE, new String[] { "3" });
        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);
        assertEquals(WRONG_RESP_COUNT, 1, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 1, response.getSearchResults().size());

        for (Metric result : response.getSearchResults()) {
            page4Map.put(result.getId(), result);
        }

        requestMap.put(CURRENT_PAGE, new String[] { "4" });
        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);
        assertEquals(WRONG_RESP_COUNT, 0, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 0, response.getSearchResults().size());

        requestMap.put(CURRENT_PAGE, new String[] { "90000" });
        searchRequest = new MetricSearchRequest(requestMap);
        response = metricService.searchMetrics(searchRequest);
        assertEquals(WRONG_RESP_COUNT, 0, response.getReturnCount());
        assertEquals(WRONG_RES_COUNT, 0, response.getSearchResults().size());

        HashMap<String, Metric> allMetrics = new HashMap<String, Metric>();
        allMetrics.putAll(page1Map);
        allMetrics.putAll(page2Map);
        allMetrics.putAll(page3Map);
        allMetrics.putAll(page4Map);
        assertEquals("should be 10 unique items", 10, allMetrics.size());
    }

    @Test
    public void testNoFilter() {
        createMetrics(10, "node", COOL);
        createMetrics(10, "node", "uncool");

        Map<String, String[]> requestMap = getTestPagingParams(ONE_HUNDRED);

        MetricSearchRequest searchRequest = new MetricSearchRequest(requestMap);

        SearchResponse<Metric> response = metricService.searchMetrics(searchRequest);

        assertEquals("wrong response count should get all items", 20, response.getReturnCount());
        assertEquals("wrong number of results should get all items", 20, response.getSearchResults().size());

    }

    @Test
    public void testCreateMetricWithIntegerValue() {
        Metric newMetric = new Metric();
        newMetric.setMetricName("testMetric");
        newMetric.setMetricType(CategoryType.UTILIZATION);
        newMetric.setMetricValue(10);
        newMetric.setNode("node1");
        newMetric.setServer("server1");
        newMetric.setComponent("unitTest");

        Metric retMetric = metricService.addMetric(newMetric);

        assertThat(retMetric.getMetricValue().intValue(), is(10));

    }

    @Test
    public void testCreateMetricWithDoubleValue() {
        Metric newMetric = new Metric();
        newMetric.setMetricName("testMetric");
        newMetric.setMetricType(CategoryType.UTILIZATION);
        newMetric.setMetricValue(3.14159d);
        newMetric.setNode("node1");
        newMetric.setServer("server1");
        newMetric.setComponent("unitTest");

        Metric retMetric = metricService.addMetric(newMetric);

        assertThat(retMetric.getMetricValue().doubleValue(), is(3.14159d));
    }

    @Test
    public void testVerifyInsertTimestamp() {
        Metric newMetric = new Metric();
        newMetric.setMetricName("testMetric");
        newMetric.setMetricType(CategoryType.UTILIZATION);
        newMetric.setMetricValue(1);
        newMetric.setNode("node1");
        newMetric.setServer("server1");
        newMetric.setComponent("unitTest");

        Metric retMetric = metricService.addMetric(newMetric);

        assertThat(retMetric.getInsertTimestamp(), notNullValue());
    }

    private void createMetrics(final int count, final String property, final Object value) {
        for (int i = 0; i < count; i++) {
            Metric metric = FACTORY.manufacturePojo(Metric.class);
            metric.setId(null);
            try {
                BeanUtils.setProperty(metric, property, value);
            } catch (Exception e) {
                throw new MAException("error with data setup.", e);
            }
            metricService.addMetric(metric);
        }
    }

    private void createMetrics(final int count, final Map<String, Object> values) {
        for (int i = 0; i < count; i++) {
            Metric metric = FACTORY.manufacturePojo(Metric.class);
            metric.setId(null);

            try {
                for (Entry<String, Object> entry : values.entrySet()) {
                    BeanUtils.setProperty(metric, entry.getKey(), entry.getValue());
                }
            } catch (Exception e) {
                throw new MAException("error with data setup.", e);
            }
            metricService.addMetric(metric);
        }
    }

}