Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFDatagramPacketEncoderTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core;
10
11 import static org.mockito.Mockito.times;
12 import static org.mockito.Mockito.verify;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.GenericFutureListener;
16
17 import java.net.InetSocketAddress;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
26 import org.opendaylight.openflowjava.protocol.impl.core.connection.UdpMessageListenerWrapper;
27 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
30
31 /**
32  * @author michal.polkorab
33  *
34  */
35 public class OFDatagramPacketEncoderTest {
36
37     @Mock ChannelHandlerContext ctx;
38     @Mock GenericFutureListener<Future<Void>> listener;
39     @Mock SerializationFactory factory;
40
41     private UdpMessageListenerWrapper wrapper;
42     private InetSocketAddress address = new InetSocketAddress("10.0.0.1", 6653);
43     private List<Object> out;
44
45     /**
46      * Initializes mocks and other objects
47      * @param version openflow protocol wire version
48      */
49     public void startUp(Short version) {
50         MockitoAnnotations.initMocks(this);
51         out = new ArrayList<>();
52         HelloInputBuilder builder = new HelloInputBuilder();
53         builder.setVersion(version);
54         HelloInput hello = builder.build();
55         wrapper = new UdpMessageListenerWrapper(hello, listener, address);
56     }
57
58     /**
59      * Tests encoding
60      */
61     @Test
62     public void testCorrectEncode() {
63         startUp((short) EncodeConstants.OF13_VERSION_ID);
64         OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder();
65         encoder.setSerializationFactory(factory);
66         try {
67             encoder.encode(ctx, wrapper, out);
68         } catch (Exception e) {
69             Assert.fail();
70         }
71     }
72
73     /**
74      * Tests encoding
75      */
76     @Test
77     public void testIncorrectEncode() {
78         startUp(null);
79         OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder();
80         encoder.setSerializationFactory(factory);
81         try {
82             encoder.encode(ctx, wrapper, out);
83         } catch (Exception e) {
84             verify(wrapper, times(1)).getListener();
85             Assert.assertEquals("List should be empty", 0, out.size());
86         }
87     }
88 }