/*
 * 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.yang.stmt;

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

import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;

public class Bug6972Test {

    @Ignore
    @Test
    public void allUnitsShouldBeTheSameInstance() throws Exception {
        final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug6972");
        assertNotNull(schemaContext);
        assertEquals(3, schemaContext.getModules().size());

        final Revision revision = Revision.of("2016-10-20");
        final Module foo = schemaContext.findModule("foo", revision).get();
        final Module bar = schemaContext.findModule("bar", revision).get();
        final Module baz = schemaContext.findModule("baz", revision).get();

        final QName barExportCont = QName.create("bar-ns", "bar-export", revision);
        final QName barFooCont = QName.create("bar-ns", "bar-foo", revision);
        final QName barFooLeaf = QName.create("bar-ns", "foo", revision);

        final UnitsEffectiveStatement unitsBar1 = getEffectiveUnits(bar, barExportCont, barFooLeaf);
        final UnitsEffectiveStatement unitsBar2 = getEffectiveUnits(bar, barFooCont, barFooLeaf);

        final QName bazExportCont = QName.create("baz-ns", "baz-export", revision);
        final QName bazFooCont = QName.create("baz-ns", "baz-foo", revision);
        final QName bazFooLeaf = QName.create("baz-ns", "foo", revision);

        final UnitsEffectiveStatement unitsBaz1 = getEffectiveUnits(baz, bazExportCont, bazFooLeaf);
        final UnitsEffectiveStatement unitsBaz2 = getEffectiveUnits(baz, bazFooCont, bazFooLeaf);

        assertTrue(unitsBar1 == unitsBar2 && unitsBar1 == unitsBaz1 && unitsBar1 == unitsBaz2);
    }

    private static UnitsEffectiveStatement getEffectiveUnits(final Module module, final QName containerQName,
            final QName leafQName) {
        UnitsEffectiveStatement units = null;

        final ContainerSchemaNode cont = (ContainerSchemaNode) module.getDataChildByName(containerQName);
        assertNotNull(cont);
        final LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(leafQName);
        assertNotNull(leaf);

        for (EffectiveStatement<?, ?> effStmt : ((LeafEffectiveStatement) leaf).effectiveSubstatements()) {
            if (effStmt instanceof UnitsEffectiveStatement) {
                units = (UnitsEffectiveStatement) effStmt;
                break;
            }
        }

        return units;
    }
}
@@ -301,17 +301,16 @@
 tml%0A */%0A
-%0A
 package 
@@ -480,39 +480,14 @@
 sert
-True;%0A%0Aimport org.junit.Ignore;
+Same;%0A
 %0Aimp
@@ -1133,21 +1133,8 @@
 st %7B
-%0A%0A    @Ignore
 %0A   
@@ -2041,46 +2041,27 @@
     
-final UnitsEffectiveStatement 
+assertSame(
 unitsBar
 2 = 
@@ -2056,19 +2056,18 @@
 unitsBar
-2 =
+1,
  getEffe
@@ -2097,32 +2097,33 @@
 ont, barFooLeaf)
+)
 ;%0A%0A        final
@@ -2358,49 +2358,29 @@
     
-final UnitsEffectiveStatement 
+assertSame(
 unitsBa
-z1 =
+r1,
  get
@@ -2417,32 +2417,33 @@
 ont, bazFooLeaf)
+)
 ;%0A        final 
@@ -2440,49 +2440,29 @@
     
-final UnitsEffectiveStatement 
+assertSame(
 unitsBa
-z2 =
+r1,
  get
@@ -2508,104 +2508,8 @@
 eaf)
-;%0A%0A        assertTrue(unitsBar1 == unitsBar2 && unitsBar1 == unitsBaz1 && unitsBar1 == unitsBaz2
 );%0A 

Enable Bug6972Test