Merge "Move match converter to new converter system"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsContextImplTest.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;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.util.concurrent.Futures;
18 import java.util.Collections;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Matchers;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
28 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class StatisticsContextImplTest extends StatisticsContextImpMockInitiation {
40
41     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImplTest.class);
42
43     private static final Long TEST_XID = 55L;
44     private StatisticsContextImpl<MultipartReply> statisticsContext;
45     private ConvertorManager convertorManager;
46
47     @Before
48     public void setUp() throws Exception {
49         convertorManager = ConvertorManagerFactory.createDefaultManager();
50         when(mockedDeviceInfo.reserveXidForDeviceMessage()).thenReturn(TEST_XID);
51         Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
52         initStatisticsContext();
53     }
54
55     private void initStatisticsContext() {
56         statisticsContext = new StatisticsContextImpl<>(
57                 mockedDeviceContext, convertorManager,
58                 MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext),
59                 true,
60                 false, 3000, 50000);
61
62         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
63         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
64     }
65
66     @Test
67     public void testCreateRequestContext() {
68         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
69         assertNotNull(requestContext);
70         assertEquals(TEST_XID, requestContext.getXid().getValue());
71         Assert.assertFalse(requestContext.getFuture().isDone());
72     }
73
74     /**
75      * There is nothing to check in close method
76      */
77     @Test
78     public void testClose() throws Exception {
79         final StatisticsContextImpl<MultipartReply> statisticsContext = new StatisticsContextImpl<>(
80                 mockedDeviceContext, convertorManager,
81                 MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext),
82                 true, false, 3000, 50000);
83
84         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
85         statisticsContext.close();
86         try {
87             Assert.assertTrue(requestContext.getFuture().isDone());
88             final RpcResult<?> rpcResult = requestContext.getFuture().get();
89             Assert.assertFalse(rpcResult.isSuccessful());
90             Assert.assertFalse(rpcResult.isSuccessful());
91         } catch (final Exception e) {
92             LOG.error("request future value should be finished", e);
93             Assert.fail("request context closing failed");
94         }
95     }
96
97     @Test
98     public void testGatherDynamicData_none() throws Exception {
99         statisticsContext.instantiateServiceInstance();
100         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
101     }
102
103     @Test
104     public void testGatherDynamicData_all() throws Exception {
105         Mockito.reset(mockedDeviceState);
106         when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
107         when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
108         when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
109         when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
110         when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
111         when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
112         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_ID);
113         initStatisticsContext();
114
115         when(mockedStatisticsGatheringService.getStatisticsOfType(Matchers.any(EventIdentifier.class),
116                     Matchers.any(MultipartType.class)))
117                 .thenReturn(
118                         Futures.immediateFuture(RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
119                             .build())
120                 );
121         when(mockedStatisticsOnFlyGatheringService.getStatisticsOfType(Matchers.any(EventIdentifier.class),
122                     Matchers.any(MultipartType.class)))
123                 .thenReturn(
124                         Futures.immediateFuture(RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
125                             .build())
126                 );
127
128         statisticsContext.registerMastershipWatcher(mockedMastershipWatcher);
129         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
130         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
131         statisticsContext.instantiateServiceInstance();
132
133         verify(mockedStatisticsGatheringService, times(7))
134                 .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class));
135         verify(mockedStatisticsOnFlyGatheringService)
136                 .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class));
137         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
138     }
139 }