12f5de2af3d296d1cd7730cade2ccc24b66e2d7a
[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.Captor;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
25 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
26 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33
34 /**
35  * Test for {@link MultipartRequestCallback}.
36  */
37 @RunWith(MockitoJUnitRunner.class)
38 public class MultipartRequestCallbackTest {
39
40     @Mock
41     private DeviceContext deviceContext;
42     @Mock
43     private RequestContext<List<MultipartReply>> requestContext;
44     @Mock
45     private MessageSpy spy;
46     @Mock
47     private MultiMsgCollector multiMsgCollector;
48     @Captor
49     private ArgumentCaptor<RpcResult<List<MultipartReply>>> rpcResultCapt;
50
51     private MultipartRequestCallback multipartRequestCallback;
52
53     @Before
54     public void setUp() throws Exception {
55         Mockito.doNothing().when(requestContext).setResult(rpcResultCapt.capture());
56         Mockito.when(deviceContext.getMessageSpy()).thenReturn(spy);
57         Mockito.when(deviceContext.getMultiMsgCollector(Matchers.<RequestContext>any())).thenReturn(multiMsgCollector);
58         multipartRequestCallback = new MultipartRequestCallback(requestContext, MultipartRequestInput.class, deviceContext);
59     }
60
61     /**
62      * end collecting
63      *
64      * @throws Exception
65      */
66     @Test
67     public void testOnSuccess1() throws Exception {
68         multipartRequestCallback.onSuccess(null);
69         Mockito.verify(multiMsgCollector).endCollecting(Matchers.<EventIdentifier>any());
70     }
71
72     /**
73      * fail adding to collection
74      *
75      * @throws Exception
76      */
77     @Test
78     public void testOnSuccess2() throws Exception {
79         multipartRequestCallback.onSuccess(new EchoOutputBuilder().build());
80         final RpcResult<List<MultipartReply>> rpcResult = rpcResultCapt.getValue();
81         Assert.assertNotNull(rpcResult);
82         Assert.assertFalse(rpcResult.isSuccessful());
83     }
84
85     /**
86      * successfully added to collection
87      *
88      * @throws Exception
89      */
90     @Test
91     public void testOnSuccess3() throws Exception {
92         final MultipartReplyMessage replyMessage = new MultipartReplyMessageBuilder().build();
93         multipartRequestCallback.onSuccess(replyMessage);
94         Mockito.verify(multiMsgCollector).addMultipartMsg(Matchers.eq(replyMessage), Matchers.<EventIdentifier>any());
95     }
96 }