ddde6dfd88d8c17f31c6df3c8e76d10250e09c2f
[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
28 /**
29  * Fake IMdsalApiManager useful for tests.
30  *
31  * <p>Read e.g.
32  * http://googletesting.blogspot.ch/2013/07/testing-on-toilet-know-your-test-doubles.html
33  * and http://martinfowler.com/articles/mocksArentStubs.html for more background.
34  *
35  * <p>This class is abstract just to save reading lines and typing keystrokes to
36  * manually implement a bunch of methods we're not yet interested in.  Create instances
37  * of it using it's static {@link #newInstance()} method.
38  *
39  * @author Michael Vorburger
40  */
41 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
42
43     private List<FlowEntity> flows;
44
45     public static TestIMdsalApiManager newInstance() {
46         return Mockito.mock(TestIMdsalApiManager.class, realOrException());
47     }
48
49     /**
50      * Get list of installed flows.
51      * Prefer the {@link #assertFlows(Iterable)} instead of using this and checking yourself.
52      * @return immutable copy of list of flows
53      */
54     public synchronized List<FlowEntity> getFlows() {
55         return ImmutableList.copyOf(getOrNewFlows());
56     }
57
58     private synchronized List<FlowEntity> getOrNewFlows() {
59         if (flows == null) {
60             flows = new ArrayList<>();
61         }
62         return flows;
63     }
64
65     public synchronized void assertFlows(Iterable<FlowEntity> expectedFlows) {
66         checkNonEmptyFlows(expectedFlows);
67         List<FlowEntity> nonNullFlows = getOrNewFlows();
68         if (!Iterables.isEmpty(expectedFlows)) {
69             assertTrue("No Flows created (bean wiring may be broken?)", !nonNullFlows.isEmpty());
70         }
71         // TODO Support Iterable <-> List directly within XtendBeanGenerator
72         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
73         assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
74     }
75
76
77     private synchronized void checkNonEmptyFlows(Iterable<FlowEntity> expectedFlows) {
78         if (!Iterables.isEmpty(expectedFlows)) {
79             assertTrue("No Flows created (bean wiring may be broken?)", !getOrNewFlows().isEmpty());
80         }
81     }
82
83     public synchronized void assertFlowsInAnyOrder(Iterable<FlowEntity> expectedFlows) {
84         checkNonEmptyFlows(expectedFlows);
85         // TODO Support Iterable <-> List directly within XtendBeanGenerator
86         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
87
88         // FYI: This containsExactlyElementsIn() assumes that FlowEntity, and everything in it,
89         // has correctly working equals() implementations.  assertEqualBeans() does not assume
90         // that, and would work even without equals, because it only uses property reflection.
91         // Normally this will lead to the same result, but if one day it doesn't (because of
92         // a bug in an equals() implementation somewhere), then it's worth to keep this diff
93         // in mind.
94
95         // FTR: This use of G Truth and then catch AssertionError and using assertEqualBeans iff NOK
96         // (thus discarding the message from G Truth) is a bit of a hack, but it works well...
97         // If you're tempted to improve this, please remember that correctly re-implementing
98         // containsExactlyElementsIn (or Hamcrest's similar containsInAnyOrder) isn't a 1 line
99         // trivia... e.g. a.containsAll(b) && b.containsAll(a) isn't sufficient, because it
100         // won't work for duplicates (which we frequently have here); and ordering before is
101         // not viable because FlowEntity is not Comparable, and Comparator based on hashCode
102         // is not a good idea (different instances can have same hashCode), and e.g. on
103         // System#identityHashCode even less so.
104         try {
105             assertThat(flows).containsExactlyElementsIn(expectedFlowsAsNewArrayList);
106         } catch (AssertionError e) {
107             // The point of this is basically just that our assertEqualBeans output,
108             // in case of a comparison failure, is *A LOT* more clearly readable
109             // than what G Truth (or Hamcrest) can do based on toString.
110             assertEqualBeans(expectedFlowsAsNewArrayList, flows);
111         }
112     }
113
114     @Override
115     public synchronized void installFlow(FlowEntity flowEntity) {
116         getOrNewFlows().add(flowEntity);
117     }
118
119     @Override
120     public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
121             FlowEntity flowEntity) {
122         getOrNewFlows().remove(flowEntity);
123         return Futures.immediateCheckedFuture(null);
124     }
125
126     @Override
127     public void batchedAddFlow(BigInteger dpId, FlowEntity flowEntity) {
128         getOrNewFlows().add(flowEntity);
129     }
130
131     @Override
132     public void batchedRemoveFlow(BigInteger dpId, FlowEntity flowEntity) {
133         getOrNewFlows().remove(flowEntity);
134     }
135
136 }