2 * Copyright (c) 2018 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.data.impl;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.when;
23 import static org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration.DEFAULT_CONFIGURATION;
25 import com.google.common.io.ByteSource;
26 import com.google.common.util.concurrent.Futures;
27 import io.fd.honeycomb.data.ReadableDataManager;
28 import java.io.StringWriter;
29 import javax.xml.transform.OutputKeys;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34 import org.custommonkey.xmlunit.DetailedDiff;
35 import org.custommonkey.xmlunit.Diff;
36 import org.custommonkey.xmlunit.XMLUnit;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
42 import org.opendaylight.netconf.api.DocumentedException;
43 import org.opendaylight.netconf.api.xml.XmlUtil;
44 import org.opendaylight.netconf.mapping.api.NetconfOperation;
45 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
46 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
47 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
48 import org.opendaylight.netconf.mdsal.connector.ops.Commit;
49 import org.opendaylight.netconf.mdsal.connector.ops.EditConfig;
50 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
51 import org.opendaylight.netconf.util.test.XmlFileLoader;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
54 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
57 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
58 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61 import org.w3c.dom.Document;
63 public class EditConfigTest {
64 private static final Logger LOG = LoggerFactory.getLogger(EditConfigTest.class);
65 private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session";
66 private static final Document RPC_REPLY_OK = getReplyOk();
69 private ReadableDataManager operationalData;
71 private DOMSchemaService schemaService;
73 private CurrentSchemaContext currentSchemaContext;
74 private DataBroker dataBroker;
77 public void setUp() throws Exception {
78 MockitoAnnotations.initMocks(this);
80 final SchemaContext schemaContext =
81 YangParserTestUtils.parseYangResources(EditConfigTest.class, "/models/test-edit-config.yang");
82 when(schemaService.registerSchemaContextListener(any())).thenAnswer(invocation -> {
83 SchemaContextListener listener = invocation.getArgument(0);
84 listener.onGlobalContextUpdated(schemaContext);
85 return new ListenerRegistration<SchemaContextListener>() {
91 public SchemaContextListener getInstance() {
96 currentSchemaContext = new CurrentSchemaContext(schemaService, sourceIdentifier -> {
97 final YangTextSchemaSource yangTextSchemaSource =
98 YangTextSchemaSource.delegateForByteSource(sourceIdentifier, ByteSource.wrap("module test".getBytes()));
99 return Futures.immediateCheckedFuture(yangTextSchemaSource);
102 final DataTree dataTree = new InMemoryDataTreeFactory().create(DEFAULT_CONFIGURATION);
103 dataTree.setSchemaContext(schemaContext);
104 dataBroker = DataBroker.create(new ModifiableDataTreeManager(dataTree), operationalData);
105 XMLUnit.setIgnoreWhitespace(true);
106 XMLUnit.setIgnoreAttributeOrder(true);
110 public void testMissingMandatoryNode() throws Exception {
111 final TransactionProvider transactionProvider = new TransactionProvider(dataBroker, SESSION_ID_FOR_REPORTING);
112 verifyResponse(edit("messages/edit-config/edit-config-missing-mandatory-node.xml", transactionProvider));
114 verifyResponse(commit(transactionProvider));
115 fail("Should have failed due to missing mandatory node");
116 } catch (DocumentedException e) {
117 assertTrue(e.getMessage().contains("missing mandatory descendant"));
121 private static Document getReplyOk() {
124 doc = XmlFileLoader.xmlFileToDocument("messages/rpc-reply_ok.xml");
125 } catch (final Exception e) {
126 LOG.debug("unable to load rpc reply ok.", e);
127 doc = XmlUtil.newDocument();
132 private static void verifyResponse(final Document actual) throws Exception {
133 final DetailedDiff dd = new DetailedDiff(new Diff(RPC_REPLY_OK, actual));
134 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
136 LOG.warn("Actual response:");
137 printDocument(actual);
138 LOG.warn("Expected response:");
139 printDocument(RPC_REPLY_OK);
140 fail("Differences found: " + dd.toString());
144 private static void printDocument(final Document doc) throws Exception {
145 final TransformerFactory tf = TransformerFactory.newInstance();
146 final Transformer transformer = tf.newTransformer();
147 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
148 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
149 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
150 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
151 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
153 final StringWriter writer = new StringWriter();
154 transformer.transform(new DOMSource(doc), new StreamResult(writer));
155 LOG.warn(writer.getBuffer().toString());
158 private Document edit(final String resource, final TransactionProvider transactionProvider) throws Exception {
159 final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
160 transactionProvider);
161 return executeOperation(editConfig, resource);
164 private static Document commit(final TransactionProvider transactionProvider) throws Exception {
165 final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
166 return executeOperation(commit, "messages/commit.xml");
169 private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
170 final Document request = XmlFileLoader.xmlFileToDocument(filename);
171 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
172 LOG.debug("Got response {}", response);