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
8 package org.opendaylight.yangtools.yang.data.impl.codec;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.Collections;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
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.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
24 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
27 * Unit tests for BitsCodecString.
29 * @author Thomas Pantelis
31 public class BitsCodecStringTest {
32 private static BitsTypeDefinition toBitsTypeDefinition(final String... bits) {
33 final BitsTypeBuilder b = BaseTypes.bitsTypeBuilder(QName.create("foo", "foo"));
36 for (String bit : bits) {
37 BitsTypeDefinition.Bit mockBit = mock(BitsTypeDefinition.Bit.class);
38 doReturn(bit).when(mockBit).getName();
39 doReturn(Uint32.valueOf(pos)).when(mockBit).getPosition();
47 @SuppressWarnings("unchecked")
49 public void testSerialize() {
50 BitsCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(toBitsTypeDefinition("foo"),
53 String serialized = codec.serialize(ImmutableSet.of("foo", "bar"));
54 assertNotNull(serialized);
55 assertTrue(serialized.contains("foo"));
56 assertTrue(serialized.contains("bar"));
58 assertEquals("", codec.serialize(ImmutableSet.of()));
61 @SuppressWarnings("unchecked")
63 public void testDeserialize() {
64 BitsCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(
65 toBitsTypeDefinition("bit1", "bit2"), BitsCodec.class);
67 assertEquals("deserialize", ImmutableSet.of("bit1", "bit2"), codec.deserialize(" bit1 bit2 "));
68 assertEquals("deserialize", Collections.emptySet(), codec.deserialize(""));
70 TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, "bit1 bit3");