670275ce79674e1230ed80e085ec67c8859db391
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / impl / Bug1862Test.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.impl;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import com.google.common.collect.ImmutableRangeSet;
15 import com.google.common.collect.Range;
16 import com.google.common.collect.RangeSet;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 public class Bug1862Test {
27     @Test
28     public void restrictedTypedefTransformationTest() {
29         final EffectiveModelContext context = YangParserTestUtils.parseYangResources(Bug1862Test.class,
30             "/base-yang-types.yang", "/test-type-provider.yang");
31         final List<GeneratedType> types = DefaultBindingGenerator.generateFor(context);
32         assertEquals(41, types.size());
33         final MethodSignature fooGetter = types.stream()
34             .filter(type -> type.getFullyQualifiedName().equals(
35                 "org.opendaylight.yang.gen.v1.urn.opendaylight.org.test.type.provider.model.rev140912.Foo"))
36             .findFirst().orElseThrow()
37             .getMethodDefinitions().stream()
38             .filter(method -> method.getName().equals("getBug1862RestrictedTypedef"))
39             .findFirst().orElseThrow();
40
41         final Type returnType = fooGetter.getReturnType();
42         assertThat(returnType, instanceOf(GeneratedTransferObject.class));
43         final RangeSet<?> range = ((GeneratedTransferObject) returnType).getRestrictions().getRangeConstraint()
44             .orElseThrow().getAllowedRanges();
45         assertEquals(ImmutableRangeSet.of(Range.closed((byte) 1, (byte) 100)), range);
46     }
47 }