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.Mockito.verify;
11 import static org.mockito.Mockito.when;
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;
48 public class SalTableServiceImplTest extends ServiceMocking {
51 RpcProviderRegistry mockedRpcProviderRegistry;
53 private SettableFuture<Object> handleResultFuture;
54 private SalTableServiceImpl salTableService;
58 handleResultFuture = SettableFuture.create();
59 when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
60 Mockito.doAnswer(new Answer<Void>() {
62 public Void answer(InvocationOnMock invocation) throws Throwable {
63 final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
64 callback.onSuccess(null);
68 .when(mockedOutboundQueue).commitEntry(
69 Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
71 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
72 salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext,
73 convertorManager, MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext));
77 public void testUpdateTableFail1() throws ExecutionException, InterruptedException {
78 Mockito.doAnswer(new Answer<Void>() {
80 public Void answer(InvocationOnMock invocation) throws Throwable {
81 final RpcResult<List<MultipartReply>> rpcResult =
82 RpcResultBuilder.<List<MultipartReply>>failed().build();
83 handleResultFuture.set(rpcResult);
86 }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
88 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
89 Assert.assertNotNull(rpcResultFuture);
90 verify(mockedRequestContextStack).createRequestContext();
94 public void testUpdateTableFail2() throws ExecutionException, InterruptedException {
95 Mockito.doAnswer(new Answer<Void>() {
97 public Void answer(InvocationOnMock invocation) throws Throwable {
98 final RpcResult<List<MultipartReply>> rpcResult =
99 RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
101 handleResultFuture.set(rpcResult);
104 }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
106 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
107 Assert.assertNotNull(rpcResultFuture);
108 verify(mockedRequestContextStack).createRequestContext();
112 public void testUpdateTableSuccess() throws ExecutionException, InterruptedException {
113 Mockito.doAnswer(new Answer<Void>() {
115 public Void answer(InvocationOnMock invocation) throws Throwable {
116 TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
117 .setTableId((short) 0)
120 .setTableFeatureProperties(Collections.<TableFeatureProperties>emptyList());
121 MultipartReplyTableFeaturesBuilder mpTableFeaturesBld = new MultipartReplyTableFeaturesBuilder()
122 .setTableFeatures(Collections.singletonList(tableFeaturesBld.build()));
123 MultipartReplyTableFeaturesCaseBuilder mpBodyBld = new MultipartReplyTableFeaturesCaseBuilder()
124 .setMultipartReplyTableFeatures(mpTableFeaturesBld.build());
125 MultipartReplyMessageBuilder mpResultBld = new MultipartReplyMessageBuilder()
126 .setType(MultipartType.OFPMPTABLEFEATURES)
127 .setMultipartReplyBody(mpBodyBld.build())
129 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
130 .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
132 handleResultFuture.set(rpcResult);
135 }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
137 final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
138 Assert.assertNotNull(rpcResultFuture);
139 verify(mockedRequestContextStack).createRequestContext();
142 private UpdateTableInput prepareUpdateTable() {
143 UpdateTableInputBuilder updateTableInputBuilder = new UpdateTableInputBuilder();
144 UpdatedTableBuilder updatedTableBuilder = new UpdatedTableBuilder();
145 updatedTableBuilder.setTableFeatures(Collections.<TableFeatures>emptyList());
146 updateTableInputBuilder.setUpdatedTable(updatedTableBuilder.build());
147 return updateTableInputBuilder.build();