/*
 * 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.jboss.as.domain.management.security.adduser;

import org.jboss.as.domain.management.logging.DomainManagementLogger;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertTrue;

/**
 * Test the duplicated user check state
 *
 * @author <a href="mailto:flemming.harms@gmail.com">Flemming Harms</a>
 */
public class DuplicateUserCheckStateTestCase extends PropertyTestHelper {

    @Test
    public void newUser() throws IOException {
        values.setExistingUser(false);
        values.setGroups(ROLES);
        PreModificationState userCheckState = new PreModificationState(consoleMock, values);

        AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().
                expectedDisplayText(DomainManagementLogger.ROOT_LOGGER.aboutToAddUser(values.getUserName(), values.getRealm())).
                expectedDisplayText(AddUser.NEW_LINE).
                expectedDisplayText(DomainManagementLogger.ROOT_LOGGER.isCorrectPrompt() + " " + DomainManagementLogger.ROOT_LOGGER.yes() + "/" + DomainManagementLogger.ROOT_LOGGER.no() + "?").
                expectedDisplayText(" ").
                expectedInput(DomainManagementLogger.ROOT_LOGGER.yes()).
                expectedDisplayText(DomainManagementLogger.ROOT_LOGGER.addedUser(values.getUserName(), values.getUserFiles().get(0).getCanonicalPath())).
                expectedDisplayText(AddUser.NEW_LINE).
                expectedDisplayText(DomainManagementLogger.ROOT_LOGGER.addedGroups(values.getUserName(), values.getGroups(), values.getGroupFiles().get(0).getCanonicalPath())).
                expectedDisplayText(AddUser.NEW_LINE);
        consoleMock.setResponses(consoleBuilder);

        State nextState = userCheckState.execute();
        assertTrue(nextState instanceof ConfirmationChoice);
        nextState = nextState.execute();
        assertTrue(nextState instanceof AddUserState);
        nextState.execute();
        consoleBuilder.validate();
    }

    @Test
    public void existingUSer() throws IOException {
        values.setExistingUser(true);
        values.setGroups(ROLES);
        PreModificationState userCheckState = new PreModificationState(consoleMock, values);

        AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().
                expectedDisplayText(DomainManagementLogger.ROOT_LOGGER.updateUser(values.getUserName(), values.getUserFiles().get(0).getCanonicalPath())).
                expectedDisplayText(AddUser.NEW_LINE).
                expectedDisplayText(DomainManagementLogger.ROOT_LOGGER.updatedGroups(values.getUserName(), values.getGroups(), values.getGroupFiles().get(0).getCanonicalPath())).
                expectedDisplayText(AddUser.NEW_LINE);
        consoleMock.setResponses(consoleBuilder);

        State nextState = userCheckState.execute();
        assertTrue(nextState instanceof UpdateUser);
        nextState = nextState.execute();
        consoleBuilder.validate();
    }

}
