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.parser.stmt.rfc7950;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertFalse;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import java.util.Arrays;
18 import java.util.Collection;
20 import java.util.stream.Collectors;
21 import org.junit.jupiter.api.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
28 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
30 class Bug6869Test extends AbstractYangTest {
31 private static final QName ROOT = QName.create("foo", "root");
32 private static final QName GRP_LEAF = QName.create("foo", "grp-leaf");
35 void identityNoFeaureTest() throws Exception {
36 final var schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang", Set.of());
37 assertNotNull(schemaContext);
39 final var identities = getIdentities(schemaContext);
40 assertEquals(0, identities.size());
42 final LeafSchemaNode grpLeaf = assertInstanceOf(LeafSchemaNode.class,
43 schemaContext.findDataTreeChild(ROOT, GRP_LEAF).orElseThrow());
44 assertFalse(grpLeaf.isMandatory());
48 void identityAllFeauresTest() throws Exception {
49 final var schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6869/foo.yang",
50 createFeaturesSet("identity-feature", "mandatory-leaf", "tls", "ssh", "two", "three"));
51 assertNotNull(schemaContext);
53 final var identities = getIdentities(schemaContext);
54 assertEquals(1, identities.size());
56 final LeafSchemaNode grpLeaf = assertInstanceOf(LeafSchemaNode.class,
57 schemaContext.findDataTreeChild(ROOT, GRP_LEAF).orElseThrow());
58 assertTrue(grpLeaf.isMandatory());
61 private static Collection<? extends IdentitySchemaNode> getIdentities(final EffectiveModelContext schemaContext) {
62 final var modules = schemaContext.getModules();
63 assertEquals(1, modules.size());
64 final Module module = modules.iterator().next();
65 return module.getIdentities();
68 private static Set<QName> createFeaturesSet(final String... featureNames) {
69 return Arrays.stream(featureNames)
70 .map(featureName -> QName.create("foo", featureName))
71 .collect(Collectors.toUnmodifiableSet());
75 void invalidYang10Test() {
76 assertInvalidSubstatementException(startsWith("IF_FEATURE is not valid for IDENTITY"),
77 "/rfc7950/bug6869/invalid10.yang");