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.binding.generator;
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.contains;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Mockito.mock;
18 import com.google.common.collect.Range;
19 import java.util.List;
20 import java.util.Optional;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.binding.model.api.Restrictions;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
28 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
31 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
33 import org.opendaylight.yangtools.yang.model.ri.type.DerivedTypes;
34 import org.opendaylight.yangtools.yang.model.ri.type.InvalidLengthConstraintException;
35 import org.opendaylight.yangtools.yang.model.ri.type.RestrictedTypes;
36 import org.opendaylight.yangtools.yang.model.ri.type.StringTypeBuilder;
38 public class BindingGeneratorUtilTest {
39 private static final QName ROOT = QName.create("test", "root");
42 public void getRestrictionsTest() throws InvalidLengthConstraintException {
43 final PatternConstraint constraint = mock(PatternConstraint.class);
45 final StringTypeBuilder builder =
46 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT);
48 builder.addPatternConstraint(constraint);
49 builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), List.of(ValueRange.of(1, 2)));
51 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
53 assertNotNull(restrictions);
54 assertEquals(Set.of(Range.closed(1, 2)),
55 restrictions.getLengthConstraint().orElseThrow().getAllowedRanges().asRanges());
56 assertFalse(restrictions.getRangeConstraint().isPresent());
57 assertEquals(1, restrictions.getPatternConstraints().size());
59 assertFalse(restrictions.isEmpty());
60 assertThat(restrictions.getPatternConstraints(), contains(constraint));
64 public void getEmptyRestrictionsTest() {
65 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT).build();
66 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
68 assertNotNull(restrictions);
69 assertTrue(restrictions.isEmpty());
73 public void getDefaultIntegerRestrictionsTest() {
74 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT).build();
75 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
77 assertNotNull(restrictions);
78 assertFalse(restrictions.isEmpty());
79 assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(),
80 restrictions.getRangeConstraint());
81 assertEquals(Optional.empty(), restrictions.getLengthConstraint());
82 assertEquals(List.of(), restrictions.getPatternConstraints());
86 public void getDefaultUnsignedIntegerRestrictionsTest() {
87 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT).build();
88 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
90 assertNotNull(restrictions);
91 assertFalse(restrictions.isEmpty());
92 assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(),
93 restrictions.getRangeConstraint());
94 assertEquals(Optional.empty(), restrictions.getLengthConstraint());
95 assertEquals(List.of(), restrictions.getPatternConstraints());
99 public void getDefaultDecimalRestrictionsTest() {
100 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT).setFractionDigits(10).build();
101 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT).build();
103 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
105 assertNotNull(restrictions);
106 assertFalse(restrictions.isEmpty());
107 assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
108 assertEquals(Optional.empty(), restrictions.getLengthConstraint());
109 assertEquals(List.of(), restrictions.getPatternConstraints());
113 public void unicodeCharReplaceTest() {
114 String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
116 assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
117 BindingGeneratorUtil.replaceAllIllegalChars(inputString));