blob: 705a7e1921f4c9b3c5124fdd050e701402d75599 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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 */
Jonathan Hart33b81f22016-02-05 09:25:50 -080016package org.onosproject.routing;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080017
Jonathan Hart33b81f22016-02-05 09:25:50 -080018import org.easymock.EasyMock;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080019import org.easymock.IArgumentMatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.intent.Intent;
Luca Prete86ac7d12015-12-02 23:36:49 -080021import org.onosproject.net.intent.IntentUtils;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080022
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080023/**
24 * Helper class for testing operations submitted to the IntentService.
25 */
26public final class TestIntentServiceHelper {
27 /**
28 * Default constructor to prevent instantiation.
29 */
30 private TestIntentServiceHelper() {
31 }
32
33 /**
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080034 * 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 Hart33b81f22016-02-05 09:25:50 -080040 public static Intent eqExceptId(Intent intent) {
41 EasyMock.reportMatcher(new IdAgnosticIntentMatcher(intent));
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080042 return intent;
43 }
44
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080045 /*
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -080046 * EasyMock matcher that matches {@link Intent} but
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080047 * 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 Radoslavovdde22ae2014-11-24 11:47:17 -080055 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080056 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 Milkey4fd3ceb2015-12-10 14:43:08 -080086 return IntentUtils.intentsAreEqual(intent, providedIntent);
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080087 }
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080088 }
89
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080090}