BUG-4283: experimenter msg support - deserialization part
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / ExperimenterMessageFactoryTest.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.deserialization.factories;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.when;
13
14 import io.netty.buffer.ByteBuf;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
23 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterIdDeserializerKey;
24 import org.opendaylight.openflowjava.util.ByteBufUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.experimenter.core.ExperimenterDataOfChoice;
27
28 /**
29  * @author michal.polkorab
30  *
31  */
32 @RunWith(MockitoJUnitRunner.class)
33 public class ExperimenterMessageFactoryTest {
34
35     @Mock DeserializerRegistry registry;
36     @Mock OFDeserializer<ExperimenterDataOfChoice> deserializer;
37     @Mock ExperimenterDataOfChoice message;
38     private ExperimenterMessageFactory factory;
39
40     /**
41      * Initializes mocks
42      */
43     @Before
44     public void startUp() {
45         factory = new ExperimenterMessageFactory();
46     }
47
48     /**
49      * Test deserializer lookup correctness
50      */
51     @Test
52     public void test() {
53         when(registry.getDeserializer(any(ExperimenterIdDeserializerKey.class))).thenReturn(deserializer);
54         when(deserializer.deserialize(any(ByteBuf.class))).thenReturn(message);
55
56         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 01 02 03 00 00 00 10 00 00 00 20");
57         factory.injectDeserializerRegistry(registry);
58         ExperimenterMessage deserializedMessage = factory.deserialize(buffer);
59         Assert.assertEquals("Wrong return value", message, deserializedMessage.getExperimenterDataOfChoice());
60         Assert.assertEquals("ByteBuf index moved", 0, buffer.readableBytes());
61     }
62 }