9e662519820b6a4866e8512f3c459bddcdfe5664
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / experimenter / BundleAddMessageFactoryTest.java
1 /*
2  * Copyright (c) 2016 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.serialization.experimenter;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Matchers;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
24 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
25 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
26 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
27 import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleFlags;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.bundle.experimenter.property.BundleExperimenterPropertyData;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleAddMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleAddMessageBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.bundle.add.message.Message;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortMod;
35
36 /**
37  * Test for {@link org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.BundleAddMessageFactory}.
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class BundleAddMessageFactoryTest {
41
42     private OFSerializer<BundleAddMessage> factory;
43     @Mock
44     SerializerRegistry registry;
45     @Mock
46     OFSerializer<PortMod> portModSerializer;
47     @Mock
48     OFSerializer<BundleExperimenterPropertyData> propertySerializer;
49
50     @Before
51     public void setUp() {
52         SerializerRegistry registry = new SerializerRegistryImpl();
53         registry.init();
54         factory = registry.getSerializer(ExperimenterSerializerKeyFactory.createExperimenterMessageSerializerKey(
55                 EncodeConstants.OF13_VERSION_ID, EncodeConstants.ONF_EXPERIMENTER_ID,
56                 EncodeConstants.ONF_ET_BUNDLE_ADD_MESSAGE));
57     }
58
59     @Test
60     public void testSerializeWithoutProperties() {
61         BundleAddMessageBuilder builder = new BundleAddMessageBuilder();
62         builder.setBundleId(new BundleId(1L));
63         builder.setFlags(new BundleFlags(true, false));
64
65         Message innerMessage = AbstractBundleMessageFactoryTest.createPortModCase();
66         builder.setMessage(innerMessage);
67
68         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
69         Mockito.when(registry.getSerializer(Matchers.any(MessageTypeKey.class))).thenReturn(portModSerializer);
70         ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry);
71         factory.serialize(builder.build(), out);
72
73         Assert.assertEquals("Wrong bundle ID", 1L, out.readUnsignedInt());
74         long padding = out.readUnsignedShort();
75         Assert.assertEquals("Wrong flags", 1, out.readUnsignedShort());
76         Mockito.verify(portModSerializer, Mockito.times(1)).serialize((PortMod)innerMessage, out);
77     }
78
79     @Test
80     public void testSerializeWithExperimenterProperty() {
81         BundleAddMessageBuilder builder = new BundleAddMessageBuilder();
82         builder.setBundleId(new BundleId(2L));
83         builder.setFlags(new BundleFlags(true, false));
84
85         Message innerMessage = AbstractBundleMessageFactoryTest.createPortModCase();
86         builder.setMessage(innerMessage);
87
88         BundleExperimenterPropertyData data = AbstractBundleMessageFactoryTest.createBundleExperimenterPropertyData();
89         builder.setBundleProperty(AbstractBundleMessageFactoryTest.createListWithBundleExperimenterProperty(data));
90
91         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
92         Mockito.when(registry.getSerializer(Matchers.any(MessageTypeKey.class)))
93                 .thenReturn(portModSerializer)
94                 .thenReturn(propertySerializer);
95         ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry);
96         factory.serialize(builder.build(), out);
97
98         Assert.assertEquals("Wrong bundle ID", 2L, out.readUnsignedInt());
99         long padding = out.readUnsignedShort();
100         Assert.assertEquals("Wrong flags", 1, out.readUnsignedShort());
101         Mockito.verify(portModSerializer, Mockito.times(1)).serialize((PortMod)innerMessage, out);
102         Mockito.verify(propertySerializer, Mockito.times(1)).serialize(data, out);
103     }
104
105 }