Refactor YangLibProviderTest
[netconf.git] / netconf / yanglib / src / test / java / org / opendaylight / yanglib / impl / YangLibProviderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.yanglib.impl;
9
10 import static org.mockito.ArgumentMatchers.eq;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.verifyNoMoreInteractions;
14 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.apache.commons.io.FileUtils;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.opendaylight.mdsal.binding.api.DataBroker;
32 import org.opendaylight.mdsal.binding.api.WriteTransaction;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.Uint32;
48 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
49 import org.opendaylight.yangtools.yang.model.repo.api.YangIRSchemaSource;
50 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
51 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
52 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
53 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
54 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
55
56 @RunWith(MockitoJUnitRunner.StrictStubs.class)
57 public class YangLibProviderTest {
58     private static final File CACHE_DIR = new File("target/yanglib");
59
60     @Mock
61     private DataBroker dataBroker;
62
63     @Mock
64     private WriteTransaction writeTransaction;
65
66     private YangLibProvider yangLibProvider;
67
68     @BeforeClass
69     public static void staticSetup() {
70         if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
71             throw new RuntimeException("Failed to create " + CACHE_DIR);
72         }
73     }
74
75     @AfterClass
76     public static void staticCleanup() {
77         FileUtils.deleteQuietly(CACHE_DIR);
78     }
79
80     @Before
81     public void setUp() {
82         try {
83             if (CACHE_DIR.exists()) {
84                 FileUtils.cleanDirectory(CACHE_DIR);
85             }
86         } catch (IOException e) {
87             // Ignore
88         }
89
90         doReturn(emptyFluentFuture()).when(writeTransaction).commit();
91         doReturn(writeTransaction).when(dataBroker).newWriteOnlyTransaction();
92
93         final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
94                 .setBindingPort(Uint32.valueOf(300)).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
95         yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new DefaultYangParserFactory());
96     }
97
98     @Test
99     public void testSchemaSourceRegistered() {
100         yangLibProvider.init();
101
102         List<PotentialSchemaSource<?>> list = new ArrayList<>();
103         list.add(
104                 PotentialSchemaSource.create(new SourceIdentifier("no-revision"),
105                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
106
107         list.add(
108                 PotentialSchemaSource.create(new SourceIdentifier("with-revision", "2016-04-28"),
109                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
110
111         yangLibProvider.schemaSourceRegistered(list);
112
113         Map<ModuleKey, Module> newModulesList = new HashMap<>();
114
115         Module newModule = new ModuleBuilder()
116                 .setName(new YangIdentifier("no-revision"))
117                 .setRevision(LegacyRevisionUtils.emptyRevision())
118                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
119                 .build();
120
121         newModulesList.put(newModule.key(), newModule);
122
123         newModule = new ModuleBuilder()
124                 .setName(new YangIdentifier("with-revision"))
125                 .setRevision(new Revision(new RevisionIdentifier("2016-04-28")))
126                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
127                 .build();
128
129         newModulesList.put(newModule.key(), newModule);
130
131         verify(dataBroker).newWriteOnlyTransaction();
132         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
133                 eq(InstanceIdentifier.create(ModulesState.class)),
134                 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
135         verify(writeTransaction).commit();
136     }
137
138     @Test
139     public void testFilteringEmptySchemaSourceRegistered() {
140         yangLibProvider.init();
141
142         // test empty list of schema sources registered
143         yangLibProvider.schemaSourceRegistered(Collections.emptyList());
144         // expected behavior is to do nothing
145         verifyNoMoreInteractions(dataBroker, writeTransaction);
146     }
147
148     @Test
149     public void testFilteringNonYangSchemaSourceRegistered() {
150         yangLibProvider.init();
151
152         // test list of non yang schema sources registered
153         final var nonYangSources = new ArrayList<PotentialSchemaSource<?>>();
154         nonYangSources.add(PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
155             YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue()));
156         nonYangSources.add(PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
157             YangIRSchemaSource.class, Costs.IMMEDIATE.getValue()));
158         yangLibProvider.schemaSourceRegistered(nonYangSources);
159
160         // expected behavior is to do nothing
161         verifyNoMoreInteractions(dataBroker, writeTransaction);
162     }
163
164     @Test
165     public void testSchemaSourceWithRevisionUnregistered() {
166         yangLibProvider.init();
167
168         // try to unregister YANG source with revision
169         final var schemaSourceWithRevision = PotentialSchemaSource.create(
170             new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
171             YangTextSchemaSource.class, Costs.LOCAL_IO.getValue());
172         yangLibProvider.schemaSourceUnregistered(schemaSourceWithRevision);
173
174         // source is unregistered
175         verify(dataBroker).newWriteOnlyTransaction();
176         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
177             eq(InstanceIdentifier.create(ModulesState.class)
178                 .child(Module.class,
179                     new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
180                         new Revision(new RevisionIdentifier("2016-04-28"))))));
181         verify(writeTransaction).commit();
182     }
183
184     @Test
185     public void testSchemaSourceWithoutRevisionUnregistered() {
186         yangLibProvider.init();
187
188         // try to unregister YANG source without revision
189         final var schemaSourceWithoutRevision = PotentialSchemaSource.create(
190             new SourceIdentifier("unregistered-yang-schema-without-revision"), YangTextSchemaSource.class,
191             Costs.LOCAL_IO.getValue());
192         yangLibProvider.schemaSourceUnregistered(schemaSourceWithoutRevision);
193
194         // source is unregistered
195         verify(dataBroker).newWriteOnlyTransaction();
196         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
197             eq(InstanceIdentifier.create(ModulesState.class)
198                 .child(Module.class,
199                     new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
200                         LegacyRevisionUtils.emptyRevision()))));
201         verify(writeTransaction).commit();
202     }
203
204     @Test
205     public void testNonYangSchemaSourceUnregistered() {
206         yangLibProvider.init();
207
208         // try to unregister non-YANG source
209         final var nonYangSources = PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
210             YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue());
211         yangLibProvider.schemaSourceUnregistered(nonYangSources);
212
213         // expected behaviour is to do nothing if non yang based source is unregistered
214         verifyNoMoreInteractions(dataBroker, writeTransaction);
215     }
216 }