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.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;
34 public class ScanningSchemaServiceProviderTest {
36 private ArrayList<URL> yangs;
37 private ScanningSchemaServiceProvider schemaService;
41 yangs = new ArrayList<>();
42 addYang("/odl-datastore-test.yang");
44 schemaService = new ScanningSchemaServiceProvider();
45 assertNotNull(schemaService);
46 addYangs(schemaService);
51 schemaService.close();
54 private void addYang(final String yang) {
55 yangs.add(ScanningSchemaServiceProvider.class.getResource(yang));
59 public void initJarScanningSchemaServiceTest() throws Exception {
60 assertNotNull(schemaService.getGlobalContext());
61 assertNotNull(schemaService.getSchemaContext());
62 assertEquals(schemaService.getGlobalContext(), schemaService.getSchemaContext());
66 public void listenersTests() {
67 assertFalse(schemaService.hasListeners());
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());
78 public void notifyListenersTest() {
79 final EffectiveModelContext baseSchemaCtx = schemaService.getGlobalContext();
80 assertNotNull(baseSchemaCtx);
81 assertTrue(baseSchemaCtx.getModules().size() == 1);
83 final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
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());
92 addYang("/empty-test1.yang");
93 addYangs(schemaService);
95 final EffectiveModelContext nextSchemaCtx = schemaService.getGlobalContext();
96 assertNotNull(nextSchemaCtx);
97 assertTrue(nextSchemaCtx.getModules().size() == 2);
99 assertNotEquals(baseSchemaCtx, nextSchemaCtx);
101 schemaService.notifyListeners(nextSchemaCtx);
102 assertEquals(nextSchemaCtx, actualSchemaCtx.getSchemaContext());
104 addYang("/empty-test2.yang");
105 addYangs(schemaService);
107 final EffectiveModelContext unregistredListenerSchemaCtx = schemaService.getGlobalContext();
108 assertNotNull(unregistredListenerSchemaCtx);
109 assertTrue(unregistredListenerSchemaCtx.getModules().size() == 3);
111 assertNotEquals(baseSchemaCtx, unregistredListenerSchemaCtx);
112 assertNotEquals(nextSchemaCtx, unregistredListenerSchemaCtx);
114 registerSchemaContextListener.close();
115 schemaService.notifyListeners(unregistredListenerSchemaCtx);
117 assertNotEquals(unregistredListenerSchemaCtx, actualSchemaCtx.getSchemaContext());
118 assertEquals(nextSchemaCtx, actualSchemaCtx.getSchemaContext());
120 schemaService.registerSchemaContextListener(schemaCtxListener);
121 assertEquals(unregistredListenerSchemaCtx, actualSchemaCtx.getSchemaContext());
125 public void tryToUpdateSchemaCtxTest() {
126 final SchemaContext baseSchemaContext = schemaService.getSchemaContext();
127 assertNotNull(baseSchemaContext);
128 assertTrue(baseSchemaContext.getModules().size() == 1);
130 final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
131 schemaService.registerSchemaContextListener(actualSchemaCtx::setSchemaContext);
133 assertEquals(baseSchemaContext, actualSchemaCtx.getSchemaContext());
135 addYang("/empty-test1.yang");
136 addYangs(schemaService);
138 final SchemaContext nextSchemaContext = schemaService.getSchemaContext();
139 assertNotNull(baseSchemaContext);
140 assertTrue(baseSchemaContext.getModules().size() == 1);
142 assertNotEquals(baseSchemaContext, nextSchemaContext);
144 schemaService.tryToUpdateSchemaContext();
145 assertEquals(nextSchemaContext, actualSchemaCtx.getSchemaContext());
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());
158 public void getSupportedExtensionsTest() {
159 assertEquals(schemaService.getExtensions().values().iterator().next(), schemaService);
162 @Test(expected = UnsupportedOperationException.class)
163 public void getSessionContextTest() {
164 schemaService.getSessionContext();
167 private void addYangs(final ScanningSchemaServiceProvider service) {
168 final List<Registration> registerAvailableYangs = service.registerAvailableYangs(yangs);
169 assertTrue(!registerAvailableYangs.isEmpty());
172 private class SchemaContextHolder {
174 private EffectiveModelContext schemaCtx;
176 public void setSchemaContext(final EffectiveModelContext ctx) {
180 public EffectiveModelContext getSchemaContext() {