<commit_before>/*
 * Copyright (c) 2015, 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.appserver.integration.tests.multitenancy;

import org.apache.commons.httpclient.HttpStatus;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.appserver.integration.common.clients.AuthenticateStubUtil;
import org.wso2.appserver.integration.common.clients.ResourceAdminServiceClient;
import org.wso2.appserver.integration.common.clients.WebAppAdminClient;
import org.wso2.appserver.integration.common.utils.ASIntegrationTest;
import org.wso2.appserver.integration.common.utils.WebAppDeploymentUtil;
import org.wso2.appserver.integration.common.utils.WebAppTypes;
import org.wso2.carbon.automation.engine.frameworkutils.FrameworkPathUtil;
import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil;
import org.wso2.carbon.integration.common.admin.client.TenantManagementServiceClient;
import org.wso2.carbon.integration.common.utils.exceptions.AutomationUtilException;
import org.wso2.carbon.registry.resource.stub.common.xsd.ResourceData;
import org.wso2.carbon.security.mgt.stub.keystore.GetKeyStoresResponse;
import org.wso2.carbon.security.mgt.stub.keystore.KeyStoreAdminServiceStub;
import org.wso2.carbon.security.mgt.stub.keystore.xsd.KeyStoreData;
import org.wso2.carbon.tenant.mgt.stub.TenantMgtAdminServiceExceptionException;
import org.wso2.carbon.tenant.mgt.stub.beans.xsd.TenantInfoBean;

import java.io.File;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.GregorianCalendar;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

/**
 * This class contains test cases to test the different use cases of multi-tenancy
 */
public class MultiTenancyTestCase extends ASIntegrationTest {

	private final String FIRST_TENANT_DOMAIN = "multitenancytest1.com";
	private final String FIRST_TENANT_USER = "testuser1";
	private final String SECOND_TENANT_DOMAIN = "multitenancytest2.com";
	private final String SECOND_TENANT_USER = "testuser2";
	private final String THIRD_TENANT_DOMAIN = "multitenancytest3.com";
	private final String THIRD_TENANT_USER = "testuser3";
	private WebAppAdminClient webAppAdminClient;
	private TenantManagementServiceClient tenantManagementServiceClient;
	private TenantInfoBean tenantInfoBean;

	@BeforeClass(alwaysRun = true)
	public void init() throws Exception {
		super.init();
		tenantManagementServiceClient = new TenantManagementServiceClient(backendURL, loginLogoutClient.login());
	}

	@Test(groups = "wso2.as", description = "Test the information after adding a new tenant")
	public void testAddTenant() throws Exception {

		tenantManagementServiceClient = new TenantManagementServiceClient(backendURL, loginLogoutClient.login());
		tenantManagementServiceClient.addTenant(FIRST_TENANT_DOMAIN, "admin123", FIRST_TENANT_USER, "Demo");

		tenantInfoBean = tenantManagementServiceClient.getTenant(FIRST_TENANT_DOMAIN);
		assertNotNull(tenantInfoBean, "Tenant was not added properly");
		assertTrue(tenantInfoBean.getActive(), "The added tenant is not active");
		assertEquals(tenantInfoBean.getFirstname(), FIRST_TENANT_USER,
		             "Tenant first name is not matching with the added one");
		assertEquals(tenantInfoBean.getUsagePlan(), "", "Tenant usage plan is not matching with the one that added");

	}

