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