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