BUG-4340: raise test coverage to 80% - services related
[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     }
68
69     @Test
70     public void testUpdateTableFail1() throws ExecutionException, InterruptedException {
71         Mockito.doAnswer(new Answer<Void>() {
72             @Override
73             public Void answer(InvocationOnMock invocation) throws Throwable {
74                 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().build();
75                 handleResultFuture.set(rpcResult);
76                 return null;
77             }
78         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
79
80         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
81         Assert.assertNotNull(rpcResultFuture);
82         verify(mockedRequestContextStack).createRequestContext();
83     }
84
85     @Test
86     public void testUpdateTableFail2() throws ExecutionException, InterruptedException {
87         Mockito.doAnswer(new Answer<Void>() {
88             @Override
89             public Void answer(InvocationOnMock invocation) throws Throwable {
90                 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
91                         .build();
92                 handleResultFuture.set(rpcResult);
93                 return null;
94             }
95         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
96
97         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
98         Assert.assertNotNull(rpcResultFuture);
99         verify(mockedRequestContextStack).createRequestContext();
100     }
101
102     @Test
103     public void testUpdateTableSuccess() throws ExecutionException, InterruptedException {
104         Mockito.doAnswer(new Answer<Void>() {
105             @Override
106             public Void answer(InvocationOnMock invocation) throws Throwable {
107                 TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
108                         .setTableId((short) 0)
109                         .setName("Zafod")
110                         .setMaxEntries(42L)
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())
119                         .setXid(21L);
120                 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
121                         .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
122                         .build();
123                 handleResultFuture.set(rpcResult);
124                 return null;
125             }
126         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
127
128         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
129         Assert.assertNotNull(rpcResultFuture);
130         verify(mockedRequestContextStack).createRequestContext();
131     }
132
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();
139     }
140
141 }