Moved duplicated isUpdateAcceptable method to IntentData
and wrote a unit test for it.

Change-Id: I8b38476c41fba70abbba7ed0b37364696f17966d
diff --git a/core/api/src/test/java/org/onosproject/net/intent/IntentDataTest.java b/core/api/src/test/java/org/onosproject/net/intent/IntentDataTest.java
index a953149..9c4cf7e 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/IntentDataTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/IntentDataTest.java
@@ -23,8 +23,10 @@
 
 import com.google.common.testing.EqualsTester;
 
+import static junit.framework.TestCase.assertFalse;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertTrue;
 import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
 import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
 
@@ -97,4 +99,81 @@
                 .addEqualityGroup(data3, data3Copy)
                 .testEquals();
     }
+
+    @Test
+    public void testIsUpdateAcceptable() {
+        // Going from null to something is always allowed
+        assertTrue(IntentData.isUpdateAcceptable(null, data1));
+
+        // we can go from older version to newer but not they other way
+        assertTrue(IntentData.isUpdateAcceptable(data1, data2));
+        assertFalse(IntentData.isUpdateAcceptable(data2, data1));
+
+        IntentData installing = new IntentData(intent1, IntentState.INSTALLING, timestamp1);
+        IntentData installed = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
+        IntentData withdrawing = new IntentData(intent1, IntentState.WITHDRAWING, timestamp1);
+        IntentData withdrawn = new IntentData(intent1, IntentState.WITHDRAWN, timestamp1);
+
+        IntentData failed = new IntentData(intent1, IntentState.FAILED, timestamp1);
+        IntentData purgeReq = new IntentData(intent1, IntentState.PURGE_REQ, timestamp1);
+
+        IntentData compiling = new IntentData(intent1, IntentState.COMPILING, timestamp1);
+        IntentData recompiling = new IntentData(intent1, IntentState.RECOMPILING, timestamp1);
+        IntentData installReq = new IntentData(intent1, IntentState.INSTALL_REQ, timestamp1);
+        IntentData withdrawReq = new IntentData(intent1, IntentState.WITHDRAW_REQ, timestamp1);
+
+        // We can't change to the same state
+        assertFalse(IntentData.isUpdateAcceptable(installing, installing));
+        assertFalse(IntentData.isUpdateAcceptable(installed, installed));
+
+        // From installing we can change to installed
+        assertTrue(IntentData.isUpdateAcceptable(installing, installed));
+
+        // Sanity checks in case the manager submits bogus state transitions
+        assertFalse(IntentData.isUpdateAcceptable(installing, withdrawing));
+        assertFalse(IntentData.isUpdateAcceptable(installing, withdrawn));
+        assertFalse(IntentData.isUpdateAcceptable(installed, withdrawing));
+        assertFalse(IntentData.isUpdateAcceptable(installed, withdrawn));
+
+        // We can't change to the same state
+        assertFalse(IntentData.isUpdateAcceptable(withdrawing, withdrawing));
+        assertFalse(IntentData.isUpdateAcceptable(withdrawn, withdrawn));
+
+        // From withdrawing we can change to withdrawn
+        assertTrue(IntentData.isUpdateAcceptable(withdrawing, withdrawn));
+
+        // Sanity checks in case the manager submits bogus state transitions
+        assertFalse(IntentData.isUpdateAcceptable(withdrawing, installing));
+        assertFalse(IntentData.isUpdateAcceptable(withdrawing, installed));
+        assertFalse(IntentData.isUpdateAcceptable(withdrawn, installing));
+        assertFalse(IntentData.isUpdateAcceptable(withdrawn, installed));
+
+        // We can't go from failed to failed
+        assertFalse(IntentData.isUpdateAcceptable(failed, failed));
+
+        // But we can go from any install* or withdraw* state to failed
+        assertTrue(IntentData.isUpdateAcceptable(installing, failed));
+        assertTrue(IntentData.isUpdateAcceptable(installed, failed));
+        assertTrue(IntentData.isUpdateAcceptable(withdrawing, failed));
+        assertTrue(IntentData.isUpdateAcceptable(withdrawn, failed));
+
+        // We can go from anything to purgeReq
+        assertTrue(IntentData.isUpdateAcceptable(installing, purgeReq));
+        assertTrue(IntentData.isUpdateAcceptable(installed, purgeReq));
+        assertTrue(IntentData.isUpdateAcceptable(withdrawing, purgeReq));
+        assertTrue(IntentData.isUpdateAcceptable(withdrawn, purgeReq));
+        assertTrue(IntentData.isUpdateAcceptable(failed, purgeReq));
+
+        // We can't go from purgeReq back to anything else
+        assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawn));
+        assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawing));
+        assertFalse(IntentData.isUpdateAcceptable(purgeReq, installed));
+        assertFalse(IntentData.isUpdateAcceptable(purgeReq, installing));
+
+        // We're never allowed to store transient states
+        assertFalse(IntentData.isUpdateAcceptable(installing, compiling));
+        assertFalse(IntentData.isUpdateAcceptable(installing, recompiling));
+        assertFalse(IntentData.isUpdateAcceptable(installing, installReq));
+        assertFalse(IntentData.isUpdateAcceptable(installing, withdrawReq));
+    }
 }
diff --git a/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java b/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
index 14abd83..8bef293 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
@@ -454,9 +454,8 @@
                 return -1;
             }
             MockTimestamp that = (MockTimestamp) o;
-            return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
+            return this.value - that.value;
         }
     }
 
-
 }