blob: f7bb7a37ebddcda007c3f526eda3723e06313796 [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;
Ray Milkey43a28222015-02-23 13:57:58 -080028import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
29import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
Ray Milkey8010bb42015-02-12 11:53:40 -080030
31/**
32 * Unit tests for intent data objects.
33 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070034public class IntentDataTest extends AbstractIntentTest {
Ray Milkey8010bb42015-02-12 11:53:40 -080035
36 private Timestamp timestamp1;
37 private Timestamp timestamp2;
38 private Timestamp timestamp3;
39
40 private Intent intent1;
41 private Intent intent2;
42 private Intent intent3;
43
44 private IntentData data1;
45 private IntentData data1Copy;
46 private IntentData data2;
47 private IntentData data2Copy;
48 private IntentData data3;
49 private IntentData data3Copy;
50
51 IdGenerator idGenerator;
52
53 @Before
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070054 public void setUp() {
55 super.setUp();
Ray Milkey8010bb42015-02-12 11:53:40 -080056
57 timestamp1 = new MockTimestamp(1);
58 timestamp2 = new MockTimestamp(2);
59 timestamp3 = new MockTimestamp(3);
60
61 intent1 = new MockIntent(1L);
62 intent2 = new MockIntent(2L);
63 intent3 = new MockIntent(3L);
64
65 data1 = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
66 data1Copy = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
67 data2 = new IntentData(intent2, IntentState.INSTALLED, timestamp2);
68 data2Copy = new IntentData(intent2, IntentState.INSTALLED, timestamp2);
69 data3 = new IntentData(intent3, IntentState.INSTALLED, timestamp3);
70 data3Copy = new IntentData(intent3, IntentState.INSTALLED, timestamp3);
71 }
72
Ray Milkey8010bb42015-02-12 11:53:40 -080073 /**
74 * Checks that intent data objects are properly constructed.
75 */
76 @Test
77 public void checkConstruction() {
78 assertThat(data1.state(), is(IntentState.INSTALLED));
79 assertThat(data1.version(), is(timestamp1));
80 assertThat(data1.intent(), is(intent1));
81 }
82
83 /**
84 * Checks equals() for intent data objects.
85 */
86 @Test
87 public void checkEquals() {
88 new EqualsTester()
89 .addEqualityGroup(data1, data1Copy)
90 .addEqualityGroup(data2, data2Copy)
91 .addEqualityGroup(data3, data3Copy)
92 .testEquals();
93 }
Jonathan Hart72175c22015-03-24 18:55:58 -070094
95 @Test
96 public void testIsUpdateAcceptable() {
97 // Going from null to something is always allowed
98 assertTrue(IntentData.isUpdateAcceptable(null, data1));
99
100 // we can go from older version to newer but not they other way
101 assertTrue(IntentData.isUpdateAcceptable(data1, data2));
102 assertFalse(IntentData.isUpdateAcceptable(data2, data1));
103
104 IntentData installing = new IntentData(intent1, IntentState.INSTALLING, timestamp1);
105 IntentData installed = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
106 IntentData withdrawing = new IntentData(intent1, IntentState.WITHDRAWING, timestamp1);
107 IntentData withdrawn = new IntentData(intent1, IntentState.WITHDRAWN, timestamp1);
108
109 IntentData failed = new IntentData(intent1, IntentState.FAILED, timestamp1);
110 IntentData purgeReq = new IntentData(intent1, IntentState.PURGE_REQ, timestamp1);
111
112 IntentData compiling = new IntentData(intent1, IntentState.COMPILING, timestamp1);
113 IntentData recompiling = new IntentData(intent1, IntentState.RECOMPILING, timestamp1);
114 IntentData installReq = new IntentData(intent1, IntentState.INSTALL_REQ, timestamp1);
115 IntentData withdrawReq = new IntentData(intent1, IntentState.WITHDRAW_REQ, timestamp1);
116
117 // We can't change to the same state
118 assertFalse(IntentData.isUpdateAcceptable(installing, installing));
119 assertFalse(IntentData.isUpdateAcceptable(installed, installed));
120
121 // From installing we can change to installed
122 assertTrue(IntentData.isUpdateAcceptable(installing, installed));
123
124 // Sanity checks in case the manager submits bogus state transitions
125 assertFalse(IntentData.isUpdateAcceptable(installing, withdrawing));
126 assertFalse(IntentData.isUpdateAcceptable(installing, withdrawn));
127 assertFalse(IntentData.isUpdateAcceptable(installed, withdrawing));
128 assertFalse(IntentData.isUpdateAcceptable(installed, withdrawn));
129
130 // We can't change to the same state
131 assertFalse(IntentData.isUpdateAcceptable(withdrawing, withdrawing));
132 assertFalse(IntentData.isUpdateAcceptable(withdrawn, withdrawn));
133
134 // From withdrawing we can change to withdrawn
135 assertTrue(IntentData.isUpdateAcceptable(withdrawing, withdrawn));
136
137 // Sanity checks in case the manager submits bogus state transitions
138 assertFalse(IntentData.isUpdateAcceptable(withdrawing, installing));
139 assertFalse(IntentData.isUpdateAcceptable(withdrawing, installed));
140 assertFalse(IntentData.isUpdateAcceptable(withdrawn, installing));
141 assertFalse(IntentData.isUpdateAcceptable(withdrawn, installed));
142
143 // We can't go from failed to failed
144 assertFalse(IntentData.isUpdateAcceptable(failed, failed));
145
146 // But we can go from any install* or withdraw* state to failed
147 assertTrue(IntentData.isUpdateAcceptable(installing, failed));
148 assertTrue(IntentData.isUpdateAcceptable(installed, failed));
149 assertTrue(IntentData.isUpdateAcceptable(withdrawing, failed));
150 assertTrue(IntentData.isUpdateAcceptable(withdrawn, failed));
151
152 // We can go from anything to purgeReq
153 assertTrue(IntentData.isUpdateAcceptable(installing, purgeReq));
154 assertTrue(IntentData.isUpdateAcceptable(installed, purgeReq));
155 assertTrue(IntentData.isUpdateAcceptable(withdrawing, purgeReq));
156 assertTrue(IntentData.isUpdateAcceptable(withdrawn, purgeReq));
157 assertTrue(IntentData.isUpdateAcceptable(failed, purgeReq));
158
159 // We can't go from purgeReq back to anything else
160 assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawn));
161 assertFalse(IntentData.isUpdateAcceptable(purgeReq, withdrawing));
162 assertFalse(IntentData.isUpdateAcceptable(purgeReq, installed));
163 assertFalse(IntentData.isUpdateAcceptable(purgeReq, installing));
164
165 // We're never allowed to store transient states
166 assertFalse(IntentData.isUpdateAcceptable(installing, compiling));
167 assertFalse(IntentData.isUpdateAcceptable(installing, recompiling));
168 assertFalse(IntentData.isUpdateAcceptable(installing, installReq));
169 assertFalse(IntentData.isUpdateAcceptable(installing, withdrawReq));
170 }
Ray Milkey8010bb42015-02-12 11:53:40 -0800171}