2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker.schema;
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;
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;
33 public class ScanningSchemaServiceProviderTest {
35 private ArrayList<URL> yangs;
36 private ScanningSchemaServiceProvider schemaService;
40 yangs = new ArrayList<>();
41 addYang("/odl-datastore-test.yang");
43 schemaService = new ScanningSchemaServiceProvider();
44 assertNotNull(schemaService);
45 addYangs(schemaService);
50 schemaService.close();
53 private void addYang(final String yang) {
54 yangs.add(ScanningSchemaServiceProvider.class.getResource(yang));
58 public void initJarScanningSchemaServiceTest() throws Exception {
59 assertNotNull(schemaService.getGlobalContext());
60 assertNotNull(schemaService.getSchemaContext());
61 assertEquals(schemaService.getGlobalContext(), schemaService.getSchemaContext());
65 public void listenersTests() {
66 assertFalse(schemaService.hasListeners());
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());
77 public void notifyListenersTest() {
78 final SchemaContext baseSchemaCtx = schemaService.getGlobalContext();
79 assertNotNull(baseSchemaCtx);
80 assertTrue(baseSchemaCtx.getModules().size() == 1);
82 final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
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());
91 addYang("/empty-test1.yang");
92 addYangs(schemaService);
94 final SchemaContext nextSchemaCtx = schemaService.getGlobalContext();
95 assertNotNull(nextSchemaCtx);
96 assertTrue(nextSchemaCtx.getModules().size() == 2);
98 assertNotEquals(baseSchemaCtx, nextSchemaCtx);
100 schemaService.notifyListeners(nextSchemaCtx);
101 assertEquals(nextSchemaCtx, actualSchemaCtx.getSchemaContext());
103 addYang("/empty-test2.yang");
104 addYangs(schemaService);
106 final SchemaContext unregistredListenerSchemaCtx = schemaService.getGlobalContext();
107 assertNotNull(unregistredListenerSchemaCtx);
108 assertTrue(unregistredListenerSchemaCtx.getModules().size() == 3);
110 assertNotEquals(baseSchemaCtx, unregistredListenerSchemaCtx);
111 assertNotEquals(nextSchemaCtx, unregistredListenerSchemaCtx);
113 registerSchemaContextListener.close();
114 schemaService.notifyListeners(unregistredListenerSchemaCtx);
116 assertNotEquals(unregistredListenerSchemaCtx, actualSchemaCtx.getSchemaContext());
117 assertEquals(nextSchemaCtx, actualSchemaCtx.getSchemaContext());
119 schemaService.registerSchemaContextListener(schemaCtxListener);
120 assertEquals(unregistredListenerSchemaCtx, actualSchemaCtx.getSchemaContext());
124 public void tryToUpdateSchemaCtxTest() {
125 final SchemaContext baseSchemaContext = schemaService.getSchemaContext();
126 assertNotNull(baseSchemaContext);
127 assertTrue(baseSchemaContext.getModules().size() == 1);
129 final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
130 schemaService.registerSchemaContextListener(actualSchemaCtx::setSchemaContext);
132 assertEquals(baseSchemaContext, actualSchemaCtx.getSchemaContext());
134 addYang("/empty-test1.yang");
135 addYangs(schemaService);
137 final SchemaContext nextSchemaContext = schemaService.getSchemaContext();
138 assertNotNull(baseSchemaContext);
139 assertTrue(baseSchemaContext.getModules().size() == 1);
141 assertNotEquals(baseSchemaContext, nextSchemaContext);
143 schemaService.tryToUpdateSchemaContext();
144 assertEquals(nextSchemaContext, actualSchemaCtx.getSchemaContext());
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());
157 public void getSupportedExtensionsTest() {
158 assertEquals(schemaService.getExtensions().values().iterator().next(), schemaService);
161 @Test(expected = UnsupportedOperationException.class)
162 public void getSessionContextTest() {
163 schemaService.getSessionContext();
166 private void addYangs(final ScanningSchemaServiceProvider service) {
167 final List<Registration> registerAvailableYangs = service.registerAvailableYangs(yangs);
168 assertTrue(!registerAvailableYangs.isEmpty());
171 private class SchemaContextHolder {
173 private SchemaContext schemaCtx;
175 public void setSchemaContext(final SchemaContext ctx) {
179 public SchemaContext getSchemaContext() {