4c879ff0efc482f59190b392cda5fb367671fbe3
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / MultipleRevImportBug6875Test.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.yangtools.yang.parser.repo;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Lists;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
31 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
33
34 public class MultipleRevImportBug6875Test {
35     private static final String BAR_NS = "bar";
36     private static final String BAR_REV_1 = "2017-02-06";
37     private static final String BAR_REV_2 = "1999-01-01";
38     private static final String BAR_REV_3 = "1970-01-01";
39     private static final String FOO_NS = "foo";
40     private static final String FOO_REV = "1970-01-01";
41
42     @Test
43     public void testYang11() throws Exception {
44         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
45                 "shared-schema-repo-multiple-rev-import-test");
46
47         final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider("/rfc7950/bug6875/yang1-1/foo.yang");
48         final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
49         final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
50         final SettableSchemaProvider<ASTSchemaSource> bar3 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
51
52         setAndRegister(sharedSchemaRepository, foo);
53         setAndRegister(sharedSchemaRepository, bar1);
54         setAndRegister(sharedSchemaRepository, bar2);
55         setAndRegister(sharedSchemaRepository, bar3);
56
57         final SchemaContextFactory fact = sharedSchemaRepository
58                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
59
60         final ListenableFuture<SchemaContext> schemaContextFuture = fact
61                 .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId()));
62         assertTrue(schemaContextFuture.isDone());
63
64         final SchemaContext context = schemaContextFuture.get();
65         assertEquals(context.getModules().size(), 4);
66
67         assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-1"))) instanceof ContainerSchemaNode);
68         assertTrue(findNode(context, ImmutableList.of(foo("root"), foo("my-container-2"))) instanceof ContainerSchemaNode);
69
70         assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-1"))) instanceof ContainerSchemaNode);
71         assertTrue(findNode(context, ImmutableList.of(bar3("root"), foo("my-container-2"))) instanceof ContainerSchemaNode);
72
73         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-1"))));
74         assertNull(findNode(context, ImmutableList.of(bar2("root"), foo("my-container-2"))));
75
76         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-1"))));
77         assertNull(findNode(context, ImmutableList.of(bar1("root"), foo("my-container-2"))));
78     }
79
80     @Test
81     public void testYang10() throws Exception {
82         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository(
83                 "shared-schema-repo-multiple-rev-import-test");
84
85         final SettableSchemaProvider<ASTSchemaSource> foo = getSourceProvider("/rfc7950/bug6875/yang1-0/foo.yang");
86         final SettableSchemaProvider<ASTSchemaSource> bar1 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
87         final SettableSchemaProvider<ASTSchemaSource> bar2 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
88
89         setAndRegister(sharedSchemaRepository, foo);
90         setAndRegister(sharedSchemaRepository, bar1);
91         setAndRegister(sharedSchemaRepository, bar2);
92
93         final SchemaContextFactory fact = sharedSchemaRepository
94                 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
95
96         final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture = fact
97                 .createSchemaContext(Lists.newArrayList(foo.getId(), bar1.getId(), bar2.getId()));
98         assertTrue(schemaContextFuture.isDone());
99
100         try {
101             schemaContextFuture.checkedGet();
102             fail("Test should fail due to invalid imports of yang source.");
103         } catch (final SchemaResolutionException e) {
104             assertTrue(e.getCause().getMessage().startsWith("Module:bar imported twice with different revisions"));
105         }
106     }
107
108     private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
109             final SettableSchemaProvider<ASTSchemaSource> source) {
110         source.register(sharedSchemaRepository);
111         source.setResult();
112     }
113
114     private static SettableSchemaProvider<ASTSchemaSource> getSourceProvider(final String resourceName)
115             throws Exception {
116         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
117         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
118             ASTSchemaSource.class);
119     }
120
121     private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qNames) {
122         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qNames, true));
123     }
124
125     private static QName foo(final String localName) {
126         return QName.create(FOO_NS, FOO_REV, localName);
127     }
128
129     private static QName bar1(final String localName) {
130         return QName.create(BAR_NS, BAR_REV_1, localName);
131     }
132
133     private static QName bar2(final String localName) {
134         return QName.create(BAR_NS, BAR_REV_2, localName);
135     }
136
137     private static QName bar3(final String localName) {
138         return QName.create(BAR_NS, BAR_REV_3, localName);
139     }
140 }