Add methods that allows registering any serializer
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / experimenter / BundleControlFactoryTest.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.BundleControlType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleFlags;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundlePropertyType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.bundle.experimenter.property.BundleExperimenterPropertyData;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleControl;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleControlBuilder;
35
36 /**
37  * Test for {@link org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.BundleControlFactory}.
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class BundleControlFactoryTest {
41
42     private OFSerializer<BundleControl> factory;
43     @Mock
44     SerializerRegistry registry;
45     @Mock
46     OFSerializer<BundleExperimenterPropertyData> serializer;
47
48     @Before
49     public void setUp() throws Exception {
50         SerializerRegistry registry = new SerializerRegistryImpl();
51         registry.init();
52         factory = registry.getSerializer(ExperimenterSerializerKeyFactory.createExperimenterMessageSerializerKey(
53                 EncodeConstants.OF13_VERSION_ID, EncodeConstants.ONF_EXPERIMENTER_ID, EncodeConstants.ONF_ET_BUNDLE_CONTROL));
54     }
55
56     @Test
57     public void testSerializeWithoutProperties() {
58         BundleControlBuilder builder = new BundleControlBuilder();
59         builder.setBundleId(new BundleId(1L));
60         builder.setType(BundleControlType.ONFBCTOPENREQUEST);
61         builder.setFlags(new BundleFlags(true, true));
62
63         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
64         factory.serialize(builder.build(), out);
65
66         Assert.assertEquals("Wrong bundle ID", 1L, out.readUnsignedInt());
67         Assert.assertEquals("Wrong type", BundleControlType.ONFBCTOPENREQUEST.getIntValue(), out.readUnsignedShort());
68         Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort());
69         Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
70     }
71
72     @Test
73     public void testSerializeWithExperimenterProperty() {
74         BundleControlBuilder builder = new BundleControlBuilder();
75         builder.setBundleId(new BundleId(3L));
76         builder.setType(BundleControlType.ONFBCTCOMMITREQUEST);
77         builder.setFlags(new BundleFlags(false, true));
78
79         BundleExperimenterPropertyData data = AbstractBundleMessageFactoryTest.createBundleExperimenterPropertyData();
80         builder.setBundleProperty(AbstractBundleMessageFactoryTest.createListWithBundleExperimenterProperty(data));
81
82         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
83         Mockito.when(registry.getSerializer(Matchers.any(MessageTypeKey.class))).thenReturn(serializer);
84         ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry);
85         factory.serialize(builder.build(), out);
86
87         Assert.assertEquals("Wrong bundle ID", 3L, out.readUnsignedInt());
88         Assert.assertEquals("Wrong type", BundleControlType.ONFBCTCOMMITREQUEST.getIntValue(), out.readUnsignedShort());
89         Assert.assertEquals("Wrong flags", 2, out.readUnsignedShort());
90         Assert.assertEquals("Wrong property type", BundlePropertyType.ONFETBPTEXPERIMENTER.getIntValue(), out.readUnsignedShort());
91         int length = out.readUnsignedShort();
92         Assert.assertEquals("Wrong experimenter ID", 1, out.readUnsignedInt());
93         Assert.assertEquals("Wrong experimenter type", 2, out.readUnsignedInt());
94         Mockito.verify(serializer, Mockito.times(1)).serialize(data, out);
95     }
96
97 }