Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 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 | } |