Migrate Optional.get() callers
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / BindingGeneratorUtilTest.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 package org.opendaylight.mdsal.binding.generator;
9
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;
17
18 import com.google.common.collect.Range;
19 import java.util.List;
20 import java.util.Optional;
21 import java.util.Set;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.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;
37
38 public class BindingGeneratorUtilTest {
39     private static final QName ROOT = QName.create("test", "root");
40
41     @Test
42     public void getRestrictionsTest() throws InvalidLengthConstraintException {
43         final PatternConstraint constraint = mock(PatternConstraint.class);
44
45         final StringTypeBuilder builder =
46                 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT);
47
48         builder.addPatternConstraint(constraint);
49         builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), List.of(ValueRange.of(1, 2)));
50
51         Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
52
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());
58
59         assertFalse(restrictions.isEmpty());
60         assertThat(restrictions.getPatternConstraints(), contains(constraint));
61     }
62
63     @Test
64     public void getEmptyRestrictionsTest() {
65         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT).build();
66         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
67
68         assertNotNull(restrictions);
69         assertTrue(restrictions.isEmpty());
70     }
71
72     @Test
73     public void getDefaultIntegerRestrictionsTest() {
74         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT).build();
75         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
76
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());
83     }
84
85     @Test
86     public void getDefaultUnsignedIntegerRestrictionsTest() {
87         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT).build();
88         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
89
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());
96     }
97
98     @Test
99     public void getDefaultDecimalRestrictionsTest() {
100         final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT).setFractionDigits(10).build();
101         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT).build();
102
103         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
104
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());
110     }
111
112     @Test
113     public void unicodeCharReplaceTest() {
114         String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
115
116         assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
117             BindingGeneratorUtil.replaceAllIllegalChars(inputString));
118     }
119 }