alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | ab98466 | 2014-12-04 18:56:18 -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 | */ |
Jonathan Hart | 33b81f2 | 2016-02-05 09:25:50 -0800 | [diff] [blame] | 16 | package org.onosproject.routing; |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | 33b81f2 | 2016-02-05 09:25:50 -0800 | [diff] [blame] | 18 | import org.easymock.EasyMock; |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 19 | import org.easymock.IArgumentMatcher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.intent.Intent; |
Luca Prete | 86ac7d1 | 2015-12-02 23:36:49 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.IntentUtils; |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 22 | |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Helper class for testing operations submitted to the IntentService. |
| 25 | */ |
| 26 | public final class TestIntentServiceHelper { |
| 27 | /** |
| 28 | * Default constructor to prevent instantiation. |
| 29 | */ |
| 30 | private TestIntentServiceHelper() { |
| 31 | } |
| 32 | |
| 33 | /** |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 34 | * Matcher method to set the expected intent to match against |
| 35 | * (ignoring the intent ID for the intent). |
| 36 | * |
| 37 | * @param intent the expected Intent |
| 38 | * @return the submitted Intent |
| 39 | */ |
Jonathan Hart | 33b81f2 | 2016-02-05 09:25:50 -0800 | [diff] [blame] | 40 | public static Intent eqExceptId(Intent intent) { |
| 41 | EasyMock.reportMatcher(new IdAgnosticIntentMatcher(intent)); |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 42 | return intent; |
| 43 | } |
| 44 | |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 45 | /* |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 46 | * EasyMock matcher that matches {@link Intent} but |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 47 | * ignores the {@link IntentId} when matching. |
| 48 | * <p/> |
| 49 | * The normal intent equals method tests that the intent IDs are equal, |
| 50 | * however in these tests we can't know what the intent IDs will be in |
| 51 | * advance, so we can't set up expected intents with the correct IDs. Thus, |
| 52 | * the solution is to use an EasyMock matcher that verifies that all the |
| 53 | * value properties of the provided intent match the expected values, but |
| 54 | * ignores the intent ID when testing equality. |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 55 | */ |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 56 | private static final class IdAgnosticIntentMatcher implements |
| 57 | IArgumentMatcher { |
| 58 | |
| 59 | private final Intent intent; |
| 60 | private String providedString; |
| 61 | |
| 62 | /** |
| 63 | * Constructor taking the expected intent to match against. |
| 64 | * |
| 65 | * @param intent the expected intent |
| 66 | */ |
| 67 | public IdAgnosticIntentMatcher(Intent intent) { |
| 68 | this.intent = intent; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void appendTo(StringBuffer strBuffer) { |
| 73 | strBuffer.append("IntentMatcher unable to match: " |
| 74 | + providedString); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean matches(Object object) { |
| 79 | if (!(object instanceof Intent)) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | Intent providedIntent = (Intent) object; |
| 84 | providedString = providedIntent.toString(); |
| 85 | |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 86 | return IntentUtils.intentsAreEqual(intent, providedIntent); |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 87 | } |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 90 | } |