Add methods that allows registering any serializer
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / 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.deserialization.experimenter;
10
11 import io.netty.buffer.ByteBuf;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Matchers;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
23 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
24 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
25 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
26 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
27 import org.opendaylight.openflowjava.util.ByteBufUtils;
28 import org.opendaylight.openflowjava.util.ExperimenterDeserializerKeyFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleControlType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleFlags;
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.BundleProperty;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.BundleExperimenterProperty;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.bundle.experimenter.property.BundleExperimenterPropertyData;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleControl;
36
37 /**
38  * Tests for {@link org.opendaylight.openflowjava.protocol.impl.deserialization.experimenter.BundleControlFactory}.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class BundleControlFactoryTest {
42
43     private OFDeserializer<BundleControl> factory;
44     @Mock
45     DeserializerRegistry registry;
46     @Mock
47     OFDeserializer<BundleExperimenterPropertyData> experimenterPropertyDeserializer;
48
49     @Before
50     public void startUp() {
51         DeserializerRegistry registry = new DeserializerRegistryImpl();
52         registry.init();
53         factory = registry.getDeserializer(ExperimenterDeserializerKeyFactory.createExperimenterMessageDeserializerKey(
54                 EncodeConstants.OF13_VERSION_ID, EncodeConstants.ONF_EXPERIMENTER_ID, EncodeConstants.ONF_ET_BUNDLE_CONTROL));
55     }
56
57     @Test
58     public void testDeserializeWithoutProperties() {
59         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
60                                                        + "00 01 " // type
61                                                        + "00 03"); // flags
62         BundleControl builtByFactory = factory.deserialize(buffer);
63         Assert.assertEquals(1, builtByFactory.getBundleId().getValue().intValue());
64         BundleFlags flags = new BundleFlags(true, true);
65         Assert.assertEquals("Wrong atomic flag", flags.isAtomic(), builtByFactory.getFlags().isAtomic());
66         Assert.assertEquals("Wrong ordered flag", flags.isOrdered(), builtByFactory.getFlags().isOrdered());
67         Assert.assertEquals("Wrong type", BundleControlType.ONFBCTOPENREPLY, builtByFactory.getType());
68         Assert.assertTrue("Properties not empty", builtByFactory.getBundleProperty().isEmpty());
69     }
70
71     @Test
72     public void testDeserializeWithProperties() {
73         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
74                                                        + "00 05 " // type
75                                                        + "00 02 " // flags
76                                                        + "ff ff " // type 1
77                                                        + "00 0c " // length 1
78                                                        + "00 00 00 01 " // experimenter ID 1
79                                                        + "00 00 00 02 " // experimenter type 1
80                                                        + "00 00 00 00 " // experimenter data 1
81                                                        + "00 00 " // type 2
82                                                        + "00 04 " // length 2
83                                                        + "00 00 00 00"); // data 2
84         Mockito.when(registry.getDeserializer(Matchers.any(MessageCodeKey.class))).thenReturn(experimenterPropertyDeserializer);
85         ((DeserializerRegistryInjector)factory).injectDeserializerRegistry(registry);
86         BundleControl builtByFactory = BufferHelper.deserialize(factory, buffer);
87         Assert.assertEquals(1, builtByFactory.getBundleId().getValue().intValue());
88         BundleFlags flags = new BundleFlags(false, true);
89         Assert.assertEquals("Wrong atomic flag", flags.isAtomic(), builtByFactory.getFlags().isAtomic());
90         Assert.assertEquals("Wrong ordered flag", flags.isOrdered(), builtByFactory.getFlags().isOrdered());
91         Assert.assertEquals("Wrong type", BundleControlType.ONFBCTCOMMITREPLY, builtByFactory.getType());
92         BundleProperty property = builtByFactory.getBundleProperty().get(0);
93         Assert.assertEquals("Wrong bundle property type", BundlePropertyType.ONFETBPTEXPERIMENTER, property.getType());
94         BundleExperimenterProperty experimenterProperty = (BundleExperimenterProperty) property.getBundlePropertyEntry();
95         Assert.assertEquals("Wrong experimenter ID", 1, experimenterProperty.getExperimenter().getValue().intValue());
96         Assert.assertEquals("Wrong experimenter type", 2, experimenterProperty.getExpType().longValue());
97         Mockito.verify(experimenterPropertyDeserializer, Mockito.times(1)).deserialize(buffer);
98     }
99
100 }