Fix codestyle
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / sal / SalTableServiceImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.sal;
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.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Future;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
28 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
29 import org.opendaylight.openflowplugin.impl.services.ServiceMocking;
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     @Mock
51     RpcProviderRegistry mockedRpcProviderRegistry;
52
53     private SettableFuture<Object> handleResultFuture;
54     private SalTableServiceImpl salTableService;
55
56     @Override
57     public void setup() {
58         handleResultFuture = SettableFuture.create();
59         when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
60         Mockito.doAnswer((Answer<Void>) invocation -> {
61             final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
62             callback.onSuccess(null);
63             return null;
64         })
65                 .when(mockedOutboundQueue).commitEntry(
66                 Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
67
68         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
69         salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext,
70                 convertorManager, MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext));
71     }
72
73     @Test
74     public void testUpdateTableFail1() throws ExecutionException, InterruptedException {
75         Mockito.doAnswer((Answer<Void>) invocation -> {
76             final RpcResult<List<MultipartReply>> rpcResult =
77                     RpcResultBuilder.<List<MultipartReply>>failed().build();
78             handleResultFuture.set(rpcResult);
79             return null;
80         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
81
82         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
83         Assert.assertNotNull(rpcResultFuture);
84         verify(mockedRequestContextStack).createRequestContext();
85     }
86
87     @Test
88     public void testUpdateTableFail2() throws ExecutionException, InterruptedException {
89         Mockito.doAnswer((Answer<Void>) invocation -> {
90             final RpcResult<List<MultipartReply>> rpcResult =
91                     RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
92                     .build();
93             handleResultFuture.set(rpcResult);
94             return null;
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((Answer<Void>) invocation -> {
105             TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
106                     .setTableId((short) 0)
107                     .setName("Zafod")
108                     .setMaxEntries(42L)
109                     .setTableFeatureProperties(Collections.<TableFeatureProperties>emptyList());
110             MultipartReplyTableFeaturesBuilder mpTableFeaturesBld = new MultipartReplyTableFeaturesBuilder()
111                     .setTableFeatures(Collections.singletonList(tableFeaturesBld.build()));
112             MultipartReplyTableFeaturesCaseBuilder mpBodyBld = new MultipartReplyTableFeaturesCaseBuilder()
113                     .setMultipartReplyTableFeatures(mpTableFeaturesBld.build());
114             MultipartReplyMessageBuilder mpResultBld = new MultipartReplyMessageBuilder()
115                     .setType(MultipartType.OFPMPTABLEFEATURES)
116                     .setMultipartReplyBody(mpBodyBld.build())
117                     .setXid(21L);
118             final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
119                     .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
120                     .build();
121             handleResultFuture.set(rpcResult);
122             return null;
123         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
124
125         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
126         Assert.assertNotNull(rpcResultFuture);
127         verify(mockedRequestContextStack).createRequestContext();
128     }
129
130     private UpdateTableInput prepareUpdateTable() {
131         UpdateTableInputBuilder updateTableInputBuilder = new UpdateTableInputBuilder();
132         UpdatedTableBuilder updatedTableBuilder = new UpdatedTableBuilder();
133         updatedTableBuilder.setTableFeatures(Collections.<TableFeatures>emptyList());
134         updateTableInputBuilder.setUpdatedTable(updatedTableBuilder.build());
135         return updateTableInputBuilder.build();
136     }
137
138 }