Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [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 | */ |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 17 | package org.opencord.aaa.impl; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 18 | |
| 19 | import org.junit.After; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 22 | |
| 23 | import java.util.concurrent.Executors; |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 24 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 26 | |
| 27 | public class StateMachineTest { |
| 28 | StateMachine stateMachine = null; |
| 29 | |
| 30 | @Before |
| 31 | public void setUp() { |
| 32 | System.out.println("Set Up."); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 33 | StateMachine.setDelegate(e -> { }); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 34 | stateMachine = new StateMachine("session0", Executors.newSingleThreadScheduledExecutor()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @After |
| 38 | public void tearDown() { |
| 39 | System.out.println("Tear Down."); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 40 | stateMachine = null; |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | /** |
| 45 | * Test all the basic inputs from state to state: IDLE -> STARTED -> PENDING -> AUTHORIZED -> IDLE |
| 46 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 47 | public void basic() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 48 | System.out.println("======= BASIC =======."); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 49 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 50 | |
| 51 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 52 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 53 | |
| 54 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 55 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 56 | |
| 57 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 58 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 59 | |
| 60 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 61 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
| 65 | /** |
| 66 | * Test all inputs from an IDLE state (starting with the ones that are not impacting the current state) |
| 67 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 68 | public void testIdleState() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 69 | System.out.println("======= IDLE STATE TEST =======."); |
| 70 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 71 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 72 | |
| 73 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 74 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 75 | |
| 76 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 77 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 78 | |
| 79 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 80 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 81 | |
| 82 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 83 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Test |
| 87 | /** |
| 88 | * Test all inputs from an STARTED state (starting with the ones that are not impacting the current state) |
| 89 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 90 | public void testStartedState() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 91 | System.out.println("======= STARTED STATE TEST =======."); |
| 92 | stateMachine.start(); |
| 93 | |
| 94 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 95 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 96 | |
| 97 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 98 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 99 | |
| 100 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 101 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 102 | |
| 103 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 104 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 105 | |
| 106 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 107 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | /** |
| 112 | * Test all inputs from a PENDING state (starting with the ones that are not impacting the current state). |
| 113 | * The next valid state for this test is AUTHORIZED |
| 114 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 115 | public void testPendingStateToAuthorized() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 116 | System.out.println("======= PENDING STATE TEST (AUTHORIZED) =======."); |
| 117 | stateMachine.start(); |
| 118 | stateMachine.requestAccess(); |
| 119 | |
| 120 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 121 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 122 | |
| 123 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 124 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 125 | |
| 126 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 127 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 128 | |
| 129 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 130 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 131 | |
| 132 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 133 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Test |
| 137 | /** |
| 138 | * Test all inputs from an PENDING state (starting with the ones that are not impacting the current state). |
| 139 | * The next valid state for this test is UNAUTHORIZED |
| 140 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 141 | public void testPendingStateToUnauthorized() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 142 | System.out.println("======= PENDING STATE TEST (DENIED) =======."); |
| 143 | stateMachine.start(); |
| 144 | stateMachine.requestAccess(); |
| 145 | |
| 146 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 147 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 148 | |
| 149 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 150 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 151 | |
| 152 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 153 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 154 | |
| 155 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 156 | assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 157 | |
| 158 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 159 | assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Test |
| 163 | /** |
| 164 | * Test all inputs from an AUTHORIZED state (starting with the ones that are not impacting the current state). |
| 165 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 166 | public void testAuthorizedState() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 167 | System.out.println("======= AUTHORIZED STATE TEST =======."); |
| 168 | stateMachine.start(); |
| 169 | stateMachine.requestAccess(); |
| 170 | stateMachine.authorizeAccess(); |
| 171 | |
| 172 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 173 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 174 | |
| 175 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 176 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 177 | |
| 178 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 179 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 180 | |
| 181 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 182 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 183 | |
| 184 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 185 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Test |
| 189 | /** |
| 190 | * Test all inputs from an UNAUTHORIZED state (starting with the ones that are not impacting the current state). |
| 191 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 192 | public void testUnauthorizedState() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 193 | System.out.println("======= UNAUTHORIZED STATE TEST =======."); |
| 194 | stateMachine.start(); |
| 195 | stateMachine.requestAccess(); |
| 196 | stateMachine.denyAccess(); |
| 197 | |
| 198 | stateMachine.start(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 199 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 200 | |
| 201 | stateMachine.requestAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 202 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 203 | |
| 204 | stateMachine.authorizeAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 205 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 206 | |
| 207 | stateMachine.denyAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 208 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 209 | |
| 210 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 211 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 214 | } |