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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Fake IMdsalApiManager useful for tests.
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.
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.
41 * @author Michael Vorburger
43 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
45 private static final Logger LOG = LoggerFactory.getLogger(TestIMdsalApiManager.class);
47 private List<FlowEntity> flows;
49 public static TestIMdsalApiManager newInstance() {
50 return Mockito.mock(TestIMdsalApiManager.class, realOrException());
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
58 public synchronized List<FlowEntity> getFlows() {
59 return ImmutableList.copyOf(getOrNewFlows());
62 private synchronized List<FlowEntity> getOrNewFlows() {
64 flows = new ArrayList<>();
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());
75 // TODO Support Iterable <-> List directly within XtendBeanGenerator
76 List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
77 assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
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());
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);
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
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.
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);
128 public synchronized void installFlow(FlowEntity flowEntity) {
129 getOrNewFlows().add(flowEntity);
133 public synchronized CheckedFuture<Void, TransactionCommitFailedException> installFlow(BigInteger dpId,
134 FlowEntity flowEntity) {
135 installFlow(flowEntity);
136 return Futures.immediateCheckedFuture(null);
140 public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
141 FlowEntity flowEntity) {
142 getOrNewFlows().remove(flowEntity);
143 return Futures.immediateCheckedFuture(null);
147 public synchronized void batchedAddFlow(BigInteger dpId, FlowEntity flowEntity) {
148 getOrNewFlows().add(flowEntity);
152 public synchronized void batchedRemoveFlow(BigInteger dpId, FlowEntity flowEntity) {
153 getOrNewFlows().remove(flowEntity);