Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.intent; |
| 17 | |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.core.IdGenerator; |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 22 | import org.onosproject.store.Timestamp; |
| 23 | |
| 24 | import com.google.common.testing.EqualsTester; |
| 25 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 26 | import static junit.framework.TestCase.assertFalse; |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.is; |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 29 | import static org.junit.Assert.assertTrue; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 30 | import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
| 31 | import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp; |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Unit tests for intent data objects. |
| 35 | */ |
| 36 | public class IntentDataTest { |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 37 | |
| 38 | private Timestamp timestamp1; |
| 39 | private Timestamp timestamp2; |
| 40 | private Timestamp timestamp3; |
| 41 | |
| 42 | private Intent intent1; |
| 43 | private Intent intent2; |
| 44 | private Intent intent3; |
| 45 | |
| 46 | private IntentData data1; |
| 47 | private IntentData data1Copy; |
| 48 | private IntentData data2; |
| 49 | private IntentData data2Copy; |
| 50 | private IntentData data3; |
| 51 | private IntentData data3Copy; |
| 52 | |
| 53 | IdGenerator idGenerator; |
| 54 | |
| 55 | @Before |
| 56 | public void setUpTest() { |
| 57 | idGenerator = new MockIdGenerator(); |
| 58 | Intent.bindIdGenerator(idGenerator); |
| 59 | |
| 60 | timestamp1 = new MockTimestamp(1); |
| 61 | timestamp2 = new MockTimestamp(2); |
| 62 | timestamp3 = new MockTimestamp(3); |
| 63 | |
| 64 | intent1 = new MockIntent(1L); |
| 65 | intent2 = new MockIntent(2L); |
| 66 | intent3 = new MockIntent(3L); |
| 67 | |
| 68 | data1 = new IntentData(intent1, IntentState.INSTALLED, timestamp1); |
| 69 | data1Copy = new IntentData(intent1, IntentState.INSTALLED, timestamp1); |
| 70 | data2 = new IntentData(intent2, IntentState.INSTALLED, timestamp2); |
| 71 | data2Copy = new IntentData(intent2, IntentState.INSTALLED, timestamp2); |
| 72 | data3 = new IntentData(intent3, IntentState.INSTALLED, timestamp3); |
| 73 | data3Copy = new IntentData(intent3, IntentState.INSTALLED, timestamp3); |
| 74 | } |
| 75 | |
| 76 | @After |
| 77 | public void tearDownTest() { |
| 78 | Intent.unbindIdGenerator(idGenerator); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Checks that intent data objects are properly constructed. |
| 83 | */ |
| 84 | @Test |
| 85 | public void checkConstruction() { |
| 86 | assertThat(data1.state(), is(IntentState.INSTALLED)); |
| 87 | assertThat(data1.version(), is(timestamp1)); |
| 88 | assertThat(data1.intent(), is(intent1)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks equals() for intent data objects. |
| 93 | */ |
| 94 | @Test |
| 95 | public void checkEquals() { |
| 96 | new EqualsTester() |
| 97 | .addEqualityGroup(data1, data1Copy) |
| 98 | .addEqualityGroup(data2, data2Copy) |
| 99 | .addEqualityGroup(data3, data3Copy) |
| 100 | .testEquals(); |
| 101 | } |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 102 | |
| 103 | @Test |
| 104 | public void testIsUpdateAcceptable() { |
| 105 | // Going from null to something is always allowed |
| 106 | assertTrue(IntentData.isUpdateAcceptable(null, data1)); |
| 107 | |
| 108 | // we can go from older version to newer but not they other way |
| 109 | assertTrue(IntentData.isUpdateAcceptable(data1, data2)); |
| 110 | assertFalse(IntentData.isUpdateAcceptable(data2, data1)); |
| 111 | |
| 112 | IntentData installing = new IntentData(intent1, IntentState.INSTALLING, timestamp1); |
| 113 | IntentData installed = new IntentData(intent1, IntentState.INSTALLED, timestamp1); |
| 114 | IntentData withdrawing = new IntentData(intent1, IntentState.WITHDRAWING, timestamp1); |
| 115 | IntentData withdrawn = new IntentData(intent1, IntentState.WITHDRAWN, timestamp1); |
| 116 | |
| 117 | IntentData failed = new IntentData(intent1, IntentState.FAILED, timestamp1); |
| 118 | IntentData purgeReq = new IntentData(intent1, IntentState.PURGE_REQ, timestamp1); |
| 119 | |
| 120 | IntentData compiling = new IntentData(intent1, IntentState.COMPILING, timestamp1); |
| 121 | IntentData recompiling = new IntentData(intent1, IntentState.RECOMPILING, timestamp1); |
| 122 | IntentData installReq = new IntentData(intent1, IntentState.INSTALL_REQ, timestamp1); |
| 123 | IntentData withdrawReq = new IntentData(intent1, IntentState.WITHDRAW_REQ, timestamp1); |
| 124 | |
| 125 | // We can't change to the same state |
| 126 | assertFalse(IntentData.isUpdateAcceptable(installing, installing)); |
| 127 | assertFalse(IntentData.isUpdateAcceptable(installed, installed)); |
| 128 | |
| 129 | // From installing we can change to installed |
| 130 | assertTrue(IntentData.isUpdateAcceptable(installing, installed)); |
| 131 | |
| 132 | // Sanity checks in case the manager submits bogus state transitions |
| 133 | assertFalse(IntentData.isUpdateAcceptable(installing, withdrawing)); |
| 134 | assertFalse(IntentData.isUpdateAcceptable(installing, withdrawn)); |
| 135 | assertFalse(IntentData.isUpdateAcceptable(installed, withdrawing)); |
| 136 | assertFalse(IntentData.isUpdateAcceptable(installed, withdrawn)); |
| 137 | |
| 138 | // We can't change to the same state |
| 139 | assertFalse(IntentData.isUpdateAcceptable(withdrawing, withdrawing)); |
| 140 | assertFalse(IntentData.isUpdateAcceptable(withdrawn, withdrawn)); |
| 141 | |
| 142 | // From withdrawing we can change to withdrawn |
| 143 | assertTrue(IntentData.isUpdateAcceptable(withdrawing, withdrawn)); |
| 144 | |
| 145 | // Sanity checks in case the manager submits bogus state transitions |
| 146 | assertFalse(IntentData.isUpdateAcceptable(withdrawing, installing)); |
| 147 | assertFalse(IntentData.isUpdateAcceptable(withdrawing, installed)); |
| 148 | assertFalse(IntentData.isUpdateAcceptable(withdrawn, installing)); |
| 149 | assertFalse(IntentData.isUpdateAcceptable(withdrawn, installed)); |
| 150 | |
| 151 | // We can't go from failed to failed |
| 152 | assertFalse(IntentData.isUpdateAcceptable(failed, failed)); |
| 153 | |
| 154 | // But we can go from any install* or withdraw* state to failed |
| 155 | assertTrue(IntentData.isUpdateAcceptable(installing, failed)); |
| 156 | assertTrue(IntentData.isUpdateAcceptable(installed, failed)); |
| 157 | assertTrue(IntentData.isUpdateAcceptable(withdrawing, failed)); |
| 158 | assertTrue(IntentData.isUpdateAcceptable(withdrawn, failed)); |
| 159 | |
| 160 | // We can go from anything to purgeReq |
| 161 | assertTrue(IntentData.isUpdateAcceptable(installing, purgeReq)); |
| 162 | assertTrue(IntentData.isUpdateAcceptable(installed, purgeReq)); |
| 163 | assertTrue(IntentData.isUpdateAcceptable(withdrawing, purgeReq)); |
| 164 | assertTrue(IntentData.isUpdateAcceptable(withdrawn, purgeReq)); |
| 165 | assertTrue(IntentData.isUpdateAcceptable(failed, purgeReq)); |
| 166 | |
| 167 | // We can't go from purgeReq back to anything else |
| 168 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawn)); |
| 169 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawing)); |
| 170 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, installed)); |
| 171 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, installing)); |
| 172 | |
| 173 | // We're never allowed to store transient states |
| 174 | assertFalse(IntentData.isUpdateAcceptable(installing, compiling)); |
| 175 | assertFalse(IntentData.isUpdateAcceptable(installing, recompiling)); |
| 176 | assertFalse(IntentData.isUpdateAcceptable(installing, installReq)); |
| 177 | assertFalse(IntentData.isUpdateAcceptable(installing, withdrawReq)); |
| 178 | } |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 179 | } |