Promote SchemaSourceRepresentation
[yangtools.git] / parser / 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 package org.opendaylight.yangtools.yang.parser.repo;
9
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16
17 import java.util.Optional;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.jupiter.api.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
25
26 public class MultipleRevImportBug6875Test {
27     private static final String BAR_NS = "bar";
28     private static final String BAR_REV_1 = "2017-02-06";
29     private static final String BAR_REV_2 = "1999-01-01";
30     private static final String BAR_REV_3 = "1970-01-01";
31     private static final String FOO_NS = "foo";
32
33     @Test
34     public void testYang11() throws Exception {
35         final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-multiple-rev-import-test");
36
37         final var foo = getSourceProvider("/rfc7950/bug6875/yang1-1/foo.yang");
38         final var bar1 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang");
39         final var bar2 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang");
40         final var bar3 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang");
41
42         setAndRegister(sharedSchemaRepository, foo);
43         setAndRegister(sharedSchemaRepository, bar1);
44         setAndRegister(sharedSchemaRepository, bar2);
45         setAndRegister(sharedSchemaRepository, bar3);
46
47         final var schemaContextFuture = sharedSchemaRepository.createEffectiveModelContextFactory()
48             .createEffectiveModelContext(foo.getId(), bar1.getId(), bar2.getId(), bar3.getId());
49         assertTrue(schemaContextFuture.isDone());
50
51         final var context = schemaContextFuture.get();
52         assertEquals(context.getModules().size(), 4);
53
54         assertInstanceOf(ContainerSchemaNode.class,
55                 context.findDataTreeChild(foo("root"), foo("my-container-1")).orElse(null));
56         assertInstanceOf(ContainerSchemaNode.class,
57                 context.findDataTreeChild(foo("root"), foo("my-container-2")).orElse(null));
58
59         assertInstanceOf(ContainerSchemaNode.class,
60                 context.findDataTreeChild(bar3("root"), foo("my-container-1")).orElse(null));
61         assertInstanceOf(ContainerSchemaNode.class,
62                 context.findDataTreeChild(bar3("root"), foo("my-container-2")).orElse(null));
63
64         assertEquals(Optional.empty(), context.findDataTreeChild(bar2("root"), foo("my-container-1")));
65         assertEquals(Optional.empty(), context.findDataTreeChild(bar2("root"), foo("my-container-2")));
66
67         assertEquals(Optional.empty(), context.findDataTreeChild(bar1("root"), foo("my-container-1")));
68         assertEquals(Optional.empty(), context.findDataTreeChild(bar1("root"), foo("my-container-2")));
69     }
70
71     @Test
72     public void testYang10() throws Exception {
73         final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-multiple-rev-import-test");
74
75         final var foo = getSourceProvider("/rfc7950/bug6875/yang1-0/foo.yang");
76         final var bar1 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang");
77         final var bar2 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang");
78
79         setAndRegister(sharedSchemaRepository, foo);
80         setAndRegister(sharedSchemaRepository, bar1);
81         setAndRegister(sharedSchemaRepository, bar2);
82
83         final var schemaContextFuture = sharedSchemaRepository.createEffectiveModelContextFactory()
84             .createEffectiveModelContext(foo.getId(), bar1.getId(), bar2.getId());
85         assertTrue(schemaContextFuture.isDone());
86
87         final var ex = assertThrows(ExecutionException.class, schemaContextFuture::get);
88         final var cause = assertInstanceOf(IllegalArgumentException.class, ex.getCause());
89         assertThat(cause.getMessage(), startsWith("Module:bar imported twice with different revisions"));
90     }
91
92     private static void setAndRegister(final SharedSchemaRepository sharedSchemaRepository,
93             final SettableSchemaProvider<YangIRSchemaSource> source) {
94         source.register(sharedSchemaRepository);
95         source.setResult();
96     }
97
98     private static SettableSchemaProvider<YangIRSchemaSource> getSourceProvider(final String resourceName)
99             throws Exception {
100         return SettableSchemaProvider.createImmediate(
101             TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)),
102             YangIRSchemaSource.class);
103     }
104
105     private static QName foo(final String localName) {
106         return QName.create(FOO_NS, localName);
107     }
108
109     private static QName bar1(final String localName) {
110         return QName.create(BAR_NS, BAR_REV_1, localName);
111     }
112
113     private static QName bar2(final String localName) {
114         return QName.create(BAR_NS, BAR_REV_2, localName);
115     }
116
117     private static QName bar3(final String localName) {
118         return QName.create(BAR_NS, BAR_REV_3, localName);
119     }
120 }