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 org.junit.Assert.assertTrue;
11 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
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;
28 * Fake IMdsalApiManager useful for tests.
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.
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.
38 * @author Michael Vorburger
40 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
42 private List<FlowEntity> flows;
44 public static TestIMdsalApiManager newInstance() {
45 return Mockito.mock(TestIMdsalApiManager.class, realOrException());
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
53 public synchronized List<FlowEntity> getFlows() {
54 return ImmutableList.copyOf(getOrNewFlows());
57 private synchronized List<FlowEntity> getOrNewFlows() {
59 flows = new ArrayList<>();
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());
69 // TODO Support Iterable <-> List directly within XtendBeanGenerator
70 List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
71 AssertDataObjects.assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
75 public synchronized void installFlow(FlowEntity flowEntity) {
76 getOrNewFlows().add(flowEntity);
80 public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
81 FlowEntity flowEntity) {
82 getOrNewFlows().remove(flowEntity);
83 return Futures.immediateCheckedFuture(null);