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