blob: 8914f176e505d0ff2a17fac10b9e881c0e8bf4bd [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();
Thomas Vachuska23235962017-02-03 11:44:15 -080060 Intent.unbindIdGenerator(mockGenerator);
Ray Milkey5a7787a2015-02-23 11:44:30 -080061 Intent.bindIdGenerator(mockGenerator);
62
63 intent1 = new MockIntent(1L);
64 intent2 = new MockIntent(2L);
65 intent3 = new MockIntent(3L);
66 }
67
68 /**
69 * Removes id generator from the Intent class.
70 */
71 @After
72 public void localTearDown() {
73 Intent.unbindIdGenerator(mockGenerator);
74 }
75
76 /**
77 * Hamcrest matcher to check that a collection of intent data objects
78 * contains an entry for a given intent.
79 */
80 private static final class IntentDataMatcher
81 extends TypeSafeDiagnosingMatcher<Collection<IntentData>> {
82
83 final Intent intent;
84
85 public IntentDataMatcher(Intent intent) {
86 this.intent = intent;
87 }
88
89 /**
90 * Check that the given collection of intent data contains a specific
91 * intent.
92 *
93 * @param operations collection of intent data
94 * @param description description
95 * @return true if the collection contains the intent, false otherwise.
96 */
97 public boolean matchesSafely(Collection<IntentData> operations,
98 Description description) {
99 for (IntentData operation : operations) {
100 if (operation.key().equals(intent.key())) {
101 if (operation.state() != IntentState.INSTALLED) {
102 description.appendText("state was " + operation.state());
103 return false;
104 }
105 return true;
106 }
107 }
108 description.appendText("key was not found " + intent.key());
109 return false;
110 }
111
112 @Override
113 public void describeTo(Description description) {
114 description.appendText("INSTALLED state intent with key " + intent.key());
115 }
116 }
117
118 /**
119 * Mock batch delegate class. Gets calls from the accumulator and checks
120 * that the operations have been properly compressed.
121 */
122 private class MockIntentBatchDelegate
123 implements IntentBatchDelegate {
124 public void execute(Collection<IntentData> operations) {
125 assertThat(operations, hasSize(3));
126 assertThat(operations, containsIntent(intent1));
127 assertThat(operations, containsIntent(intent2));
128 assertThat(operations, containsIntent(intent3));
129 }
130 }
131
132 /**
133 * Tests that the accumulator properly compresses operations on the same
134 * intents.
135 */
136 @Test
137 public void checkAccumulator() {
138
139 MockIntentBatchDelegate delegate = new MockIntentBatchDelegate();
140 IntentAccumulator accumulator = new IntentAccumulator(delegate);
141
142 List<IntentData> intentDataItems = ImmutableList.of(
143 new IntentData(intent1, IntentState.INSTALLING,
144 new MockTimestamp(1)),
145 new IntentData(intent2, IntentState.INSTALLING,
146 new MockTimestamp(1)),
147 new IntentData(intent3, IntentState.INSTALLED,
148 new MockTimestamp(1)),
149 new IntentData(intent2, IntentState.INSTALLED,
150 new MockTimestamp(1)),
151 new IntentData(intent2, IntentState.INSTALLED,
152 new MockTimestamp(1)),
153 new IntentData(intent1, IntentState.INSTALLED,
154 new MockTimestamp(1)));
155
156
157 accumulator.processItems(intentDataItems);
158 }
159
160
161}