Merge "Added JSON and XML payloads tabs with RFC 8040 URL"
[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.ArgumentMatchers.anyLong;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
13
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.concurrent.Future;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.mockito.ArgumentMatchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.stubbing.Answer;
25 import org.opendaylight.mdsal.binding.api.RpcProviderService;
26 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
27 import org.opendaylight.openflowplugin.impl.services.ServiceMocking;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeaturesBuilder;
36 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;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43
44 public class SalTableServiceImplTest extends ServiceMocking {
45
46     @Mock
47     RpcProviderService mockedRpcProviderRegistry;
48
49     private SettableFuture<Object> handleResultFuture;
50     private SalTableServiceImpl salTableService;
51
52     @Override
53     public void setup() {
54         handleResultFuture = SettableFuture.create();
55         when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
56         Mockito.doAnswer((Answer<Void>) invocation -> {
57             final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
58             callback.onSuccess(null);
59             return null;
60         })
61                 .when(mockedOutboundQueue).commitEntry(
62                 anyLong(), ArgumentMatchers.any(), ArgumentMatchers.any());
63
64         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
65         salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext,
66                 convertorManager, MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext));
67     }
68
69     @Test
70     public void testUpdateTableFail1() {
71         Mockito.doAnswer((Answer<Void>) invocation -> {
72             final RpcResult<List<MultipartReply>> rpcResult =
73                     RpcResultBuilder.<List<MultipartReply>>failed().build();
74             handleResultFuture.set(rpcResult);
75             return null;
76         }).when(multiMessageCollector).endCollecting(ArgumentMatchers.any());
77
78         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
79         Assert.assertNotNull(rpcResultFuture);
80         verify(mockedRequestContextStack).createRequestContext();
81     }
82
83     @Test
84     public void testUpdateTableFail2() {
85         Mockito.doAnswer((Answer<Void>) invocation -> {
86             final RpcResult<List<MultipartReply>> rpcResult =
87                     RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
88                     .build();
89             handleResultFuture.set(rpcResult);
90             return null;
91         }).when(multiMessageCollector).endCollecting(ArgumentMatchers.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 testUpdateTableSuccess() {
100         Mockito.doAnswer((Answer<Void>) invocation -> {
101             TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
102                     .setTableId((short) 0)
103                     .setName("Zafod")
104                     .setMaxEntries(42L)
105                     .setTableFeatureProperties(Collections.emptyList());
106             MultipartReplyTableFeaturesBuilder mpTableFeaturesBld = new MultipartReplyTableFeaturesBuilder()
107                     .setTableFeatures(Collections.singletonList(tableFeaturesBld.build()));
108             MultipartReplyTableFeaturesCaseBuilder mpBodyBld = new MultipartReplyTableFeaturesCaseBuilder()
109                     .setMultipartReplyTableFeatures(mpTableFeaturesBld.build());
110             MultipartReplyMessageBuilder mpResultBld = new MultipartReplyMessageBuilder()
111                     .setType(MultipartType.OFPMPTABLEFEATURES)
112                     .setMultipartReplyBody(mpBodyBld.build())
113                     .setXid(21L);
114             final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
115                     .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
116                     .build();
117             handleResultFuture.set(rpcResult);
118             return null;
119         }).when(multiMessageCollector).endCollecting(ArgumentMatchers.any());
120
121         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
122         Assert.assertNotNull(rpcResultFuture);
123         verify(mockedRequestContextStack).createRequestContext();
124     }
125
126     private static UpdateTableInput prepareUpdateTable() {
127         UpdateTableInputBuilder updateTableInputBuilder = new UpdateTableInputBuilder();
128         UpdatedTableBuilder updatedTableBuilder = new UpdatedTableBuilder();
129         updatedTableBuilder.setTableFeatures(Collections.emptyList());
130         updateTableInputBuilder.setUpdatedTable(updatedTableBuilder.build());
131         return updateTableInputBuilder.build();
132     }
133
134 }