blob: 62ad289d590b4af6827e107927917511c3a861f1 [file] [log] [blame]
Ray Milkey8010bb42015-02-12 11:53:40 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey8010bb42015-02-12 11:53:40 -08003 *
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 */
16package org.onosproject.net.intent;
17
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070018import com.google.common.testing.EqualsTester;
Ray Milkey8010bb42015-02-12 11:53:40 -080019import org.junit.Before;
20import org.junit.Test;
21import org.onosproject.core.IdGenerator;
Ray Milkey8010bb42015-02-12 11:53:40 -080022import org.onosproject.store.Timestamp;
23
Jonathan Hart72175c22015-03-24 18:55:58 -070024import static junit.framework.TestCase.assertFalse;
Ray Milkey8010bb42015-02-12 11:53:40 -080025import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.is;
Jonathan Hart72175c22015-03-24 18:55:58 -070027import static org.junit.Assert.assertTrue;
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -070028import static org.onosproject.net.intent.IntentState.FAILED;
Ray Milkey43a28222015-02-23 13:57:58 -080029import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
30import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
Ray Milkey8010bb42015-02-12 11:53:40 -080031
32/**
33 * Unit tests for intent data objects.
34 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070035public class IntentDataTest extends AbstractIntentTest {
Ray Milkey8010bb42015-02-12 11:53:40 -080036
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 HIGUCHI4f8a3772017-05-16 20:23:49 -070054 @Override
Ray Milkey8010bb42015-02-12 11:53:40 -080055 @Before
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070056 public void setUp() {
57 super.setUp();
Ray Milkey8010bb42015-02-12 11:53:40 -080058
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 Milkey8010bb42015-02-12 11:53:40 -080075 /**
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 Hart72175c22015-03-24 18:55:58 -070096
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 HIGUCHI4f8a3772017-05-16 20:23:49 -0700107 IntentData installed = IntentData.nextState(installing, IntentState.INSTALLED);
Jonathan Hart72175c22015-03-24 18:55:58 -0700108 IntentData withdrawing = new IntentData(intent1, IntentState.WITHDRAWING, timestamp1);
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700109 IntentData withdrawn = IntentData.nextState(withdrawing, IntentState.WITHDRAWN);
Jonathan Hart72175c22015-03-24 18:55:58 -0700110
111 IntentData failed = new IntentData(intent1, IntentState.FAILED, timestamp1);
Jonathan Hart72175c22015-03-24 18:55:58 -0700112
113 IntentData compiling = new IntentData(intent1, IntentState.COMPILING, timestamp1);
114 IntentData recompiling = new IntentData(intent1, IntentState.RECOMPILING, timestamp1);
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700115
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 Hart72175c22015-03-24 18:55:58 -0700119
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 HIGUCHI4f8a3772017-05-16 20:23:49 -0700126 // transition in reverse should be rejected
127 assertFalse(IntentData.isUpdateAcceptable(installed, installing));
Jonathan Hart72175c22015-03-24 18:55:58 -0700128
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 HIGUCHI4f8a3772017-05-16 20:23:49 -0700152 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 Hart72175c22015-03-24 18:55:58 -0700156
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 HIGUCHI4f8a3772017-05-16 20:23:49 -0700173 assertFalse(IntentData.isUpdateAcceptable(installing, installing));
174 assertFalse(IntentData.isUpdateAcceptable(installing, withdrawing));
Jonathan Hart72175c22015-03-24 18:55:58 -0700175 }
Ray Milkey8010bb42015-02-12 11:53:40 -0800176}