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;
16 import com.google.common.util.concurrent.CheckedFuture;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.internal.util.io.IOUtil;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.concepts.Registration;
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.SchemaSourceException;
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 SchemaContextListener listener = prepareSchemaCtxListener(actualSchemaCtx);
71 final ListenerRegistration<SchemaContextListener> registerSchemaContextListener =
72 schemaService.registerSchemaContextListener(listener);
73 assertEquals(registerSchemaContextListener.getInstance(), listener);
74 assertEquals(schemaService.getSchemaContext(), actualSchemaCtx.getSchemaContext());
78 public void notifyListenersTest() {
79 final SchemaContext baseSchemaCtx = schemaService.getGlobalContext();
80 assertNotNull(baseSchemaCtx);
81 assertTrue(baseSchemaCtx.getModules().size() == 1);
83 final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
85 final SchemaContextListener schemaCtxListener = prepareSchemaCtxListener(actualSchemaCtx);
86 final ListenerRegistration<SchemaContextListener> 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 SchemaContext 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 SchemaContext unregistredListenerSchemaCtx = schemaService.getGlobalContext();
108 assertNotNull(unregistredListenerSchemaCtx);
109 assertTrue(unregistredListenerSchemaCtx.getModules().size() == 3);
111 assertNotEquals(baseSchemaCtx, unregistredListenerSchemaCtx);
112 assertNotEquals(nextSchemaCtx, unregistredListenerSchemaCtx);
114 schemaService.removeListener(schemaCtxListener);
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 final SchemaContextListener schemaCtxListener = prepareSchemaCtxListener(actualSchemaCtx);
133 schemaService.registerSchemaContextListener(schemaCtxListener);
135 assertEquals(baseSchemaContext, actualSchemaCtx.getSchemaContext());
137 addYang("/empty-test1.yang");
138 addYangs(schemaService);
140 final SchemaContext nextSchemaContext = schemaService.getSchemaContext();
141 assertNotNull(baseSchemaContext);
142 assertTrue(baseSchemaContext.getModules().size() == 1);
144 assertNotEquals(baseSchemaContext, nextSchemaContext);
146 schemaService.tryToUpdateSchemaContext();
147 assertEquals(nextSchemaContext, actualSchemaCtx.getSchemaContext());
150 @SuppressWarnings("deprecation")
152 public void getSourceTest() throws Exception {
153 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("odl-datastore-test", "2014-03-13");
154 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source =
155 schemaService.getSource(sourceIdentifier);
156 final YangTextSchemaSource yangTextSchemaSource = source.checkedGet();
157 final Collection<String> lines = IOUtil.readLines(yangTextSchemaSource.openStream());
158 assertEquals("module odl-datastore-test {", lines.iterator().next());
162 public void getSupportedExtensionsTest() {
163 assertEquals(schemaService.getSupportedExtensions().values().iterator().next(), schemaService);
166 @Test(expected = UnsupportedOperationException.class)
167 public void getSessionContextTest() {
168 schemaService.getSessionContext();
171 private void addYangs(final ScanningSchemaServiceProvider service) {
172 final List<Registration> registerAvailableYangs = service.registerAvailableYangs(yangs);
173 assertTrue(!registerAvailableYangs.isEmpty());
176 private SchemaContextListener prepareSchemaCtxListener(final SchemaContextHolder actualSchemaCtx) {
177 return new SchemaContextListener() {
180 public void onGlobalContextUpdated(final SchemaContext context) {
181 actualSchemaCtx.setSchemaContext(context);
186 private class SchemaContextHolder {
188 private SchemaContext schemaCtx;
190 public void setSchemaContext(final SchemaContext ctx) {
194 public SchemaContext getSchemaContext() {