Make BitsTypeDefinition.Bit.position() return Uint32
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codecs / BitsCodecStringTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.data.impl.codecs;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.collect.ImmutableSet;
18 import java.util.Collections;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.Uint32;
21 import org.opendaylight.yangtools.yang.data.api.codec.BitsCodec;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
25 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
26
27 /**
28  * Unit tests for BitsCodecString.
29  *
30  * @author Thomas Pantelis
31  *
32  */
33 public class BitsCodecStringTest {
34     private  static BitsTypeDefinition toBitsTypeDefinition(final String... bits) {
35         final BitsTypeBuilder b = BaseTypes.bitsTypeBuilder(mock(SchemaPath.class));
36
37         long pos = 0;
38         for (String bit : bits) {
39             BitsTypeDefinition.Bit mockBit = mock(BitsTypeDefinition.Bit.class);
40             when(mockBit.getName()).thenReturn(bit);
41             when(mockBit.getPosition()).thenReturn(Uint32.valueOf(pos));
42             b.addBit(mockBit);
43             ++pos;
44         }
45
46         return b.build();
47     }
48
49     @SuppressWarnings("unchecked")
50     @Test
51     public void testSerialize() {
52         BitsCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(toBitsTypeDefinition("foo"),
53                 BitsCodec.class);
54
55         String serialized = codec.serialize(ImmutableSet.of("foo", "bar"));
56         assertNotNull(serialized);
57         assertTrue(serialized.contains("foo"));
58         assertTrue(serialized.contains("bar"));
59
60         assertEquals("", codec.serialize(ImmutableSet.of()));
61     }
62
63     @SuppressWarnings("unchecked")
64     @Test
65     public void testDeserialize() {
66         BitsCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(
67             toBitsTypeDefinition("bit1", "bit2"), BitsCodec.class);
68
69         assertEquals("deserialize", ImmutableSet.of("bit1", "bit2"), codec.deserialize("  bit1 bit2     "));
70         assertEquals("deserialize", Collections.emptySet(), codec.deserialize(""));
71
72         TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, "bit1 bit3");
73     }
74 }