/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2009-2011 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2011 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.web.notification;
import static org.junit.Assert.assertEquals;
import java.util.Date;
import java.util.Properties;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.test.MockLogAppender;
import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
import org.opennms.core.utils.BeanUtils;
import org.opennms.netmgt.dao.DatabasePopulator;
import org.opennms.netmgt.dao.db.JUnitConfigurationEnvironment;
import org.opennms.netmgt.dao.db.JUnitTemporaryDatabase;
import org.opennms.web.filter.Filter;
import org.opennms.web.notification.filter.AcknowledgedByFilter;
import org.opennms.web.notification.filter.InterfaceFilter;
import org.opennms.web.notification.filter.NodeFilter;
import org.opennms.web.notification.filter.NotificationCriteria;
import org.opennms.web.notification.filter.NotificationIdFilter;
import org.opennms.web.notification.filter.NotificationIdListFilter;
import org.opennms.web.notification.filter.ResponderFilter;
import org.opennms.web.notification.filter.ServiceFilter;
import org.opennms.web.notification.filter.UserFilter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"classpath:/META-INF/opennms/applicationContext-dao.xml",
"classpath*:/META-INF/opennms/component-dao.xml",
"classpath*:/META-INF/opennms/component-service.xml",
"classpath:/daoWebRepositoryTestContext.xml",
"classpath:/jdbcWebRepositoryTestContext.xml",
"classpath:/META-INF/opennms/applicationContext-minimal-conf.xml"
})
@JUnitConfigurationEnvironment
@JUnitTemporaryDatabase
public class WebNotificationRepositoryFilterTest implements InitializingBean {
@Autowired
DatabasePopulator m_dbPopulator;
@Autowired
@Qualifier("dao")
WebNotificationRepository m_daoNotificationRepo;
@Autowired
@Qualifier("jdbc")
WebNotificationRepository m_jdbcNotificationRepo;
@Override
public void afterPropertiesSet() throws Exception {
BeanUtils.assertAutowiring(this);
}
@BeforeClass
public static void setUpLogging(){
Properties props = new Properties();
props.setProperty("log4j.logger.org.hibernate", "INFO");
props.setProperty("log4j.logger.org.springframework", "INFO");
props.setProperty("log4j.logger.org.hibernate.SQL", "DEBUG");
MockLogAppender.setupLogging(props);
}
@Before
public void setUp(){
m_dbPopulator.populateDatabase();
}
@Test
@Transactional
public void testAcknowledgeByFilter(){
m_daoNotificationRepo.acknowledgeMatchingNotification("TestUser", new Date(), new NotificationCriteria());
AcknowledgedByFilter filter = new AcknowledgedByFilter("TestUser");
assert1Result(filter);
}
@Test
@Transactional
public void testInterfaceFilter(){
InterfaceFilter filter = new InterfaceFilter("192.168.1.1");
assert1Result(filter);
}
@Test
@JUnitTemporaryDatabase // Relies on specific IDs so we need a fresh database
public void testNodeFilter(){
NodeFilter filter = new NodeFilter(1);
assert1Result(filter);
}
@Test
@JUnitTemporaryDatabase // Relies on specific IDs so we need a fresh database
public void testNotificationIdFilter(){
NotificationIdFilter filter = new NotificationIdFilter(1);
assert1Result(filter);
}
@Test
@JUnitTemporaryDatabase // Relies on specific IDs so we need a fresh database
public void testNotificationIdListFilter(){
int[] ids = {1};
NotificationIdListFilter filter = new NotificationIdListFilter(ids);
assert1Result(filter);
}
@Test
@Transactional
public void testResponderFilter(){
m_daoNotificationRepo.acknowledgeMatchingNotification("TestUser", new Date(), new NotificationCriteria());
ResponderFilter filter = new ResponderFilter("TestUser");
assert1Result(filter);
}
@Test
@JUnitTemporaryDatabase // Relies on specific IDs so we need a fresh database
public void testServiceFilter(){
Notification[] notifs = m_daoNotificationRepo.getMatchingNotifications(new NotificationCriteria());
System.out.println(notifs[0].getServiceId());
ServiceFilter filter = new ServiceFilter(1);
assert1Result(filter);
}
@Test
@Transactional
public void testUserFilter(){
UserFilter filter = new UserFilter("TestUser");
assert1Result(filter);
}
private void assert1Result(Filter filter){
System.out.println(filter.getSql());
NotificationCriteria criteria = new NotificationCriteria(filter);
Notification[] notifs = m_daoNotificationRepo.getMatchingNotifications(criteria);
assertEquals(1, notifs.length);
notifs = m_jdbcNotificationRepo.getMatchingNotifications(criteria);
assertEquals(1, notifs.length);
}
}