package org.opennms.netmgt.eventd;

import junit.framework.TestCase;

import org.exolab.castor.xml.ValidationException;
import org.opennms.test.ConfigurationTestUtils;

public class EventConfigurationManagerTest extends TestCase {
    public void testLoadConfigurationSingleConfig() throws Exception {
        EventConfigurationManager.loadConfiguration(ConfigurationTestUtils.getReaderForResource(this, "/org/opennms/netmgt/config/eventd/singleConfig/eventconf.xml"));
    }

    public void testLoadConfigurationTwoDeepConfig() throws Exception {
        EventConfigurationManager.loadConfiguration(ConfigurationTestUtils.getReaderForResource(this, "/org/opennms/netmgt/config/eventd/twoDeepConfig/eventconf.xml"));
    }

    public void testLoadConfigurationThreeDeepConfig() throws Exception {
        boolean caughtExceptionThatWeWanted = false;
        
        try {
            EventConfigurationManager.loadConfiguration(ConfigurationTestUtils.getReaderForResource(this, "/org/opennms/netmgt/config/eventd/threeDeepConfig/eventconf.xml"));
        } catch (ValidationException e) {
            if (e.getMessage().contains("cannot include other configuration files")) {
                caughtExceptionThatWeWanted = true;
            } else {
                throw e;
            }
        }
        
        if (!caughtExceptionThatWeWanted) {
            fail("Did not get the exception that we wanted");
        }
    }
    
    public void testLoadConfigurationTwoDeepConfigWithGlobal() throws Exception {
        boolean caughtExceptionThatWeWanted = false;
        
        try {
            EventConfigurationManager.loadConfiguration(ConfigurationTestUtils.getReaderForResource(this, "/org/opennms/netmgt/config/eventd/twoDeepConfigWithGlobal/eventconf.xml"));
        } catch (ValidationException e) {
            if (e.getMessage().contains("cannot have a 'global' element")) {
                caughtExceptionThatWeWanted = true;
            } else {
                throw e;
            }
        }
        
        if (!caughtExceptionThatWeWanted) {
            fail("Did not get the exception that we wanted");
        }
    }
}

@@ -41,82 +41,306 @@
 rt j
-unit.framework.TestCase;%0A%0Aimport org.exolab.castor.xml.ValidationException
+ava.io.File;%0Aimport java.io.FilenameFilter;%0Aimport java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport junit.framework.TestCase;%0A%0Aimport org.exolab.castor.xml.ValidationException;%0Aimport org.opennms.netmgt.dao.castor.CastorUtils;%0Aimport org.opennms.netmgt.xml.eventconf.Events
 ;%0Aim
@@ -384,16 +384,61 @@
 stUtils;
+%0Aimport org.springframework.util.StringUtils;
 %0A%0Apublic
@@ -2412,10 +2412,2221 @@
 %7D%0A    %7D%0A
+    %0A    /**%0A     * This is disabled because the last check fails.%0A     */%0A    public void DISABLEDtestIncludedEventFilesExistAndNoExtras() throws Exception %7B%0A        File eventConfFile = ConfigurationTestUtils.getFileForConfigFile(%22eventconf.xml%22);%0A        File eventsDirFile = new File(eventConfFile.getParentFile(), %22events%22);%0A        assertTrue(%22events directory exists at %22 + eventsDirFile.getAbsolutePath(), eventsDirFile.exists());%0A        assertTrue(%22events directory is a directory at %22 + eventsDirFile.getAbsolutePath(), eventsDirFile.isDirectory());%0A        %0A        File%5B%5D eventFilesOnDiskArray = eventsDirFile.listFiles(new FilenameFilter() %7B%0A            public boolean accept(File file, String name) %7B%0A                return name.endsWith(%22.xml%22);%0A            %7D %7D);%0A        Set%3CFile%3E eventFilesOnDisk = new HashSet%3CFile%3E(Arrays.asList(eventFilesOnDiskArray));%0A%0A        Events events = CastorUtils.unmarshal(Events.class, ConfigurationTestUtils.getReaderForConfigFile(%22eventconf.xml%22));%0A        Set%3CFile%3E eventFilesIncluded = new HashSet%3CFile%3E(events.getEventFileCollection().size());%0A        for (String eventFile : events.getEventFileCollection()) %7B%0A            eventFilesIncluded.add(new File(eventConfFile.getParentFile(), eventFile.replaceAll(%22@install.etc.dir@/%22, %22%22)));%0A        %7D%0A        %0A        Set%3CFile%3E includedNotOnDisk = new HashSet%3CFile%3E(eventFilesIncluded);%0A        includedNotOnDisk.removeAll(eventFilesOnDisk);%0A        if (!includedNotOnDisk.isEmpty()) %7B%0A            fail(%22Event configuration file %22 + eventConfFile.getAbsolutePath() + %22 references included files that could not be found:%5Cn%5Ct%22%0A                    + StringUtils.collectionToDelimitedString(includedNotOnDisk, %22%5Cn%5Ct%22));%0A        %7D%0A        %0A        Set%3CFile%3E onDiskNotIncluded = new HashSet%3CFile%3E(eventFilesOnDisk);%0A        onDiskNotIncluded.removeAll(eventFilesIncluded);%0A        if (!onDiskNotIncluded.isEmpty()) %7B%0A            fail(%22Events directory %22 + eventsDirFile.getAbsolutePath() + %22 contains event files that are not referenced in event configuration file %22 + eventConfFile.getAbsolutePath() + %22:%5Cn%5Ct%22%0A                    + StringUtils.collectionToDelimitedString(onDiskNotIncluded, %22%5Cn%5Ct%22));%0A        %7D%0A    %7D%0A
 %7D%0A

Test that included event files exist and that there are no extra event files that exist but are not used.