Add missing license headers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalTableServiceImplTest.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 package org.opendaylight.openflowplugin.impl.services;
9
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.math.BigInteger;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Future;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.openflowplugin.api.OFConstants;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeaturesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeaturesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45
46 public class SalTableServiceImplTest extends ServiceMocking {
47
48     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
49     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
50     private static final int DUMMY_MAX_REQUEST = 88;
51
52     @Mock
53     RpcProviderRegistry mockedRpcProviderRegistry;
54
55     private SettableFuture<Object> handleResultFuture;
56     private SalTableServiceImpl salTableService;
57
58     @Override
59     public void setup() {
60         handleResultFuture = SettableFuture.create();
61         when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
62         Mockito.doAnswer(new Answer<Void>() {
63             @Override
64             public Void answer(InvocationOnMock invocation) throws Throwable {
65                 final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
66                 callback.onSuccess(null);
67                 return null;
68             }
69         })
70                 .when(mockedOutboundQueue).commitEntry(
71                 Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
72
73         salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext,
74                 mockedDeviceContext.getPrimaryConnectionContext().getNodeId());
75     }
76
77     @Test
78     public void testUpdateTableFail1() throws ExecutionException, InterruptedException {
79         Mockito.doAnswer(new Answer<Void>() {
80             @Override
81             public Void answer(InvocationOnMock invocation) throws Throwable {
82                 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().build();
83                 handleResultFuture.set(rpcResult);
84                 return null;
85             }
86         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
87
88         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
89         Assert.assertNotNull(rpcResultFuture);
90         verify(mockedRequestContextStack).createRequestContext();
91     }
92
93     @Test
94     public void testUpdateTableFail2() throws ExecutionException, InterruptedException {
95         Mockito.doAnswer(new Answer<Void>() {
96             @Override
97             public Void answer(InvocationOnMock invocation) throws Throwable {
98                 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
99                         .build();
100                 handleResultFuture.set(rpcResult);
101                 return null;
102             }
103         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
104
105         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
106         Assert.assertNotNull(rpcResultFuture);
107         verify(mockedRequestContextStack).createRequestContext();
108     }
109
110     @Test
111     public void testUpdateTableSuccess() throws ExecutionException, InterruptedException {
112         Mockito.doAnswer(new Answer<Void>() {
113             @Override
114             public Void answer(InvocationOnMock invocation) throws Throwable {
115                 TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
116                         .setTableId((short) 0)
117                         .setName("Zafod")
118                         .setMaxEntries(42L)
119                         .setTableFeatureProperties(Collections.<TableFeatureProperties>emptyList());
120                 MultipartReplyTableFeaturesBuilder mpTableFeaturesBld = new MultipartReplyTableFeaturesBuilder()
121                         .setTableFeatures(Collections.singletonList(tableFeaturesBld.build()));
122                 MultipartReplyTableFeaturesCaseBuilder mpBodyBld = new MultipartReplyTableFeaturesCaseBuilder()
123                         .setMultipartReplyTableFeatures(mpTableFeaturesBld.build());
124                 MultipartReplyMessageBuilder mpResultBld = new MultipartReplyMessageBuilder()
125                         .setType(MultipartType.OFPMPTABLEFEATURES)
126                         .setMultipartReplyBody(mpBodyBld.build())
127                         .setXid(21L);
128                 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
129                         .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
130                         .build();
131                 handleResultFuture.set(rpcResult);
132                 return null;
133             }
134         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
135
136         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
137         Assert.assertNotNull(rpcResultFuture);
138         verify(mockedRequestContextStack).createRequestContext();
139     }
140
141     private UpdateTableInput prepareUpdateTable() {
142         UpdateTableInputBuilder updateTableInputBuilder = new UpdateTableInputBuilder();
143         UpdatedTableBuilder updatedTableBuilder = new UpdatedTableBuilder();
144         updatedTableBuilder.setTableFeatures(Collections.<TableFeatures>emptyList());
145         updateTableInputBuilder.setUpdatedTable(updatedTableBuilder.build());
146         return updateTableInputBuilder.build();
147     }
148
149 }