Update unit tests
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / AbstractDirectStatisticsServiceTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.when;
13
14 import java.math.BigInteger;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
27 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
28 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
29 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
31 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
32 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
33 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
35 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public abstract class AbstractDirectStatisticsServiceTest {
49     protected static final Long PORT_NO = 1L;
50     protected static final BigInteger DATAPATH_ID = BigInteger.TEN;
51     protected static final short OF_VERSION = OFConstants.OFP_VERSION_1_3;
52     protected static final String NODE_ID = "openflow:10";
53
54     @Mock
55     protected RequestContextStack requestContextStack;
56     @Mock
57     protected DeviceContext deviceContext;
58     @Mock
59     protected ConnectionContext connectionContext;
60     @Mock
61     protected FeaturesReply features;
62     @Mock
63     protected MessageSpy messageSpy;
64     @Mock
65     protected OutboundQueue outboundQueueProvider;
66     @Mock
67     protected MultiMsgCollector multiMsgCollector;
68     @Mock
69     protected TranslatorLibrary translatorLibrary;
70     @Mock
71     protected DeviceState deviceState;
72     @Mock
73     protected DeviceInfo deviceInfo;
74     @Mock
75     protected GetFeaturesOutput getFeaturesOutput;
76
77     protected NodeConnectorId nodeConnectorId;
78     protected KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
79     protected ConvertorManager convertorManager;
80     protected MultipartWriterProvider multipartWriterProvider;
81     protected static NodeRef createNodeRef(String nodeIdValue) {
82         InstanceIdentifier<Node> nodePath = InstanceIdentifier.create(Nodes.class)
83                 .child(Node.class, new NodeKey(new NodeId(nodeIdValue)));
84
85         return new NodeRef(nodePath);
86     }
87
88     @Before
89     public void init() throws Exception {
90         nodeConnectorId = InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(
91                 DATAPATH_ID, PORT_NO, OpenflowVersion.get(OF_VERSION));
92
93         nodeInstanceIdentifier = InstanceIdentifier
94                 .create(Nodes.class)
95                 .child(Node.class, new NodeKey(new NodeId(NODE_ID)));
96
97         convertorManager = ConvertorManagerFactory.createDefaultManager();
98         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
99         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
100         when(deviceContext.getMultiMsgCollector(any())).thenReturn(multiMsgCollector);
101         when(deviceContext.oook()).thenReturn(translatorLibrary);
102         when(deviceContext.getDeviceState()).thenReturn(deviceState);
103         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
104         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
105         when(deviceInfo.getNodeId()).thenReturn(new NodeId(NODE_ID));
106         when(deviceInfo.getVersion()).thenReturn(OF_VERSION);
107         when(deviceInfo.getDatapathId()).thenReturn(DATAPATH_ID);
108         when(getFeaturesOutput.getVersion()).thenReturn(OF_VERSION);
109         when(getFeaturesOutput.getDatapathId()).thenReturn(DATAPATH_ID);
110         when(connectionContext.getFeatures()).thenReturn(features);
111         when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
112         when(features.getVersion()).thenReturn(OF_VERSION);
113         when(features.getDatapathId()).thenReturn(DATAPATH_ID);
114         multipartWriterProvider = MultipartWriterProviderFactory.createDefaultProvider(deviceContext);
115         setUp();
116     }
117
118     protected abstract void setUp() throws Exception;
119
120     @Test
121     public abstract void testBuildRequestBody() throws Exception;
122
123     @Test
124     public abstract void testBuildReply() throws Exception;
125
126     @Test
127     public abstract void testStoreStatistics() throws Exception;
128 }