	@Test(groups = "wso2.as", description = "Test logging to the added tenant and test tenant keystore",
			dependsOnMethods = "testAddTenant")
	public void testTenantLogin() throws Exception {
		sessionCookie = loginLogoutClient.login(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN, "admin123",
		                                        asServer.getInstance().getHosts().get("default"));
		assertNotNull(sessionCookie, "Cannot login to the added tenant");

		String certificate = FIRST_TENANT_DOMAIN.replace('.', '-') + ".jks";

		ResourceAdminServiceClient resourceAdminServiceClient =
				new ResourceAdminServiceClient(backendURL, sessionCookie);
		ResourceData[] resourceData = resourceAdminServiceClient
				.getResource("/_system/governance/repository/security/key-stores/" + certificate);
		assertNotNull(resourceData, "Tenant key store is not created");

		//Verify tenant key store using stub
		String serviceName = "KeyStoreAdminService";
		String endPoint = backendURL + serviceName;

		KeyStoreAdminServiceStub keyStoreAdminServiceStub = new KeyStoreAdminServiceStub(endPoint);
		AuthenticateStubUtil.authenticateStub(sessionCookie, keyStoreAdminServiceStub);

		GetKeyStoresResponse getKeyStoresResponse = keyStoreAdminServiceStub.getKeyStores();
		KeyStoreData keyStoreData = getKeyStoresResponse.get_return()[0];

		assertNotNull(keyStoreData, "Tenant key store is null");
		assertEquals(keyStoreData.getKeyStoreName(), certificate, "Invalid tenant key store name");
	}

	@Test(groups = "wso2.as", description = "Test adding duplicate tenant",	dependsOnMethods = "testTenantLogin",
            expectedExceptions = { TenantMgtAdminServiceExceptionException.class },
			expectedExceptionsMessageRegExp = "TenantMgtAdminServiceExceptionException")
	public void testAddingDuplicateTenant() throws Exception {
		Date date = new Date();
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		TenantInfoBean newTenantInfoBean = new TenantInfoBean();
		newTenantInfoBean.setActive(true);
		newTenantInfoBean.setEmail(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN);
		newTenantInfoBean.setAdmin(FIRST_TENANT_USER);
		newTenantInfoBean.setAdminPassword("<PASSWORD>");
		newTenantInfoBean.setUsagePlan("Demo");
		newTenantInfoBean.setLastname(FIRST_TENANT_USER + "wso2automation");
		newTenantInfoBean.setSuccessKey("true");
		newTenantInfoBean.setCreatedDate(calendar);
		newTenantInfoBean.setTenantDomain(FIRST_TENANT_DOMAIN);
		newTenantInfoBean.setFirstname(FIRST_TENANT_USER);

		tenantManagementServiceClient.addTenant(newTenantInfoBean);
	}

	@Test(groups = "wso2.as", description = "Deactivate loaded tenant and attempt to log",
			dependsOnMethods = "testAddingDuplicateTenant", expectedExceptions = {
			AutomationUtilException.class }, expectedExceptionsMessageRegExp = "Error while login as " +
			                                                                   FIRST_TENANT_USER + "@" +
			                                                                   FIRST_TENANT_DOMAIN)
	public void testTenantDeactivation() throws Exception {
		tenantManagementServiceClient.deactivateTenant(FIRST_TENANT_DOMAIN);
		loginLogoutClient.login(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN, "admin123",
		                        asServer.getInstance().getHosts().get("default"));
	}

	//https://wso2.org/jira/browse/WSAS-2022
	@Test(enabled = false, groups = "wso2.as", description = "Test access a webapp deployed by a deactivated tenant and re-activate that tenant and access web app",
			dependsOnMethods = "testTenantDeactivation")
	public void testAccessWebappOfDeactivatedTenant() throws Exception {
		tenantManagementServiceClient.activateTenant(FIRST_TENANT_DOMAIN);
		sessionCookie = loginLogoutClient.login(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN, "admin123",
		                                        asServer.getInstance().getHosts().get("default"));
		webAppAdminClient = new WebAppAdminClient(backendURL, sessionCookie);
		tenantManagementServiceClient.deactivateTenant(FIRST_TENANT_DOMAIN);
		webAppAdminClient.uploadWarFile(
				FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "AS" + File.separator +
				"war" + File.separator + "HelloWorldWebapp.war");
		assertTrue(WebAppDeploymentUtil.isWebApplicationDeployed(backendURL, sessionCookie, "HelloWorldWebapp"),
		           "HelloWorldWebapp Deployment failed");
		String webAppURL = getWebAppURL(WebAppTypes.WEBAPPS) +
		                   "/t/" + FIRST_TENANT_DOMAIN +
		                   "/webapps/HelloWorldWebapp/";
		assertEquals(HttpRequestUtil.sendGetRequest(webAppURL, null).getResponseCode(), HttpStatus.SC_NOT_FOUND,
		             "After de-activating a tenant tenant web app is accessible");

		tenantManagementServiceClient.activateTenant(FIRST_TENANT_DOMAIN);
		assertEquals(HttpRequestUtil.sendGetRequest(webAppURL, null).getResponseCode(), HttpStatus.SC_OK,
		             "After activating the tenant tenant web app is not accessible");

	}

