2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.data.impl.codecs;
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;
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;
28 * Unit tests for BitsCodecString.
30 * @author Thomas Pantelis
33 public class BitsCodecStringTest {
34 private static BitsTypeDefinition toBitsTypeDefinition(final String... bits) {
35 final BitsTypeBuilder b = BaseTypes.bitsTypeBuilder(mock(SchemaPath.class));
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));
49 @SuppressWarnings("unchecked")
51 public void testSerialize() {
52 BitsCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(toBitsTypeDefinition("foo"),
55 String serialized = codec.serialize(ImmutableSet.of("foo", "bar"));
56 assertNotNull(serialized);
57 assertTrue(serialized.contains("foo"));
58 assertTrue(serialized.contains("bar"));
60 assertEquals("", codec.serialize(ImmutableSet.of()));
63 @SuppressWarnings("unchecked")
65 public void testDeserialize() {
66 BitsCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(
67 toBitsTypeDefinition("bit1", "bit2"), BitsCodec.class);
69 assertEquals("deserialize", ImmutableSet.of("bit1", "bit2"), codec.deserialize(" bit1 bit2 "));
70 assertEquals("deserialize", Collections.emptySet(), codec.deserialize(""));
72 TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, "bit1 bit3");