2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.extension.onf.deserializer;
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.BundleControlOnf;
33 * Tests for {@link org.opendaylight.openflowplugin.extension.onf.deserializer.BundleControlFactory}.
35 @RunWith(MockitoJUnitRunner.class)
36 public class BundleControlFactoryTest {
38 private final OFDeserializer<BundleControlOnf> factory = new BundleControlFactory();
40 private DeserializerRegistry registry;
42 private OFDeserializer<BundlePropertyExperimenterData> experimenterPropertyDeserializer;
45 public void testDeserializeWithoutProperties() {
46 ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
49 BundleControlOnf builtByFactory = factory.deserialize(buffer);
50 Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
51 BundleFlags flags = new BundleFlags(true, true);
52 Assert.assertEquals("Wrong atomic flag",
53 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
54 Assert.assertEquals("Wrong ordered flag",
55 flags.isOrdered(), builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
56 Assert.assertEquals("Wrong type",
57 BundleControlType.ONFBCTOPENREPLY, builtByFactory.getOnfControlGroupingData().getType());
58 Assert.assertTrue("Properties not empty",
59 builtByFactory.getOnfControlGroupingData().getBundleProperty().isEmpty());
63 public void testDeserializeWithProperties() {
64 ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
68 + "00 0c " // length 1
69 + "00 00 00 01 " // experimenter ID 1
70 + "00 00 00 02 " // experimenter type 1
71 + "00 00 00 00 " // experimenter data 1
73 + "00 04 " // length 2
74 + "00 00 00 00"); // data 2
75 Mockito.when(registry.getDeserializer(Matchers.any(MessageCodeKey.class)))
76 .thenReturn(experimenterPropertyDeserializer);
77 ((DeserializerRegistryInjector)factory).injectDeserializerRegistry(registry);
78 BundleControlOnf builtByFactory = factory.deserialize(buffer);
79 Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
80 BundleFlags flags = new BundleFlags(false, true);
81 Assert.assertEquals("Wrong atomic flag",
82 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
83 Assert.assertEquals("Wrong ordered flag",
85 builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
86 Assert.assertEquals("Wrong type",
87 BundleControlType.ONFBCTCOMMITREPLY, builtByFactory.getOnfControlGroupingData().getType());
88 BundleProperty property = builtByFactory.getOnfControlGroupingData().getBundleProperty().get(0);
89 Assert.assertEquals("Wrong bundle property type",
90 BundlePropertyType.ONFETBPTEXPERIMENTER, property.getType());
91 BundlePropertyExperimenter experimenterProperty
92 = (BundlePropertyExperimenter) property.getBundlePropertyEntry();
93 Assert.assertEquals("Wrong experimenter ID",
94 1, experimenterProperty.getExperimenter().getValue().intValue());
95 Assert.assertEquals("Wrong experimenter type",
96 2, experimenterProperty.getExpType().longValue());
97 Mockito.verify(experimenterPropertyDeserializer, Mockito.times(1)).deserialize(buffer);