X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Frestconf%2Futils%2Fmapping%2FRestconfMappingNodeUtilTest.java;h=f650f85a567426cce83910cff4a9b8b03fec7ebc;hb=dca9aa60cd4584298c414ff2722dff4f406fe0de;hp=c078a8468fb44a948007c4e1057fcc1be9e41ace;hpb=266c09557b7cc6d97879e1c7efa7a7e0503c855a;p=netconf.git diff --git a/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/utils/mapping/RestconfMappingNodeUtilTest.java b/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/utils/mapping/RestconfMappingNodeUtilTest.java index c078a8468f..f650f85a56 100644 --- a/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/utils/mapping/RestconfMappingNodeUtilTest.java +++ b/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/utils/mapping/RestconfMappingNodeUtilTest.java @@ -8,22 +8,16 @@ package org.opendaylight.restconf.utils.mapping; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.google.common.collect.Sets; -import java.util.AbstractMap; +import java.net.URI; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; +import java.util.Date; import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import org.junit.Assert; import org.junit.Before; @@ -34,15 +28,11 @@ import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; -import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException; -import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag; -import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType; -import org.opendaylight.restconf.Draft18; import org.opendaylight.restconf.Draft18.IetfYangLibrary; import org.opendaylight.restconf.Draft18.MonitoringModule; import org.opendaylight.restconf.Draft18.MonitoringModule.QueryParams; import org.opendaylight.restconf.Draft18.RestconfModule; -import org.opendaylight.restconf.utils.schema.context.RestconfSchemaUtil; +import org.opendaylight.restconf.utils.parser.ParserIdentifier; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -56,9 +46,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerAttrNode; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; @@ -80,20 +67,17 @@ public class RestconfMappingNodeUtilTest { private static Set modules; private static SchemaContext schemaContext; - private static SchemaContext schemaContextCapabilites; + private static SchemaContext schemaContextMonitoring; private static Set modulesRest; - private Set allStreamChildNodes; - @BeforeClass public static void loadTestSchemaContextAndModules() throws Exception { - RestconfMappingNodeUtilTest.schemaContext = TestRestconfUtils.loadSchemaContext( + schemaContext = TestRestconfUtils.loadSchemaContext( "/modules/restconf-module-testing"); - RestconfMappingNodeUtilTest.schemaContextCapabilites = TestRestconfUtils.loadSchemaContext("/modules"); - RestconfMappingNodeUtilTest.modules = schemaContextCapabilites.getModules(); - RestconfMappingNodeUtilTest.modulesRest = - TestRestconfUtils.loadSchemaContext("/modules/restconf-module-testing").getModules(); + schemaContextMonitoring = TestRestconfUtils.loadSchemaContext("/modules"); + modules = schemaContextMonitoring.getModules(); + modulesRest = TestRestconfUtils.loadSchemaContext("/modules/restconf-module-testing").getModules(); } @Before @@ -106,9 +90,6 @@ public class RestconfMappingNodeUtilTest { QName.create("", RestconfMappingNodeConstants.REPLAY_SUPPORT)); when(this.leafReplayLog.getQName()).thenReturn(QName.create(RestconfMappingNodeConstants.REPLAY_LOG)); when(this.leafEvents.getQName()).thenReturn(QName.create("", RestconfMappingNodeConstants.EVENTS)); - - this.allStreamChildNodes = Sets.newHashSet( - this.leafName, this.leafDescription, this.leafReplaySupport, this.leafReplayLog, this.leafEvents); } /** @@ -130,7 +111,7 @@ public class RestconfMappingNodeUtilTest { @Test public void restconfStateCapabilitesTest() { - final Module monitoringModule = schemaContextCapabilites + final Module monitoringModule = schemaContextMonitoring .findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE); final NormalizedNode>> normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule); @@ -154,251 +135,67 @@ public class RestconfMappingNodeUtilTest { Assert.assertTrue(listOfValues.contains(QueryParams.WITH_DEFAULTS)); } - /** - * Positive test of writing one stream to {@link MonitoringModule#STREAM_LIST_SCHEMA_NODE} and checking if stream - * was correctly written. - */ - @Test - public void toStreamEntryNodeTest() { - // test stream name - final String stream1 = "stream-1"; - - // get list stream node from Restconf module - final ListSchemaNode listStream = (ListSchemaNode) RestconfSchemaUtil.getRestconfSchemaNode( - getTestingRestconfModule("ietf-restconf"), MonitoringModule.STREAM_LIST_SCHEMA_NODE); - - // write stream to list stream node - final MapEntryNode mapEntryNode = RestconfMappingNodeUtil.toStreamEntryNode(stream1, listStream); - - // verify - verifyStream(stream1, mapEntryNode); - } - - /** - * Try to map streams when {@link MonitoringModule#STREAM_LIST_SCHEMA_NODE} is null. - * Test is expected to fail catching IllegalStateException. - */ - @Test - public void toStreamEntryNodeNullListStreamNegativeTest() { - this.thrown.expect(IllegalStateException.class); - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", null); - } - - /** - * Test trying to map streams to {@link MonitoringModule#STREAM_LIST_SCHEMA_NODE} which is not of type list. - * Test is expected to fail with IllegalStateException. - */ @Test - public void toStreamEntryNodeIllegalListStreamNegativeTest() { - this.thrown.expect(IllegalStateException.class); - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", mock(LeafSchemaNode.class)); - } - - /** - * Test case with target {@link MonitoringModule#STREAM_LIST_SCHEMA_NODE} which does not contain any child nodes. - * Test is catching RestconfDocumentedException and error type, error tag and error status code are - * compared to expected values. - */ - @Test - public void toStreamEntryNodeSchemaNodeWithoutChildsNegativeTest() { - final ListSchemaNode mockListNode = mock(ListSchemaNode.class); - when(mockListNode.getChildNodes()).thenReturn(Collections.EMPTY_SET); - - try { - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", mockListNode); - fail("Test should fail due to no child nodes in" - + MonitoringModule.STREAM_LIST_SCHEMA_NODE - + " node"); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", - ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", - ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", - 404, e.getErrors().get(0).getErrorTag().getStatusCode()); - } - } + public void toStreamEntryNodeTest() throws Exception { + final YangInstanceIdentifier path = + ParserIdentifier.toInstanceIdentifier("nested-module:depth1-cont/depth2-leaf1", schemaContextMonitoring, null).getInstanceIdentifier(); + final Date start = new Date(); + final String outputType = "XML"; + final URI uri = new URI("uri"); + final Module monitoringModule = schemaContextMonitoring + .findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE); + final boolean exist = true; - /** - * Test case when target list stream does not contain child with name {@link RestconfMappingNodeConstants#NAME}. - * Test is catching RestconfDocumentedException and error type, error tag and error status code are - * compared to expected values. - */ - @Test - public void toStreamEntryNodeMissingStreamNameNegativeTest() { - prepareMockListWithMissingLeaf(this.leafName); + final Map map = + prepareMap(path.getLastPathArgument().getNodeType().getLocalName(), uri, start, outputType); - try { - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - fail("Test should fail due to missing " - + RestconfMappingNodeConstants.NAME - + " node in " + MonitoringModule.STREAM_LIST_SCHEMA_NODE); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", - ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", - ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", - 404, e.getErrors().get(0).getErrorTag().getStatusCode()); - } + final NormalizedNode mappedData = RestconfMappingNodeUtil.mapDataChangeNotificationStreamByIetfRestconfMonitoring(path, start, outputType, uri, + monitoringModule, exist, schemaContextMonitoring); + assertNotNull(mappedData); + testData(map, mappedData); } - /** - * Test case when target list stream does not contain child with name - * {@link RestconfMappingNodeConstants#DESCRIPTION}. Test is catching RestconfDocumentedException and - * checking error type and error tag. - */ @Test - public void toStreamEntryNodeMissingStreamDescriptionNegativeTest() { - prepareMockListWithMissingLeaf(this.leafDescription); - - try { - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - fail("Test should fail due to missing " - + RestconfMappingNodeConstants.DESCRIPTION - + " node in " + MonitoringModule.STREAM_LIST_SCHEMA_NODE); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", - ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", - ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", - 404, e.getErrors().get(0).getErrorTag().getStatusCode()); - } - } + public void toStreamEntryNodeNotifiTest() throws Exception { + final Date start = new Date(); + final String outputType = "JSON"; + final URI uri = new URI("uri"); + final Module monitoringModule = schemaContextMonitoring + .findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE); + final boolean exist = true; - /** - * Test case when target list stream does not contain child with name - * {@link RestconfMappingNodeConstants#REPLAY_SUPPORT}. Test is catching RestconfDocumentedException - * and checking error type and error tag. - */ - @Test - public void toStreamEntryNodeMissingStreamReplaySupportNegativeTest() { - prepareMockListWithMissingLeaf(this.leafReplaySupport); + final Map map = prepareMap("notifi", uri, start, outputType); + map.put(MonitoringModule.LEAF_DESCR_STREAM_QNAME, "Notifi"); - try { - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - fail("Test should fail due to missing " - + RestconfMappingNodeConstants.REPLAY_SUPPORT - + " node in " + MonitoringModule.STREAM_LIST_SCHEMA_NODE); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", - ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", - ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", - 404, e.getErrors().get(0).getErrorTag().getStatusCode()); - } + final QName notifiQName = QName.create("urn:nested:module", "2014-06-3", "notifi"); + final NormalizedNode mappedData = + RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(notifiQName, schemaContextMonitoring.getNotifications(), start, + outputType, uri, monitoringModule, exist); + assertNotNull(mappedData); + testData(map, mappedData); } - /** - * Test case when target list stream does not contain child with name - * {@link RestconfMappingNodeConstants#REPLAY_LOG}. Test is catching RestconfDocumentedException and - * checking error type and error tag. - */ - @Test - public void toStreamEntryNodeMissingStreamReplayLogNegativeTest() { - prepareMockListWithMissingLeaf(this.leafReplayLog); - - try { - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - fail("Test should fail due to missing " - + RestconfMappingNodeConstants.REPLAY_LOG - + " node in " + MonitoringModule.STREAM_LIST_SCHEMA_NODE); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", - ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", - ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", - 404, e.getErrors().get(0).getErrorTag().getStatusCode()); - } + private Map prepareMap(final String name, final URI uri, final Date start, final String outputType) { + final Map map = new HashMap<>(); + map.put(MonitoringModule.LEAF_NAME_STREAM_QNAME, name); + map.put(MonitoringModule.LEAF_LOCATION_ACCESS_QNAME, uri.toString()); + map.put(MonitoringModule.LEAF_REPLAY_SUPP_STREAM_QNAME, true); + map.put(MonitoringModule.LEAF_START_TIME_STREAM_QNAME, + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'XXX").format(start)); + map.put(MonitoringModule.LEAF_ENCODING_ACCESS_QNAME, outputType); + return map; } - /** - * Test case when target list stream does not contain child with name {@link RestconfMappingNodeConstants#EVENTS}. - * Test is catching RestconfDocumentedException and checking error type, error tag and error status - * code. - */ - @Test - public void toStreamEntryNodeMissingStreamEventsNegativeTest() { - prepareMockListWithMissingLeaf(this.leafEvents); - - try { - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - fail("Test should fail due to missing " - + RestconfMappingNodeConstants.EVENTS - + " node in " + MonitoringModule.STREAM_LIST_SCHEMA_NODE); - } catch (final RestconfDocumentedException e) { - assertEquals("Error type is not correct", - ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType()); - assertEquals("Error tag is not correct", - ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag()); - assertEquals("Error status code is not correct", - 404, e.getErrors().get(0).getErrorTag().getStatusCode()); + private void testData(final Map map, final NormalizedNode mappedData) { + for (final DataContainerChild child : ((MapEntryNode) mappedData).getValue()) { + if (child instanceof LeafNode) { + final LeafNode leaf = ((LeafNode) child); + Assert.assertTrue(map.containsKey(leaf.getNodeType())); + Assert.assertEquals(map.get(leaf.getNodeType()), leaf.getValue()); + } } } - /** - * Test case when target list stream contains child with name {@link RestconfMappingNodeConstants#NAME}. Test is - * expecting IllegalStateException. - */ - @Test - public void toStreamEntryNodeStreamNameNegativeTest() { - prepareMockListWithIllegalLeaf(this.leafName); - - this.thrown.expect(IllegalStateException.class); - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - } - - /** - * Test case when target list stream contains child with name {@link RestconfMappingNodeConstants#DESCRIPTION}. - * Test is expecting IllegalStateException. - */ - @Test - public void toStreamEntryNodeStreamDescriptionNegativeTest() { - prepareMockListWithIllegalLeaf(this.leafDescription); - - this.thrown.expect(IllegalStateException.class); - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - } - - /** - * Test case when target list stream contains child with name {@link RestconfMappingNodeConstants#REPLAY_SUPPORT}. - * Test is expecting IllegalStateException. - */ - @Test - public void toStreamEntryNodeStreamReplaySupportNegativeTest() { - prepareMockListWithIllegalLeaf(this.leafReplaySupport); - - this.thrown.expect(IllegalStateException.class); - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - } - - /** - * Test case when target list stream contains child with name {@link RestconfMappingNodeConstants#REPLAY_LOG}. - * Test is expecting IllegalStateException. - */ - @Test - public void toStreamEntryNodeStreamReplayLogNegativeTest() { - prepareMockListWithIllegalLeaf(this.leafReplayLog); - - this.thrown.expect(IllegalStateException.class); - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - } - - /** - * Test case when target list stream contains child with name {@link RestconfMappingNodeConstants#EVENTS}. Test is - * expecting IllegalStateException. - */ - @Test - public void toStreamEntryNodeStreamEventsNegativeTest() { - prepareMockListWithIllegalLeaf(this.leafEvents); - - this.thrown.expect(IllegalStateException.class); - RestconfMappingNodeUtil.toStreamEntryNode("stream-1", this.mockStreamList); - } - /** * Verify loaded modules * @@ -457,93 +254,4 @@ public class RestconfMappingNodeUtilTest { loadedModules.remove(name); } } - - /** - * Verify if a stream was correctly written into {@link MonitoringModule#STREAM_LIST_SCHEMA_NODE} node in Restconf - * module. - * @param streamName Expected stream name - * @param streamNode Writetn strem node from Restconf module - */ - private final void verifyStream(final String streamName, final MapEntryNode streamNode) { - assertNotNull("Stream node can not be null", streamNode); - final Iterator entries = ((AbstractImmutableDataContainerAttrNode) streamNode) - .getChildren().entrySet().iterator(); - boolean notAllowedKey = false; - - while (entries.hasNext()) { - final Entry e = ((AbstractMap.SimpleImmutableEntry) entries.next()); - final String key = ((YangInstanceIdentifier.NodeIdentifier) e.getKey()).getNodeType().getLocalName(); - - switch (key) { - case RestconfMappingNodeConstants.NAME : - assertEquals("Stream name value is not as expected", - streamName, ((LeafNode) e.getValue()).getValue()); - break; - case RestconfMappingNodeConstants.DESCRIPTION : - assertEquals("Stream description value is not as expected", - RestconfMappingStreamConstants.DESCRIPTION, ((LeafNode) e.getValue()).getValue()); - break; - case RestconfMappingNodeConstants.REPLAY_SUPPORT : - assertEquals("Stream replay support value is not as expected", - RestconfMappingStreamConstants.REPLAY_SUPPORT, ((LeafNode) e.getValue()).getValue()); - break; - case RestconfMappingNodeConstants.REPLAY_LOG : - assertEquals("Stream replay log value is not as expected", - RestconfMappingStreamConstants.REPLAY_LOG, ((LeafNode) e.getValue()).getValue()); - break; - case RestconfMappingNodeConstants.EVENTS : - assertEquals("Stream events value is not as expected", - RestconfMappingStreamConstants.EVENTS, ((LeafNode) e.getValue()).getValue()); - break; - default: - notAllowedKey = true; - break; - } - } - - assertFalse("Not allowed key in list stream found", notAllowedKey); - } - - /** - * There are multiple testing Restconf modules for different test cases. It is possible to distinguish them by - * name or by namespace. This method is looking for Restconf test module by its name. - * @param s Testing Restconf module name - * @return Restconf module - */ - private Module getTestingRestconfModule(final String s) { - return RestconfMappingNodeUtilTest.schemaContext.findModuleByName( - s, Draft18.RestconfModule.IETF_RESTCONF_QNAME.getRevision()); - } - - /** - * Updates {@link this#mockStreamList} to NOT contains specified leaf. - * @param leaf Leaf to be missing - */ - private void prepareMockListWithMissingLeaf(final LeafSchemaNode leaf) { - // prepare set of leaf without selected leaf - final Set childLeafs = new HashSet<>(this.allStreamChildNodes); - childLeafs.remove(leaf); - - // mock list leaf nodes - when(this.mockStreamList.getChildNodes()).thenReturn(childLeafs); - } - - /** - * Updates {@link this#mockStreamList} to contains specified leaf which is not of type {@link LeafSchemaNode}. - * @param leaf Leaf to be changes - */ - private void prepareMockListWithIllegalLeaf(final LeafSchemaNode leaf) { - // prepare set of leaf without selected leaf - final Set childLeafs = new HashSet<>(this.allStreamChildNodes); - childLeafs.remove(leaf); - - // add leaf-list with the same local name as removed leaf - final String localName = leaf.getQName().getLocalName(); - final LeafListSchemaNode mockLeafList = mock(LeafListSchemaNode.class); - when(mockLeafList.getQName()).thenReturn(QName.create("", localName)); - childLeafs.add(mockLeafList); - - // mock list leaf nodes - when(this.mockStreamList.getChildNodes()).thenReturn(childLeafs); - } }