OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestCallbackTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.openflowplugin.impl.services;
10
11 import java.util.List;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.ArgumentCaptor;
17 import org.mockito.ArgumentMatchers;
18 import org.mockito.Captor;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.mockito.stubbing.OngoingStubbing;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
26 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
28 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerMultipartRequestCallback;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
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.MultipartReplyMessage;
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.MultipartRequestInput;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35
36 /**
37  * Test for {@link org.opendaylight.openflowplugin.impl.services.AbstractMultipartRequestCallback}.
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class MultipartRequestCallbackTest {
41
42     @Mock
43     private DeviceContext deviceContext;
44     @Mock
45     private RequestContext<List<MultipartReply>> requestContext;
46     @Mock
47     private MessageSpy spy;
48     @Mock
49     private MultiMsgCollector<MultipartReply> multiMsgCollector;
50     @Captor
51     private ArgumentCaptor<RpcResult<List<MultipartReply>>> rpcResultCapt;
52
53     private AbstractMultipartRequestCallback<MultipartReply> multipartRequestCallback;
54
55     @Before
56     public void setUp() throws Exception {
57         Mockito.doNothing().when(requestContext).setResult(rpcResultCapt.capture());
58         Mockito.when(deviceContext.getMessageSpy()).thenReturn(spy);
59
60         final OngoingStubbing<MultiMsgCollector<MultipartReply>> when =
61             Mockito.when(deviceContext.getMultiMsgCollector(ArgumentMatchers.any()));
62         when.thenReturn(multiMsgCollector);
63         multipartRequestCallback = new MultiLayerMultipartRequestCallback<>(requestContext,
64                                                                             MultipartRequestInput.class,
65                                                                             deviceContext,
66                                                                             null);
67     }
68
69     /**
70      * End collecting.
71      */
72     @Test
73     public void testOnSuccess1() throws Exception {
74         multipartRequestCallback.onSuccess(null);
75         Mockito.verify(multiMsgCollector).endCollecting(ArgumentMatchers.<EventIdentifier>any());
76     }
77
78     /**
79      * Fail adding to collection.
80      */
81     @Test
82     public void testOnSuccess2() throws Exception {
83         multipartRequestCallback.onSuccess(new EchoOutputBuilder().build());
84         final RpcResult<List<MultipartReply>> rpcResult = rpcResultCapt.getValue();
85         Assert.assertNotNull(rpcResult);
86         Assert.assertFalse(rpcResult.isSuccessful());
87     }
88
89     /**
90      * Successfully added to collection.
91      */
92     @Test
93     public void testOnSuccess3() throws Exception {
94         final MultipartReplyMessage replyMessage = new MultipartReplyMessageBuilder().build();
95         multipartRequestCallback.onSuccess(replyMessage);
96         Mockito.verify(multiMsgCollector)
97                 .addMultipartMsg(ArgumentMatchers.eq(replyMessage), ArgumentMatchers.eq(false), ArgumentMatchers.any());
98     }
99 }