Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 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 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 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 | |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 24 | import static junit.framework.TestCase.assertFalse; |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 25 | import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | import static org.hamcrest.Matchers.is; |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertTrue; |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 28 | import static org.onosproject.net.intent.IntentState.FAILED; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 29 | import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
| 30 | import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp; |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Unit tests for intent data objects. |
| 34 | */ |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 35 | public class IntentDataTest extends AbstractIntentTest { |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 36 | |
| 37 | private Timestamp timestamp1; |
| 38 | private Timestamp timestamp2; |
| 39 | private Timestamp timestamp3; |
| 40 | |
| 41 | private Intent intent1; |
| 42 | private Intent intent2; |
| 43 | private Intent intent3; |
| 44 | |
| 45 | private IntentData data1; |
| 46 | private IntentData data1Copy; |
| 47 | private IntentData data2; |
| 48 | private IntentData data2Copy; |
| 49 | private IntentData data3; |
| 50 | private IntentData data3Copy; |
| 51 | |
| 52 | IdGenerator idGenerator; |
| 53 | |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 54 | @Override |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 55 | @Before |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 56 | public void setUp() { |
| 57 | super.setUp(); |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 58 | |
| 59 | timestamp1 = new MockTimestamp(1); |
| 60 | timestamp2 = new MockTimestamp(2); |
| 61 | timestamp3 = new MockTimestamp(3); |
| 62 | |
| 63 | intent1 = new MockIntent(1L); |
| 64 | intent2 = new MockIntent(2L); |
| 65 | intent3 = new MockIntent(3L); |
| 66 | |
| 67 | data1 = new IntentData(intent1, IntentState.INSTALLED, timestamp1); |
| 68 | data1Copy = new IntentData(intent1, IntentState.INSTALLED, timestamp1); |
| 69 | data2 = new IntentData(intent2, IntentState.INSTALLED, timestamp2); |
| 70 | data2Copy = new IntentData(intent2, IntentState.INSTALLED, timestamp2); |
| 71 | data3 = new IntentData(intent3, IntentState.INSTALLED, timestamp3); |
| 72 | data3Copy = new IntentData(intent3, IntentState.INSTALLED, timestamp3); |
| 73 | } |
| 74 | |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Checks that intent data objects are properly constructed. |
| 77 | */ |
| 78 | @Test |
| 79 | public void checkConstruction() { |
| 80 | assertThat(data1.state(), is(IntentState.INSTALLED)); |
| 81 | assertThat(data1.version(), is(timestamp1)); |
| 82 | assertThat(data1.intent(), is(intent1)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Checks equals() for intent data objects. |
| 87 | */ |
| 88 | @Test |
| 89 | public void checkEquals() { |
| 90 | new EqualsTester() |
| 91 | .addEqualityGroup(data1, data1Copy) |
| 92 | .addEqualityGroup(data2, data2Copy) |
| 93 | .addEqualityGroup(data3, data3Copy) |
| 94 | .testEquals(); |
| 95 | } |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 96 | |
| 97 | @Test |
| 98 | public void testIsUpdateAcceptable() { |
| 99 | // Going from null to something is always allowed |
| 100 | assertTrue(IntentData.isUpdateAcceptable(null, data1)); |
| 101 | |
| 102 | // we can go from older version to newer but not they other way |
| 103 | assertTrue(IntentData.isUpdateAcceptable(data1, data2)); |
| 104 | assertFalse(IntentData.isUpdateAcceptable(data2, data1)); |
| 105 | |
| 106 | IntentData installing = new IntentData(intent1, IntentState.INSTALLING, timestamp1); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 107 | IntentData installed = IntentData.nextState(installing, IntentState.INSTALLED); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 108 | IntentData withdrawing = new IntentData(intent1, IntentState.WITHDRAWING, timestamp1); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 109 | IntentData withdrawn = IntentData.nextState(withdrawing, IntentState.WITHDRAWN); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 110 | |
| 111 | IntentData failed = new IntentData(intent1, IntentState.FAILED, timestamp1); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 112 | |
| 113 | IntentData compiling = new IntentData(intent1, IntentState.COMPILING, timestamp1); |
| 114 | IntentData recompiling = new IntentData(intent1, IntentState.RECOMPILING, timestamp1); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 115 | |
| 116 | IntentData installReq = new IntentData(intent1, IntentState.INSTALL_REQ, timestamp2); |
| 117 | IntentData withdrawReq = new IntentData(intent1, IntentState.WITHDRAW_REQ, timestamp2); |
| 118 | IntentData purgeReq = new IntentData(intent1, IntentState.PURGE_REQ, timestamp2); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 119 | |
| 120 | // We can't change to the same state |
| 121 | assertFalse(IntentData.isUpdateAcceptable(installing, installing)); |
| 122 | assertFalse(IntentData.isUpdateAcceptable(installed, installed)); |
| 123 | |
| 124 | // From installing we can change to installed |
| 125 | assertTrue(IntentData.isUpdateAcceptable(installing, installed)); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 126 | // transition in reverse should be rejected |
| 127 | assertFalse(IntentData.isUpdateAcceptable(installed, installing)); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 128 | |
| 129 | // Sanity checks in case the manager submits bogus state transitions |
| 130 | assertFalse(IntentData.isUpdateAcceptable(installing, withdrawing)); |
| 131 | assertFalse(IntentData.isUpdateAcceptable(installing, withdrawn)); |
| 132 | assertFalse(IntentData.isUpdateAcceptable(installed, withdrawing)); |
| 133 | assertFalse(IntentData.isUpdateAcceptable(installed, withdrawn)); |
| 134 | |
| 135 | // We can't change to the same state |
| 136 | assertFalse(IntentData.isUpdateAcceptable(withdrawing, withdrawing)); |
| 137 | assertFalse(IntentData.isUpdateAcceptable(withdrawn, withdrawn)); |
| 138 | |
| 139 | // From withdrawing we can change to withdrawn |
| 140 | assertTrue(IntentData.isUpdateAcceptable(withdrawing, withdrawn)); |
| 141 | |
| 142 | // Sanity checks in case the manager submits bogus state transitions |
| 143 | assertFalse(IntentData.isUpdateAcceptable(withdrawing, installing)); |
| 144 | assertFalse(IntentData.isUpdateAcceptable(withdrawing, installed)); |
| 145 | assertFalse(IntentData.isUpdateAcceptable(withdrawn, installing)); |
| 146 | assertFalse(IntentData.isUpdateAcceptable(withdrawn, installed)); |
| 147 | |
| 148 | // We can't go from failed to failed |
| 149 | assertFalse(IntentData.isUpdateAcceptable(failed, failed)); |
| 150 | |
| 151 | // But we can go from any install* or withdraw* state to failed |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 152 | assertTrue(IntentData.isUpdateAcceptable(installing, IntentData.nextState(installing, FAILED))); |
| 153 | assertTrue(IntentData.isUpdateAcceptable(installed, IntentData.nextState(installed, FAILED))); |
| 154 | assertTrue(IntentData.isUpdateAcceptable(withdrawing, IntentData.nextState(withdrawing, FAILED))); |
| 155 | assertTrue(IntentData.isUpdateAcceptable(withdrawn, IntentData.nextState(withdrawn, FAILED))); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 156 | |
| 157 | // We can go from anything to purgeReq |
| 158 | assertTrue(IntentData.isUpdateAcceptable(installing, purgeReq)); |
| 159 | assertTrue(IntentData.isUpdateAcceptable(installed, purgeReq)); |
| 160 | assertTrue(IntentData.isUpdateAcceptable(withdrawing, purgeReq)); |
| 161 | assertTrue(IntentData.isUpdateAcceptable(withdrawn, purgeReq)); |
| 162 | assertTrue(IntentData.isUpdateAcceptable(failed, purgeReq)); |
| 163 | |
| 164 | // We can't go from purgeReq back to anything else |
| 165 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawn)); |
| 166 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawing)); |
| 167 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, installed)); |
| 168 | assertFalse(IntentData.isUpdateAcceptable(purgeReq, installing)); |
| 169 | |
| 170 | // We're never allowed to store transient states |
| 171 | assertFalse(IntentData.isUpdateAcceptable(installing, compiling)); |
| 172 | assertFalse(IntentData.isUpdateAcceptable(installing, recompiling)); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 173 | assertFalse(IntentData.isUpdateAcceptable(installing, installing)); |
| 174 | assertFalse(IntentData.isUpdateAcceptable(installing, withdrawing)); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 175 | } |
Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame] | 176 | } |