package org.opennms.netmgt.correlation.drools;

import java.util.ArrayList;
import java.util.List;

import static org.easymock.EasyMock.expect;

import org.easymock.EasyMock;
import org.opennms.netmgt.EventConstants;
import org.opennms.netmgt.utils.EventBuilder;
import org.opennms.netmgt.xml.event.Event;


public class NodeParentRulesTest extends CorrelationRulesTestCase {
    
    List<Object> mocks = new ArrayList<Object>();
    
    public <T> T createMock(Class<T> mockClass) {
        T mock = EasyMock.createMock(mockClass);
        mocks.add(mock);
        return mock;
    }
    
    public void replay() {
        EasyMock.replay(mocks.toArray());
    }
    
    public void verify(DroolsCorrelationEngine engine) {
        EasyMock.verify(mocks.toArray());
        EasyMock.reset(mocks.toArray());
        super.verify(engine);
    }
    
    public void testParentNodeDown() throws Exception {
        
        //anticipate(createRootCauseEvent(1, 1));
        
        NodeService nodeService = createMock(NodeService.class);
        
        expect(nodeService.getParentNode(1L)).andReturn(null);
        
        replay();
        
        DroolsCorrelationEngine engine = findEngineByName("nodeParentRules");
        engine.setGlobal("nodeService", nodeService);
        
        engine.correlate(createNodeDownEvent(1));

        // event + root cause
        m_anticipatedMemorySize = 2;
        
        verify(engine);
        
        anticipate(createRootCauseResolvedEvent(1, 1));
        
    }
    
    private Event createRootCauseResolvedEvent(int symptom, int cause) {
        return new EventBuilder(createNodeEvent("rootCauseResolved", cause)).getEvent();

    }

    private Event createRootCauseEvent(int symptom, int cause) {
        return new EventBuilder(createNodeEvent("rootCauseEvent", cause)).getEvent();
    }


    public Event createNodeDownEvent(int nodeid) {
        return createNodeEvent(EventConstants.NODE_DOWN_EVENT_UEI, nodeid);
    }
    
    public Event createNodeUpEvent(int nodeid) {
        return createNodeEvent(EventConstants.NODE_UP_EVENT_UEI, nodeid);
    }

    private Event createNodeEvent(String uei, int nodeid) {
        return new EventBuilder(uei, "test")
            .setNodeid(nodeid)
            .getEvent();
    }
    



}
@@ -45,60 +45,8 @@
 s;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 impo
@@ -90,38 +90,8 @@
 t;%0A%0A
-import org.easymock.EasyMock;%0A
 impo
@@ -217,16 +217,60 @@
 .Event;%0A
+import org.opennms.test.mock.EasyMockUtils;%0A
 %0A%0Apublic
@@ -339,475 +339,60 @@
     
-%0A    List%3CObject%3E mocks = new ArrayList%3CObject%3E();%0A    %0A    public %3CT%3E T createMock(Class%3CT%3E mockClass) %7B%0A        T mock = EasyMock.createMock(mockClass);%0A        mocks.add(mock);%0A        return mock;%0A    %7D%0A    %0A    public void replay() %7B%0A        EasyMock.replay(mocks.toArray());%0A    %7D%0A    %0A    public void verify(DroolsCorrelationEngine engine) %7B%0A        EasyMock.verify(mocks.toArray());%0A        EasyMock.reset(mocks.toArray());%0A        super.verify(engine);%0A    %7D
+private EasyMockUtils m_mocks = new EasyMockUtils();
 %0A   
@@ -551,16 +551,24 @@
 rvice = 
+m_mocks.
 createMo
@@ -683,14 +683,25 @@
     
+m_mocks.
 replay
+All
 ();%0A
@@ -969,32 +969,61 @@
 e = 2;%0A        %0A
+        m_mocks.verifyAll();%0A
         verify(e
@@ -1277,32 +1277,58 @@
 vent();%0A%0A    %7D%0A%0A
+    // Currently unused%0A//
     private Even
@@ -1368,32 +1368,34 @@
 m, int cause) %7B%0A
+//
         return n
@@ -1460,24 +1460,26 @@
 getEvent();%0A
+//
     %7D%0A%0A%0A    

Use EasyMockUtils and comment-out an unused method.