OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / extension / openflowplugin-extension-onf / src / test / java / org / opendaylight / openflowplugin / extension / onf / serializer / BundleControlFactoryTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.extension.onf.serializer;
10
11 import static org.mockito.ArgumentMatchers.any;
12
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;
32
33 /**
34  * Test for {@link org.opendaylight.openflowplugin.extension.onf.serializer.BundleControlFactory}.
35  */
36 @RunWith(MockitoJUnitRunner.class)
37 public class BundleControlFactoryTest extends AbstractBundleMessageFactoryTest {
38
39     private final OFSerializer<BundleControlOnf> factory = new BundleControlFactory();
40
41     @Test
42     public void testSerializeWithoutProperties() {
43         testSerialize(false);
44     }
45
46     @Test
47     public void testSerializeWithExperimenterProperty() {
48         testSerialize(true);
49     }
50
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));
56
57         if (withProperty) {
58             dataBuilder.setBundleProperty(new ArrayList<>(Collections.singleton(
59                     BundleTestUtils.createExperimenterProperty(propertyExperimenterData))));
60             Mockito.when(registry.getSerializer(any())).thenReturn(propertySerializer);
61             ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry);
62         }
63
64         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
65         factory.serialize(new BundleControlOnfBuilder().setOnfControlGroupingData(dataBuilder.build()).build(), out);
66
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());
70         if (withProperty) {
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);
77
78         } else {
79             Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
80         }
81     }
82 }