blob: c241271616bddd8c896bc5fe4e24a4dccd401f2b [file] [log] [blame]
Ray Milkey5a7787a2015-02-23 11:44:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070018import com.google.common.collect.ImmutableList;
Ray Milkey5a7787a2015-02-23 11:44:30 -080019import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
Ray Milkey5a7787a2015-02-23 11:44:30 -080021import org.junit.Before;
22import org.junit.Test;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070023import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkey5a7787a2015-02-23 11:44:30 -080024import org.onosproject.net.intent.Intent;
25import org.onosproject.net.intent.IntentBatchDelegate;
26import org.onosproject.net.intent.IntentData;
27import org.onosproject.net.intent.IntentState;
28import org.onosproject.net.intent.IntentTestsMocks.MockIntent;
29import org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
Ray Milkey5a7787a2015-02-23 11:44:30 -080030
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070031import java.util.Collection;
32import java.util.List;
Ray Milkey5a7787a2015-02-23 11:44:30 -080033
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.hasSize;
36
37/**
38 * Unit tests for the intent accumulator.
39 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070040public class IntentAccumulatorTest extends AbstractIntentTest {
Ray Milkey5a7787a2015-02-23 11:44:30 -080041
42 Intent intent1;
43 Intent intent2;
44 Intent intent3;
Ray Milkey5a7787a2015-02-23 11:44:30 -080045
46 private static IntentDataMatcher containsIntent(Intent intent) {
47 return new IntentDataMatcher(intent);
48 }
49
50 /**
51 * Creates mock intents used by the test.
52 */
53 @Before
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070054 public void setUp() {
55 super.setUp();
Ray Milkey5a7787a2015-02-23 11:44:30 -080056
57 intent1 = new MockIntent(1L);
58 intent2 = new MockIntent(2L);
59 intent3 = new MockIntent(3L);
60 }
61
62 /**
Ray Milkey5a7787a2015-02-23 11:44:30 -080063 * Hamcrest matcher to check that a collection of intent data objects
64 * contains an entry for a given intent.
65 */
66 private static final class IntentDataMatcher
67 extends TypeSafeDiagnosingMatcher<Collection<IntentData>> {
68
69 final Intent intent;
70
71 public IntentDataMatcher(Intent intent) {
72 this.intent = intent;
73 }
74
75 /**
76 * Check that the given collection of intent data contains a specific
77 * intent.
78 *
79 * @param operations collection of intent data
80 * @param description description
81 * @return true if the collection contains the intent, false otherwise.
82 */
83 public boolean matchesSafely(Collection<IntentData> operations,
84 Description description) {
85 for (IntentData operation : operations) {
86 if (operation.key().equals(intent.key())) {
87 if (operation.state() != IntentState.INSTALLED) {
88 description.appendText("state was " + operation.state());
89 return false;
90 }
91 return true;
92 }
93 }
94 description.appendText("key was not found " + intent.key());
95 return false;
96 }
97
98 @Override
99 public void describeTo(Description description) {
100 description.appendText("INSTALLED state intent with key " + intent.key());
101 }
102 }
103
104 /**
105 * Mock batch delegate class. Gets calls from the accumulator and checks
106 * that the operations have been properly compressed.
107 */
108 private class MockIntentBatchDelegate
109 implements IntentBatchDelegate {
110 public void execute(Collection<IntentData> operations) {
111 assertThat(operations, hasSize(3));
112 assertThat(operations, containsIntent(intent1));
113 assertThat(operations, containsIntent(intent2));
114 assertThat(operations, containsIntent(intent3));
115 }
116 }
117
118 /**
119 * Tests that the accumulator properly compresses operations on the same
120 * intents.
121 */
122 @Test
123 public void checkAccumulator() {
124
125 MockIntentBatchDelegate delegate = new MockIntentBatchDelegate();
126 IntentAccumulator accumulator = new IntentAccumulator(delegate);
127
128 List<IntentData> intentDataItems = ImmutableList.of(
129 new IntentData(intent1, IntentState.INSTALLING,
130 new MockTimestamp(1)),
131 new IntentData(intent2, IntentState.INSTALLING,
132 new MockTimestamp(1)),
133 new IntentData(intent3, IntentState.INSTALLED,
134 new MockTimestamp(1)),
135 new IntentData(intent2, IntentState.INSTALLED,
136 new MockTimestamp(1)),
137 new IntentData(intent2, IntentState.INSTALLED,
138 new MockTimestamp(1)),
139 new IntentData(intent1, IntentState.INSTALLED,
140 new MockTimestamp(1)));
141
142
143 accumulator.processItems(intentDataItems);
144 }
145
146
147}