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.BundleControl;
33 * Tests for {@link org.opendaylight.openflowplugin.extension.onf.deserializer.BundleControlFactory}.
35 @RunWith(MockitoJUnitRunner.class)
36 public class BundleControlFactoryTest {
38 private final OFDeserializer<BundleControl> factory = new BundleControlFactory();
40 DeserializerRegistry registry;
42 OFDeserializer<BundlePropertyExperimenterData> experimenterPropertyDeserializer;
45 public void testDeserializeWithoutProperties() {
46 ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
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());
59 public void testDeserializeWithProperties() {
60 ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
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
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);