Fix checkstyle violations in openflowplugin extensions
[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 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;
31
32 /**
33  * Test for {@link org.opendaylight.openflowplugin.extension.onf.serializer.BundleControlFactory}.
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class BundleControlFactoryTest extends AbstractBundleMessageFactoryTest {
37
38     private final OFSerializer<BundleControlOnf> factory = new BundleControlFactory();
39
40     @Test
41     public void testSerializeWithoutProperties() {
42         testSerialize(false);
43     }
44
45     @Test
46     public void testSerializeWithExperimenterProperty() {
47         testSerialize(true);
48     }
49
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));
55
56         if (withProperty) {
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);
61         }
62
63         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
64         factory.serialize(new BundleControlOnfBuilder().setOnfControlGroupingData(dataBuilder.build()).build(), out);
65
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());
69         if (withProperty) {
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);
76
77         } else {
78             Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
79         }
80     }
81 }