38c8c8a14c95c04dfea9b6eda9f1c6ad8c471347
[genius.git] / mdsalutil / mdsalutil-api / src / test / java / org / opendaylight / genius / mdsalutil / interfaces / testutils / TestIMdsalApiManager.java
1 /*
2  * Copyright (c) 2016, 2017 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.genius.mdsalutil.interfaces.testutils;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.opendaylight.mdsal.binding.testutils.AssertDataObjects.assertEqualBeans;
14 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
15
16 import ch.vorburger.xtendbeans.XtendBeanGenerator;
17 import com.google.common.collect.ComparisonChain;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.Iterables;
20 import com.google.common.collect.Lists;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import com.google.common.util.concurrent.Futures;
23 import java.math.BigInteger;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.junit.ComparisonFailure;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.genius.mdsalutil.FlowEntity;
30 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Fake IMdsalApiManager useful for tests.
37  *
38  * <p>Read e.g.
39  * http://googletesting.blogspot.ch/2013/07/testing-on-toilet-know-your-test-doubles.html
40  * and http://martinfowler.com/articles/mocksArentStubs.html for more background.
41  *
42  * <p>This class is abstract just to save reading lines and typing keystrokes to
43  * manually implement a bunch of methods we're not yet interested in.  Create instances
44  * of it using it's static {@link #newInstance()} method.
45  *
46  * @author Michael Vorburger
47  * @autor Faseela K
48  */
49 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
50
51     private static final Logger LOG = LoggerFactory.getLogger(TestIMdsalApiManager.class);
52
53     private List<FlowEntity> flows;
54     private List<Group> groups;
55
56     public static TestIMdsalApiManager newInstance() {
57         return Mockito.mock(TestIMdsalApiManager.class, realOrException());
58     }
59
60     /**
61      * Get list of installed flows.
62      * Prefer the {@link #assertFlows(Iterable)} instead of using this and checking yourself.
63      * @return immutable copy of list of flows
64      */
65     public synchronized List<FlowEntity> getFlows() {
66         return ImmutableList.copyOf(getOrNewFlows());
67     }
68
69     private synchronized List<FlowEntity> getOrNewFlows() {
70         if (flows == null) {
71             flows = new ArrayList<>();
72         }
73         return flows;
74     }
75
76     private synchronized List<Group> getOrNewGroups() {
77         if (groups == null) {
78             groups = new ArrayList<>();
79         }
80         return groups;
81     }
82
83     public synchronized void assertFlows(Iterable<FlowEntity> expectedFlows) {
84         checkNonEmptyFlows(expectedFlows);
85         List<FlowEntity> nonNullFlows = getOrNewFlows();
86         if (!Iterables.isEmpty(expectedFlows)) {
87             assertTrue("No Flows created (bean wiring may be broken?)", !nonNullFlows.isEmpty());
88         }
89         // TODO Support Iterable <-> List directly within XtendBeanGenerator
90         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
91         assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
92     }
93
94
95     private synchronized void checkNonEmptyFlows(Iterable<FlowEntity> expectedFlows) {
96         if (!Iterables.isEmpty(expectedFlows)) {
97             assertTrue("No Flows created (bean wiring may be broken?)", !getOrNewFlows().isEmpty());
98         }
99     }
100
101     // ComparisonException doesn’t allow us to keep the cause (which we don’t care about anyway)
102     @SuppressWarnings("checkstyle:AvoidHidingCauseException")
103     public synchronized void assertFlowsInAnyOrder(Iterable<FlowEntity> expectedFlows) {
104         checkNonEmptyFlows(expectedFlows);
105         // TODO Support Iterable <-> List directly within XtendBeanGenerator
106         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
107
108         List<FlowEntity> sortedFlows = sortFlows(flows);
109         List<FlowEntity> sortedExpectedFlows = sortFlows(expectedFlowsAsNewArrayList);
110
111         // FYI: This containsExactlyElementsIn() assumes that FlowEntity, and everything in it,
112         // has correctly working equals() implementations.  assertEqualBeans() does not assume
113         // that, and would work even without equals, because it only uses property reflection.
114         // Normally this will lead to the same result, but if one day it doesn't (because of
115         // a bug in an equals() implementation somewhere), then it's worth to keep this diff
116         // in mind.
117
118         // FTR: This use of G Truth and then catch AssertionError and using assertEqualBeans iff NOK
119         // (thus discarding the message from G Truth) is a bit of a hack, but it works well...
120         // If you're tempted to improve this, please remember that correctly re-implementing
121         // containsExactlyElementsIn (or Hamcrest's similar containsInAnyOrder) isn't a 1 line
122         // trivia... e.g. a.containsAll(b) && b.containsAll(a) isn't sufficient, because it
123         // won't work for duplicates (which we frequently have here); and ordering before is
124         // not viable because FlowEntity is not Comparable, and Comparator based on hashCode
125         // is not a good idea (different instances can have same hashCode), and e.g. on
126         // System#identityHashCode even less so.
127         try {
128             assertThat(sortedFlows).containsExactlyElementsIn(sortedExpectedFlows);
129         } catch (AssertionError e) {
130             // We LOG the AssertionError just for clarity why containsExactlyElementsIn() failed
131             LOG.warn("assert containsExactlyElementsIn() failed", e);
132             // We LOG the expected and actual flow in case of a failed assertion
133             // because, even though that is typically just a HUGE String that's
134             // hard to read (the diff printed subsequently by assertEqualBeans
135             // is, much, more readable), there are cases when looking more closely
136             // at the full toString() output of the flows is still useful, so:
137             // TIP: Use e.g. 'wdiff -n expected.txt actual.txt | colordiff' to compare these!
138             LOG.warn("assert failed [order ignored!]; expected flows: {}", sortedExpectedFlows);
139             LOG.warn("assert failed [order ignored!]; actual flows  : {}", sortedFlows);
140             // The point of now also doing assertEqualBeans() is just that its output,
141             // in case of a comparison failure, is *A LOT* more clearly readable
142             // than what G Truth (or Hamcrest) can do based on toString.
143             assertEqualBeans(sortedExpectedFlows, sortedFlows);
144             if (sortedExpectedFlows.toString().equals(sortedFlows.toString())
145                     && !sortedExpectedFlows.equals(sortedFlows)) {
146                 fail("Suspected toString, missing getter, equals (hashCode) bug in FlowEntity related class!!! :-(");
147             }
148             throw new ComparisonFailure(
149                     "assertEqualBeans() MUST fail - given that the assertThat.containsExactlyElementsIn() just failed!"
150                     // Beware, we're using XtendBeanGenerator instead of XtendYangBeanGenerator like in
151                     // AssertDataObjects, but for FlowEntity it's the same... it only makes a difference for DataObjects
152                     + " What is missing in: " + new XtendBeanGenerator().getExpression(sortedFlows),
153                     sortedExpectedFlows.toString(), sortedFlows.toString());
154             // If this ^^^ occurs, then there is probably a bug in ch.vorburger.xtendbeans
155         }
156     }
157
158     private List<FlowEntity> sortFlows(Iterable<FlowEntity> flowsToSort) {
159         List<FlowEntity> sortedFlows = Lists.newArrayList(flowsToSort);
160         sortedFlows.sort((flow1, flow2) -> ComparisonChain.start()
161                 .compare(flow1.getTableId(), flow2.getTableId())
162                 .compare(flow1.getPriority(), flow2.getPriority())
163                 .compare(flow1.getFlowId(), flow2.getFlowId())
164                 .result());
165         return sortedFlows;
166     }
167
168     @Override
169     public synchronized CheckedFuture<Void, TransactionCommitFailedException> installFlow(FlowEntity flowEntity) {
170         getOrNewFlows().add(flowEntity);
171         return Futures.immediateCheckedFuture(null);
172     }
173
174     @Override
175     public synchronized CheckedFuture<Void, TransactionCommitFailedException> installFlow(BigInteger dpId,
176             FlowEntity flowEntity) {
177         // TODO should dpId be considered here? how? Copy clone FlowEntity and change its dpId?
178         return installFlow(flowEntity);
179     }
180
181     @Override
182     public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
183             FlowEntity flowEntity) {
184         // TODO should dpId be considered here? how? Copy clone FlowEntity and change its dpId?
185         getOrNewFlows().remove(flowEntity);
186         return Futures.immediateCheckedFuture(null);
187     }
188
189     @Override
190     public synchronized void batchedAddFlow(BigInteger dpId, FlowEntity flowEntity) {
191         getOrNewFlows().add(flowEntity);
192     }
193
194     @Override
195     public synchronized void batchedRemoveFlow(BigInteger dpId, FlowEntity flowEntity) {
196         getOrNewFlows().remove(flowEntity);
197     }
198
199     @Override
200     public void syncInstallGroup(BigInteger dpId, Group group, long delayTime) {
201         getOrNewGroups().add(group);
202     }
203
204     @Override
205     public void syncInstallGroup(BigInteger dpId, Group group) {
206         getOrNewGroups().add(group);
207     }
208
209     @Override
210     public void syncRemoveGroup(BigInteger dpId, Group groupEntity) {
211         getOrNewGroups().remove(groupEntity);
212     }
213 }