Java tutorial
/******************************************************************************* * 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.junit.Assert.assertEquals; import java.lang.reflect.InvocationTargetException; 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.junit.Test; import org.opentestsystem.shared.monitoringalerting.AbstractPersistenceEmbeddedTest; import org.opentestsystem.shared.monitoringalerting.domain.Alert; import org.opentestsystem.shared.monitoringalerting.domain.MAException; import org.opentestsystem.shared.monitoringalerting.domain.search.AlertSearchRequest; import org.opentestsystem.shared.monitoringalerting.gateway.AlertService; import org.opentestsystem.shared.search.domain.SearchFilter; import org.opentestsystem.shared.search.domain.SearchFilter.SearchDataType; import org.opentestsystem.shared.search.domain.SearchResponse; import org.springframework.beans.factory.annotation.Autowired; @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseConcurrentHashMap", "PMD.AvoidCatchingGenericException" }) public class AlertServiceTest extends AbstractPersistenceEmbeddedTest { @Autowired private AlertService alertService; private static final String COOL = "cool"; private static final String PAGE_SIZE = "pageSize"; private static final String CURRENT_PAGE = "currentPage"; private static final String SORT_DIR = "sortDir"; private static final String ASC = "asc"; private static final String SORT_KEY = "sortKey"; private static final String SERVER = "server"; private static final String WRONG_RESP_COUNT = "wrong response count"; private static final String WRONG_RESULTS_COUNT = "wrong number of results count"; private static final String SEARCH_FILTER_BAD = "search did not filter objects correctly"; private static final String ALERT_TYPE = "alertType"; @Test public void testStringFilters() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { List<SearchFilter> keys = new AlertSearchRequest(new HashMap<String, String[]>()).getSearchFilters(); for (SearchFilter filter : keys) { if (filter.getDataType().equals(SearchDataType.String)) { createAlerts(10, filter.getSearchApiName(), COOL); createAlerts(10, filter.getSearchApiName(), "bogus"); Map<String, String[]> requestMap = new HashMap<String, String[]>(); // NOPMD requestMap.put(PAGE_SIZE, new String[] { "100" }); requestMap.put(CURRENT_PAGE, new String[] { "0" }); requestMap.put(SORT_DIR, new String[] { ASC }); requestMap.put(SORT_KEY, new String[] { SERVER }); requestMap.put(filter.getSearchApiName(), new String[] { COOL }); AlertSearchRequest searchRequest = new AlertSearchRequest(requestMap); SearchResponse<Alert> response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 10, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 10, response.getSearchResults().size()); for (Alert result : response.getSearchResults()) { assertEquals(SEARCH_FILTER_BAD, COOL, BeanUtils.getProperty(result, filter.getSearchApiName())); } } } } @Test public void testMultipleFilters() { Map<String, Object> params = new HashMap<String, Object>(); params.put(ALERT_TYPE, COOL); params.put("alternateKey.server", "serverA"); Map<String, Object> params2 = new HashMap<String, Object>(); params2.put(ALERT_TYPE, "uncool"); params2.put("alternateKey.server", "serverB"); Map<String, Object> params3 = new HashMap<String, Object>(); params3.put(ALERT_TYPE, COOL); params3.put("alternateKey.server", "serverC"); createAlerts(10, params); createAlerts(10, params2); createAlerts(10, params3); Map<String, String[]> requestMap = new HashMap<String, String[]>(); requestMap.put(PAGE_SIZE, new String[] { "100" }); requestMap.put(CURRENT_PAGE, new String[] { "0" }); requestMap.put(SORT_KEY, new String[] { SERVER }); requestMap.put(SORT_DIR, new String[] { ASC }); requestMap.put(ALERT_TYPE, new String[] { COOL }); requestMap.put(SERVER, new String[] { "serverc" }); AlertSearchRequest searchRequest = new AlertSearchRequest(requestMap); SearchResponse<Alert> response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 10, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 10, response.getSearchResults().size()); for (Alert result : response.getSearchResults()) { assertEquals(SEARCH_FILTER_BAD, COOL, result.getAlertType()); assertEquals(SEARCH_FILTER_BAD, "serverc", result.getServer()); } requestMap.remove(SERVER); searchRequest = new AlertSearchRequest(requestMap); response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 20, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 20, response.getSearchResults().size()); for (Alert result : response.getSearchResults()) { assertEquals(SEARCH_FILTER_BAD, COOL, result.getAlertType()); } requestMap.put(ALERT_TYPE, new String[] { COOL }); requestMap.put(SERVER, new String[] { "serverb" }); searchRequest = new AlertSearchRequest(requestMap); response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 0, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 0, response.getSearchResults().size()); } @Test public void testPagingFeatures() { HashMap<String, Alert> page1Map = new HashMap<String, Alert>(); HashMap<String, Alert> page2Map = new HashMap<String, Alert>(); HashMap<String, Alert> page3Map = new HashMap<String, Alert>(); HashMap<String, Alert> page4Map = new HashMap<String, Alert>(); createAlerts(10, ALERT_TYPE, COOL); createAlerts(10, ALERT_TYPE, "uncool"); Map<String, String[]> requestMap = new HashMap<String, String[]>(); requestMap.put(PAGE_SIZE, new String[] { "3" }); requestMap.put(CURRENT_PAGE, new String[] { "0" }); requestMap.put(SORT_DIR, new String[] { ASC }); requestMap.put(SORT_KEY, new String[] { SERVER }); requestMap.put(ALERT_TYPE, new String[] { COOL }); AlertSearchRequest searchRequest = new AlertSearchRequest(requestMap); SearchResponse<Alert> response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 3, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 3, response.getSearchResults().size()); for (Alert result : response.getSearchResults()) { page1Map.put(result.getId(), result); } requestMap.put(CURRENT_PAGE, new String[] { "1" }); searchRequest = new AlertSearchRequest(requestMap); response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 3, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 3, response.getSearchResults().size()); for (Alert result : response.getSearchResults()) { page2Map.put(result.getId(), result); } requestMap.put(CURRENT_PAGE, new String[] { "2" }); searchRequest = new AlertSearchRequest(requestMap); response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 3, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 3, response.getSearchResults().size()); for (Alert result : response.getSearchResults()) { page3Map.put(result.getId(), result); } requestMap.put(CURRENT_PAGE, new String[] { "3" }); searchRequest = new AlertSearchRequest(requestMap); response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 1, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 1, response.getSearchResults().size()); for (Alert result : response.getSearchResults()) { page4Map.put(result.getId(), result); } requestMap.put(CURRENT_PAGE, new String[] { "4" }); searchRequest = new AlertSearchRequest(requestMap); response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 0, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 0, response.getSearchResults().size()); requestMap.put(CURRENT_PAGE, new String[] { "90000" }); searchRequest = new AlertSearchRequest(requestMap); response = alertService.searchAlerts(searchRequest); assertEquals(WRONG_RESP_COUNT, 0, response.getReturnCount()); assertEquals(WRONG_RESULTS_COUNT, 0, response.getSearchResults().size()); HashMap<String, Alert> allAlerts = new HashMap<String, Alert>(); allAlerts.putAll(page1Map); allAlerts.putAll(page2Map); allAlerts.putAll(page3Map); allAlerts.putAll(page4Map); assertEquals("should be 10 unique items", 10, allAlerts.size()); } @Test public void testNoFilter() { createAlerts(10, ALERT_TYPE, COOL); createAlerts(10, ALERT_TYPE, "uncool"); Map<String, String[]> requestMap = new HashMap<String, String[]>(); requestMap.put(PAGE_SIZE, new String[] { "100" }); requestMap.put(CURRENT_PAGE, new String[] { "0" }); requestMap.put(SORT_DIR, new String[] { ASC }); requestMap.put(SORT_KEY, new String[] { SERVER }); AlertSearchRequest searchRequest = new AlertSearchRequest(requestMap); SearchResponse<Alert> response = alertService.searchAlerts(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()); } private void createAlerts(final int count, final String property, final String value) { for (int i = 0; i < count; i++) { Alert alert = FACTORY.manufacturePojo(Alert.class); alert.setId(null); try { BeanUtils.setProperty(alert, property, value); } catch (Exception e) { throw new MAException("error with data setup.", e); } alertService.addAlert(alert); } } private void createAlerts(final int count, final Map<String, Object> values) { for (int i = 0; i < count; i++) { Alert alert = FACTORY.manufacturePojo(Alert.class); alert.setId(null); try { for (Entry<String, Object> entry : values.entrySet()) { BeanUtils.setProperty(alert, entry.getKey(), entry.getValue()); } } catch (Exception e) { throw new MAException("error with data setup.", e); } alertService.addAlert(alert); } } }