InterfaceManagerTest as a new-style component/API/end2end test
[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.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;
22
23 /**
24  * Fake IMdsalApiManager useful for tests.
25  *
26  * <p>Read e.g.
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.
29  *
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.
33  *
34  * @author Michael Vorburger
35  */
36 public abstract class TestIMdsalApiManager implements IMdsalApiManager {
37
38     private List<FlowEntity> flows;
39
40     public static TestIMdsalApiManager newInstance() {
41         return Mockito.mock(TestIMdsalApiManager.class, realOrException());
42     }
43
44     private synchronized List<FlowEntity> initializeFlows() {
45         return Collections.synchronizedList(new ArrayList<>());
46     }
47
48     public List<FlowEntity> getFlows() {
49         if (flows == null) {
50             flows = initializeFlows();
51         }
52         return flows;
53     }
54
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());
59         }
60         // TODO Support Iterable <-> List directly within XtendBeanGenerator
61         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
62         AssertDataObjects.assertEqualBeans(expectedFlowsAsNewArrayList, flows);
63     }
64
65     @Override
66     public void installFlow(FlowEntity flowEntity) {
67         getFlows().add(flowEntity);
68     }
69
70 }