blob: 4df7f9de0c12ab02551d2c0df986453195955e9f [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080017
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080018import org.easymock.IArgumentMatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.intent.Intent;
Luca Prete86ac7d12015-12-02 23:36:49 -080020import org.onosproject.net.intent.IntentUtils;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080021
Sho SHIMIZU4931ee52015-02-03 21:09:28 -080022import static org.easymock.EasyMock.reportMatcher;
23
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080024/**
25 * Helper class for testing operations submitted to the IntentService.
26 */
27public final class TestIntentServiceHelper {
28 /**
29 * Default constructor to prevent instantiation.
30 */
31 private TestIntentServiceHelper() {
32 }
33
34 /**
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080035 * Matcher method to set the expected intent to match against
36 * (ignoring the intent ID for the intent).
37 *
38 * @param intent the expected Intent
39 * @return the submitted Intent
40 */
41 static Intent eqExceptId(Intent intent) {
42 reportMatcher(new IdAgnosticIntentMatcher(intent));
43 return intent;
44 }
45
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080046 /*
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -080047 * EasyMock matcher that matches {@link Intent} but
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080048 * ignores the {@link IntentId} when matching.
49 * <p/>
50 * The normal intent equals method tests that the intent IDs are equal,
51 * however in these tests we can't know what the intent IDs will be in
52 * advance, so we can't set up expected intents with the correct IDs. Thus,
53 * the solution is to use an EasyMock matcher that verifies that all the
54 * value properties of the provided intent match the expected values, but
55 * ignores the intent ID when testing equality.
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080056 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080057 private static final class IdAgnosticIntentMatcher implements
58 IArgumentMatcher {
59
60 private final Intent intent;
61 private String providedString;
62
63 /**
64 * Constructor taking the expected intent to match against.
65 *
66 * @param intent the expected intent
67 */
68 public IdAgnosticIntentMatcher(Intent intent) {
69 this.intent = intent;
70 }
71
72 @Override
73 public void appendTo(StringBuffer strBuffer) {
74 strBuffer.append("IntentMatcher unable to match: "
75 + providedString);
76 }
77
78 @Override
79 public boolean matches(Object object) {
80 if (!(object instanceof Intent)) {
81 return false;
82 }
83
84 Intent providedIntent = (Intent) object;
85 providedString = providedIntent.toString();
86
Jonathan Hart9a426f82015-09-03 15:43:13 +020087 return IntentUtils.equals(intent, providedIntent);
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080088 }
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080089 }
90
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080091}