1c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/*
2c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Copyright 2008 the original author or authors.
3c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
4c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * you may not use this file except in compliance with the License.
6c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * You may obtain a copy of the License at
7c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
8c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
10c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Unless required by applicable law or agreed to in writing, software
11c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * See the License for the specific language governing permissions and
14c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * limitations under the License.
15c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */
16c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpackage org.mockftpserver.fake.command
17c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
18c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.command.Command
19c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.command.CommandHandler
20c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.command.CommandNames
21c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.command.ReplyCodes
22c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.session.SessionKeys
23c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
24c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/**
25c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Tests for AcctCommandHandler
26c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
27c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @version $Revision$ - $Date$
28c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
29c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @author Chris Mair
30c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */
31c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairclass AcctCommandHandlerTest extends AbstractFakeCommandHandlerTest {
32c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
33c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    def USERNAME = "user123"
34c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    def ACCOUNT_NAME = "account123"
35c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
36c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    boolean testNotLoggedIn = false
37c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
38c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    void testHandleCommand() {
39c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        handleCommand([ACCOUNT_NAME])
40c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertSessionReply(ReplyCodes.ACCT_OK, ['acct', USERNAME])
41c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertAccountNameInSession(true)
42c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
43c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
44c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    void testHandleCommand_UsernameNotSetInSession() {
45c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        session.removeAttribute(SessionKeys.USERNAME)
46c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        testHandleCommand_MissingRequiredSessionAttribute()
47c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertAccountNameInSession(false)
48c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
49c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
50c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    void testHandleCommand_MissingAccountNameParameter() {
51c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        testHandleCommand_MissingRequiredParameter([])
52c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertAccountNameInSession(false)
53c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
54c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
55c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    //-------------------------------------------------------------------------
56c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    // Abstract and Overridden Methods
57c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    //-------------------------------------------------------------------------
58c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
59c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    void setUp() {
60c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        super.setUp()
61c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        session.setAttribute(SessionKeys.USERNAME, USERNAME)
62c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
63c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
64c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    CommandHandler createCommandHandler() {
65c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        new AcctCommandHandler()
66c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
67c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
68c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    Command createValidCommand() {
69c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return new Command(CommandNames.ACCT, [ACCOUNT_NAME])
70c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
71c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
72c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    //-------------------------------------------------------------------------
73c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    // Helper Methods
74c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    //-------------------------------------------------------------------------
75c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
76c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
77c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Assert that the account name is stored in the session, depending on the value of isAccountNameInSession.
78c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @param isAccountNameInSession - true if the account name is expected in the session; false if it is not expected
79c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
80c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    private void assertAccountNameInSession(boolean isAccountNameInSession) {
81c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        def expectedValue = isAccountNameInSession ? ACCOUNT_NAME : null
82c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assert session.getAttribute(SessionKeys.ACCOUNT_NAME) == expectedValue
83c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
84c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
85c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair}