/*
 * Copyright (C) 2005-2010 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see 
 * The entire application context is loaded as is, but the integrity fail-
 * mode is set to throw an exception.
 * 
 * TODO: Role name restrictions must be checked
 * 
 * @author Derek Hulley
 */
@SuppressWarnings("unused")
@Category(OwnJVMTestsCategory.class)
public class IntegrityTest extends TestCase
{
    private static Log logger = LogFactory.getLog(IntegrityTest.class);
    
    public static final String NAMESPACE = "http://www.alfresco.org/test/IntegrityTest";
    public static final String TEST_PREFIX = "test";
    
    public static final QName TEST_TYPE_WITHOUT_ANYTHING = QName.createQName(NAMESPACE, "typeWithoutAnything");
    public static final QName TEST_TYPE_WITH_ASPECT = QName.createQName(NAMESPACE, "typeWithAspect");
    public static final QName TEST_TYPE_WITH_PROPERTIES = QName.createQName(NAMESPACE, "typeWithProperties");
    public static final QName TEST_TYPE_WITH_ENCRYPTED_PROPERTIES = QName.createQName(NAMESPACE, "typeWithEncryptedProperties");
    public static final QName TEST_TYPE_WITH_ASSOCS = QName.createQName(NAMESPACE, "typeWithAssocs");
    public static final QName TEST_TYPE_WITH_CHILD_ASSOCS = QName.createQName(NAMESPACE, "typeWithChildAssocs");
    public static final QName TEST_TYPE_WITH_NON_ENFORCED_CHILD_ASSOCS = QName.createQName(NAMESPACE, "typeWithNonEnforcedChildAssocs");
    public static final QName TEST_TYPE_WITH_NON_ENFORCED_TARGET_ASSOCS = QName.createQName(NAMESPACE, "typeWithNonEnforcedTargetAssocs");
    
    public static final QName TEST_ASSOC_NODE_ZEROMANY_ZEROMANY = QName.createQName(NAMESPACE, "assoc-0to* - 0to*");
    public static final QName TEST_ASSOC_CHILD_ZEROMANY_ZEROMANY = QName.createQName(NAMESPACE, "child-0to* - 0to*");
    public static final QName TEST_ASSOC_NODE_ONE_ONE = QName.createQName(NAMESPACE, "assoc-1to1 - 1to1");
    public static final QName TEST_ASSOC_NODE_ONE_MANY = QName.createQName(NAMESPACE, "assoc-1to1 - 0to*");
    public static final QName TEST_ASSOC_CHILD_ONE_ONE = QName.createQName(NAMESPACE, "child-1to1 - 1to1");
    public static final QName TEST_ASSOC_ASPECT_ONE_ONE = QName.createQName(NAMESPACE, "aspect-assoc-1to1 - 1to1");
    public static final QName TEST_ASSOC_CHILD_NON_ENFORCED = QName.createQName(NAMESPACE, "child-non-enforced");
    public static final QName TEST_ASSOC_TARGET_NON_ENFORCED = QName.createQName(NAMESPACE, "target-non-enforced");
    
    public static final QName TEST_ASPECT_WITH_PROPERTIES = QName.createQName(NAMESPACE, "aspectWithProperties");
    public static final QName TEST_ASPECT_WITH_ASSOC = QName.createQName(NAMESPACE, "aspectWithAssoc");
    public static final QName TEST_ASPECT_WITH_ASPECT = QName.createQName(NAMESPACE, "aspectWithAspect");
    
