Merge "Add NodeConfiguratorImpl enqueue trace"
[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.MessageSpy;
27 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerMultipartRequestCallback;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
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.MultipartRequestInput;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34
35 /**
36  * Test for {@link org.opendaylight.openflowplugin.impl.services.AbstractMultipartRequestCallback}.
37  */
38 @RunWith(MockitoJUnitRunner.class)
39 public class MultipartRequestCallbackTest {
40
41     @Mock
42     private DeviceContext deviceContext;
43     @Mock
44     private RequestContext<List<MultipartReply>> requestContext;
45     @Mock
46     private MessageSpy spy;
47     @Mock
48     private MultiMsgCollector<MultipartReply> multiMsgCollector;
49     @Captor
50     private ArgumentCaptor<RpcResult<List<MultipartReply>>> rpcResultCapt;
51
52     private AbstractMultipartRequestCallback<MultipartReply> multipartRequestCallback;
53
54     @Before
55     public void setUp() {
56         Mockito.doNothing().when(requestContext).setResult(rpcResultCapt.capture());
57         Mockito.when(deviceContext.getMessageSpy()).thenReturn(spy);
58
59         final OngoingStubbing<MultiMsgCollector<MultipartReply>> when =
60             Mockito.when(deviceContext.getMultiMsgCollector(ArgumentMatchers.any()));
61         when.thenReturn(multiMsgCollector);
62         multipartRequestCallback = new MultiLayerMultipartRequestCallback<>(requestContext,
63                                                                             MultipartRequestInput.class,
64                                                                             deviceContext,
65                                                                             null);
66     }
67
68     /**
69      * End collecting.
70      */
71     @Test
72     public void testOnSuccess1() {
73         multipartRequestCallback.onSuccess(null);
74         Mockito.verify(multiMsgCollector).endCollecting(ArgumentMatchers.any());
75     }
76
77     /**
78      * Fail adding to collection.
79      */
80     @Test
81     public void testOnSuccess2() {
82         multipartRequestCallback.onSuccess(new EchoOutputBuilder().build());
83         final RpcResult<List<MultipartReply>> rpcResult = rpcResultCapt.getValue();
84         Assert.assertNotNull(rpcResult);
85         Assert.assertFalse(rpcResult.isSuccessful());
86     }
87
88     /**
89      * Successfully added to collection.
90      */
91     @Test
92     public void testOnSuccess3() {
93         final MultipartReplyMessage replyMessage = new MultipartReplyMessageBuilder().build();
94         multipartRequestCallback.onSuccess(replyMessage);
95         Mockito.verify(multiMsgCollector)
96                 .addMultipartMsg(ArgumentMatchers.eq(replyMessage), ArgumentMatchers.eq(false), ArgumentMatchers.any());
97     }
98 }