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