    public static final QName TEST_PROP_TEXT_A = QName.createQName(NAMESPACE, "prop-text-a");
    public static final QName TEST_PROP_TEXT_B = QName.createQName(NAMESPACE, "prop-text-b");
    public static final QName TEST_PROP_TEXT_C = QName.createQName(NAMESPACE, "prop-text-c");
    public static final QName TEST_PROP_TEXT_D = QName.createQName(NAMESPACE, "prop-text-d");
    public static final QName TEST_PROP_INT_A = QName.createQName(NAMESPACE, "prop-int-a");
    public static final QName TEST_PROP_INT_B = QName.createQName(NAMESPACE, "prop-int-b");
    public static final QName TEST_PROP_INT_C = QName.createQName(NAMESPACE, "prop-int-c");
    public static final QName TEST_PROP_ENCRYPTED_A = QName.createQName(NAMESPACE, "prop-encrypted-a");
    public static final QName TEST_PROP_ENCRYPTED_B = QName.createQName(NAMESPACE, "prop-encrypted-b");
    public static final QName TEST_PROP_ENCRYPTED_C = QName.createQName(NAMESPACE, "prop-encrypted-c");
    
    public static ApplicationContext ctx;
    static
    {
        ctx = ApplicationContextHelper.getApplicationContext();
    }
    
    private IntegrityChecker integrityChecker;
    private ServiceRegistry serviceRegistry;
    private NodeService nodeService;
    private NodeRef rootNodeRef;
    private Map 
     * Does nothing.
     */
    public void testCreateTargetOfAssocsWithMandatorySourcesMissing() throws Exception
    {
//        // this is the target of 3 associations where the source cardinality is 1..1
//        NodeRef target = createNode("abc", TEST_TYPE_WITHOUT_ANYTHING, null);
//        
//        checkIntegrityExpectFailure("Failed to detect missing mandatory assoc sources", 3);
        logger.error("Method commented out: testCreateTargetOfAssocsWithMandatorySourcesMissing");
    }
    /**
     * TODO: Reactivate once cascade delete notifications are back on
     *  
     * Does nothing.
     */
    public void testRemoveSourcesOfMandatoryAssocs() throws Exception
    {
//        // this is the target of 3 assoc types where the source cardinality is 1..1
//        NodeRef targetAndChild = createNode("targetAndChild", TEST_TYPE_WITHOUT_ANYTHING, null);
//        
//        NodeRef source = createNode("source", TEST_TYPE_WITH_ASSOCS, null);
//        nodeService.createAssociation(source, targetAndChild, TEST_ASSOC_NODE_ONE_ONE);
//
//        NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null);
//        nodeService.addChild(parent, targetAndChild, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "mandatoryChild"));
//
//        NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null);
//        nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null);
//        nodeService.createAssociation(aspectSource, targetAndChild, TEST_ASSOC_ASPECT_ONE_ONE);
//        
//        checkIntegrityNoFailure();
//        
//        // remove source nodes
//        nodeService.deleteNode(source);
//        nodeService.deleteNode(parent);
//        nodeService.deleteNode(aspectSource);
//        
//        checkIntegrityExpectFailure("Failed to detect removal of mandatory assoc sources", 3);
        logger.error("Method commented out: testRemoveSourcesOfMandatoryAssocs");
    }
    
