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.serializer;
11 import static org.mockito.ArgumentMatchers.any;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
23 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
24 import org.opendaylight.openflowplugin.extension.onf.BundleTestUtils;
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.BundleId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundlePropertyType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControlOnf;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControlOnfBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.bundle.control.onf.OnfControlGroupingDataBuilder;
34 * Test for {@link org.opendaylight.openflowplugin.extension.onf.serializer.BundleControlFactory}.
36 @RunWith(MockitoJUnitRunner.class)
37 public class BundleControlFactoryTest extends AbstractBundleMessageFactoryTest {
39 private final OFSerializer<BundleControlOnf> factory = new BundleControlFactory();
42 public void testSerializeWithoutProperties() {
47 public void testSerializeWithExperimenterProperty() {
51 private void testSerialize(final boolean withProperty) {
52 final OnfControlGroupingDataBuilder dataBuilder = new OnfControlGroupingDataBuilder();
53 dataBuilder.setBundleId(new BundleId(1L));
54 dataBuilder.setType(BundleControlType.ONFBCTOPENREQUEST);
55 dataBuilder.setFlags(new BundleFlags(true, true));
58 dataBuilder.setBundleProperty(new ArrayList<>(Collections.singleton(
59 BundleTestUtils.createExperimenterProperty(propertyExperimenterData))));
60 Mockito.when(registry.getSerializer(any())).thenReturn(propertySerializer);
61 ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry);
64 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
65 factory.serialize(new BundleControlOnfBuilder().setOnfControlGroupingData(dataBuilder.build()).build(), out);
67 Assert.assertEquals("Wrong bundle ID", 1L, out.readUnsignedInt());
68 Assert.assertEquals("Wrong type", BundleControlType.ONFBCTOPENREQUEST.getIntValue(), out.readUnsignedShort());
69 Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort());
71 Assert.assertEquals("Wrong property type", BundlePropertyType.ONFETBPTEXPERIMENTER.getIntValue(),
72 out.readUnsignedShort());
73 out.readUnsignedShort(); // length
74 Assert.assertEquals("Wrong experimenter ID", 1, out.readUnsignedInt());
75 Assert.assertEquals("Wrong experimenter type", 2, out.readUnsignedInt());
76 Mockito.verify(propertySerializer, Mockito.times(1)).serialize(propertyExperimenterData, out);
79 Assert.assertTrue("Unexpected data", out.readableBytes() == 0);