Add BundleControl converter from OFJ
[openflowplugin.git] / extension / openflowplugin-extension-onf / src / test / java / org / opendaylight / openflowplugin / extension / onf / deserializer / BundleControlFactoryTest.java
1 /*
2  * Copyright (c) 2017 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.openflowplugin.extension.onf.deserializer;
10
11 import io.netty.buffer.ByteBuf;
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
22 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
23 import org.opendaylight.openflowplugin.extension.onf.ByteBufUtils;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleControlType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundlePropertyType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.common.grouping.BundleProperty;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.BundlePropertyExperimenter;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.bundle.property.experimenter.BundlePropertyExperimenterData;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControl;
31
32 /**
33  * Tests for {@link org.opendaylight.openflowplugin.extension.onf.deserializer.BundleControlFactory}.
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class BundleControlFactoryTest {
37
38     private final OFDeserializer<BundleControl> factory = new BundleControlFactory();
39     @Mock
40     DeserializerRegistry registry;
41     @Mock
42     OFDeserializer<BundlePropertyExperimenterData> experimenterPropertyDeserializer;
43
44     @Test
45     public void testDeserializeWithoutProperties() {
46         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
47                                                        + "00 01 " // type
48                                                        + "00 03"); // flags
49         BundleControl builtByFactory = factory.deserialize(buffer);
50         Assert.assertEquals(1, builtByFactory.getBundleId().getValue().intValue());
51         BundleFlags flags = new BundleFlags(true, true);
52         Assert.assertEquals("Wrong atomic flag", flags.isAtomic(), builtByFactory.getFlags().isAtomic());
53         Assert.assertEquals("Wrong ordered flag", flags.isOrdered(), builtByFactory.getFlags().isOrdered());
54         Assert.assertEquals("Wrong type", BundleControlType.ONFBCTOPENREPLY, builtByFactory.getType());
55         Assert.assertTrue("Properties not empty", builtByFactory.getBundleProperty().isEmpty());
56     }
57
58     @Test
59     public void testDeserializeWithProperties() {
60         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
61                                                        + "00 05 " // type
62                                                        + "00 02 " // flags
63                                                        + "ff ff " // type 1
64                                                        + "00 0c " // length 1
65                                                        + "00 00 00 01 " // experimenter ID 1
66                                                        + "00 00 00 02 " // experimenter type 1
67                                                        + "00 00 00 00 " // experimenter data 1
68                                                        + "00 00 " // type 2
69                                                        + "00 04 " // length 2
70                                                        + "00 00 00 00"); // data 2
71         Mockito.when(registry.getDeserializer(Matchers.any(MessageCodeKey.class))).thenReturn(experimenterPropertyDeserializer);
72         ((DeserializerRegistryInjector)factory).injectDeserializerRegistry(registry);
73         BundleControl builtByFactory = factory.deserialize(buffer);
74         Assert.assertEquals(1, builtByFactory.getBundleId().getValue().intValue());
75         BundleFlags flags = new BundleFlags(false, true);
76         Assert.assertEquals("Wrong atomic flag", flags.isAtomic(), builtByFactory.getFlags().isAtomic());
77         Assert.assertEquals("Wrong ordered flag", flags.isOrdered(), builtByFactory.getFlags().isOrdered());
78         Assert.assertEquals("Wrong type", BundleControlType.ONFBCTCOMMITREPLY, builtByFactory.getType());
79         BundleProperty property = builtByFactory.getBundleProperty().get(0);
80         Assert.assertEquals("Wrong bundle property type", BundlePropertyType.ONFETBPTEXPERIMENTER, property.getType());
81         BundlePropertyExperimenter experimenterProperty = (BundlePropertyExperimenter) property.getBundlePropertyEntry();
82         Assert.assertEquals("Wrong experimenter ID", 1, experimenterProperty.getExperimenter().getValue().intValue());
83         Assert.assertEquals("Wrong experimenter type", 2, experimenterProperty.getExpType().longValue());
84         Mockito.verify(experimenterPropertyDeserializer, Mockito.times(1)).deserialize(buffer);
85     }
86
87 }