    public void testCreateSourceOfAssocsWithMandatoryTargetsPresent() throws Exception
    {
        NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
        NodeRef target = createNode("target", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(source, target, TEST_ASSOC_NODE_ONE_ONE);
        
        NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null);
        NodeRef child = createNode("child", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.addChild(parent, child, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one"));
        
        NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null);
        NodeRef aspectTarget = createNode("aspectTarget", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(aspectSource, aspectTarget, TEST_ASSOC_ASPECT_ONE_ONE);
        
        checkIntegrityNoFailure();
    }
    public void testCreateSourceOfAssocsWithMandatoryTargetsMissing() throws Exception
    {
        NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
        
        NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null);
        NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null);
        
        checkIntegrityExpectFailure("Failed to detect missing assoc targets", 3);
    }
    public void testRemoveTargetsOfMandatoryAssocs() throws Exception
    {
        NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
        NodeRef target = createNode("target", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(source, target, TEST_ASSOC_NODE_ONE_ONE);
        
        NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null);
        NodeRef child = createNode("child", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.addChild(parent, child, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one"));
        
        NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null);
        NodeRef aspectTarget = createNode("aspectTarget", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(aspectSource, aspectTarget, TEST_ASSOC_ASPECT_ONE_ONE);
        
        checkIntegrityNoFailure();
        
        // remove target nodes
        nodeService.deleteNode(target);
        nodeService.deleteNode(child);
        nodeService.deleteNode(aspectTarget);
        
        checkIntegrityExpectFailure("Failed to detect removal of mandatory assoc targets", 3);
    }
    public void testExcessTargetsOfOneToOneAssocs() throws Exception
    {
        NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
        NodeRef target1 = createNode("target1", TEST_TYPE_WITHOUT_ANYTHING, null);
        NodeRef target2 = createNode("target2", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(source, target1, TEST_ASSOC_NODE_ONE_ONE);
        nodeService.createAssociation(source, target2, TEST_ASSOC_NODE_ONE_ONE);
        
        NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null);
        NodeRef child1 = createNode("child1", TEST_TYPE_WITHOUT_ANYTHING, null);
        NodeRef child2 = createNode("child2", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.addChild(parent, child1, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one-first"));
        nodeService.addChild(parent, child2, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one-second"));
        
        NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null);
        NodeRef aspectTarget1 = createNode("aspectTarget1", TEST_TYPE_WITHOUT_ANYTHING, null);
        NodeRef aspectTarget2 = createNode("aspectTarget2", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(aspectSource, aspectTarget1, TEST_ASSOC_ASPECT_ONE_ONE);
        nodeService.createAssociation(aspectSource, aspectTarget2, TEST_ASSOC_ASPECT_ONE_ONE);
        
        checkIntegrityExpectFailure("Failed to detect excess target cardinality for one-to-one assocs", 3);
    }
    public void testExcessTargetsOfOneToManyAssocs() throws Exception
    {
        NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
        NodeRef target1 = createNode("target1", TEST_TYPE_WITHOUT_ANYTHING, null);
        NodeRef target2 = createNode("target2", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(source, target1, TEST_ASSOC_NODE_ONE_MANY);
        nodeService.createAssociation(source, target2, TEST_ASSOC_NODE_ONE_MANY);
        
        checkIntegrityExpectFailure("Failed to detect excess source cardinality for one-to-many assocs", 1);
    }
    public void testSourceAssocAfterDeletion() throws Exception
    {
        NodeRef source1 = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
        NodeRef source2 = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
        NodeRef target1 = createNode("target1", TEST_TYPE_WITHOUT_ANYTHING, null);
        NodeRef target2 = createNode("target1", TEST_TYPE_WITHOUT_ANYTHING, null);
        nodeService.createAssociation(source1, target1, TEST_ASSOC_NODE_ONE_ONE);
        nodeService.createAssociation(source2, target2, TEST_ASSOC_NODE_ONE_ONE);
        checkIntegrityNoFailure();
        nodeService.createAssociation(source1, target1, TEST_ASSOC_NODE_ONE_MANY);
        nodeService.createAssociation(source2, target1, TEST_ASSOC_NODE_ONE_MANY);
        // Both (or either of) the associations are in violation
        checkIntegrityExpectFailure("Failed to detect excess source cardinality for one-to-many assocs", 1);
        
        // Now remove one of the associations
        nodeService.removeAssociation(source2, target1, TEST_ASSOC_NODE_ONE_MANY);
        checkIntegrityNoFailure();
        
        // Now remove the last association
        nodeService.removeAssociation(source1, target1, TEST_ASSOC_NODE_ONE_MANY);
        checkIntegrityNoFailure();
    }
    
    /**
     * Test for MNT-12367
     */
    public void testRelaxedMandatoryAssociation() throws Exception
    {
        assertEquals("Per-transaction override not correct", false, IntegrityChecker.isWarnInTransaction());
        // create node
        NodeRef nodeRef = createNode("relaxedAssocNode", TEST_TYPE_WITH_NON_ENFORCED_TARGET_ASSOCS, null);
        checkIntegrityNoFailure();
    }
}