227b2f45d59b6b66208fdc7372d1ed5d368da52b
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestOnTheFlyCallbackTest.java
1 package org.opendaylight.openflowplugin.impl.services;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.mockito.Mock;
7 import org.mockito.runners.MockitoJUnitRunner;
8 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
9 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
10 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
11 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
12 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
13 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
18 import org.opendaylight.yangtools.yang.binding.Augmentation;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import org.opendaylight.yangtools.yang.common.RpcError;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.concurrent.ExecutionException;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class MultipartRequestOnTheFlyCallbackTest {
38
39
40     private static final String DUMMY_NODE_ID = "dummyNodeId";
41     private static final String DUMMY_EVENT_NAME = "dummy event name 1";
42     private static final String DUMMY_DEVICE_ID = "dummy device id 1";
43     private static final Long DUMMY_XID = 55L;
44     @Mock
45     private DeviceContext mockedDeviceContext;
46
47     @Mock
48     RequestContext<List<MultipartReply>> mockedRequestContext;
49     @Mock
50     ConnectionContext mockedPrimaryConnection;
51     @Mock
52     NodeId mockedNodeId;
53
54     private AbstractRequestContext<List<MultipartReply>> dummyRequestContext = new AbstractRequestContext<List<MultipartReply>>(DUMMY_XID) {
55
56         @Override
57         public void close() {
58
59         }
60     };
61
62     private EventIdentifier dummyEventIdentifier = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
63
64     @Before
65     public void initialization() {
66         when(mockedDeviceContext.getMessageSpy()).thenReturn(new MessageIntelligenceAgencyImpl());
67         when(mockedNodeId.toString()).thenReturn(DUMMY_NODE_ID);
68         when(mockedPrimaryConnection.getNodeId()).thenReturn(mockedNodeId);
69         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnection);
70     }
71
72
73     @Test
74     public void testOnSuccessWithNull() throws Exception {
75         final MultipartRequestOnTheFlyCallback multipartRequestOnTheFlyCallback = new MultipartRequestOnTheFlyCallback(dummyRequestContext, String.class, mockedDeviceContext, dummyEventIdentifier);
76         multipartRequestOnTheFlyCallback.onSuccess(null);
77         final RpcResult<List<MultipartReply>> expectedRpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
78         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
79         assertEquals(expectedRpcResult.getErrors(), actualResult.getErrors());
80         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
81         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
82     }
83
84     @Test
85     public void testOnSuccessWithNotMultiNoMultipart() throws ExecutionException, InterruptedException {
86         final MultipartRequestOnTheFlyCallback multipartRequestOnTheFlyCallback = new MultipartRequestOnTheFlyCallback(dummyRequestContext, String.class, mockedDeviceContext, dummyEventIdentifier);
87         HelloMessage mockedHelloMessage = mock(HelloMessage.class);
88         multipartRequestOnTheFlyCallback.onSuccess(mockedHelloMessage);
89
90         final RpcResult<List<MultipartReply>> expectedRpcResult =
91                 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
92                         String.format("Unexpected response type received %s.", mockedHelloMessage.getClass())).build();
93         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
94         assertNotNull(actualResult.getErrors());
95         assertEquals(1, actualResult.getErrors().size());
96
97         final RpcError actualError = actualResult.getErrors().iterator().next();
98         assertEquals(actualError.getMessage(), String.format("Unexpected response type received %s.", mockedHelloMessage.getClass()));
99         assertEquals(actualError.getErrorType(),RpcError.ErrorType.APPLICATION);
100         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
101         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
102
103     }
104 }