Make DOMSchemaService operate of EffectiveModelContext
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / schema / ScanningSchemaServiceProviderTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies 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.dom.broker.schema;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.internal.util.io.IOUtil;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.opendaylight.yangtools.yang.common.Revision;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
31 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33
34 public class ScanningSchemaServiceProviderTest {
35
36     private ArrayList<URL> yangs;
37     private ScanningSchemaServiceProvider schemaService;
38
39     @Before
40     public void setup() {
41         yangs = new ArrayList<>();
42         addYang("/odl-datastore-test.yang");
43
44         schemaService = new ScanningSchemaServiceProvider();
45         assertNotNull(schemaService);
46         addYangs(schemaService);
47     }
48
49     @After
50     public void close() {
51         schemaService.close();
52     }
53
54     private void addYang(final String yang) {
55         yangs.add(ScanningSchemaServiceProvider.class.getResource(yang));
56     }
57
58     @Test
59     public void initJarScanningSchemaServiceTest() throws Exception {
60         assertNotNull(schemaService.getGlobalContext());
61         assertNotNull(schemaService.getSchemaContext());
62         assertEquals(schemaService.getGlobalContext(), schemaService.getSchemaContext());
63     }
64
65     @Test
66     public void listenersTests() {
67         assertFalse(schemaService.hasListeners());
68
69         final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
70         final EffectiveModelContextListener listener = actualSchemaCtx::setSchemaContext;
71         final ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener =
72                 schemaService.registerSchemaContextListener(listener);
73         assertEquals(registerSchemaContextListener.getInstance(), listener);
74         assertEquals(schemaService.getSchemaContext(), actualSchemaCtx.getSchemaContext());
75     }
76
77     @Test
78     public void notifyListenersTest() {
79         final EffectiveModelContext baseSchemaCtx = schemaService.getGlobalContext();
80         assertNotNull(baseSchemaCtx);
81         assertTrue(baseSchemaCtx.getModules().size() == 1);
82
83         final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
84
85         final EffectiveModelContextListener schemaCtxListener = actualSchemaCtx::setSchemaContext;
86         final ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener =
87                 schemaService.registerSchemaContextListener(schemaCtxListener);
88         assertEquals(registerSchemaContextListener.getInstance(), schemaCtxListener);
89         assertNotNull(actualSchemaCtx.getSchemaContext());
90         assertEquals(baseSchemaCtx, actualSchemaCtx.getSchemaContext());
91
92         addYang("/empty-test1.yang");
93         addYangs(schemaService);
94
95         final EffectiveModelContext nextSchemaCtx = schemaService.getGlobalContext();
96         assertNotNull(nextSchemaCtx);
97         assertTrue(nextSchemaCtx.getModules().size() == 2);
98
99         assertNotEquals(baseSchemaCtx, nextSchemaCtx);
100
101         schemaService.notifyListeners(nextSchemaCtx);
102         assertEquals(nextSchemaCtx, actualSchemaCtx.getSchemaContext());
103
104         addYang("/empty-test2.yang");
105         addYangs(schemaService);
106
107         final EffectiveModelContext unregistredListenerSchemaCtx = schemaService.getGlobalContext();
108         assertNotNull(unregistredListenerSchemaCtx);
109         assertTrue(unregistredListenerSchemaCtx.getModules().size() == 3);
110
111         assertNotEquals(baseSchemaCtx, unregistredListenerSchemaCtx);
112         assertNotEquals(nextSchemaCtx, unregistredListenerSchemaCtx);
113
114         registerSchemaContextListener.close();
115         schemaService.notifyListeners(unregistredListenerSchemaCtx);
116
117         assertNotEquals(unregistredListenerSchemaCtx, actualSchemaCtx.getSchemaContext());
118         assertEquals(nextSchemaCtx, actualSchemaCtx.getSchemaContext());
119
120         schemaService.registerSchemaContextListener(schemaCtxListener);
121         assertEquals(unregistredListenerSchemaCtx, actualSchemaCtx.getSchemaContext());
122     }
123
124     @Test
125     public void tryToUpdateSchemaCtxTest() {
126         final SchemaContext baseSchemaContext = schemaService.getSchemaContext();
127         assertNotNull(baseSchemaContext);
128         assertTrue(baseSchemaContext.getModules().size() == 1);
129
130         final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
131         schemaService.registerSchemaContextListener(actualSchemaCtx::setSchemaContext);
132
133         assertEquals(baseSchemaContext, actualSchemaCtx.getSchemaContext());
134
135         addYang("/empty-test1.yang");
136         addYangs(schemaService);
137
138         final SchemaContext nextSchemaContext = schemaService.getSchemaContext();
139         assertNotNull(baseSchemaContext);
140         assertTrue(baseSchemaContext.getModules().size() == 1);
141
142         assertNotEquals(baseSchemaContext, nextSchemaContext);
143
144         schemaService.tryToUpdateSchemaContext();
145         assertEquals(nextSchemaContext, actualSchemaCtx.getSchemaContext());
146     }
147
148     @Test
149     public void getSourceTest() throws Exception {
150         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("odl-datastore-test",
151             Revision.of("2014-03-13"));
152         final YangTextSchemaSource yangTextSchemaSource = schemaService.getSource(sourceIdentifier).get();
153         final Collection<String> lines = IOUtil.readLines(yangTextSchemaSource.openStream());
154         assertEquals("module odl-datastore-test {", lines.iterator().next());
155     }
156
157     @Test
158     public void getSupportedExtensionsTest() {
159         assertEquals(schemaService.getExtensions().values().iterator().next(), schemaService);
160     }
161
162     @Test(expected = UnsupportedOperationException.class)
163     public void getSessionContextTest() {
164         schemaService.getSessionContext();
165     }
166
167     private void addYangs(final ScanningSchemaServiceProvider service) {
168         final List<Registration> registerAvailableYangs = service.registerAvailableYangs(yangs);
169         assertTrue(!registerAvailableYangs.isEmpty());
170     }
171
172     private class SchemaContextHolder {
173
174         private EffectiveModelContext schemaCtx;
175
176         public void setSchemaContext(final EffectiveModelContext ctx) {
177             schemaCtx = ctx;
178         }
179
180         public EffectiveModelContext getSchemaContext() {
181             return schemaCtx;
182         }
183     }
184 }