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 static org.mockito.ArgumentMatchers.any;
13 import io.netty.buffer.ByteBuf;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.junit.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.openflowplugin.extension.onf.ByteBufUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleControlType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundlePropertyType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.common.grouping.BundleProperty;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.BundlePropertyExperimenter;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.bundle.property.experimenter.BundlePropertyExperimenterData;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControlOnf;
34 * Tests for {@link org.opendaylight.openflowplugin.extension.onf.deserializer.BundleControlFactory}.
36 @RunWith(MockitoJUnitRunner.class)
37 public class BundleControlFactoryTest {
39 private final OFDeserializer<BundleControlOnf> factory = new BundleControlFactory();
41 private DeserializerRegistry registry;
43 private OFDeserializer<BundlePropertyExperimenterData> experimenterPropertyDeserializer;
46 public void testDeserializeWithoutProperties() {
47 ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
50 BundleControlOnf builtByFactory = factory.deserialize(buffer);
51 Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
52 BundleFlags flags = new BundleFlags(true, true);
53 Assert.assertEquals("Wrong atomic flag",
54 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
55 Assert.assertEquals("Wrong ordered flag",
56 flags.isOrdered(), builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
57 Assert.assertEquals("Wrong type",
58 BundleControlType.ONFBCTOPENREPLY, builtByFactory.getOnfControlGroupingData().getType());
59 Assert.assertTrue("Properties not empty",
60 builtByFactory.getOnfControlGroupingData().nonnullBundleProperty().isEmpty());
64 public void testDeserializeWithProperties() {
65 ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
69 + "00 0c " // length 1
70 + "00 00 00 01 " // experimenter ID 1
71 + "00 00 00 02 " // experimenter type 1
72 + "00 00 00 00 " // experimenter data 1
74 + "00 04 " // length 2
75 + "00 00 00 00"); // data 2
76 Mockito.when(registry.getDeserializer(any(MessageCodeKey.class)))
77 .thenReturn(experimenterPropertyDeserializer);
78 ((DeserializerRegistryInjector)factory).injectDeserializerRegistry(registry);
79 BundleControlOnf builtByFactory = factory.deserialize(buffer);
80 Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
81 BundleFlags flags = new BundleFlags(false, true);
82 Assert.assertEquals("Wrong atomic flag",
83 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
84 Assert.assertEquals("Wrong ordered flag",
86 builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
87 Assert.assertEquals("Wrong type",
88 BundleControlType.ONFBCTCOMMITREPLY, builtByFactory.getOnfControlGroupingData().getType());
89 BundleProperty property = builtByFactory.getOnfControlGroupingData().getBundleProperty().get(0);
90 Assert.assertEquals("Wrong bundle property type",
91 BundlePropertyType.ONFETBPTEXPERIMENTER, property.getType());
92 BundlePropertyExperimenter experimenterProperty
93 = (BundlePropertyExperimenter) property.getBundlePropertyEntry();
94 Assert.assertEquals("Wrong experimenter ID",
95 1, experimenterProperty.getExperimenter().getValue().intValue());
96 Assert.assertEquals("Wrong experimenter type",
97 2, experimenterProperty.getExpType().longValue());
98 Mockito.verify(experimenterPropertyDeserializer, Mockito.times(1)).deserialize(buffer);