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