2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services.sal;
10 import static org.mockito.ArgumentMatchers.anyLong;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
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;
44 public class SalTableServiceImplTest extends ServiceMocking {
47 RpcProviderService mockedRpcProviderRegistry;
49 private SettableFuture<Object> handleResultFuture;
50 private SalTableServiceImpl salTableService;
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);
61 .when(mockedOutboundQueue).commitEntry(
62 anyLong(), ArgumentMatchers.any(), ArgumentMatchers.any());
64 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
65 salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext,
66 convertorManager, MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext));
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);
76 }).when(multiMessageCollector).endCollecting(ArgumentMatchers.any());
78 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
79 Assert.assertNotNull(rpcResultFuture);
80 verify(mockedRequestContextStack).createRequestContext();
84 public void testUpdateTableFail2() {
85 Mockito.doAnswer((Answer<Void>) invocation -> {
86 final RpcResult<List<MultipartReply>> rpcResult =
87 RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
89 handleResultFuture.set(rpcResult);
91 }).when(multiMessageCollector).endCollecting(ArgumentMatchers.any());
93 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
94 Assert.assertNotNull(rpcResultFuture);
95 verify(mockedRequestContextStack).createRequestContext();
99 public void testUpdateTableSuccess() {
100 Mockito.doAnswer((Answer<Void>) invocation -> {
101 TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
102 .setTableId((short) 0)
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())
114 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
115 .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
117 handleResultFuture.set(rpcResult);
119 }).when(multiMessageCollector).endCollecting(ArgumentMatchers.any());
121 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
122 Assert.assertNotNull(rpcResultFuture);
123 verify(mockedRequestContextStack).createRequestContext();
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();