blob: de215c6899fb7d38c7f7808698455014d66719d4 [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
Ray Milkey8010bb42015-02-12 11:53:40 -080018import org.junit.After;
19import 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
24import com.google.common.testing.EqualsTester;
25
Jonathan Hart72175c22015-03-24 18:55:58 -070026import static junit.framework.TestCase.assertFalse;
Ray Milkey8010bb42015-02-12 11:53:40 -080027import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.is;
Jonathan Hart72175c22015-03-24 18:55:58 -070029import static org.junit.Assert.assertTrue;
Ray Milkey43a28222015-02-23 13:57:58 -080030import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
31import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
Ray Milkey8010bb42015-02-12 11:53:40 -080032
33/**
34 * Unit tests for intent data objects.
35 */
36public class IntentDataTest {
Ray Milkey8010bb42015-02-12 11:53:40 -080037
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();
Thomas Vachuska23235962017-02-03 11:44:15 -080058 Intent.unbindIdGenerator(idGenerator);
Ray Milkey8010bb42015-02-12 11:53:40 -080059 Intent.bindIdGenerator(idGenerator);
60
61 timestamp1 = new MockTimestamp(1);
62 timestamp2 = new MockTimestamp(2);
63 timestamp3 = new MockTimestamp(3);
64
65 intent1 = new MockIntent(1L);
66 intent2 = new MockIntent(2L);
67 intent3 = new MockIntent(3L);
68
69 data1 = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
70 data1Copy = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
71 data2 = new IntentData(intent2, IntentState.INSTALLED, timestamp2);
72 data2Copy = new IntentData(intent2, IntentState.INSTALLED, timestamp2);
73 data3 = new IntentData(intent3, IntentState.INSTALLED, timestamp3);
74 data3Copy = new IntentData(intent3, IntentState.INSTALLED, timestamp3);
75 }
76
77 @After
78 public void tearDownTest() {
79 Intent.unbindIdGenerator(idGenerator);
80 }
81
82 /**
83 * Checks that intent data objects are properly constructed.
84 */
85 @Test
86 public void checkConstruction() {
87 assertThat(data1.state(), is(IntentState.INSTALLED));
88 assertThat(data1.version(), is(timestamp1));
89 assertThat(data1.intent(), is(intent1));
90 }
91
92 /**
93 * Checks equals() for intent data objects.
94 */
95 @Test
96 public void checkEquals() {
97 new EqualsTester()
98 .addEqualityGroup(data1, data1Copy)
99 .addEqualityGroup(data2, data2Copy)
100 .addEqualityGroup(data3, data3Copy)
101 .testEquals();
102 }
Jonathan Hart72175c22015-03-24 18:55:58 -0700103
104 @Test
105 public void testIsUpdateAcceptable() {
106 // Going from null to something is always allowed
107 assertTrue(IntentData.isUpdateAcceptable(null, data1));
108
109 // we can go from older version to newer but not they other way
110 assertTrue(IntentData.isUpdateAcceptable(data1, data2));
111 assertFalse(IntentData.isUpdateAcceptable(data2, data1));
112
113 IntentData installing = new IntentData(intent1, IntentState.INSTALLING, timestamp1);
114 IntentData installed = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
115 IntentData withdrawing = new IntentData(intent1, IntentState.WITHDRAWING, timestamp1);
116 IntentData withdrawn = new IntentData(intent1, IntentState.WITHDRAWN, timestamp1);
117
118 IntentData failed = new IntentData(intent1, IntentState.FAILED, timestamp1);
119 IntentData purgeReq = new IntentData(intent1, IntentState.PURGE_REQ, timestamp1);
120
121 IntentData compiling = new IntentData(intent1, IntentState.COMPILING, timestamp1);
122 IntentData recompiling = new IntentData(intent1, IntentState.RECOMPILING, timestamp1);
123 IntentData installReq = new IntentData(intent1, IntentState.INSTALL_REQ, timestamp1);
124 IntentData withdrawReq = new IntentData(intent1, IntentState.WITHDRAW_REQ, timestamp1);
125
126 // We can't change to the same state
127 assertFalse(IntentData.isUpdateAcceptable(installing, installing));
128 assertFalse(IntentData.isUpdateAcceptable(installed, installed));
129
130 // From installing we can change to installed
131 assertTrue(IntentData.isUpdateAcceptable(installing, installed));
132
133 // Sanity checks in case the manager submits bogus state transitions
134 assertFalse(IntentData.isUpdateAcceptable(installing, withdrawing));
135 assertFalse(IntentData.isUpdateAcceptable(installing, withdrawn));
136 assertFalse(IntentData.isUpdateAcceptable(installed, withdrawing));
137 assertFalse(IntentData.isUpdateAcceptable(installed, withdrawn));
138
139 // We can't change to the same state
140 assertFalse(IntentData.isUpdateAcceptable(withdrawing, withdrawing));
141 assertFalse(IntentData.isUpdateAcceptable(withdrawn, withdrawn));
142
143 // From withdrawing we can change to withdrawn
144 assertTrue(IntentData.isUpdateAcceptable(withdrawing, withdrawn));
145
146 // Sanity checks in case the manager submits bogus state transitions
147 assertFalse(IntentData.isUpdateAcceptable(withdrawing, installing));
148 assertFalse(IntentData.isUpdateAcceptable(withdrawing, installed));
149 assertFalse(IntentData.isUpdateAcceptable(withdrawn, installing));
150 assertFalse(IntentData.isUpdateAcceptable(withdrawn, installed));
151
152 // We can't go from failed to failed
153 assertFalse(IntentData.isUpdateAcceptable(failed, failed));
154
155 // But we can go from any install* or withdraw* state to failed
156 assertTrue(IntentData.isUpdateAcceptable(installing, failed));
157 assertTrue(IntentData.isUpdateAcceptable(installed, failed));
158 assertTrue(IntentData.isUpdateAcceptable(withdrawing, failed));
159 assertTrue(IntentData.isUpdateAcceptable(withdrawn, failed));
160
161 // We can go from anything to purgeReq
162 assertTrue(IntentData.isUpdateAcceptable(installing, purgeReq));
163 assertTrue(IntentData.isUpdateAcceptable(installed, purgeReq));
164 assertTrue(IntentData.isUpdateAcceptable(withdrawing, purgeReq));
165 assertTrue(IntentData.isUpdateAcceptable(withdrawn, purgeReq));
166 assertTrue(IntentData.isUpdateAcceptable(failed, purgeReq));
167
168 // We can't go from purgeReq back to anything else
169 assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawn));
170 assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawing));
171 assertFalse(IntentData.isUpdateAcceptable(purgeReq, installed));
172 assertFalse(IntentData.isUpdateAcceptable(purgeReq, installing));
173
174 // We're never allowed to store transient states
175 assertFalse(IntentData.isUpdateAcceptable(installing, compiling));
176 assertFalse(IntentData.isUpdateAcceptable(installing, recompiling));
177 assertFalse(IntentData.isUpdateAcceptable(installing, installReq));
178 assertFalse(IntentData.isUpdateAcceptable(installing, withdrawReq));
179 }
Ray Milkey8010bb42015-02-12 11:53:40 -0800180}