1 package org.opendaylight.openflowplugin.impl.services;
3 import static org.mockito.Mockito.verify;
4 import static org.mockito.Mockito.when;
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;
39 public class SalTableServiceImplTest extends ServiceMocking {
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;
46 RpcProviderRegistry mockedRpcProviderRegistry;
48 private SettableFuture<Object> handleResultFuture;
49 private SalTableServiceImpl salTableService;
53 handleResultFuture = SettableFuture.create();
54 when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
55 Mockito.doAnswer(new Answer<Void>() {
57 public Void answer(InvocationOnMock invocation) throws Throwable {
58 final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
59 callback.onSuccess(null);
63 .when(mockedOutboundQueue).commitEntry(
64 Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
66 salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext);
70 public void testUpdateTableFail1() throws ExecutionException, InterruptedException {
71 Mockito.doAnswer(new Answer<Void>() {
73 public Void answer(InvocationOnMock invocation) throws Throwable {
74 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().build();
75 handleResultFuture.set(rpcResult);
78 }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
80 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
81 Assert.assertNotNull(rpcResultFuture);
82 verify(mockedRequestContextStack).createRequestContext();
86 public void testUpdateTableFail2() throws ExecutionException, InterruptedException {
87 Mockito.doAnswer(new Answer<Void>() {
89 public Void answer(InvocationOnMock invocation) throws Throwable {
90 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
92 handleResultFuture.set(rpcResult);
95 }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
97 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
98 Assert.assertNotNull(rpcResultFuture);
99 verify(mockedRequestContextStack).createRequestContext();
103 public void testUpdateTableSuccess() throws ExecutionException, InterruptedException {
104 Mockito.doAnswer(new Answer<Void>() {
106 public Void answer(InvocationOnMock invocation) throws Throwable {
107 TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
108 .setTableId((short) 0)
111 .setTableFeatureProperties(Collections.<TableFeatureProperties>emptyList());
112 MultipartReplyTableFeaturesBuilder mpTableFeaturesBld = new MultipartReplyTableFeaturesBuilder()
113 .setTableFeatures(Collections.singletonList(tableFeaturesBld.build()));
114 MultipartReplyTableFeaturesCaseBuilder mpBodyBld = new MultipartReplyTableFeaturesCaseBuilder()
115 .setMultipartReplyTableFeatures(mpTableFeaturesBld.build());
116 MultipartReplyMessageBuilder mpResultBld = new MultipartReplyMessageBuilder()
117 .setType(MultipartType.OFPMPTABLEFEATURES)
118 .setMultipartReplyBody(mpBodyBld.build())
120 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
121 .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
123 handleResultFuture.set(rpcResult);
126 }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
128 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
129 Assert.assertNotNull(rpcResultFuture);
130 verify(mockedRequestContextStack).createRequestContext();
133 private UpdateTableInput prepareUpdateTable() {
134 UpdateTableInputBuilder updateTableInputBuilder = new UpdateTableInputBuilder();
135 UpdatedTableBuilder updatedTableBuilder = new UpdatedTableBuilder();
136 updatedTableBuilder.setTableFeatures(Collections.<TableFeatures>emptyList());
137 updateTableInputBuilder.setUpdatedTable(updatedTableBuilder.build());
138 return updateTableInputBuilder.build();