Merge "Updated TestIMdsalApiManager.java to support installFlow() with CheckedFuture...
[genius.git] / mdsalutil / mdsalutil-api / src / test / java / org / opendaylight / genius / mdsalutil / interfaces / testutils / TestIMdsalApiManager.java
1 /*
2  * Copyright (c) 2016 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.opendaylight.mdsal.binding.testutils.AssertDataObjects.assertEqualBeans;
13 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
14
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.Lists;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.Futures;
20 import java.math.BigInteger;
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.genius.mdsalutil.FlowEntity;
26 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Fake IMdsalApiManager useful for tests.
32  *
33  * <p>Read e.g.
34  * http://googletesting.blogspot.ch/2013/07/testing-on-toilet-know-your-test-doubles.html
35  * and http://martinfowler.com/articles/mocksArentStubs.html for more background.
36  *
37  * <p>This class is abstract just to save reading lines and typing keystrokes to
38  * manually implement a bunch of methods we're not yet interested in.  Create instances
39  * of it using it's static {@link #newInstance()} method.
40  *
41  * @author Michael Vorburger
42  */
43 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
44
45     private static final Logger LOG = LoggerFactory.getLogger(TestIMdsalApiManager.class);
46
47     private List<FlowEntity> flows;
48
49     public static TestIMdsalApiManager newInstance() {
50         return Mockito.mock(TestIMdsalApiManager.class, realOrException());
51     }
52
53     /**
54      * Get list of installed flows.
55      * Prefer the {@link #assertFlows(Iterable)} instead of using this and checking yourself.
56      * @return immutable copy of list of flows
57      */
58     public synchronized List<FlowEntity> getFlows() {
59         return ImmutableList.copyOf(getOrNewFlows());
60     }
61
62     private synchronized List<FlowEntity> getOrNewFlows() {
63         if (flows == null) {
64             flows = new ArrayList<>();
65         }
66         return flows;
67     }
68
69     public synchronized void assertFlows(Iterable<FlowEntity> expectedFlows) {
70         checkNonEmptyFlows(expectedFlows);
71         List<FlowEntity> nonNullFlows = getOrNewFlows();
72         if (!Iterables.isEmpty(expectedFlows)) {
73             assertTrue("No Flows created (bean wiring may be broken?)", !nonNullFlows.isEmpty());
74         }
75         // TODO Support Iterable <-> List directly within XtendBeanGenerator
76         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
77         assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
78     }
79
80
81     private synchronized void checkNonEmptyFlows(Iterable<FlowEntity> expectedFlows) {
82         if (!Iterables.isEmpty(expectedFlows)) {
83             assertTrue("No Flows created (bean wiring may be broken?)", !getOrNewFlows().isEmpty());
84         }
85     }
86
87     public synchronized void assertFlowsInAnyOrder(Iterable<FlowEntity> expectedFlows) {
88         checkNonEmptyFlows(expectedFlows);
89         // TODO Support Iterable <-> List directly within XtendBeanGenerator
90         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
91
92         // FYI: This containsExactlyElementsIn() assumes that FlowEntity, and everything in it,
93         // has correctly working equals() implementations.  assertEqualBeans() does not assume
94         // that, and would work even without equals, because it only uses property reflection.
95         // Normally this will lead to the same result, but if one day it doesn't (because of
96         // a bug in an equals() implementation somewhere), then it's worth to keep this diff
97         // in mind.
98
99         // FTR: This use of G Truth and then catch AssertionError and using assertEqualBeans iff NOK
100         // (thus discarding the message from G Truth) is a bit of a hack, but it works well...
101         // If you're tempted to improve this, please remember that correctly re-implementing
102         // containsExactlyElementsIn (or Hamcrest's similar containsInAnyOrder) isn't a 1 line
103         // trivia... e.g. a.containsAll(b) && b.containsAll(a) isn't sufficient, because it
104         // won't work for duplicates (which we frequently have here); and ordering before is
105         // not viable because FlowEntity is not Comparable, and Comparator based on hashCode
106         // is not a good idea (different instances can have same hashCode), and e.g. on
107         // System#identityHashCode even less so.
108         try {
109             assertThat(flows).containsExactlyElementsIn(expectedFlowsAsNewArrayList);
110         } catch (AssertionError e) {
111             // We LOG the AssertionError just for clarity why containsExactlyElementsIn() failed
112             LOG.warn("assert containsExactlyElementsIn() failed", e);
113             // We LOG the expected and actual flow in case of a failed assertion
114             // because, even though that is typically just a HUGE String that's
115             // hard to read (the diff printed subsequently by assertEqualBeans
116             // is, much, more readable), there are cases when looking more closely
117             // at the full toString() output of the flows is still useful, so:
118             LOG.warn("assert failed [order ignored!]; expected flows: {}", expectedFlowsAsNewArrayList);
119             LOG.warn("assert failed [order ignored!]; actual flows  : {}", flows);
120             // The point of this is basically just that our assertEqualBeans output,
121             // in case of a comparison failure, is *A LOT* more clearly readable
122             // than what G Truth (or Hamcrest) can do based on toString.
123             assertEqualBeans(expectedFlowsAsNewArrayList, flows);
124         }
125     }
126
127     @Override
128     public synchronized void installFlow(FlowEntity flowEntity) {
129         getOrNewFlows().add(flowEntity);
130     }
131
132     @Override
133     public synchronized CheckedFuture<Void, TransactionCommitFailedException> installFlow(BigInteger dpId,
134             FlowEntity flowEntity) {
135         installFlow(flowEntity);
136         return Futures.immediateCheckedFuture(null);
137     }
138
139     @Override
140     public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
141             FlowEntity flowEntity) {
142         getOrNewFlows().remove(flowEntity);
143         return Futures.immediateCheckedFuture(null);
144     }
145
146     @Override
147     public synchronized void batchedAddFlow(BigInteger dpId, FlowEntity flowEntity) {
148         getOrNewFlows().add(flowEntity);
149     }
150
151     @Override
152     public synchronized void batchedRemoveFlow(BigInteger dpId, FlowEntity flowEntity) {
153         getOrNewFlows().remove(flowEntity);
154     }
155
156 }