/*
 * Copyright (c) 2018, 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.am.integration.tests.application.groupSharing;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.am.integration.clients.store.api.v1.dto.ApplicationDTO;
import org.wso2.am.integration.clients.store.api.v1.dto.ApplicationInfoDTO;
import org.wso2.am.integration.test.impl.RestAPIStoreImpl;
import org.wso2.am.integration.test.utils.UserManagementUtils;
import org.wso2.am.integration.test.utils.base.APIMIntegrationBaseTest;
import org.wso2.am.integration.test.utils.base.APIMIntegrationConstants;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.automation.test.utils.http.client.HttpResponse;

import java.util.List;

import static org.testng.Assert.assertFalse;
import static org.wso2.am.integration.test.utils.base.APIMIntegrationConstants.SUPER_TENANT_DOMAIN;

public class ApplicationSharingTestCase extends APIMIntegrationBaseTest {

    private static final String APPLICATION_NAME = "TestApplication";
    private static final String USER_ONE = "userOne";
    private static final String USER_TWO = "userTwo";
    private static final String PASSWORD = "test@123";
    private static final String FIRST_NAME = "John";
    private static final String LAST_NAME = "Doe";
    private static final String EMAIL = "john.doe@test.com";
    private static final String ORGANIZATION = "Test";
    private String userOneApplicationId;
    private String userTwoApplicationId;
    private RestAPIStoreImpl restAPIStoreClientUser1;
    private RestAPIStoreImpl restAPIStoreClientUser2;
    private static final Log log = LogFactory.getLog(ApplicationSharingTestCase.class);

    @Factory(dataProvider = "userModeDataProvider")
    public ApplicationSharingTestCase(TestUserMode userMode) {

        this.userMode = userMode;
    }

    @DataProvider
    public static Object[][] userModeDataProvider() {
        return new Object[][]{
                new Object[]{TestUserMode.SUPER_TENANT_ADMIN}
        };
    }

    @BeforeClass(alwaysRun = true)
    public void setEnvironment() throws Exception {

        super.init(userMode);
        createUsersAndApplications();
    }

    @Test(groups = "wso2.am", description = "Remove user one's application and check if user two's application also " +
            "is getting deleted")
    public void testUserTwoApplicationRemoval() throws Exception {

        restAPIStoreClientUser1.removeApplicationById(userOneApplicationId);
        List<ApplicationInfoDTO> user2AllAppsList = restAPIStoreClientUser2.getAllApps().getList();
        boolean isUserTwoAppDeleted =
                user2AllAppsList.stream()
                        .noneMatch(applicationInfoDTO -> userTwoApplicationId.equals(applicationInfoDTO.getApplicationId()));
        assertFalse(isUserTwoAppDeleted, "Deletion of User One's application has deleted User Two's " +
                "application too");
    }

    @AfterClass(alwaysRun = true)
    public void destroy() throws Exception {
        restAPIStoreClientUser2.removeApplicationById(userTwoApplicationId);
        super.cleanUp();
    }

    private void createUsersAndApplications() throws Exception {
        //signup of user one
        UserManagementUtils.signupUser(USER_ONE, PASSWORD, FIRST_NAME, ORGANIZATION);
        //signup of user two
        UserManagementUtils.signupUser(USER_TWO, PASSWORD, FIRST_NAME, ORGANIZATION);

        restAPIStoreClientUser1 = new RestAPIStoreImpl(USER_ONE, PASSWORD, SUPER_TENANT_DOMAIN, storeURLHttps);
        restAPIStoreClientUser2 = new RestAPIStoreImpl(USER_TWO, PASSWORD, SUPER_TENANT_DOMAIN, storeURLHttps);

        // Create Application for user one
        HttpResponse appCreationResponse1 = restAPIStoreClientUser1.createApplication(APPLICATION_NAME,
                "App created by user1", APIMIntegrationConstants.APPLICATION_TIER.UNLIMITED,
                ApplicationDTO.TokenTypeEnum.JWT);
        userOneApplicationId = appCreationResponse1.getData();

        // Create Application for user two
        HttpResponse appCreationResponse2 = restAPIStoreClientUser2.createApplication(APPLICATION_NAME,
                "App created by user2", APIMIntegrationConstants.APPLICATION_TIER.UNLIMITED,
                ApplicationDTO.TokenTypeEnum.JWT);
        userTwoApplicationId = appCreationResponse2.getData();
    }
}
