From 13d25e9a60ac6393590795ec0b4d2bc390627497 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Thu, 9 Aug 2018 09:50:14 +0200 Subject: Rename TestValidate to EditConfigTest The test does not use RPC in contrast to ValidateTest from: https://gerrit.fd.io/r/#/c/14040/ Change-Id: I57da44256515cf10afbd86ef7d7b1607caa3f52e Signed-off-by: Marek Gradzki --- .../io/fd/honeycomb/data/impl/EditConfigTest.java | 175 +++++++++++++++++++++ .../io/fd/honeycomb/data/impl/TestValidate.java | 175 --------------------- .../edit-config-missing-mandatory-node.xml | 2 +- .../test/resources/models/test-edit-config.yang | 27 ++++ .../src/test/resources/models/test-validate.yang | 27 ---- 5 files changed, 203 insertions(+), 203 deletions(-) create mode 100644 infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/EditConfigTest.java delete mode 100644 infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/TestValidate.java create mode 100644 infra/it/it-test/src/test/resources/models/test-edit-config.yang delete mode 100644 infra/it/it-test/src/test/resources/models/test-validate.yang diff --git a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/EditConfigTest.java b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/EditConfigTest.java new file mode 100644 index 000000000..cae1e4de2 --- /dev/null +++ b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/EditConfigTest.java @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.data.impl; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration.DEFAULT_CONFIGURATION; + +import com.google.common.io.ByteSource; +import com.google.common.util.concurrent.Futures; +import io.fd.honeycomb.data.ReadableDataManager; +import java.io.StringWriter; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.custommonkey.xmlunit.DetailedDiff; +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.config.util.xml.DocumentedException; +import org.opendaylight.controller.config.util.xml.XmlUtil; +import org.opendaylight.controller.sal.core.api.model.SchemaService; +import org.opendaylight.netconf.mapping.api.NetconfOperation; +import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution; +import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext; +import org.opendaylight.netconf.mdsal.connector.TransactionProvider; +import org.opendaylight.netconf.mdsal.connector.ops.Commit; +import org.opendaylight.netconf.mdsal.connector.ops.EditConfig; +import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier; +import org.opendaylight.netconf.util.test.XmlFileLoader; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; +import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; + +public class EditConfigTest { + private static final Logger LOG = LoggerFactory.getLogger(EditConfigTest.class); + private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session"; + private static final Document RPC_REPLY_OK = getReplyOk(); + + @Mock + private ReadableDataManager operationalData; + @Mock + private SchemaService schemaService; + + private CurrentSchemaContext currentSchemaContext; + private DataBroker dataBroker; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + final SchemaContext schemaContext = + YangParserTestUtils.parseYangResources(EditConfigTest.class, "/models/test-edit-config.yang"); + when(schemaService.registerSchemaContextListener(any())).thenAnswer(invocation -> { + SchemaContextListener listener = invocation.getArgument(0); + listener.onGlobalContextUpdated(schemaContext); + return new ListenerRegistration() { + @Override + public void close() { + } + + @Override + public SchemaContextListener getInstance() { + return listener; + } + }; + }); + currentSchemaContext = new CurrentSchemaContext(schemaService, sourceIdentifier -> { + final YangTextSchemaSource yangTextSchemaSource = + YangTextSchemaSource.delegateForByteSource(sourceIdentifier, ByteSource.wrap("module test".getBytes())); + return Futures.immediateCheckedFuture(yangTextSchemaSource); + }); + + final DataTree dataTree = new InMemoryDataTreeFactory().create(DEFAULT_CONFIGURATION); + dataTree.setSchemaContext(schemaContext); + dataBroker = DataBroker.create(new ModifiableDataTreeManager(dataTree), operationalData); + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setIgnoreAttributeOrder(true); + } + + @Test + public void testMissingMandatoryNode() throws Exception { + final TransactionProvider transactionProvider = new TransactionProvider(dataBroker, SESSION_ID_FOR_REPORTING); + verifyResponse(edit("messages/edit-config/edit-config-missing-mandatory-node.xml", transactionProvider)); + try { + verifyResponse(commit(transactionProvider)); + fail("Should have failed due to missing mandatory node"); + } catch (DocumentedException e) { + assertTrue(e.getMessage().contains("missing mandatory descendant")); + } + } + + private static Document getReplyOk() { + Document doc; + try { + doc = XmlFileLoader.xmlFileToDocument("messages/rpc-reply_ok.xml"); + } catch (final Exception e) { + LOG.debug("unable to load rpc reply ok.", e); + doc = XmlUtil.newDocument(); + } + return doc; + } + + private static void verifyResponse(final Document actual) throws Exception { + final DetailedDiff dd = new DetailedDiff(new Diff(RPC_REPLY_OK, actual)); + dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier()); + if (!dd.similar()) { + LOG.warn("Actual response:"); + printDocument(actual); + LOG.warn("Expected response:"); + printDocument(RPC_REPLY_OK); + fail("Differences found: " + dd.toString()); + } + } + + private static void printDocument(final Document doc) throws Exception { + final TransformerFactory tf = TransformerFactory.newInstance(); + final Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + + final StringWriter writer = new StringWriter(); + transformer.transform(new DOMSource(doc), new StreamResult(writer)); + LOG.warn(writer.getBuffer().toString()); + } + + private Document edit(final String resource, final TransactionProvider transactionProvider) throws Exception { + final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, + transactionProvider); + return executeOperation(editConfig, resource); + } + + private static Document commit(final TransactionProvider transactionProvider) throws Exception { + final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider); + return executeOperation(commit, "messages/commit.xml"); + } + + private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception { + final Document request = XmlFileLoader.xmlFileToDocument(filename); + final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT); + LOG.debug("Got response {}", response); + return response; + } +} diff --git a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/TestValidate.java b/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/TestValidate.java deleted file mode 100644 index 5bc933f5e..000000000 --- a/infra/it/it-test/src/test/java/io/fd/honeycomb/data/impl/TestValidate.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright (c) 2018 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.data.impl; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; -import static org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration.DEFAULT_CONFIGURATION; - -import com.google.common.io.ByteSource; -import com.google.common.util.concurrent.Futures; -import io.fd.honeycomb.data.ReadableDataManager; -import java.io.StringWriter; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import org.custommonkey.xmlunit.DetailedDiff; -import org.custommonkey.xmlunit.Diff; -import org.custommonkey.xmlunit.XMLUnit; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.opendaylight.controller.config.util.xml.DocumentedException; -import org.opendaylight.controller.config.util.xml.XmlUtil; -import org.opendaylight.controller.sal.core.api.model.SchemaService; -import org.opendaylight.netconf.mapping.api.NetconfOperation; -import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution; -import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext; -import org.opendaylight.netconf.mdsal.connector.TransactionProvider; -import org.opendaylight.netconf.mdsal.connector.ops.Commit; -import org.opendaylight.netconf.mdsal.connector.ops.EditConfig; -import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier; -import org.opendaylight.netconf.util.test.XmlFileLoader; -import org.opendaylight.yangtools.concepts.ListenerRegistration; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; -import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; -import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; -import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; - -public class TestValidate { - private static final Logger LOG = LoggerFactory.getLogger(TestValidate.class); - private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session"; - private static final Document RPC_REPLY_OK = getReplyOk(); - - @Mock - private ReadableDataManager operationalData; - @Mock - private SchemaService schemaService; - - private CurrentSchemaContext currentSchemaContext; - private DataBroker dataBroker; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - - final SchemaContext schemaContext = - YangParserTestUtils.parseYangResources(TestValidate.class, "/models/test-validate.yang"); - when(schemaService.registerSchemaContextListener(any())).thenAnswer(invocation -> { - SchemaContextListener listener = invocation.getArgument(0); - listener.onGlobalContextUpdated(schemaContext); - return new ListenerRegistration() { - @Override - public void close() { - } - - @Override - public SchemaContextListener getInstance() { - return listener; - } - }; - }); - currentSchemaContext = new CurrentSchemaContext(schemaService, sourceIdentifier -> { - final YangTextSchemaSource yangTextSchemaSource = - YangTextSchemaSource.delegateForByteSource(sourceIdentifier, ByteSource.wrap("module test".getBytes())); - return Futures.immediateCheckedFuture(yangTextSchemaSource); - }); - - final DataTree dataTree = new InMemoryDataTreeFactory().create(DEFAULT_CONFIGURATION); - dataTree.setSchemaContext(schemaContext); - dataBroker = DataBroker.create(new ModifiableDataTreeManager(dataTree), operationalData); - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setIgnoreAttributeOrder(true); - } - - @Test - public void testValidateMissingMandatoryNode() throws Exception { - final TransactionProvider transactionProvider = new TransactionProvider(dataBroker, SESSION_ID_FOR_REPORTING); - verifyResponse(edit("messages/edit-config/edit-config-missing-mandatory-node.xml", transactionProvider)); - try { - verifyResponse(commit(transactionProvider)); - fail("Should have failed due to missing mandatory node"); - } catch (DocumentedException e) { - assertTrue(e.getMessage().contains("missing mandatory descendant")); - } - } - - private static Document getReplyOk() { - Document doc; - try { - doc = XmlFileLoader.xmlFileToDocument("messages/rpc-reply_ok.xml"); - } catch (final Exception e) { - LOG.debug("unable to load rpc reply ok.", e); - doc = XmlUtil.newDocument(); - } - return doc; - } - - private static void verifyResponse(final Document actual) throws Exception { - final DetailedDiff dd = new DetailedDiff(new Diff(RPC_REPLY_OK, actual)); - dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier()); - if (!dd.similar()) { - LOG.warn("Actual response:"); - printDocument(actual); - LOG.warn("Expected response:"); - printDocument(RPC_REPLY_OK); - fail("Differences found: " + dd.toString()); - } - } - - private static void printDocument(final Document doc) throws Exception { - final TransformerFactory tf = TransformerFactory.newInstance(); - final Transformer transformer = tf.newTransformer(); - transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); - transformer.setOutputProperty(OutputKeys.METHOD, "xml"); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); - - final StringWriter writer = new StringWriter(); - transformer.transform(new DOMSource(doc), new StreamResult(writer)); - LOG.warn(writer.getBuffer().toString()); - } - - private Document edit(final String resource, final TransactionProvider transactionProvider) throws Exception { - final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, - transactionProvider); - return executeOperation(editConfig, resource); - } - - private static Document commit(final TransactionProvider transactionProvider) throws Exception { - final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider); - return executeOperation(commit, "messages/commit.xml"); - } - - private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception { - final Document request = XmlFileLoader.xmlFileToDocument(filename); - final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT); - LOG.debug("Got response {}", response); - return response; - } -} diff --git a/infra/it/it-test/src/test/resources/messages/edit-config/edit-config-missing-mandatory-node.xml b/infra/it/it-test/src/test/resources/messages/edit-config/edit-config-missing-mandatory-node.xml index 829ef567a..6d3adfba7 100644 --- a/infra/it/it-test/src/test/resources/messages/edit-config/edit-config-missing-mandatory-node.xml +++ b/infra/it/it-test/src/test/resources/messages/edit-config/edit-config-missing-mandatory-node.xml @@ -21,7 +21,7 @@ none - + item1> diff --git a/infra/it/it-test/src/test/resources/models/test-edit-config.yang b/infra/it/it-test/src/test/resources/models/test-edit-config.yang new file mode 100644 index 000000000..aa810ec08 --- /dev/null +++ b/infra/it/it-test/src/test/resources/models/test-edit-config.yang @@ -0,0 +1,27 @@ +module test-edit-config { + yang-version 1; + namespace "urn:honeycomb:params:xml:ns:yang:test:edit:config"; + prefix "td"; + + revision "2018-06-08" { + description "Initial revision"; + } + + container top-container { + leaf name { + type string; + } + list list-in-container { + key "name"; + + leaf name { + type string; + } + + leaf description { + type string; + mandatory true; + } + } + } +} diff --git a/infra/it/it-test/src/test/resources/models/test-validate.yang b/infra/it/it-test/src/test/resources/models/test-validate.yang deleted file mode 100644 index 144045661..000000000 --- a/infra/it/it-test/src/test/resources/models/test-validate.yang +++ /dev/null @@ -1,27 +0,0 @@ -module test-validate { - yang-version 1; - namespace "urn:honeycomb:params:xml:ns:yang:test:validate"; - prefix "td"; - - revision "2018-06-08" { - description "Initial revision"; - } - - container top-container { - leaf name { - type string; - } - list list-in-container { - key "name"; - - leaf name { - type string; - } - - leaf description { - type string; - mandatory true; - } - } - } -} -- cgit 1.2.3-korg