/*
*  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.identity.integration.test.provisioning;

import junit.framework.Assert;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.frameworkutils.FrameworkPathUtil;
import org.wso2.carbon.identity.application.common.model.idp.xsd.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.idp.xsd.Property;
import org.wso2.carbon.identity.application.common.model.idp.xsd.ProvisioningConnectorConfig;
import org.wso2.carbon.integration.common.admin.client.LogViewerClient;
import org.wso2.carbon.integration.common.utils.mgt.ServerConfigurationManager;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.identity.integration.common.clients.Idp.IdentityProviderMgtServiceClient;
import org.wso2.identity.integration.common.clients.TenantManagementServiceClient;
import org.wso2.identity.integration.common.utils.ISIntegrationTest;

import java.io.File;

/**
 * This test case tests provisioning functionality when UM tables and IDN tables are separated into two databases
 */
public class DBSeperationTestCase extends ISIntegrationTest {

    private static final String TENANT_IDP = "tenantIdp";
    private static final String TENANT_ADMIN = "admin";
    private File masterDatasourceXml;
    private File identityXml;
    private File userMgtXml;
    private File registryXml;
    private ServerConfigurationManager serverConfigurationManager;
    private IdentityProviderMgtServiceClient identityProviderMgtServiceClient;
    private TenantManagementServiceClient tenantServiceClient;
    private String TENANT_DOMAIN = "tenant.com";
    private LogViewerClient logViewer;
    private String serverConfDir;
    private String artifactsDir;

    @BeforeClass(alwaysRun = true)
    public void setUp() throws Exception {
        super.init();

        serverConfDir = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "conf";
        artifactsDir = FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "IS" + File
                .separator + "provisioning";

        masterDatasourceXml = new File(serverConfDir + File.separator + "datasources" + File.separator +
                "master-datasources.xml");
        File masterDatasourcesXmlToCopy = new File(artifactsDir + File.separator + "master-datasources-dbseperated" +
                ".xml");

        identityXml = new File(serverConfDir + File.separator + "identity" + File.separator + "identity.xml");
        File identityXmlToCopy = new File(artifactsDir + File.separator + "identity-dbseperated.xml");

        userMgtXml = new File(serverConfDir + File.separator + "user-mgt.xml");
        File userMgtXmlToCopy = new File(artifactsDir + File.separator + "user-mgt-dbseperated.xml");

        registryXml = new File(serverConfDir + File.separator + "registry.xml");
        File registryXmlToCopy = new File(artifactsDir + File.separator + "registry-dbseperated.xml");

        serverConfigurationManager = new ServerConfigurationManager(isServer);
        serverConfigurationManager.applyConfigurationWithoutRestart(masterDatasourcesXmlToCopy, masterDatasourceXml,
                true);
        serverConfigurationManager.applyConfigurationWithoutRestart(identityXmlToCopy, identityXml, true);
        serverConfigurationManager.applyConfigurationWithoutRestart(userMgtXmlToCopy, userMgtXml, true);
        serverConfigurationManager.applyConfigurationWithoutRestart(registryXmlToCopy, registryXml,
                true);
        serverConfigurationManager.restartGracefully();

        super.init();

        tenantServiceClient = new TenantManagementServiceClient(backendURL, sessionCookie);
        tenantServiceClient.addTenant(TENANT_DOMAIN, TENANT_ADMIN, "password", TENANT_ADMIN + "@" + TENANT_DOMAIN,
                TENANT_ADMIN, "User");
        identityProviderMgtServiceClient = new IdentityProviderMgtServiceClient(TENANT_ADMIN + "@" + TENANT_DOMAIN,
                "password", backendURL);
        logViewer = new LogViewerClient(backendURL, getSessionCookie());
    }

    @AfterClass(alwaysRun = true)
    public void tearDown() throws Exception {
        File masterDatasourcesXmlToCopy = new File(artifactsDir + File.separator + "master-datasources-default.xml");
        File identityXmlToCopy = new File(getISResourceLocation() + File.separator + "default-identity.xml");
        File userMgtXmlToCopy = new File(artifactsDir + File.separator + "user-mgt-default.xml");
        File registryXmlToCopy = new File(artifactsDir + File.separator + "registry-default.xml");

        serverConfigurationManager.applyConfigurationWithoutRestart(masterDatasourcesXmlToCopy, masterDatasourceXml,
                true);
        serverConfigurationManager.applyConfigurationWithoutRestart(identityXmlToCopy, identityXml, true);
        serverConfigurationManager.applyConfigurationWithoutRestart(userMgtXmlToCopy, userMgtXml, true);
        serverConfigurationManager.applyConfigurationWithoutRestart(registryXmlToCopy, registryXml,
                true);
        serverConfigurationManager.restartGracefully();
    }

    @Test(alwaysRun = true, description = "Testing update Identity Provider")
    public void updateIdpTest() {
        try {
            IdentityProvider identityProvider = new IdentityProvider();
            identityProvider.setIdentityProviderName("tenantIdp");
            ProvisioningConnectorConfig scimConfig = new ProvisioningConnectorConfig();
            scimConfig.setName("scim");
            Property userProperty = new Property();
            userProperty.setName("scim-username");
            userProperty.setValue("admin");
            scimConfig.addProvisioningProperties(userProperty);
            identityProvider.setProvisioningConnectorConfigs(new ProvisioningConnectorConfig[]{scimConfig});
            identityProviderMgtServiceClient.addIdP(identityProvider);
            identityProviderMgtServiceClient.deleteIdP(TENANT_IDP);
            Assert.assertFalse("Not functioning when UM tables and Identity tables seperated into two databases.",
                    logViewer.getAllRemoteSystemLogs()[0].getPriority().equals("ERROR"));
        } catch (Exception e) {
            Assert.fail("Identity Provider addition or deletion failed at db seperation test.");
        }

    }
}

@@ -750,123 +750,8 @@
 rt;%0A
-import org.apache.axis2.context.ConfigurationContext;%0Aimport org.apache.axis2.context.ConfigurationContextFactory;%0A
 impo
@@ -6740,23 +6740,48 @@
 se(%22
-Not functioning
+Failed to delete IdP: %22 + TENANT_IDP + %22
  whe
@@ -6812,20 +6812,49 @@
  tables 
+are %22 +%0A                    %22
 sep
-e
+a
 rated in
@@ -6872,16 +6872,75 @@
 bases.%22,
+ logViewer.getAllRemoteSystemLogs()%5B0%5D.getPriority().equals
 %0A       
@@ -6947,24 +6947,37 @@
             
+ (%22ERROR%22) &&
  logViewer.g
@@ -7010,32 +7010,89 @@
 .get
-Priority().equals(%22ERROR
+Message().contains(%22Error when provisioning%22 +%0A                    %22 IDP deletion
 %22));
@@ -7203,14 +7203,20 @@
 at d
-b
+atabase
  sep
-e
+a
 rati

Fix DBSeperationTestCase assert statement.