package azkaban.trigger;

import java.util.HashMap;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import azkaban.trigger.builtin.NyxTriggerChecker;
import azkaban.utils.NyxUtils;

/**
 * Test class for NyxTriggerChecker
 * 
 * @author gaggarwa
 *
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({ NyxUtils.class, NyxTriggerChecker.class })
public class NyxTriggerCheckerTest {
  private NyxTriggerChecker checker;
  private String dummySpec = "spec1";
  private long dummyTriggerId = 123L;

  @Before
  public void initObjects() throws Exception {
    PowerMockito.mockStatic(NyxUtils.class);
    PowerMockito.when(NyxUtils.registerNyxTrigger(dummySpec))
        .thenReturn(dummyTriggerId);
    PowerMockito.doNothing().when(NyxUtils.class, "unregisterNyxTrigger",
        dummyTriggerId);
    PowerMockito.when(NyxUtils.isNyxTriggerReady(dummyTriggerId))
        .thenReturn(true);

    checker = new NyxTriggerChecker(dummySpec, "NyxTriggerChecker_1");
  }

  @Test
  public void testDefaultTriggerRegister() throws TriggerManagerException {
    Assert.assertEquals("NyxTriggerChecker_1", checker.getId());
    Assert.assertEquals(123L, checker.getTriggerId());
    PowerMockito.verifyStatic();
    // verify register Trigger was called
    NyxUtils.registerNyxTrigger(dummySpec);
  }

  @Test
  public void testJSONserialization() throws Exception {
    @SuppressWarnings("unchecked")
    ConditionChecker retrievedChecker = NyxTriggerChecker
        .createFromJson((HashMap<String, Object>) checker.toJson());
    Assert.assertEquals(checker, retrievedChecker);
  }

  @Test
  public void testReset() throws TriggerManagerException {
    checker.reset();
    PowerMockito.verifyStatic();
    NyxUtils.unregisterNyxTrigger(dummyTriggerId);

    PowerMockito.verifyStatic(Mockito.times(2));
    NyxUtils.registerNyxTrigger(dummySpec);
  }

  @Test
  public void testStop() throws TriggerManagerException {
    checker.stopChecker();
    PowerMockito.verifyStatic();
    NyxUtils.unregisterNyxTrigger(dummyTriggerId);
  }

  @Test
  public void testType() {
    Assert.assertEquals(NyxTriggerChecker.type, checker.getType());
  }

  @Test
  public void testGetNum() {
    Assert.assertEquals(null, checker.getNum());
  }

  @Test
  public void testGetNextCheckTime() {
    Assert.assertEquals(Long.MAX_VALUE, checker.getNextCheckTime());
  }

  @Test
  public void testEval() {
    Assert.assertEquals(true, checker.eval());
  }
}

@@ -471,17 +471,16 @@
 ecker%0A *
- 
 %0A * @aut
@@ -906,25 +906,16 @@
 mySpec))
-%0A        
 .thenRet
@@ -918,16 +918,25 @@
 nReturn(
+%0A        
 dummyTri
@@ -1108,25 +1108,16 @@
 ggerId))
-%0A        
 .thenRet
@@ -1120,16 +1120,25 @@
 nReturn(
+%0A        
 true);%0A%0A
@@ -1386,20 +1386,18 @@
 tEquals(
+-
 1
-23L
 , checke
@@ -1419,127 +1419,8 @@
 ));%0A
-    PowerMockito.verifyStatic();%0A    // verify register Trigger was called%0A    NyxUtils.registerNyxTrigger(dummySpec);%0A
   %7D%0A
@@ -1559,16 +1559,24 @@
 hecker =
+%0A       
  NyxTrig
@@ -1585,25 +1585,16 @@
 rChecker
-%0A        
 .createF
@@ -1634,16 +1634,29 @@
  checker
+%0A            
 .toJson(
@@ -1775,32 +1775,52 @@
 agerException %7B%0A
+    checker.eval();%0A
     checker.rese
@@ -2065,32 +2065,52 @@
 agerException %7B%0A
+    checker.eval();%0A
     checker.stop

fix unit tests.