BUG-997 Add tests for shared schema context and fix dependency resolution bug
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaRepositoryTest.java
1 /*
2  * Copyright (c) 2014 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 junit.framework.Assert.assertEquals;
12 import static junit.framework.Assert.assertFalse;
13 import static junit.framework.Assert.assertNotNull;
14 import static junit.framework.Assert.assertSame;
15 import static junit.framework.Assert.assertTrue;
16 import static junit.framework.Assert.fail;
17 import static org.mockito.Mockito.spy;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20
21 import org.junit.Ignore;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
31
32 import com.google.common.collect.Lists;
33 import com.google.common.util.concurrent.CheckedFuture;
34
35 public class SharedSchemaRepositoryTest {
36
37     @Test
38     public void testSimpleSchemaContext() throws Exception {
39         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
40
41         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang");
42         remoteInetTypesYang.register(sharedSchemaRepository);
43         final CheckedFuture<ASTSchemaSource, SchemaSourceException> registeredSourceFuture = sharedSchemaRepository.getSchemaSource(remoteInetTypesYang.getId(), ASTSchemaSource.class);
44         assertFalse(registeredSourceFuture.isDone());
45
46         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
47
48         final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture
49                 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
50
51         assertFalse(schemaContextFuture.isDone());
52
53         // Make source appear
54         remoteInetTypesYang.setResult();
55         assertEquals(remoteInetTypesYang.getSchemaSourceRepresentation(), registeredSourceFuture.get());
56
57         // Verify schema created successfully
58         assertTrue(schemaContextFuture.isDone());
59         final SchemaContext firstSchemaContext = schemaContextFuture.checkedGet();
60         assertSchemaContext(firstSchemaContext, 1);
61
62         // Try same schema second time
63         final CheckedFuture<SchemaContext, SchemaResolutionException> secondSchemaFuture =
64                 sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
65                         .createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
66
67         // Verify second schema created successfully immediately
68         assertTrue(secondSchemaFuture.isDone());
69         // Assert same context instance is returned from first and second attempt
70         assertSame(firstSchemaContext, secondSchemaFuture.checkedGet());
71     }
72
73     @Test
74     public void testTwoSchemaContextsSharingSource() throws Exception {
75         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
76
77         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang");
78         remoteInetTypesYang.register(sharedSchemaRepository);
79         remoteInetTypesYang.setResult();
80         final SettableSchemaProvider<ASTSchemaSource> remoteTopologyYang = getImmediateYangSourceProviderFromResource("/ietf/network-topology@2013-10-21.yang");
81         remoteTopologyYang.register(sharedSchemaRepository);
82         remoteTopologyYang.setResult();
83         final SettableSchemaProvider<ASTSchemaSource> remoteModuleNoRevYang = getImmediateYangSourceProviderFromResource("/no-revision/module-without-revision.yang");
84         remoteModuleNoRevYang.register(sharedSchemaRepository);
85
86         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
87
88         final CheckedFuture<SchemaContext, SchemaResolutionException> inetAndTopologySchemaContextFuture
89                 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId(), remoteTopologyYang.getId()));
90         assertTrue(inetAndTopologySchemaContextFuture.isDone());
91         assertSchemaContext(inetAndTopologySchemaContextFuture.checkedGet(), 2);
92
93         final CheckedFuture<SchemaContext, SchemaResolutionException> inetAndNoRevSchemaContextFuture
94                 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId(), remoteModuleNoRevYang.getId()));
95         assertFalse(inetAndNoRevSchemaContextFuture.isDone());
96
97         remoteModuleNoRevYang.setResult();
98         assertTrue(inetAndNoRevSchemaContextFuture.isDone());
99         assertSchemaContext(inetAndNoRevSchemaContextFuture.checkedGet(), 2);
100     }
101
102     @Test
103     public void testFailedSchemaContext() throws Exception {
104         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
105
106         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang");
107         remoteInetTypesYang.register(sharedSchemaRepository);
108
109         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
110
111         // Make source appear
112         final Throwable ex = new IllegalStateException("failed schema");
113         remoteInetTypesYang.setException(ex);
114
115         final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture
116                 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
117
118         try {
119             schemaContextFuture.checkedGet();
120         } catch (final SchemaResolutionException e) {
121             assertNotNull(e.getCause());
122             assertNotNull(e.getCause().getCause());
123             assertSame(ex, e.getCause().getCause());
124             return;
125         }
126
127         fail("Schema context creation should have failed");
128     }
129
130     // TODO
131     @Ignore("Costs are not considered when getting sources")
132     @Test
133     public void testDifferentCosts() throws Exception {
134         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
135
136         final SettableSchemaProvider<ASTSchemaSource> immediateInetTypesYang = spy(getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
137         immediateInetTypesYang.register(sharedSchemaRepository);
138         immediateInetTypesYang.setResult();
139
140         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = spy(getRemoteYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
141         remoteInetTypesYang.register(sharedSchemaRepository);
142         remoteInetTypesYang.setResult();
143
144         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
145
146         final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture
147                 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
148
149         assertSchemaContext(schemaContextFuture.checkedGet(), 1);
150
151         final SourceIdentifier id = immediateInetTypesYang.getId();
152         verify(remoteInetTypesYang, times(0)).getSource(id);
153         verify(immediateInetTypesYang).getSource(id);
154     }
155
156     private void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
157         assertNotNull(schemaContext);
158         assertEquals(moduleSize, schemaContext.getModules().size());
159     }
160
161     private SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName) throws Exception {
162         final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
163         final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
164         return SettableSchemaProvider.createRemote(aSTSchemaSource.get(), ASTSchemaSource.class);
165     }
166
167     private SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName) throws Exception {
168         final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
169         final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
170         return SettableSchemaProvider.createImmediate(aSTSchemaSource.get(), ASTSchemaSource.class);
171     }
172
173 }