	@Test(groups = "wso2.as", dependsOnMethods = "testTenantDeactivation",
            description = "Test Login after deactivating unloaded tenant", expectedExceptions = {
            AutomationUtilException.class }, expectedExceptionsMessageRegExp = "Error while login as " +
                                                                               SECOND_TENANT_USER + "@" +
                                                                               SECOND_TENANT_DOMAIN)
    public void testLoginToUnloadedDeactivatedTenant() throws Exception {
		tenantManagementServiceClient.addTenant(SECOND_TENANT_DOMAIN, "admin123", SECOND_TENANT_USER, "Demo");
		//Deactivate unloaded Tenant
		tenantManagementServiceClient.deactivateTenant(SECOND_TENANT_DOMAIN);

		loginLogoutClient.login(SECOND_TENANT_USER + "@" + SECOND_TENANT_DOMAIN, "admin123",
		                        asServer.getInstance().getHosts().get("default"));
	}

	@Test(groups = "wso2.as", description = "Test Login after activating unloaded tenant",
			dependsOnMethods = "testLoginToUnloadedDeactivatedTenant")
	public void testLoginToReActivatedTenant() throws Exception {

		tenantManagementServiceClient.activateTenant(SECOND_TENANT_DOMAIN);

		TenantInfoBean tenantInfoBean = tenantManagementServiceClient.getTenant(SECOND_TENANT_DOMAIN);
		sessionCookie = loginLogoutClient.login(tenantInfoBean.getFirstname() + "@" + SECOND_TENANT_DOMAIN, "admin123",<commit_msg>Enabling Tenant deactivation test case  : WSAS-2022<commit_after>/*
 * Copyright (c) 2015, 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.appserver.integration.tests.multitenancy;

import org.apache.commons.httpclient.HttpStatus;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.appserver.integration.common.clients.AuthenticateStubUtil;
import org.wso2.appserver.integration.common.clients.ResourceAdminServiceClient;
import org.wso2.appserver.integration.common.clients.WebAppAdminClient;
import org.wso2.appserver.integration.common.utils.ASIntegrationTest;
import org.wso2.appserver.integration.common.utils.WebAppDeploymentUtil;
import org.wso2.appserver.integration.common.utils.WebAppTypes;
import org.wso2.carbon.automation.engine.frameworkutils.FrameworkPathUtil;
import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil;
import org.wso2.carbon.automation.test.utils.http.client.HttpResponse;
import org.wso2.carbon.integration.common.admin.client.TenantManagementServiceClient;
import org.wso2.carbon.integration.common.utils.exceptions.AutomationUtilException;
import org.wso2.carbon.registry.resource.stub.common.xsd.ResourceData;
import org.wso2.carbon.security.mgt.stub.keystore.GetKeyStoresResponse;
import org.wso2.carbon.security.mgt.stub.keystore.KeyStoreAdminServiceStub;
import org.wso2.carbon.security.mgt.stub.keystore.xsd.KeyStoreData;
import org.wso2.carbon.tenant.mgt.stub.TenantMgtAdminServiceExceptionException;
import org.wso2.carbon.tenant.mgt.stub.beans.xsd.TenantInfoBean;

import java.io.File;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.GregorianCalendar;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

/**
 * This class contains test cases to test the different use cases of multi-tenancy
 */
public class MultiTenancyTestCase extends ASIntegrationTest {

	private final String FIRST_TENANT_DOMAIN = "multitenancytest1.com";
	private final String FIRST_TENANT_USER = "testuser1";
	private final String SECOND_TENANT_DOMAIN = "multitenancytest2.com";
	private final String SECOND_TENANT_USER = "testuser2";
	private final String THIRD_TENANT_DOMAIN = "multitenancytest3.com";
	private final String THIRD_TENANT_USER = "testuser3";
	private WebAppAdminClient webAppAdminClient;
	private TenantManagementServiceClient tenantManagementServiceClient;
	private TenantInfoBean tenantInfoBean;

	@BeforeClass(alwaysRun = true)
	public void init() throws Exception {
		super.init();
		tenantManagementServiceClient = new TenantManagementServiceClient(backendURL, loginLogoutClient.login());
	}

	@Test(groups = "wso2.as", description = "Test the information after adding a new tenant")
	public void testAddTenant() throws Exception {

		tenantManagementServiceClient = new TenantManagementServiceClient(backendURL, loginLogoutClient.login());
		tenantManagementServiceClient.addTenant(FIRST_TENANT_DOMAIN, "admin123", FIRST_TENANT_USER, "Demo");

		tenantInfoBean = tenantManagementServiceClient.getTenant(FIRST_TENANT_DOMAIN);
		assertNotNull(tenantInfoBean, "Tenant was not added properly");
		assertTrue(tenantInfoBean.getActive(), "The added tenant is not active");
		assertEquals(tenantInfoBean.getFirstname(), FIRST_TENANT_USER,
		             "Tenant first name is not matching with the added one");
		assertEquals(tenantInfoBean.getUsagePlan(), "", "Tenant usage plan is not matching with the one that added");

	}

	@Test(groups = "wso2.as", description = "Test logging to the added tenant and test tenant keystore",
			dependsOnMethods = "testAddTenant")
	public void testTenantLogin() throws Exception {
		sessionCookie = loginLogoutClient.login(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN, "admin123",
		                                        asServer.getInstance().getHosts().get("default"));
		assertNotNull(sessionCookie, "Cannot login to the added tenant");

		String certificate = FIRST_TENANT_DOMAIN.replace('.', '-') + ".jks";

		ResourceAdminServiceClient resourceAdminServiceClient =
				new ResourceAdminServiceClient(backendURL, sessionCookie);
		ResourceData[] resourceData = resourceAdminServiceClient
				.getResource("/_system/governance/repository/security/key-stores/" + certificate);
		assertNotNull(resourceData, "Tenant key store is not created");

		//Verify tenant key store using stub
		String serviceName = "KeyStoreAdminService";
		String endPoint = backendURL + serviceName;

		KeyStoreAdminServiceStub keyStoreAdminServiceStub = new KeyStoreAdminServiceStub(endPoint);
		AuthenticateStubUtil.authenticateStub(sessionCookie, keyStoreAdminServiceStub);

		GetKeyStoresResponse getKeyStoresResponse = keyStoreAdminServiceStub.getKeyStores();
		KeyStoreData keyStoreData = getKeyStoresResponse.get_return()[0];

		assertNotNull(keyStoreData, "Tenant key store is null");
		assertEquals(keyStoreData.getKeyStoreName(), certificate, "Invalid tenant key store name");
	}

	@Test(groups = "wso2.as", description = "Test adding duplicate tenant",	dependsOnMethods = "testTenantLogin",
            expectedExceptions = { TenantMgtAdminServiceExceptionException.class },
			expectedExceptionsMessageRegExp = "TenantMgtAdminServiceExceptionException")
	public void testAddingDuplicateTenant() throws Exception {
		Date date = new Date();
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		TenantInfoBean newTenantInfoBean = new TenantInfoBean();
		newTenantInfoBean.setActive(true);
		newTenantInfoBean.setEmail(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN);
		newTenantInfoBean.setAdmin(FIRST_TENANT_USER);
		newTenantInfoBean.setAdminPassword("<PASSWORD>");
		newTenantInfoBean.setUsagePlan("Demo");
		newTenantInfoBean.setLastname(FIRST_TENANT_USER + "wso2automation");
		newTenantInfoBean.setSuccessKey("true");
		newTenantInfoBean.setCreatedDate(calendar);
		newTenantInfoBean.setTenantDomain(FIRST_TENANT_DOMAIN);
		newTenantInfoBean.setFirstname(FIRST_TENANT_USER);

		tenantManagementServiceClient.addTenant(newTenantInfoBean);
	}

	@Test(groups = "wso2.as", description = "Deactivate loaded tenant and attempt to log",
			dependsOnMethods = "testAddingDuplicateTenant", expectedExceptions = {
			AutomationUtilException.class }, expectedExceptionsMessageRegExp = "Error while login as " +
			                                                                   FIRST_TENANT_USER + "@" +
			                                                                   FIRST_TENANT_DOMAIN)
	public void testTenantDeactivation() throws Exception {
		tenantManagementServiceClient.deactivateTenant(FIRST_TENANT_DOMAIN);
		loginLogoutClient.login(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN, "admin123",
		                        asServer.getInstance().getHosts().get("default"));
	}

	@Test(enabled = true, groups = "wso2.as", description = "Test access a webapp deployed by a deactivated tenant " +
			"and re-activate that tenant and access web app",
			dependsOnMethods = "testTenantDeactivation")
	public void testAccessWebappOfDeactivatedTenant() throws Exception {
		tenantManagementServiceClient.activateTenant(FIRST_TENANT_DOMAIN);
		sessionCookie = loginLogoutClient.login(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN, "admin123",
		                                        asServer.getInstance().getHosts().get("default"));
		webAppAdminClient = new WebAppAdminClient(backendURL, sessionCookie);
		webAppAdminClient.uploadWarFile(
				FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "AS" + File.separator +
						"war" + File.separator + "HelloWorldWebapp.war");
		assertTrue(WebAppDeploymentUtil.isWebApplicationDeployed(backendURL, sessionCookie, "HelloWorldWebapp"),
				"HelloWorldWebapp Deployment failed");
		tenantManagementServiceClient.deactivateTenant(FIRST_TENANT_DOMAIN);
		String webAppURL = getWebAppURL(WebAppTypes.WEBAPPS) +
		                   "/t/" + FIRST_TENANT_DOMAIN +
		                   "/webapps/HelloWorldWebapp/";
		HttpResponse response = HttpRequestUtil.sendGetRequest(webAppURL, null);
		
		//todo check the correct response status code once WSAS-2094 is fixed
		assertEquals(response.getData(), "",
		             "After de-activating a tenant tenant web app is accessible");

		tenantManagementServiceClient.activateTenant(FIRST_TENANT_DOMAIN);
		assertEquals(HttpRequestUtil.sendGetRequest(webAppURL, null).getResponseCode(), HttpStatus.SC_OK,
		             "After activating the tenant tenant web app is not accessible");

	}

	@Test(groups = "wso2.as", dependsOnMethods = "testTenantDeactivation",
            description = "Test Login after deactivating unloaded tenant", expectedExceptions = {
            AutomationUtilException.class }, expectedExceptionsMessageRegExp = "Error while login as " +
                                                                               SECOND_TENANT_USER + "@" +
                                                                               SECOND_TENANT_DOMAIN)
    public void testLoginToUnloadedDeactivatedTenant() throws Exception {
		tenantManagementServiceClient.addTenant(SECOND_TENANT_DOMAIN, "admin123", SECOND_TENANT_USER, "Demo");
		//Deactivate unloaded Tenant
		tenantManagementServiceClient.deactivateTenant(SECOND_TENANT_DOMAIN);

		loginLogoutClient.login(SECOND_TENANT_USER + "@" + SECOND_TENANT_DOMAIN, "admin123",
		                        asServer.getInstance().getHosts().get("default"));
	}

	@Test(groups = "wso2.as", description = "Test Login after activating unloaded tenant",
			dependsOnMethods = "testLoginToUnloadedDeactivatedTenant")
	public void testLoginToReActivatedTenant() throws Exception {

		tenantManagementServiceClient.activateTenant(SECOND_TENANT_DOMAIN);

		TenantInfoBean tenantInfoBean = tenantManagementServiceClient.getTenant(SECOND_TENANT_DOMAIN);
		sessionCookie = loginLogoutClient.login(tenantInfoBean.getFirstname() + "@" + SECOND_TENANT_DOMAIN, "admin123",
{'fraction_non_alphanumeric': 0.09004940014114327, 'fraction_numerical': 0.006680780992707598, 'mean_word_length': 4.782372143634385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7494010', 'n_tokens_mistral': 6443, 'n_tokens_neox': 6016, 'n_words': 1226}