blob: 71b53961f90680e45b46a0cb33db2b7b2c113e49 [file] [log] [blame]
Ray Milkey5a7787a2015-02-23 11:44:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey5a7787a2015-02-23 11:44:30 -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.impl;
17
18import java.util.Collection;
19import java.util.List;
20
21import org.hamcrest.Description;
22import org.hamcrest.TypeSafeDiagnosingMatcher;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onosproject.core.IdGenerator;
27import org.onosproject.net.intent.Intent;
28import org.onosproject.net.intent.IntentBatchDelegate;
29import org.onosproject.net.intent.IntentData;
30import org.onosproject.net.intent.IntentState;
31import org.onosproject.net.intent.IntentTestsMocks.MockIntent;
32import org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
33import org.onosproject.net.intent.MockIdGenerator;
34
35import com.google.common.collect.ImmutableList;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.hasSize;
39
40/**
41 * Unit tests for the intent accumulator.
42 */
43public 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}