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.Iterables;
14 import com.google.common.collect.Lists;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import org.mockito.Mockito;
19 import org.opendaylight.genius.mdsalutil.FlowEntity;
20 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
21 import org.opendaylight.mdsal.binding.testutils.AssertDataObjects;
24 * Fake IMdsalApiManager useful for tests.
27 * http://googletesting.blogspot.ch/2013/07/testing-on-toilet-know-your-test-doubles.html
28 * and http://martinfowler.com/articles/mocksArentStubs.html for more background.
30 * <p>This class is abstract just to save reading lines and typing keystrokes to
31 * manually implement a bunch of methods we're not yet interested in. Create instances
32 * of it using it's static {@link #newInstance()} method.
34 * @author Michael Vorburger
36 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
38 private List<FlowEntity> flows;
40 public static TestIMdsalApiManager newInstance() {
41 return Mockito.mock(TestIMdsalApiManager.class, realOrException());
44 private synchronized List<FlowEntity> initializeFlows() {
45 return Collections.synchronizedList(new ArrayList<>());
48 public List<FlowEntity> getFlows() {
50 flows = initializeFlows();
55 public void assertFlows(Iterable<FlowEntity> expectedFlows) {
56 List<FlowEntity> flows = this.getFlows();
57 if (!Iterables.isEmpty(expectedFlows)) {
58 assertTrue("No Flows created (bean wiring may be broken?)", !flows.isEmpty());
60 // TODO Support Iterable <-> List directly within XtendBeanGenerator
61 List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
62 AssertDataObjects.assertEqualBeans(expectedFlowsAsNewArrayList, flows);
66 public void installFlow(FlowEntity flowEntity) {
67 getFlows().add(flowEntity);