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