Add single layer deserialization support
[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.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(Matchers.any()));
62         when.thenReturn(multiMsgCollector);
63         multipartRequestCallback = new MultiLayerMultipartRequestCallback<>(requestContext, MultipartRequestInput.class, deviceContext, null);
64     }
65
66     /**
67      * end collecting
68      *
69      * @throws Exception
70      */
71     @Test
72     public void testOnSuccess1() throws Exception {
73         multipartRequestCallback.onSuccess(null);
74         Mockito.verify(multiMsgCollector).endCollecting(Matchers.<EventIdentifier>any());
75     }
76
77     /**
78      * fail adding to collection
79      *
80      * @throws Exception
81      */
82     @Test
83     public void testOnSuccess2() throws Exception {
84         multipartRequestCallback.onSuccess(new EchoOutputBuilder().build());
85         final RpcResult<List<MultipartReply>> rpcResult = rpcResultCapt.getValue();
86         Assert.assertNotNull(rpcResult);
87         Assert.assertFalse(rpcResult.isSuccessful());
88     }
89
90     /**
91      * successfully added to collection
92      *
93      * @throws Exception
94      */
95     @Test
96     public void testOnSuccess3() throws Exception {
97         final MultipartReplyMessage replyMessage = new MultipartReplyMessageBuilder().build();
98         multipartRequestCallback.onSuccess(replyMessage);
99         Mockito.verify(multiMsgCollector).addMultipartMsg(Matchers.eq(replyMessage), Matchers.eq(false), Matchers.any());
100     }
101 }