Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 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 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 23 | import org.onosproject.net.intent.AbstractIntentTest; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.Intent; |
| 25 | import org.onosproject.net.intent.IntentBatchDelegate; |
| 26 | import org.onosproject.net.intent.IntentData; |
| 27 | import org.onosproject.net.intent.IntentState; |
| 28 | import org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
| 29 | import org.onosproject.net.intent.IntentTestsMocks.MockTimestamp; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 30 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 31 | import java.util.Collection; |
| 32 | import java.util.List; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 33 | |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.Matchers.hasSize; |
| 36 | |
| 37 | /** |
| 38 | * Unit tests for the intent accumulator. |
| 39 | */ |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 40 | public class IntentAccumulatorTest extends AbstractIntentTest { |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 41 | |
| 42 | Intent intent1; |
| 43 | Intent intent2; |
| 44 | Intent intent3; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 45 | |
| 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 Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 54 | public void setUp() { |
| 55 | super.setUp(); |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 56 | |
| 57 | intent1 = new MockIntent(1L); |
| 58 | intent2 = new MockIntent(2L); |
| 59 | intent3 = new MockIntent(3L); |
| 60 | } |
| 61 | |
| 62 | /** |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 63 | * 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 | } |