/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2016  
 */
package com.ibm.streamsx.topology.test.consistent;


import static com.ibm.streamsx.topology.consistent.ConsistentRegionConfig.operatorDriven;
import static com.ibm.streamsx.topology.consistent.ConsistentRegionConfig.periodic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.ibm.streamsx.topology.consistent.ConsistentRegionConfig;
import com.ibm.streamsx.topology.consistent.ConsistentRegionConfig.Trigger;
import com.ibm.streamsx.topology.test.TestTopology;

public class ConsistentRegionConfigTest extends TestTopology {
   
    @Test
    public void testDefaultOpDriven() {
        ConsistentRegionConfig config = operatorDriven();
        checkCRC(config, Trigger.OPERATOR_DRIVEN, -1, 180, 180, 5);
    }
    @Test
    public void testDefaultOpDrivenNew() {
        ConsistentRegionConfig config = new ConsistentRegionConfig();
        checkCRC(config, Trigger.OPERATOR_DRIVEN, -1, 180, 180, 5);
    }
    
    @Test
    public void testDefaultPeriod() {
        ConsistentRegionConfig config = periodic(37);
        checkCRC(config, Trigger.PERIODIC, 37, 180, 180, 5);
    }
    @Test
    public void testDefaultPeriodNew() {
        ConsistentRegionConfig config = new ConsistentRegionConfig(39);
        checkCRC(config, Trigger.PERIODIC, 39, 180, 180, 5);
    }
    
    @Test
    public void testChangeDrain() {
        ConsistentRegionConfig config = operatorDriven().drainTimeout(27);
        checkCRC(config, Trigger.OPERATOR_DRIVEN, -1, 27, 180, 5);
    }
    @Test
    public void testChangeReset() {
        ConsistentRegionConfig config = periodic(4).resetTimeout(99);
        checkCRC(config, Trigger.PERIODIC, 4, 180, 99, 5);
    }
    @Test
    public void testChangeAttempts() {
        ConsistentRegionConfig config = periodic(9).maxConsecutiveResetAttempts(10);
        checkCRC(config, Trigger.PERIODIC, 9, 180, 180, 10);
    }
    
    @Test
    public void testChangeAll() {
        ConsistentRegionConfig config = operatorDriven().maxConsecutiveResetAttempts(11).drainTimeout(32).resetTimeout(200);
        checkCRC(config, Trigger.OPERATOR_DRIVEN, -1, 32, 200, 11);
    }
    
    @Test(expected=IllegalArgumentException.class)
    public void testInvalidDrain0() {
        operatorDriven().drainTimeout(0);
    }
    @Test(expected=IllegalArgumentException.class)
    public void testInvalidDrainNeg() {
        operatorDriven().drainTimeout(-214);
    }
    @Test(expected=IllegalArgumentException.class)
    public void testInvalidReset0() {
        operatorDriven().resetTimeout(0);
    }
    @Test(expected=IllegalArgumentException.class)
    public void testInvalidResetNeg() {
        operatorDriven().resetTimeout(-2124);
    }
    @Test(expected=IllegalArgumentException.class)
    public void testInvalidAttempts0() {
        operatorDriven().maxConsecutiveResetAttempts(0);
    }
    @Test(expected=IllegalArgumentException.class)
    public void testInvalidAttemptsNeg() {
        operatorDriven().maxConsecutiveResetAttempts(-2);
    }
    
    @Test
    public void testEquals() {
        
        assertNotEquals(operatorDriven(), periodic(2));
        
        assertEquals(operatorDriven(), new ConsistentRegionConfig());
        assertEquals(periodic(7), new ConsistentRegionConfig(7));
        
        
        assertEquals(operatorDriven(), operatorDriven());
        assertEquals(periodic(9), periodic(9));
        
        assertNotEquals(operatorDriven(), operatorDriven().drainTimeout(15));
        assertNotEquals(operatorDriven(), operatorDriven().resetTimeout(16));
        assertNotEquals(operatorDriven(), operatorDriven().maxConsecutiveResetAttempts(17)); 
        
        assertNotEquals(periodic(9), periodic(11));
    }
    
    private static void checkCRC(ConsistentRegionConfig config,
            Trigger trigger, int period, long drain, long reset, int attempts) {
        
        assertNotNull(config.getTrigger());
        assertTrue(config.getDrainTimeout() > 0);
        assertTrue(config.getResetTimeout() > 0);
        assertTrue(config.getMaxConsecutiveResetAttempts() > 0);
        
        assertEquals(TimeUnit.SECONDS, config.getTimeUnit());
        
        assertEquals(trigger, config.getTrigger());
        assertEquals(((long) period), config.getPeriod());
        
        assertEquals(drain, config.getDrainTimeout());
        assertEquals(reset, config.getResetTimeout());
        assertEquals(attempts, config.getMaxConsecutiveResetAttempts()); 
        
        assertEquals(config, config);
        
        ConsistentRegionConfig dup;
        if (config.getTrigger() == Trigger.OPERATOR_DRIVEN)
            dup = operatorDriven();
        else
            dup = periodic(period);
        
        dup = dup.drainTimeout(drain).resetTimeout(reset).maxConsecutiveResetAttempts(attempts);
        assertEquals(config, dup);
        assertEquals(config.hashCode(), dup.hashCode());
    }
}

@@ -377,25 +377,21 @@
 t.assert
-NotEqu
+F
 als
+e
 ;%0Aimport
@@ -3315,33 +3315,29 @@
       assert
-NotEqu
+F
 als
+e
 (operatorDri
@@ -3341,18 +3341,24 @@
 Driven()
-, 
+.equals(
 periodic
@@ -3361,16 +3361,17 @@
 odic(2))
+)
 ;%0A      
@@ -3652,33 +3652,29 @@
       assert
-NotEqu
+F
 als
+e
 (operatorDri
@@ -3670,34 +3670,40 @@
 operatorDriven()
-, 
+.equals(
 operatorDriven()
@@ -3720,16 +3720,17 @@
 out(15))
+)
 ;%0A      
@@ -3733,33 +3733,29 @@
       assert
-NotEqu
+F
 als
+e
 (operatorDri
@@ -3751,34 +3751,40 @@
 operatorDriven()
-, 
+.equals(
 operatorDriven()
@@ -3801,16 +3801,17 @@
 out(16))
+)
 ;%0A      
@@ -3818,25 +3818,21 @@
   assert
-NotEqu
+F
 als
+e
 (operato
@@ -3832,34 +3832,40 @@
 operatorDriven()
-, 
+.equals(
 operatorDriven()
@@ -3897,16 +3897,17 @@
 pts(17))
+)
 ; %0A     
@@ -3928,17 +3928,13 @@
 sert
-NotEqu
+F
 als
+e
 (per
@@ -3933,34 +3933,40 @@
 alse(periodic(9)
-, 
+.equals(
 periodic(11));%0A 
@@ -3962,16 +3962,17 @@
 dic(11))
+)
 ;%0A    %7D%0A

Remove use of assertNotEquals