Merge "Add missing copyright text"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / TestUtils.java
index 7d6da6a94f3f41a89a8b107f9743297f3ff8f3e3..75cce14d09c197946ea3d9cdfe3b128d3986dca7 100644 (file)
@@ -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<Module> 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<Module> loadModulesFrom(final String yangPath) {
@@ -114,11 +88,11 @@ public final class TestUtils {
     }
 
     public static SchemaContext loadSchemaContext(final Set<Module> 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<Module> 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<Module> 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<Module> 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<Node<?>> 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<Node<?>> 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<Node<?>> reader) {
-        return readInputToCnSn(path, false, reader);
-    }
-
-    public static String writeCompNodeWithSchemaContextToOutput(final Node<?> node, final Set<Module> modules,
-            final DataSchemaNode dataSchemaNode, final MessageBodyWriter<StructuredData> 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<Node<?>> lst = new ArrayList<Node<?>>();
-        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<ImmutableCompositeNode> 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";