X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FTestUtils.java;h=75cce14d09c197946ea3d9cdfe3b128d3986dca7;hp=7d6da6a94f3f41a89a8b107f9743297f3ff8f3e3;hb=9ba2b4eca79bcc0e78099b133296801c8d45a6c4;hpb=f3fc2b47ca3e5be0b95a273664b6003d45fb9dd8 diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java index 7d6da6a94f..75cce14d09 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java @@ -8,13 +8,8 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.CheckedFuture; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; @@ -27,7 +22,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.sql.Date; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -35,9 +29,6 @@ import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.ext.MessageBodyReader; -import javax.ws.rs.ext.MessageBodyWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -47,31 +38,14 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; -import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; -import org.opendaylight.controller.sal.restconf.impl.ControllerContext; -import org.opendaylight.controller.sal.restconf.impl.NodeWrapper; -import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException; -import org.opendaylight.controller.sal.restconf.impl.RestconfError; -import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag; -import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType; -import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; -import org.opendaylight.controller.sal.restconf.impl.StructuredData; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.composite.node.schema.cnsn.parser.CnSnToNormalizedNodeParserFactory; -import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder; -import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder; -import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser; @@ -85,7 +59,7 @@ public final class TestUtils { private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class); - private final static YangContextParser parser = new YangParserImpl(); + private final static YangContextParser PARSER = new YangParserImpl(); private static Set loadModules(final String resourceDirectory) throws FileNotFoundException { final File testDir = new File(resourceDirectory); @@ -100,7 +74,7 @@ public final class TestUtils { testFiles.add(new File(testDir, fileName)); } } - return parser.parseYangModels(testFiles); + return PARSER.parseYangModels(testFiles); } public static Set loadModulesFrom(final String yangPath) { @@ -114,11 +88,11 @@ public final class TestUtils { } public static SchemaContext loadSchemaContext(final Set modules) { - return parser.resolveSchemaContext(modules); + return PARSER.resolveSchemaContext(modules); } public static SchemaContext loadSchemaContext(final String resourceDirectory) throws FileNotFoundException { - return parser.resolveSchemaContext(loadModulesFrom(resourceDirectory)); + return PARSER.resolveSchemaContext(loadModulesFrom(resourceDirectory)); } public static Module findModule(final Set modules, final String moduleName) { @@ -164,20 +138,6 @@ public final class TestUtils { } - /** - * - * Fill missing data (namespaces) and build correct data type in {@code compositeNode} according to - * {@code dataSchemaNode}. The method {@link RestconfImpl#createConfigurationData createConfigurationData} is used - * because it contains calling of method {code normalizeNode} - */ - public static void normalizeCompositeNode(final Node node, final Set modules, final String schemaNodePath) { - final RestconfImpl restconf = RestconfImpl.getInstance(); - ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules)); - - prepareMocksForRestconf(modules, restconf); - restconf.updateConfigurationData(schemaNodePath, node); - } - /** * Searches module with name {@code searchedModuleName} in {@code modules}. If module name isn't specified and * module set has only one element then this element is returned. @@ -233,84 +193,6 @@ public final class TestUtils { return buildQName(name, "", null); } - private static void addDummyNamespaceToAllNodes(final NodeWrapper wrappedNode) throws URISyntaxException { - wrappedNode.setNamespace(new URI("")); - if (wrappedNode instanceof CompositeNodeWrapper) { - for (final NodeWrapper childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) { - addDummyNamespaceToAllNodes(childNodeWrapper); - } - } - } - - private static void prepareMocksForRestconf(final Set modules, final RestconfImpl restconf) { - final ControllerContext controllerContext = ControllerContext.getInstance(); - final BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class); - - controllerContext.setSchemas(TestUtils.loadSchemaContext(modules)); - - when(mockedBrokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(NormalizedNode.class))) - .thenReturn(mock(CheckedFuture.class)); - - restconf.setControllerContext(controllerContext); - restconf.setBroker(mockedBrokerFacade); - } - - public static Node readInputToCnSn(final String path, final boolean dummyNamespaces, - final MessageBodyReader> reader) throws WebApplicationException { - - final InputStream inputStream = TestUtils.class.getResourceAsStream(path); - try { - final Node node = reader.readFrom(null, null, null, null, null, inputStream); - assertTrue(node instanceof CompositeNodeWrapper); - if (dummyNamespaces) { - try { - TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) node); - return ((CompositeNodeWrapper) node).unwrap(); - } catch (final URISyntaxException e) { - LOG.error(e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - return node; - } catch (final IOException e) { - LOG.error(e.getMessage()); - assertTrue(e.getMessage(), false); - } - return null; - } - -// public static Node readInputToCnSnNew(String path, MessageBodyReader> reader) throws WebApplicationException { -// InputStream inputStream = TestUtils.class.getResourceAsStream(path); -// try { -// return reader.readFrom(null, null, null, null, null, inputStream); -// } catch (IOException e) { -// LOG.error(e.getMessage()); -// assertTrue(e.getMessage(), false); -// } -// return null; -// } - - public static Node readInputToCnSn(final String path, final MessageBodyReader> reader) { - return readInputToCnSn(path, false, reader); - } - - public static String writeCompNodeWithSchemaContextToOutput(final Node node, final Set modules, - final DataSchemaNode dataSchemaNode, final MessageBodyWriter messageBodyWriter) throws IOException, - WebApplicationException { - - assertNotNull(dataSchemaNode); - assertNotNull("Composite node can't be null", node); - final ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); - - ControllerContext.getInstance().setSchemas(loadSchemaContext(modules)); - - assertTrue(node instanceof CompositeNode); - messageBodyWriter.writeTo(new StructuredData((CompositeNode)node, dataSchemaNode, null), null, null, null, null, - null, byteArrayOS); - - return byteArrayOS.toString(); - } - public static String loadTextFile(final String filePath) throws IOException { final FileReader fileReader = new FileReader(filePath); final BufferedReader bufReader = new BufferedReader(fileReader); @@ -341,24 +223,6 @@ public final class TestUtils { return matcher.matches(); } - public static NormalizedNode compositeNodeToDatastoreNormalizedNode(final CompositeNode compositeNode, - final DataSchemaNode schema) { - final List> lst = new ArrayList>(); - lst.add(compositeNode); - if (schema instanceof ContainerSchemaNode) { - return CnSnToNormalizedNodeParserFactory.getInstance().getContainerNodeParser() - .parse(lst, (ContainerSchemaNode) schema); - } else if (schema instanceof ListSchemaNode) { - return CnSnToNormalizedNodeParserFactory.getInstance().getMapNodeParser() - .parse(lst, (ListSchemaNode) schema); - } - - LOG.error("Top level isn't of type container, list, leaf schema node but " + schema.getClass().getSimpleName()); - - throw new RestconfDocumentedException(new RestconfError(ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, - "It wasn't possible to translate specified data to datastore readable form.")); - } - public static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName, final String namespace, final String revision) throws ParseException { return new YangInstanceIdentifier.NodeIdentifier(QName.create(namespace, revision, localName)); @@ -378,7 +242,6 @@ public final class TestUtils { public static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(final String localName, final String namespace, final String revision, final String... keysAndValues) throws ParseException { - final java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(revision); if (keysAndValues.length % 2 != 0) { new IllegalArgumentException("number of keys argument have to be divisible by 2 (map)"); } @@ -393,15 +256,6 @@ public final class TestUtils { predicate); } - public static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() { - final CompositeNodeBuilder interfaceBuilder = ImmutableCompositeNode.builder(); - interfaceBuilder.addLeaf(buildQName("name", "dummy", "2014-07-29"), "eth0"); - interfaceBuilder.addLeaf(buildQName("type", "dummy", "2014-07-29"), "ethernetCsmacd"); - interfaceBuilder.addLeaf(buildQName("enabled", "dummy", "2014-07-29"), "false"); - interfaceBuilder.addLeaf(buildQName("description", "dummy", "2014-07-29"), "some interface"); - return interfaceBuilder.toInstance(); - } - static NormalizedNode prepareNormalizedNodeWithIetfInterfacesInterfacesData() throws ParseException { final String ietfInterfacesDate = "2013-07-04"; final String namespace = "urn:ietf:params:xml:ns:yang:ietf-interfaces";