Unit test for intent accumulator
Change-Id: Id9f8c918bdddc7fe24e4eb283676fd94275e58cf
Showing
2 changed files
with
199 additions
and
0 deletions
... | @@ -28,12 +28,14 @@ import java.util.LinkedList; | ... | @@ -28,12 +28,14 @@ import java.util.LinkedList; |
28 | import java.util.List; | 28 | import java.util.List; |
29 | import java.util.Objects; | 29 | import java.util.Objects; |
30 | import java.util.Set; | 30 | import java.util.Set; |
31 | +import java.util.concurrent.atomic.AtomicLong; | ||
31 | 32 | ||
32 | import org.onosproject.core.DefaultGroupId; | 33 | import org.onosproject.core.DefaultGroupId; |
33 | import org.onosproject.core.GroupId; | 34 | import org.onosproject.core.GroupId; |
34 | import org.onosproject.net.DeviceId; | 35 | import org.onosproject.net.DeviceId; |
35 | import org.onosproject.net.ElementId; | 36 | import org.onosproject.net.ElementId; |
36 | import org.onosproject.net.Link; | 37 | import org.onosproject.net.Link; |
38 | +import org.onosproject.net.NetTestTools; | ||
37 | import org.onosproject.net.Path; | 39 | import org.onosproject.net.Path; |
38 | import org.onosproject.net.flow.FlowId; | 40 | import org.onosproject.net.flow.FlowId; |
39 | import org.onosproject.net.flow.FlowRule; | 41 | import org.onosproject.net.flow.FlowRule; |
... | @@ -56,6 +58,7 @@ import org.onosproject.net.topology.DefaultTopologyVertex; | ... | @@ -56,6 +58,7 @@ import org.onosproject.net.topology.DefaultTopologyVertex; |
56 | import org.onosproject.net.topology.LinkWeight; | 58 | import org.onosproject.net.topology.LinkWeight; |
57 | import org.onosproject.net.topology.PathService; | 59 | import org.onosproject.net.topology.PathService; |
58 | import org.onosproject.net.topology.TopologyVertex; | 60 | import org.onosproject.net.topology.TopologyVertex; |
61 | +import org.onosproject.store.Timestamp; | ||
59 | 62 | ||
60 | /** | 63 | /** |
61 | * Common mocks used by the intent framework tests. | 64 | * Common mocks used by the intent framework tests. |
... | @@ -371,5 +374,41 @@ public class IntentTestsMocks { | ... | @@ -371,5 +374,41 @@ public class IntentTestsMocks { |
371 | } | 374 | } |
372 | } | 375 | } |
373 | 376 | ||
377 | + public static class MockIntent extends Intent { | ||
378 | + private static AtomicLong counter = new AtomicLong(0); | ||
379 | + | ||
380 | + private final Long number; | ||
381 | + | ||
382 | + public MockIntent(Long number) { | ||
383 | + super(NetTestTools.APP_ID, Collections.emptyList()); | ||
384 | + this.number = number; | ||
385 | + } | ||
386 | + | ||
387 | + public Long number() { | ||
388 | + return number; | ||
389 | + } | ||
390 | + | ||
391 | + public static Long nextId() { | ||
392 | + return counter.getAndIncrement(); | ||
393 | + } | ||
394 | + } | ||
395 | + | ||
396 | + public static class MockTimestamp implements Timestamp { | ||
397 | + final int value; | ||
398 | + | ||
399 | + public MockTimestamp(int value) { | ||
400 | + this.value = value; | ||
401 | + } | ||
402 | + | ||
403 | + @Override | ||
404 | + public int compareTo(Timestamp o) { | ||
405 | + if (!(o instanceof MockTimestamp)) { | ||
406 | + return -1; | ||
407 | + } | ||
408 | + MockTimestamp that = (MockTimestamp) o; | ||
409 | + return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1)); | ||
410 | + } | ||
411 | + } | ||
412 | + | ||
374 | 413 | ||
375 | } | 414 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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.impl; | ||
17 | + | ||
18 | +import java.util.Collection; | ||
19 | +import java.util.List; | ||
20 | + | ||
21 | +import org.hamcrest.Description; | ||
22 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
23 | +import org.junit.After; | ||
24 | +import org.junit.Before; | ||
25 | +import org.junit.Test; | ||
26 | +import org.onosproject.core.IdGenerator; | ||
27 | +import org.onosproject.net.intent.Intent; | ||
28 | +import org.onosproject.net.intent.IntentBatchDelegate; | ||
29 | +import org.onosproject.net.intent.IntentData; | ||
30 | +import org.onosproject.net.intent.IntentState; | ||
31 | +import org.onosproject.net.intent.IntentTestsMocks.MockIntent; | ||
32 | +import org.onosproject.net.intent.IntentTestsMocks.MockTimestamp; | ||
33 | +import org.onosproject.net.intent.MockIdGenerator; | ||
34 | + | ||
35 | +import com.google.common.collect.ImmutableList; | ||
36 | + | ||
37 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
38 | +import static org.hamcrest.Matchers.hasSize; | ||
39 | + | ||
40 | +/** | ||
41 | + * Unit tests for the intent accumulator. | ||
42 | + */ | ||
43 | +public class IntentAccumulatorTest { | ||
44 | + | ||
45 | + Intent intent1; | ||
46 | + Intent intent2; | ||
47 | + Intent intent3; | ||
48 | + IdGenerator mockGenerator; | ||
49 | + | ||
50 | + private static IntentDataMatcher containsIntent(Intent intent) { | ||
51 | + return new IntentDataMatcher(intent); | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Creates mock intents used by the test. | ||
56 | + */ | ||
57 | + @Before | ||
58 | + public void localSetup() { | ||
59 | + mockGenerator = new MockIdGenerator(); | ||
60 | + Intent.bindIdGenerator(mockGenerator); | ||
61 | + | ||
62 | + intent1 = new MockIntent(1L); | ||
63 | + intent2 = new MockIntent(2L); | ||
64 | + intent3 = new MockIntent(3L); | ||
65 | + } | ||
66 | + | ||
67 | + /** | ||
68 | + * Removes id generator from the Intent class. | ||
69 | + */ | ||
70 | + @After | ||
71 | + public void localTearDown() { | ||
72 | + Intent.unbindIdGenerator(mockGenerator); | ||
73 | + } | ||
74 | + | ||
75 | + /** | ||
76 | + * Hamcrest matcher to check that a collection of intent data objects | ||
77 | + * contains an entry for a given intent. | ||
78 | + */ | ||
79 | + private static final class IntentDataMatcher | ||
80 | + extends TypeSafeDiagnosingMatcher<Collection<IntentData>> { | ||
81 | + | ||
82 | + final Intent intent; | ||
83 | + | ||
84 | + public IntentDataMatcher(Intent intent) { | ||
85 | + this.intent = intent; | ||
86 | + } | ||
87 | + | ||
88 | + /** | ||
89 | + * Check that the given collection of intent data contains a specific | ||
90 | + * intent. | ||
91 | + * | ||
92 | + * @param operations collection of intent data | ||
93 | + * @param description description | ||
94 | + * @return true if the collection contains the intent, false otherwise. | ||
95 | + */ | ||
96 | + public boolean matchesSafely(Collection<IntentData> operations, | ||
97 | + Description description) { | ||
98 | + for (IntentData operation : operations) { | ||
99 | + if (operation.key().equals(intent.key())) { | ||
100 | + if (operation.state() != IntentState.INSTALLED) { | ||
101 | + description.appendText("state was " + operation.state()); | ||
102 | + return false; | ||
103 | + } | ||
104 | + return true; | ||
105 | + } | ||
106 | + } | ||
107 | + description.appendText("key was not found " + intent.key()); | ||
108 | + return false; | ||
109 | + } | ||
110 | + | ||
111 | + @Override | ||
112 | + public void describeTo(Description description) { | ||
113 | + description.appendText("INSTALLED state intent with key " + intent.key()); | ||
114 | + } | ||
115 | + } | ||
116 | + | ||
117 | + /** | ||
118 | + * Mock batch delegate class. Gets calls from the accumulator and checks | ||
119 | + * that the operations have been properly compressed. | ||
120 | + */ | ||
121 | + private class MockIntentBatchDelegate | ||
122 | + implements IntentBatchDelegate { | ||
123 | + public void execute(Collection<IntentData> operations) { | ||
124 | + assertThat(operations, hasSize(3)); | ||
125 | + assertThat(operations, containsIntent(intent1)); | ||
126 | + assertThat(operations, containsIntent(intent2)); | ||
127 | + assertThat(operations, containsIntent(intent3)); | ||
128 | + } | ||
129 | + } | ||
130 | + | ||
131 | + /** | ||
132 | + * Tests that the accumulator properly compresses operations on the same | ||
133 | + * intents. | ||
134 | + */ | ||
135 | + @Test | ||
136 | + public void checkAccumulator() { | ||
137 | + | ||
138 | + MockIntentBatchDelegate delegate = new MockIntentBatchDelegate(); | ||
139 | + IntentAccumulator accumulator = new IntentAccumulator(delegate); | ||
140 | + | ||
141 | + List<IntentData> intentDataItems = ImmutableList.of( | ||
142 | + new IntentData(intent1, IntentState.INSTALLING, | ||
143 | + new MockTimestamp(1)), | ||
144 | + new IntentData(intent2, IntentState.INSTALLING, | ||
145 | + new MockTimestamp(1)), | ||
146 | + new IntentData(intent3, IntentState.INSTALLED, | ||
147 | + new MockTimestamp(1)), | ||
148 | + new IntentData(intent2, IntentState.INSTALLED, | ||
149 | + new MockTimestamp(1)), | ||
150 | + new IntentData(intent2, IntentState.INSTALLED, | ||
151 | + new MockTimestamp(1)), | ||
152 | + new IntentData(intent1, IntentState.INSTALLED, | ||
153 | + new MockTimestamp(1))); | ||
154 | + | ||
155 | + | ||
156 | + accumulator.processItems(intentDataItems); | ||
157 | + } | ||
158 | + | ||
159 | + | ||
160 | +} |
-
Please register or login to post a comment