package com.linkedin.clustermanager.monitoring;

import org.testng.annotations.Test;
import org.testng.AssertJUnit;
import java.util.HashSet;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.linkedin.clustermanager.monitoring.TestParticipantMonitor.ParticipantMonitorListener;
import com.linkedin.clustermanager.monitoring.mbeans.StateTransitionStatMonitor;
import com.linkedin.clustermanager.monitoring.mbeans.TransStatMonitorChangedListener;

public class TestParticipantMonitor
{
  class ParticipantMonitorListener implements TransStatMonitorChangedListener
  {
    HashSet<StateTransitionStatMonitor> _monitors = new HashSet<StateTransitionStatMonitor>();
    @Override
    public void onTransStatMonitorAdded(
        StateTransitionStatMonitor newStateTransitionStatMonitor)
    {
      // TODO Auto-generated method stub
      AssertJUnit.assertTrue(!_monitors.contains(newStateTransitionStatMonitor));
      _monitors.add(newStateTransitionStatMonitor);
    }
    
  }
  @Test(groups={ "unitTest" })
  public void TestReportData()
  {
    ParticipantMonitor monitor = ParticipantMonitor.getInstance();
    
    ParticipantMonitorListener monitorListener = new ParticipantMonitorListener();
    
    monitor.addTransStatMonitorChangedListener(monitorListener);
    
    StateTransitionContext cxt = new StateTransitionContext("cluster", "instance", "db_1","a-b");
    StateTransitionDataPoint data = new StateTransitionDataPoint(1000,1000,true);
    
    monitor.reportTransitionStat(cxt, data);
    AssertJUnit.assertTrue(monitorListener._monitors.size() == 1);
    
    data = new StateTransitionDataPoint(1000,500,true);
    monitor.reportTransitionStat(cxt, data);
    AssertJUnit.assertTrue(monitorListener._monitors.size() == 1);
    
    data = new StateTransitionDataPoint(1000,500,true);
    StateTransitionContext cxt2 = new StateTransitionContext("cluster", "instance", "db_2","a-b");
    monitor.reportTransitionStat(cxt2, data);
    AssertJUnit.assertTrue(monitorListener._monitors.size() == 2);
    
    AssertJUnit.assertFalse(cxt.equals(cxt2));
    AssertJUnit.assertFalse(cxt.equals(new Object()));
    AssertJUnit.assertTrue(cxt.equals(new StateTransitionContext("cluster", "instance", "db_1","a-b")));
    
    cxt2.getInstanceName();
    
    ParticipantMonitorListener monitorListener2 = new ParticipantMonitorListener();
    
    monitor.addTransStatMonitorChangedListener(monitorListener2);
    AssertJUnit.assertTrue(monitorListener2._monitors.size() == 2);
  }
}

@@ -1288,24 +1288,84 @@
 ener);%0A    %0A
+    int monitorNum = monitorListener._monitors.size();%0A    %0A
     StateTra
@@ -1446,24 +1446,24 @@
 _1%22,%22a-b%22);%0A
-
     StateTra
@@ -1636,32 +1636,45 @@
 nitors.size() ==
+ monitorNum +
  1);%0A    %0A    da
@@ -1830,16 +1830,29 @@
 ize() ==
+ monitorNum +
  1);%0A   
@@ -2112,24 +2112,37 @@
 rs.size() ==
+ monitorNum +
  2);%0A    %0A  
@@ -2535,24 +2535,24 @@
 Listener2);%0A
-
     AssertJU
@@ -2602,16 +2602,29 @@
 ize() ==
+ monitorNum +
  2);%0A  %7D

Fix participant monitor test