Bump versions to 14.0.0-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / impl / LeafrefResolutionTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12
13 import java.util.List;
14 import org.junit.Test;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
16 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
17 import org.opendaylight.mdsal.binding.model.ri.Types;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
20
21 public class LeafrefResolutionTest {
22     @Test
23     public void testLeafRefRelativeSelfReference() {
24         final EffectiveModelContext schemaContext =
25             YangParserTestUtils.parseYangResource("/leafref-relative-invalid.yang");
26         final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
27             () -> DefaultBindingGenerator.generateFor(schemaContext));
28         assertEquals(
29             "Effective model contains self-referencing leaf (urn:xml:ns:yang:lrr?revision=2015-02-25)neighbor-id",
30             iae.getMessage());
31     }
32
33     @Test
34     public void testLeafRefAbsoluteSelfReference() {
35         final EffectiveModelContext schemaContext =
36             YangParserTestUtils.parseYangResource("/leafref-absolute-invalid.yang");
37         final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
38             () -> DefaultBindingGenerator.generateFor(schemaContext));
39         assertEquals(
40             "Effective model contains self-referencing leaf (urn:xml:ns:yang:lra?revision=2015-02-25)neighbor-id",
41             iae.getMessage());
42     }
43
44     @Test
45     public void testLeafRefRelativeAndAbsoluteValidReference() {
46         final List<GeneratedType> types = DefaultBindingGenerator.generateFor(
47             YangParserTestUtils.parseYangResource("/leafref-valid.yang"));
48         assertEquals(2, types.size());
49
50         final List<MethodSignature> neighborMethods = types.stream()
51             .filter(type -> type.getName().equals("Neighbor"))
52             .findFirst()
53             .orElseThrow()
54             .getMethodDefinitions();
55         assertEquals(10, neighborMethods.size());
56
57         final MethodSignature getNeighborId = neighborMethods.stream()
58             .filter(method -> method.getName().equals("getNeighborId"))
59             .findFirst()
60             .orElseThrow();
61         assertEquals(Types.STRING, getNeighborId.getReturnType());
62
63         final MethodSignature getNeighbor2Id = neighborMethods.stream()
64             .filter(method -> method.getName().equals("getNeighbor2Id"))
65             .findFirst()
66             .orElseThrow();
67         assertEquals(Types.STRING, getNeighbor2Id.getReturnType());
68     }
69 }