/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.odlext.parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.yangtools.odlext.model.api.OpenDaylightExtensionsStatements;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;

public class Bug3874ExtensionTest {
    private static CrossSourceStatementReactor reactor;

    @BeforeClass
    public static void createReactor() {
        reactor = RFC7950Reactors.vanillaReactorBuilder()
                .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
                    AnyxmlSchemaLocationStatementSupport.getInstance())
                .addNamespaceSupport(ModelProcessingPhase.FULL_DECLARATION, AnyxmlSchemaLocationNamespace.BEHAVIOUR)
                .build();
    }

    @AfterClass
    public static void freeReactor() {
        reactor = null;
    }

    @Test
    public void test() throws Exception {
        SchemaContext context = reactor.newBuild()
                .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource("/bugs/bug3874/foo.yang")))
                .addSource(YangStatementStreamSource.create(
                    YangTextSchemaSource.forResource("/bugs/bug3874/yang-ext.yang")))
                .buildEffective();

        QNameModule foo = QNameModule.create(XMLNamespace.of("foo"));
        QName myContainer2QName = QName.create(foo, "my-container-2");
        QName myAnyXmlDataQName = QName.create(foo, "my-anyxml-data");

        DataSchemaNode dataChildByName = context.findDataChildByName(myAnyXmlDataQName).get();
        assertTrue(dataChildByName instanceof AnyxmlSchemaNode);
        AnyxmlSchemaNode anyxml = (AnyxmlSchemaNode) dataChildByName;

        SchemaNode myContainer2 = SchemaContextUtil.findDataSchemaNode(context,
            SchemaPath.create(true, myContainer2QName));
        assertTrue(myContainer2 instanceof ContainerSchemaNode);

        Collection<? extends UnknownSchemaNode> unknownSchemaNodes = anyxml.getUnknownSchemaNodes();
        assertEquals(1, unknownSchemaNodes.size());

        UnknownSchemaNode next = unknownSchemaNodes.iterator().next();
        assertTrue(next instanceof AnyxmlSchemaLocationEffectiveStatementImpl);
        AnyxmlSchemaLocationEffectiveStatementImpl anyxmlSchemaLocationUnknownNode =
                (AnyxmlSchemaLocationEffectiveStatementImpl) next;
        assertEquals(Absolute.of(myContainer2QName), anyxmlSchemaLocationUnknownNode.argument());
        assertEquals(OpenDaylightExtensionsStatements.ANYXML_SCHEMA_LOCATION.getStatementName(),
            anyxmlSchemaLocationUnknownNode.getNodeType());
        assertEquals(OpenDaylightExtensionsStatements.ANYXML_SCHEMA_LOCATION.getStatementName(),
            anyxmlSchemaLocationUnknownNode.getQName());
    }
}

@@ -362,38 +362,100 @@
 port static org.
-junit.
+hamcrest.CoreMatchers.instanceOf;%0Aimport static org.hamcrest.Matcher
 Assert.assertEqu
@@ -451,22 +451,20 @@
 t.assert
-Equals
+That
 ;%0Aimport
@@ -494,20 +494,22 @@
 t.assert
-True
+Equals
 ;%0A%0Aimpor
@@ -1202,69 +1202,8 @@
 de;%0A
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;%0A
 impo
@@ -1431,77 +1431,8 @@
 ce;%0A
-import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;%0A
 impo
@@ -3081,19 +3081,19 @@
  assertT
-rue
+hat
 (dataChi
@@ -3100,16 +3100,17 @@
 ldByName
+,
  instanc
@@ -3102,35 +3102,35 @@
 ByName, instance
-of 
+Of(
 AnyxmlSchemaNode
@@ -3129,16 +3129,23 @@
 hemaNode
+.class)
 );%0A     
@@ -3248,25 +3248,15 @@
 2 = 
-SchemaC
+c
 ontext
-Util
 .fin
@@ -3264,64 +3264,18 @@
 Data
-SchemaNode(context,%0A            SchemaPath.create(true, 
+TreeChild(
 myCo
@@ -3288,16 +3288,28 @@
 r2QName)
+.orElse(null
 );%0A     
@@ -3318,19 +3318,19 @@
  assertT
-rue
+hat
 (myConta
@@ -3334,16 +3334,17 @@
 ntainer2
+,
  instanc
@@ -3344,19 +3344,19 @@
 instance
-of 
+Of(
 Containe
@@ -3366,16 +3366,23 @@
 hemaNode
+.class)
 );%0A%0A    
@@ -3621,16 +3621,17 @@
 ertT
-rue
+hat
 (next
+,
  ins
@@ -3635,19 +3635,19 @@
 instance
-of 
+Of(
 AnyxmlSc
@@ -3680,16 +3680,23 @@
 mentImpl
+.class)
 );%0A     

Clean up Bug3874ExtensionTest