2 * Copyright (c) 2016 Cisco Systems, Inc. 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.netconf.mdsal.yang.library;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import com.google.common.collect.ImmutableList;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.mdsal.common.api.CommitInfo;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesStateBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionUtils;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.CommonLeafs.Revision;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.Submodule;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.SubmoduleBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 public class SchemaServiceToMdsalWriterTest {
47 private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
48 InstanceIdentifier.create(ModulesState.class);
51 private DOMSchemaService schemaService;
53 private DataBroker dataBroker;
55 private WriteTransaction writeTransaction;
57 private CommitInfo info;
59 private SchemaServiceToMdsalWriter schemaServiceToMdsalWriter;
63 MockitoAnnotations.initMocks(this);
65 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
66 doNothing().when(writeTransaction).put(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
67 doReturn(FluentFutures.immediateFluentFuture(info)).when(writeTransaction).commit();
68 when(schemaService.registerSchemaContextListener(any())).thenReturn(
69 new ListenerRegistration<SchemaContextListener>() {
76 public SchemaContextListener getInstance() {
80 schemaServiceToMdsalWriter = new SchemaServiceToMdsalWriter(schemaService, dataBroker);
84 public void testOnGlobalContextUpdated() throws Exception {
85 schemaServiceToMdsalWriter.start();
87 schemaServiceToMdsalWriter.onGlobalContextUpdated(getSchema());
88 verify(writeTransaction).put(eq(LogicalDatastoreType.OPERATIONAL),
89 eq(MODULES_STATE_INSTANCE_IDENTIFIER), eq(createTestModuleState()));
92 private static SchemaContext getSchema() {
93 return YangParserTestUtils.parseYangResources(SchemaServiceToMdsalWriterTest.class, "/test-module.yang",
94 "/test-submodule.yang");
97 private static ModulesState createTestModuleState() {
98 Submodule sub = new SubmoduleBuilder().setName(new YangIdentifier("test-submodule"))
99 .setRevision(RevisionUtils.emptyRevision())
102 Module module = new ModuleBuilder().setName(new YangIdentifier("test-module"))
103 .setNamespace(new Uri("test:namespace"))
104 .setRevision(new Revision(new RevisionIdentifier("2013-07-22")))
105 .setSubmodule(ImmutableList.of(sub))
106 .setConformanceType(Module.ConformanceType.Implement)
108 return new ModulesStateBuilder().setModuleSetId("0")
109 .setModule(ImmutableList.of(module)).build();