Merge "ChainableDataTreeChangeListener with addBeforeListener()"
[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 org.junit.Assert.assertTrue;
11 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
12
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.mockito.Mockito;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.genius.mdsalutil.FlowEntity;
24 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
25 import org.opendaylight.mdsal.binding.testutils.AssertDataObjects;
26
27 /**
28  * Fake IMdsalApiManager useful for tests.
29  *
30  * <p>Read e.g.
31  * http://googletesting.blogspot.ch/2013/07/testing-on-toilet-know-your-test-doubles.html
32  * and http://martinfowler.com/articles/mocksArentStubs.html for more background.
33  *
34  * <p>This class is abstract just to save reading lines and typing keystrokes to
35  * manually implement a bunch of methods we're not yet interested in.  Create instances
36  * of it using it's static {@link #newInstance()} method.
37  *
38  * @author Michael Vorburger
39  */
40 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
41
42     private List<FlowEntity> flows;
43
44     public static TestIMdsalApiManager newInstance() {
45         return Mockito.mock(TestIMdsalApiManager.class, realOrException());
46     }
47
48     /**
49      * Get list of installed flows.
50      * Prefer the {@link #assertFlows(Iterable)} instead of using this and checking yourself.
51      * @return immutable copy of list of flows
52      */
53     public synchronized List<FlowEntity> getFlows() {
54         return ImmutableList.copyOf(getOrNewFlows());
55     }
56
57     private synchronized List<FlowEntity> getOrNewFlows() {
58         if (flows == null) {
59             flows = new ArrayList<>();
60         }
61         return flows;
62     }
63
64     public synchronized void assertFlows(Iterable<FlowEntity> expectedFlows) {
65         List<FlowEntity> nonNullFlows = getOrNewFlows();
66         if (!Iterables.isEmpty(expectedFlows)) {
67             assertTrue("No Flows created (bean wiring may be broken?)", !nonNullFlows.isEmpty());
68         }
69         // TODO Support Iterable <-> List directly within XtendBeanGenerator
70         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
71         AssertDataObjects.assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
72     }
73
74     @Override
75     public synchronized void installFlow(FlowEntity flowEntity) {
76         getOrNewFlows().add(flowEntity);
77     }
78
79     @Override
80     public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
81             FlowEntity flowEntity) {
82         getOrNewFlows().remove(flowEntity);
83         return Futures.immediateCheckedFuture(null);
84     }
85
86 }