package org.opennms.netmgt.jasper.helper;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.List;

import org.junit.Test;

public class ResourcePathFileTraversalTest {
    
    private String m_baseDir = "src/test/resources/share/rrd/snmp";
    private String m_resourceName = "nsVpnMonitor";
    private int m_nodeId = 10;
    
    @Test
    public void testFindAllPathsRecursive() {
        ResourcePathFileTraversal traverser = new ResourcePathFileTraversal(new File(m_baseDir + "/" + m_nodeId + "/" + m_resourceName));
        List<String> paths = traverser.traverseDirectory();
        
        assertEquals("/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_1", paths.get(0));
        assertEquals("/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_2", paths.get(1));
    }
    
    @Test
    public void testFindTopLevelOnlyIfPassesFilenameCheck() {
        ResourcePathFileTraversal traverser = new ResourcePathFileTraversal(new File(m_baseDir + "/" + m_nodeId + "/opennms-jvm"));
        traverser.addDatasourceFilter("ThreadCount");
        traverser.addDatasourceFilter("Bogus");
        List<String> paths = traverser.traverseDirectory();
        
        assertEquals(0, paths.size());
    }
    
    @Test
    public void testFindPathsWithFilterOneFile() {
        ResourcePathFileTraversal traverser = new ResourcePathFileTraversal(new File(m_baseDir + "/" + m_nodeId + "/" + m_resourceName));
        traverser.addDatasourceFilter("http-8980");
        List<String> paths = traverser.traverseDirectory();
        
        
        assertEquals(2, paths.size());
        assertEquals("/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_2", paths.get(0));
    }
    
    @Test
    public void testFindPathsWithAndFilter() {
        ResourcePathFileTraversal traverser = new ResourcePathFileTraversal(new File(m_baseDir + "/" + m_nodeId + "/" + m_resourceName));
        traverser.addDatasourceFilter("http");
        traverser.addDatasourceFilter("icmp");
        List<String> paths = traverser.traverseDirectory();
        
        assertEquals(1, paths.size());
        assertEquals("/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_3", paths.get(0));
    }
    
    @Test
    public void testFindPathsWithFilterNoExtensions() {
        ResourcePathFileTraversal traverser = new ResourcePathFileTraversal(new File(m_baseDir + "/" + m_nodeId + "/" + m_resourceName));
        traverser.addDatasourceFilter("http");
        traverser.addDatasourceFilter("icmp");
        List<String> paths = traverser.traverseDirectory();
        
        assertEquals(1, paths.size());
        assertEquals("/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_3", paths.get(0));
    }
    
    @Test
    public void testFindPathsWithStoreByGroup() {
        System.setProperty("org.opennms.rrd.storeByGroup", "true");
        ResourcePathFileTraversal traverser = new ResourcePathFileTraversal(new File(m_baseDir + "/" + m_nodeId + "/storeby-group-jvm"));
        traverser.addDatasourceFilter("TotalMemory");
        List<String> paths = traverser.traverseDirectory();
        
        assertEquals(1, paths.size());
        assertEquals("/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/src/test/resources/share/rrd/snmp/10/storeby-group-jvm", paths.get(0));
        
        
        ResourcePathFileTraversal traverser2 = new ResourcePathFileTraversal(new File(m_baseDir + "/" + m_nodeId + "/storeby-group-jvm"));
        traverser2.addDatasourceFilter("Bogus");
        paths = traverser2.traverseDirectory();
        
        assertEquals(0, paths.size());

    }

}

@@ -80,16 +80,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Aimport
@@ -681,85 +681,37 @@
 sert
-Equals(%22/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/
+True(paths.get(0).matches(%22.*
 src/
@@ -765,29 +765,16 @@
 un_id_1%22
-, paths.get(0
 ));%0A    
@@ -787,85 +787,37 @@
 sert
-Equals(%22/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/
+True(paths.get(1).matches(%22.*
 src/
@@ -875,21 +875,8 @@
 d_2%22
-, paths.get(1
 ));%0A
@@ -1697,85 +1697,37 @@
 sert
-Equals(%22/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/
+True(paths.get(0).matches(%22.*
 src/
@@ -1781,29 +1781,16 @@
 un_id_2%22
-, paths.get(0
 ));%0A    
@@ -2211,85 +2211,37 @@
 sert
-Equals(%22/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/
+True(paths.get(0).matches(%22.*
 src/
@@ -2291,37 +2291,24 @@
 or/tun_id_3%22
-, paths.get(0
 ));%0A    %7D%0A  
@@ -2734,85 +2734,37 @@
 sert
-Equals(%22/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/
+True(paths.get(0).matches(%22.*
 src/
@@ -2818,29 +2818,16 @@
 un_id_3%22
-, paths.get(0
 ));%0A    
@@ -3279,85 +3279,37 @@
 sert
-Equals(%22/Users/thedesloge/git/opennms/integrations/opennms-jasper-extensions/
+True(paths.get(0).matches(%22.*
 src/
@@ -3363,33 +3363,11 @@
 jvm%22
-, paths.get(0));%0A        
+));
 %0A   

fix broken tests