Fix odlparent-3.0.0 checkstyle issues
[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 com.google.common.util.concurrent.CheckedFuture;
17 import java.net.URL;
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;
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 SchemaContextListener listener = prepareSchemaCtxListener(actualSchemaCtx);
71         final ListenerRegistration<SchemaContextListener> 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 SchemaContext baseSchemaCtx = schemaService.getGlobalContext();
80         assertNotNull(baseSchemaCtx);
81         assertTrue(baseSchemaCtx.getModules().size() == 1);
82
83         final SchemaContextHolder actualSchemaCtx = new SchemaContextHolder();
84
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());
91
92         addYang("/empty-test1.yang");
93         addYangs(schemaService);
94
95         final SchemaContext 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 SchemaContext unregistredListenerSchemaCtx = schemaService.getGlobalContext();
108         assertNotNull(unregistredListenerSchemaCtx);
109         assertTrue(unregistredListenerSchemaCtx.getModules().size() == 3);
110
111         assertNotEquals(baseSchemaCtx, unregistredListenerSchemaCtx);
112         assertNotEquals(nextSchemaCtx, unregistredListenerSchemaCtx);
113
114         schemaService.removeListener(schemaCtxListener);
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         final SchemaContextListener schemaCtxListener = prepareSchemaCtxListener(actualSchemaCtx);
132
133         schemaService.registerSchemaContextListener(schemaCtxListener);
134
135         assertEquals(baseSchemaContext, actualSchemaCtx.getSchemaContext());
136
137         addYang("/empty-test1.yang");
138         addYangs(schemaService);
139
140         final SchemaContext nextSchemaContext = schemaService.getSchemaContext();
141         assertNotNull(baseSchemaContext);
142         assertTrue(baseSchemaContext.getModules().size() == 1);
143
144         assertNotEquals(baseSchemaContext, nextSchemaContext);
145
146         schemaService.tryToUpdateSchemaContext();
147         assertEquals(nextSchemaContext, actualSchemaCtx.getSchemaContext());
148     }
149
150     @SuppressWarnings("deprecation")
151     @Test
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());
159     }
160
161     @Test
162     public void getSupportedExtensionsTest() {
163         assertEquals(schemaService.getSupportedExtensions().values().iterator().next(), schemaService);
164     }
165
166     @Test(expected = UnsupportedOperationException.class)
167     public void getSessionContextTest() {
168         schemaService.getSessionContext();
169     }
170
171     private void addYangs(final ScanningSchemaServiceProvider service) {
172         final List<Registration> registerAvailableYangs = service.registerAvailableYangs(yangs);
173         assertTrue(!registerAvailableYangs.isEmpty());
174     }
175
176     private SchemaContextListener prepareSchemaCtxListener(final SchemaContextHolder actualSchemaCtx) {
177         return new SchemaContextListener() {
178
179             @Override
180             public void onGlobalContextUpdated(final SchemaContext context) {
181                 actualSchemaCtx.setSchemaContext(context);
182             }
183         };
184     }
185
186     private class SchemaContextHolder {
187
188         private SchemaContext schemaCtx;
189
190         public void setSchemaContext(final SchemaContext ctx) {
191             schemaCtx = ctx;
192         }
193
194         public SchemaContext getSchemaContext() {
195             return schemaCtx;
196         }
197     }
198 }