2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.core;
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;
17 import java.net.InetSocketAddress;
18 import java.util.ArrayList;
19 import java.util.List;
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;
32 * @author michal.polkorab
35 public class OFDatagramPacketEncoderTest {
37 @Mock ChannelHandlerContext ctx;
38 @Mock GenericFutureListener<Future<Void>> listener;
39 @Mock SerializationFactory factory;
41 private UdpMessageListenerWrapper wrapper;
42 private InetSocketAddress address = new InetSocketAddress("10.0.0.1", 6653);
43 private List<Object> out;
46 * Initializes mocks and other objects
47 * @param version openflow protocol wire version
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);
62 public void testCorrectEncode() {
63 startUp((short) EncodeConstants.OF13_VERSION_ID);
64 OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder();
65 encoder.setSerializationFactory(factory);
67 encoder.encode(ctx, wrapper, out);
68 } catch (Exception e) {
77 public void testIncorrectEncode() {
79 OFDatagramPacketEncoder encoder = new OFDatagramPacketEncoder();
80 encoder.setSerializationFactory(factory);
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());