BUG-1065: translation of Ipv6ExtHeader flags to int
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorUtilTest.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
9
10 import java.lang.reflect.Constructor;
11
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * 
20  */
21 public class MatchConvertorUtilTest {
22     
23     private static Logger LOG = LoggerFactory
24             .getLogger(MatchConvertorUtilTest.class);
25
26     /**
27      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorUtil#ipv6ExthdrFlagsToInt(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags)}.
28      * @throws Exception 
29      */
30     @Test
31     public void testIpv6ExthdrFlagsToInt() throws Exception {
32         Ipv6ExthdrFlags pField;
33         Constructor<Ipv6ExthdrFlags> ctor = Ipv6ExthdrFlags.class.getConstructor(
34                 Boolean.class, Boolean.class, Boolean.class, Boolean.class, 
35                 Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class);
36         
37         int[] expectedFlagCumulants = new int[] {
38                 4, 8, 2, 16, 64, 1, 32, 128, 256
39         };
40         
41         for (int i = 0; i < 9; i++) {
42             pField = ctor.newInstance(createIpv6ExthdrFlagsCtorParams(i));
43             int intResult = MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField);
44             LOG.debug("{}:Ipv6ExthdrFlags[{}] as int = {}", i, pField, intResult);
45             Assert.assertEquals(expectedFlagCumulants[i], intResult);
46         }
47         
48         pField = new Ipv6ExthdrFlags(
49                 false, false, false, false, false, false, false, false, false);
50         Assert.assertEquals(0, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
51         
52         pField = new Ipv6ExthdrFlags(
53                 true, true, true, true, true, true, true, true, true);
54         Assert.assertEquals(511, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
55     }
56
57     /**
58      * @return
59      */
60     private static Object[] createIpv6ExthdrFlagsCtorParams(int trueIndex) {
61         Boolean[] flags = new Boolean[]{false, false, false, false, false, false, false, false, false};
62         flags[trueIndex] = true;
63         return flags;
64     }
65
66 }