Bump upstreams
[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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.clearInvocations;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
17 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
18
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
42 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
43 import org.opendaylight.yangtools.yang.model.api.source.YinSourceRepresentation;
44 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
46 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
47 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
48
49 @RunWith(MockitoJUnitRunner.StrictStubs.class)
50 public class YangLibProviderTest {
51     @Mock
52     private DataBroker dataBroker;
53     @Mock
54     private WriteTransaction writeTransaction;
55
56     private YangLibProvider yangLibProvider;
57
58     @Before
59     public void setUp() {
60         doReturn(emptyFluentFuture()).when(writeTransaction).commit();
61         doReturn(writeTransaction).when(dataBroker).newWriteOnlyTransaction();
62
63         yangLibProvider = new YangLibProvider(dataBroker, new DefaultYangParserFactory(),
64             YangLibProviderTest.class.getResource("/model").getPath(), "www.fake.com", Uint32.valueOf(300));
65     }
66
67     @Test
68     public void testSchemaSourceRegistered() {
69         // test that initial models are registered
70         verify(dataBroker).newWriteOnlyTransaction();
71         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
72             eq(InstanceIdentifier.create(ModulesState.class)),
73             eq(new ModulesStateBuilder()
74                 .setModule(BindingMap.of(new ModuleBuilder()
75                     .setName(new YangIdentifier("model1"))
76                     .setRevision(new Revision(new RevisionIdentifier("2023-02-21")))
77                     .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/model1/2023-02-21"))
78                     .build(), new ModuleBuilder()
79                     .setName(new YangIdentifier("model2"))
80                     .setRevision(LegacyRevisionUtils.emptyRevision())
81                     .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/model2"))
82                     .build()))
83                 .build()));
84         verify(writeTransaction).commit();
85     }
86
87     @Test
88     public void testFilteringEmptySchemaSourceRegistered() {
89         clearInvocations(dataBroker, writeTransaction);
90
91         // test empty list of schema sources registered
92         yangLibProvider.schemaSourceRegistered(List.of());
93         // expected behavior is to do nothing
94         verifyNoMoreInteractions(dataBroker, writeTransaction);
95     }
96
97     @Test
98     public void testFilteringNonYangSchemaSourceRegistered() {
99         clearInvocations(dataBroker, writeTransaction);
100
101         // test list of non yang schema sources registered
102         yangLibProvider.schemaSourceRegistered(List.of(
103             PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
104                 YinSourceRepresentation.class, Costs.IMMEDIATE.getValue()),
105             PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
106                 YangIRSource.class, Costs.IMMEDIATE.getValue())));
107
108         // expected behavior is to do nothing
109         verifyNoMoreInteractions(dataBroker, writeTransaction);
110     }
111
112     @Test
113     public void testSchemaSourceWithRevisionUnregistered() {
114         clearInvocations(dataBroker, writeTransaction);
115
116         // try to unregister YANG source with revision
117         final var schemaSourceWithRevision = PotentialSchemaSource.create(
118             new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
119             YangTextSource.class, Costs.LOCAL_IO.getValue());
120         yangLibProvider.schemaSourceUnregistered(schemaSourceWithRevision);
121
122         // source is unregistered
123         verify(dataBroker).newWriteOnlyTransaction();
124         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
125             eq(InstanceIdentifier.create(ModulesState.class)
126                 .child(Module.class,
127                     new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
128                         new Revision(new RevisionIdentifier("2016-04-28"))))));
129         verify(writeTransaction).commit();
130     }
131
132     @Test
133     public void testSchemaSourceWithoutRevisionUnregistered() {
134         clearInvocations(dataBroker, writeTransaction);
135
136         // try to unregister YANG source without revision
137         final var schemaSourceWithoutRevision = PotentialSchemaSource.create(
138             new SourceIdentifier("unregistered-yang-schema-without-revision"), YangTextSource.class,
139             Costs.LOCAL_IO.getValue());
140         yangLibProvider.schemaSourceUnregistered(schemaSourceWithoutRevision);
141
142         // source is unregistered
143         verify(dataBroker).newWriteOnlyTransaction();
144         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
145             eq(InstanceIdentifier.create(ModulesState.class)
146                 .child(Module.class,
147                     new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
148                         LegacyRevisionUtils.emptyRevision()))));
149         verify(writeTransaction).commit();
150     }
151
152     @Test
153     public void testNonYangSchemaSourceUnregistered() {
154         clearInvocations(dataBroker, writeTransaction);
155
156         // try to unregister non-YANG source
157         final var nonYangSources = PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
158             YinSourceRepresentation.class, Costs.IMMEDIATE.getValue());
159         yangLibProvider.schemaSourceUnregistered(nonYangSources);
160
161         // expected behaviour is to do nothing if non yang based source is unregistered
162         verifyNoMoreInteractions(dataBroker, writeTransaction);
163     }
164
165     @Test
166     public void testGetSchemaWithRevision() {
167         final var modelWithRevision = yangLibProvider.getSchema("model1", "2023-02-21");
168         assertNotNull(modelWithRevision);
169         assertEquals("""
170             module model1 {
171               namespace "model:with:revision";
172               prefix mwr;
173
174               revision 2023-02-21 {
175                 description
176                   "Initial revision;";
177               }
178
179               container test {
180                 leaf test-leaf {
181                   type string;
182                 }
183               }
184             }
185             """, modelWithRevision);
186     }
187
188     @Test
189     public void testGetSchemaWithoutRevision() {
190         final var modelWithoutRevision = yangLibProvider.getSchema("model2");
191         assertNotNull(modelWithoutRevision);
192         assertEquals("""
193             module model2 {
194               namespace "model:with:no:revision";
195               prefix mwnr;
196
197               container test {
198                 leaf test-leaf {
199                   type string;
200                 }
201               }
202             }
203             """, modelWithoutRevision);
204     }
205 }