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;
29 * Fake IMdsalApiManager useful for tests.
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.
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.
39 * @author Michael Vorburger
41 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
43 private List<FlowEntity> flows;
45 public static TestIMdsalApiManager newInstance() {
46 return Mockito.mock(TestIMdsalApiManager.class, realOrException());
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
54 public synchronized List<FlowEntity> getFlows() {
55 return ImmutableList.copyOf(getOrNewFlows());
58 private synchronized List<FlowEntity> getOrNewFlows() {
60 flows = new ArrayList<>();
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());
71 // TODO Support Iterable <-> List directly within XtendBeanGenerator
72 List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
73 assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
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());
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);
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
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.
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);
115 public synchronized void installFlow(FlowEntity flowEntity) {
116 getOrNewFlows().add(flowEntity);
120 public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
121 FlowEntity flowEntity) {
122 getOrNewFlows().remove(flowEntity);
123 return Futures.immediateCheckedFuture(null);
127 public void batchedAddFlow(BigInteger dpId, FlowEntity flowEntity) {
128 getOrNewFlows().add(flowEntity);
132 public void batchedRemoveFlow(BigInteger dpId, FlowEntity flowEntity) {
133 getOrNewFlows().remove(flowEntity);