9563c89cba011af08e82d5a1c05ec146e1c76f49
[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.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;
47
48 public class SalTableServiceImplTest extends ServiceMocking {
49
50     @Mock
51     RpcProviderRegistry mockedRpcProviderRegistry;
52
53     private SettableFuture<Object> handleResultFuture;
54     private SalTableServiceImpl salTableService;
55
56     @Override
57     public void setup() {
58         handleResultFuture = SettableFuture.create();
59         when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
60         Mockito.doAnswer(new Answer<Void>() {
61             @Override
62             public Void answer(InvocationOnMock invocation) throws Throwable {
63                 final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
64                 callback.onSuccess(null);
65                 return null;
66             }
67         })
68                 .when(mockedOutboundQueue).commitEntry(
69                 Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
70
71         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
72         salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext,
73                 convertorManager, MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext));
74     }
75
76     @Test
77     public void testUpdateTableFail1() throws ExecutionException, InterruptedException {
78         Mockito.doAnswer(new Answer<Void>() {
79             @Override
80             public Void answer(InvocationOnMock invocation) throws Throwable {
81                 final RpcResult<List<MultipartReply>> rpcResult =
82                         RpcResultBuilder.<List<MultipartReply>>failed().build();
83                 handleResultFuture.set(rpcResult);
84                 return null;
85             }
86         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
87
88         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
89         Assert.assertNotNull(rpcResultFuture);
90         verify(mockedRequestContextStack).createRequestContext();
91     }
92
93     @Test
94     public void testUpdateTableFail2() throws ExecutionException, InterruptedException {
95         Mockito.doAnswer(new Answer<Void>() {
96             @Override
97             public Void answer(InvocationOnMock invocation) throws Throwable {
98                 final RpcResult<List<MultipartReply>> rpcResult =
99                         RpcResultBuilder.success(Collections.<MultipartReply>emptyList())
100                         .build();
101                 handleResultFuture.set(rpcResult);
102                 return null;
103             }
104         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
105
106         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
107         Assert.assertNotNull(rpcResultFuture);
108         verify(mockedRequestContextStack).createRequestContext();
109     }
110
111     @Test
112     public void testUpdateTableSuccess() throws ExecutionException, InterruptedException {
113         Mockito.doAnswer(new Answer<Void>() {
114             @Override
115             public Void answer(InvocationOnMock invocation) throws Throwable {
116                 TableFeaturesBuilder tableFeaturesBld = new TableFeaturesBuilder()
117                         .setTableId((short) 0)
118                         .setName("Zafod")
119                         .setMaxEntries(42L)
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())
128                         .setXid(21L);
129                 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
130                         .success(Collections.singletonList((MultipartReply) mpResultBld.build()))
131                         .build();
132                 handleResultFuture.set(rpcResult);
133                 return null;
134             }
135         }).when(multiMessageCollector).endCollecting(Matchers.<EventIdentifier>any());
136
137         final Future<RpcResult<UpdateTableOutput>> rpcResultFuture = salTableService.updateTable(prepareUpdateTable());
138         Assert.assertNotNull(rpcResultFuture);
139         verify(mockedRequestContextStack).createRequestContext();
140     }
141
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();
148     }
149
150 }