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.mockito.Mockito.mock;
13 import java.util.List;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
17 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
19 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
21 import org.opendaylight.yangtools.yang.model.ri.type.InvalidLengthConstraintException;
22 import org.opendaylight.yangtools.yang.model.ri.type.RestrictedTypes;
23 import org.opendaylight.yangtools.yang.model.ri.type.StringTypeBuilder;
26 * Unit tests for StringCodecString.
28 * @author Thomas Pantelis
30 public class StringCodecStringTest {
32 public void testSerialize() {
33 StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.stringType(),
36 assertEquals("serialize", "foo", codec.serialize("foo"));
37 assertEquals("serialize", "", codec.serialize(""));
41 public void testDeserialize() {
42 StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.stringType(),
45 assertEquals("deserialize", "bar", codec.deserialize("bar"));
46 assertEquals("deserialize", "", codec.deserialize(""));
50 public void testDeserializeUnicode() throws InvalidLengthConstraintException {
51 final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(BaseTypes.stringType(),
52 QName.create("foo", "foo"));
53 builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), List.of(ValueRange.of(1)));
54 final StringTypeDefinition type = builder.build();
56 StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(type, StringCodec.class);
58 assertEquals("🌞", codec.deserialize("🌞"));