OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / extension / openflowplugin-extension-onf / src / test / java / org / opendaylight / openflowplugin / extension / onf / deserializer / 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.deserializer;
10
11 import static org.mockito.ArgumentMatchers.any;
12
13 import io.netty.buffer.ByteBuf;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
23 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
24 import org.opendaylight.openflowplugin.extension.onf.ByteBufUtils;
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.BundlePropertyType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.common.grouping.BundleProperty;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.BundlePropertyExperimenter;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.bundle.property.experimenter.BundlePropertyExperimenterData;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControlOnf;
32
33 /**
34  * Tests for {@link org.opendaylight.openflowplugin.extension.onf.deserializer.BundleControlFactory}.
35  */
36 @RunWith(MockitoJUnitRunner.class)
37 public class BundleControlFactoryTest {
38
39     private final OFDeserializer<BundleControlOnf> factory = new BundleControlFactory();
40     @Mock
41     private DeserializerRegistry registry;
42     @Mock
43     private OFDeserializer<BundlePropertyExperimenterData> experimenterPropertyDeserializer;
44
45     @Test
46     public void testDeserializeWithoutProperties() {
47         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
48                                                        + "00 01 " // type
49                                                        + "00 03"); // flags
50         BundleControlOnf builtByFactory = factory.deserialize(buffer);
51         Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
52         BundleFlags flags = new BundleFlags(true, true);
53         Assert.assertEquals("Wrong atomic flag",
54                 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
55         Assert.assertEquals("Wrong ordered flag",
56                 flags.isOrdered(), builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
57         Assert.assertEquals("Wrong type",
58                 BundleControlType.ONFBCTOPENREPLY, builtByFactory.getOnfControlGroupingData().getType());
59         Assert.assertTrue("Properties not empty",
60                 builtByFactory.getOnfControlGroupingData().getBundleProperty().isEmpty());
61     }
62
63     @Test
64     public void testDeserializeWithProperties() {
65         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
66                                                        + "00 05 " // type
67                                                        + "00 02 " // flags
68                                                        + "ff ff " // type 1
69                                                        + "00 0c " // length 1
70                                                        + "00 00 00 01 " // experimenter ID 1
71                                                        + "00 00 00 02 " // experimenter type 1
72                                                        + "00 00 00 00 " // experimenter data 1
73                                                        + "00 00 " // type 2
74                                                        + "00 04 " // length 2
75                                                        + "00 00 00 00"); // data 2
76         Mockito.when(registry.getDeserializer(any(MessageCodeKey.class)))
77                 .thenReturn(experimenterPropertyDeserializer);
78         ((DeserializerRegistryInjector)factory).injectDeserializerRegistry(registry);
79         BundleControlOnf builtByFactory = factory.deserialize(buffer);
80         Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
81         BundleFlags flags = new BundleFlags(false, true);
82         Assert.assertEquals("Wrong atomic flag",
83                 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
84         Assert.assertEquals("Wrong ordered flag",
85                 flags.isOrdered(),
86                 builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
87         Assert.assertEquals("Wrong type",
88                 BundleControlType.ONFBCTCOMMITREPLY, builtByFactory.getOnfControlGroupingData().getType());
89         BundleProperty property = builtByFactory.getOnfControlGroupingData().getBundleProperty().get(0);
90         Assert.assertEquals("Wrong bundle property type",
91                 BundlePropertyType.ONFETBPTEXPERIMENTER, property.getType());
92         BundlePropertyExperimenter experimenterProperty
93                 = (BundlePropertyExperimenter) property.getBundlePropertyEntry();
94         Assert.assertEquals("Wrong experimenter ID",
95                 1, experimenterProperty.getExperimenter().getValue().intValue());
96         Assert.assertEquals("Wrong experimenter type",
97                 2, experimenterProperty.getExpType().longValue());
98         Mockito.verify(experimenterPropertyDeserializer, Mockito.times(1)).deserialize(buffer);
99     }
100
101 }