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.any;
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.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.mdsal.binding.api.RpcProviderService;
25 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
26 import org.opendaylight.openflowplugin.impl.services.ServiceMocking;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeaturesBuilder;
35 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;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.opendaylight.yangtools.yang.common.Uint32;
43 import org.opendaylight.yangtools.yang.common.Uint8;
45 public class SalTableServiceImplTest extends ServiceMocking {
48 RpcProviderService mockedRpcProviderRegistry;
50 private SettableFuture<Object> handleResultFuture;
51 private SalTableServiceImpl salTableService;
55 handleResultFuture = SettableFuture.create();
56 when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
57 Mockito.doAnswer((Answer<Void>) invocation -> {
58 final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
59 callback.onSuccess(null);
61 }).when(mockedOutboundQueue).commitEntry(any(Uint32.class), any(), any());
63 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
64 salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext,
65 convertorManager, MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext));
69 public void testUpdateTableFail1() {
70 Mockito.doAnswer((Answer<Void>) invocation -> {
71 final RpcResult<List<MultipartReply>> rpcResult =
72 RpcResultBuilder.<List<MultipartReply>>failed().build();
73 handleResultFuture.set(rpcResult);
75 }).when(multiMessageCollector).endCollecting(any());
77 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
78 Assert.assertNotNull(rpcResultFuture);
79 verify(mockedRequestContextStack).createRequestContext();
83 public void testUpdateTableFail2() {
84 Mockito.doAnswer((Answer<Void>) invocation -> {
85 final RpcResult<List<MultipartReply>> rpcResult =
86 RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
88 handleResultFuture.set(rpcResult);
90 }).when(multiMessageCollector).endCollecting(any());
92 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
93 Assert.assertNotNull(rpcResultFuture);
94 verify(mockedRequestContextStack).createRequestContext();
98 public void testUpdateTableSuccess() {
99 Mockito.doAnswer((Answer<Void>) invocation -> {
100 TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
101 .setTableId(Uint8.ZERO)
103 .setMaxEntries(Uint32.valueOf(42))
104 .setTableFeatureProperties(Collections.emptyList());
105 MultipartReplyTableFeaturesBuilder mpTableFeaturesBld = new MultipartReplyTableFeaturesBuilder()
106 .setTableFeatures(Collections.singletonList(tableFeaturesBld.build()));
107 MultipartReplyTableFeaturesCaseBuilder mpBodyBld = new MultipartReplyTableFeaturesCaseBuilder()
108 .setMultipartReplyTableFeatures(mpTableFeaturesBld.build());
109 MultipartReplyMessageBuilder mpResultBld = new MultipartReplyMessageBuilder()
110 .setType(MultipartType.OFPMPTABLEFEATURES)
111 .setMultipartReplyBody(mpBodyBld.build())
112 .setXid(Uint32.valueOf(21));
113 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
114 .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
116 handleResultFuture.set(rpcResult);
118 }).when(multiMessageCollector).endCollecting(any());
120 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
121 Assert.assertNotNull(rpcResultFuture);
122 verify(mockedRequestContextStack).createRequestContext();
125 private static UpdateTableInput prepareUpdateTable() {
126 UpdateTableInputBuilder updateTableInputBuilder = new UpdateTableInputBuilder();
127 UpdatedTableBuilder updatedTableBuilder = new UpdatedTableBuilder();
128 updatedTableBuilder.setTableFeatures(Collections.emptyMap());
129 updateTableInputBuilder.setUpdatedTable(updatedTableBuilder.build());
130 return updateTableInputBuilder.build();