/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 * 
 * This 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 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This software 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.keycloak.testsuite.console.clients;

import java.util.ArrayList;
import java.util.List;
import org.jboss.arquillian.graphene.page.Page;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.testsuite.console.page.clients.settings.ClientSettings;
import static org.keycloak.testsuite.util.URLAssert.assertCurrentUrlEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

/**
 *
 * @author <a href="mailto:vramik@redhat.com">Vlastislav Ramik</a>
 */
public class ClientsTest extends AbstractClientTest {

    private ClientRepresentation newClient;
    
    @Page
    private ClientSettings clientSettingsPage;
    
    @Before
    public void beforeClientsTest() {
        newClient = createClientRepresentation(TEST_CLIENT_ID, TEST_REDIRECT_URIS);
        testRealmResource().clients().create(newClient).close();
        
        ClientRepresentation found = findClientRepByClientId(TEST_CLIENT_ID);
        assertNotNull("Client " + TEST_CLIENT_ID + " was not found.", found);
        clientSettingsPage.setId(found.getId());
    }
    
    @Test
    public void clientsCRUD() {
        //create
        clientsPage.table().createClient();
        assertCurrentUrlEquals(createClientPage);
        
        //edit
        clientsPage.navigateTo();
        clientsPage.table().editClient(TEST_CLIENT_ID);
        assertEquals(TEST_CLIENT_ID, clientSettingsPage.form().getClientId());
        
        //delete
        clientsPage.navigateTo();
        clientsPage.table().deleteClient(TEST_CLIENT_ID);
        modalDialog.confirmDeletion();
        assertFlashMessageSuccess();
        
        ClientRepresentation found = findClientRepByClientId(TEST_CLIENT_ID);
        assertNull("Deleted client " + TEST_CLIENT_ID + " was found.", found);
    }
    
    @Test
    public void tabs() {
        clientSettingsPage.navigateTo();
        
        //oidc-confidential
        WebElement tabs = clientPage.tabs().getTabs();
        List<String> visibleTabs = new ArrayList<>();
        visibleTabs.add("Settings");
        visibleTabs.add("Credentials");
        visibleTabs.add("Roles");
        visibleTabs.add("Mappers");
        visibleTabs.add("Scope");
        visibleTabs.add("Revocation");
        visibleTabs.add("Sessions");
        visibleTabs.add("Offline Access");
        visibleTabs.add("Clustering");
        visibleTabs.add("Installation");
        
        List<String> invisibleTabs = new ArrayList<>();
        invisibleTabs.add("SAML Keys");
        invisibleTabs.add("Service Account Roles");
        
        assertVisibilityOfTabs(tabs, visibleTabs, invisibleTabs);
        
        //oidc-public
        newClient.setPublicClient(true);
        clientSettingsPage.form().setAccessType(newClient);
        clientSettingsPage.form().save();
        assertFlashMessageSuccess();
        
        tabs = clientPage.tabs().getTabs();
        visibleTabs.clear();
        invisibleTabs.clear();
        
        visibleTabs.add("Settings");
        visibleTabs.add("Roles");
        visibleTabs.add("Mappers");
        visibleTabs.add("Scope");
        visibleTabs.add("Revocation");
        visibleTabs.add("Sessions");
        visibleTabs.add("Offline Access");
        visibleTabs.add("Installation");
        
        invisibleTabs.add("Credentials");
        invisibleTabs.add("SAML Keys");
        invisibleTabs.add("Clustering");
        invisibleTabs.add("Service Account Roles");
        
        assertVisibilityOfTabs(tabs, visibleTabs, invisibleTabs);
        
        //oidc-bearer-only
        newClient.setPublicClient(false);
        newClient.setBearerOnly(true);
        clientSettingsPage.form().setAccessType(newClient);
        clientSettingsPage.form().save();
        assertFlashMessageSuccess();
        
        tabs = clientPage.tabs().getTabs();
        visibleTabs.clear();
        invisibleTabs.clear();
        
        visibleTabs.add("Settings");
        visibleTabs.add("Credentials");
        visibleTabs.add("Roles");
        visibleTabs.add("Revocation");
        visibleTabs.add("Clustering");
        visibleTabs.add("Installation");
        
        invisibleTabs.add("SAML Keys");
        invisibleTabs.add("Mappers");
        invisibleTabs.add("Scope");
        invisibleTabs.add("Sessions");
        invisibleTabs.add("Service Account Roles");
        
        assertVisibilityOfTabs(tabs, visibleTabs, invisibleTabs);
    }
    
    private void assertVisibilityOfTabs(WebElement tabs, List<String> visible, List<String> invisible) {
        for (String visibleLink : visible) {
            assertTrue(tabs.findElement(By.linkText(visibleLink)).isDisplayed());
        }
        for (String invisibleLink : invisible) {
            assertTrue(tabs.findElements(By.linkText(invisibleLink)).isEmpty());
        }
    }
}
