package it.com.atlassian.stash.admin;

import com.atlassian.bitbucket.test.LicenseTestHelper;
import com.atlassian.bitbucket.test.BitbucketTestLicense;
import com.atlassian.bitbucket.test.rules.LicenseCountRule;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.webdriver.bitbucket.element.PermissionTable;
import com.atlassian.webdriver.bitbucket.page.admin.GlobalPermissionsPage;
import com.jayway.restassured.path.json.JsonPath;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TestRule;

import java.util.List;

import static com.atlassian.bitbucket.test.TestRuleOrderingSyntax.outerRule;
import static com.atlassian.webdriver.bitbucket.page.PermissionsPage.PermissionEntityType;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;

/**
 * Tries to grant the 'licensed user' permission to more users than the license allows.
 *
 * @see GlobalPermissionsTest
 */
public class GlobalPermissionsLicenseExceededTest extends BaseBrowserFuncTest {

    private static final String PREFIX = "testUPLET";

    private static final BitbucketTestLicense LICENSE = BitbucketTestLicense.builder()
            .whenClustered(
                    // clustered timebound license valid for 1 hour and 5 users
                    "AAABAA0ODAoPeNptkN1qg0AQhe99CqHXBjUJmIDQ1J/UVqO0tvfTZdIs6BpmVmnevovdQAO5PWe+m\n" +
                            "XPmISfppijcIHSD5daPtuuNu69aN/SDlcMa+LTIJuhG0HJQ8RE6RisnhLOYgsbYjIeev/SCzdUdl\n" +
                            "AahD9BjrJE1kjUqkEqjAiUw+zlLusx8Ezxb30ByMgyNeLsqM2Q373oE3QGzBLUQQ2+nSilQMbaXM\n" +
                            "85Hk7qqsrek2JXOO9KEVKTxU51H3mcZ5l4SrfZe8dq+WLoB0gppJq1U0zcoyX/Fd9eL1jShIUFTh\n" +
                            "P4nbUYSJ2C8/xKb8G7rw9h/IdXHD0bieO38AjlLjEEwLAIUFSD1ZO0ovRdejglSU1CRlqXUukECF\n" +
                            "Fm6ux0/AQbXxCCl1nsrLhQJ0AnrX02d5")
            .whenNonClustered(
                    // standalone timebound license valid for 1 hour and 5 users
                    "AAAA9w0ODAoPeNptkEFPwkAQhe/8ik08L2mrRiBpIpai1ZY2UriPm0E2abdkZtvIv7euywHj9b33z\n" +
                            "ZuZm6IzogAS4VyEwSIKFsGDSLe1iIIwmrAFPk7TAZoerO5MfICG0csJoRNXYDH+icvgVobzi9sZC\n" +
                            "8puoMXYIlskbxSgjUUDRmH6ddJ0dnwVvnh/hPQwMtTj9ah0JBs36xFsA8wazFR1rU/lWqFhrM8nd\n" +
                            "KVJWRTpe5It88kWaUDKVvFTuZ7JfR6tZTK7e5bZW/3q6QrIGiRHeqmkTzCafw9fXhq9uenbD6Tys\n" +
                            "GMkju+vd/j3rqondQTGv+/6BrxNhCQwLAIUYi8tqfX0CjHl2cY08fJzlA/s9NkCFH2IX1u9n47Xb\n" +
                            "R2R4667iet5SJIpX02co")
            .build();

    private static final int TEST_LICENSE_LIMIT = 5;

    private String license;

    @Override
    protected List<TestRule> getRetryRuleChain() {
        return outerRule(new LicenseCountRule()).
               around(super.getRetryRuleChain());
    }

    @Before
    public void setup() {
        JsonPath licenseJson = LicenseTestHelper.getLicenseJson();
        license = licenseJson.getString("license");

        LicenseTestHelper.setLicense(LICENSE.get());

        for (int i = 2; i < TEST_LICENSE_LIMIT + 1; i++) { // create one more user than the limit of the license
            String user = PREFIX + i;
            testContext.user(user, user, "password", user + "@example.com", i < TEST_LICENSE_LIMIT);
        }
    }

    @After
    public void tearDown() {
        LicenseTestHelper.setLicense(license);
    }

    @Test
    public void testUserPickerWarning() throws Exception {
        String user = PREFIX + TEST_LICENSE_LIMIT;
        PermissionTable userPermissions = loginAsAdmin(GlobalPermissionsPage.class)
                .getPermissionsTable(PermissionEntityType.USER)
                .selectItem(user)
                .selectPermission(Permission.LICENSED_USER)
                .clickAddExpectingError();

        assertThat(userPermissions.getErrors(),
                hasItem(String.format("You cannot grant this permission to user %s as it would exceed your license limit of %d.", user, TEST_LICENSE_LIMIT)));
    }
}
