Fix errors in serializers and deserializers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / QueueDirectStatisticsServiceTest.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.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import java.math.BigInteger;
20 import java.util.Arrays;
21 import java.util.List;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStats;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestQueueCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.queue._case.MultipartRequestQueue;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
34
35 public class QueueDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
36     static final Long QUEUE_NO = 1L;
37     private QueueDirectStatisticsService service;
38
39     @Override
40     public void setUp() throws Exception {
41         service = new QueueDirectStatisticsService(requestContextStack, deviceContext, convertorManager);
42     }
43
44     @Override
45     public void testBuildRequestBody() throws Exception {
46         final GetQueueStatisticsInput input = mock(GetQueueStatisticsInput.class);
47
48         when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
49         when(input.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
50         when(input.getNodeConnectorId()).thenReturn(new NodeConnectorId(NODE_ID + ":" + PORT_NO));
51
52         final MultipartRequestQueueCase body = (MultipartRequestQueueCase) service.buildRequestBody(input);
53         final MultipartRequestQueue queue = body.getMultipartRequestQueue();
54
55         assertEquals(PORT_NO, queue.getPortNo());
56         assertEquals(QUEUE_NO, queue.getQueueId());
57     }
58
59     @Override
60     public void testBuildReply() throws Exception {
61         final MultipartReply reply = mock(MultipartReply.class);
62         final MultipartReplyQueueCase queueCase = mock(MultipartReplyQueueCase.class);
63         final MultipartReplyQueue queue = mock(MultipartReplyQueue.class);
64         final QueueStats queueStat = mock(QueueStats.class);
65         final List<QueueStats> queueStats = Arrays.asList(queueStat);
66         final List<MultipartReply> input = Arrays.asList(reply);
67
68         when(queue.getQueueStats()).thenReturn(queueStats);
69         when(queueCase.getMultipartReplyQueue()).thenReturn(queue);
70         when(reply.getMultipartReplyBody()).thenReturn(queueCase);
71
72         when(queueStat.getPortNo()).thenReturn(PORT_NO);
73         when(queueStat.getQueueId()).thenReturn(QUEUE_NO);
74         when(queueStat.getTxBytes()).thenReturn(BigInteger.ONE);
75         when(queueStat.getTxErrors()).thenReturn(BigInteger.ONE);
76         when(queueStat.getTxPackets()).thenReturn(BigInteger.ONE);
77
78         final GetQueueStatisticsOutput output = service.buildReply(input, true);
79         assertTrue(output.getQueueIdAndStatisticsMap().size() > 0);
80
81         final QueueIdAndStatisticsMap map = output.getQueueIdAndStatisticsMap().get(0);
82         assertEquals(map.getQueueId().getValue(), QUEUE_NO);
83         assertEquals(map.getNodeConnectorId(), nodeConnectorId);
84     }
85
86     @Override
87     public void testStoreStatistics() throws Exception {
88         final QueueIdAndStatisticsMap map = mock(QueueIdAndStatisticsMap.class);
89         when(map.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
90
91         final List<QueueIdAndStatisticsMap> maps = Arrays.asList(map);
92         final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
93         when(output.getQueueIdAndStatisticsMap()).thenReturn(maps);
94
95         service.storeStatistics(output);
96         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
97     }
98 }