View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package jmockit.loginExample.domain.userLogin;
7   
8   import static org.testng.Assert.assertFalse;
9   import static org.testng.Assert.assertThrows;
10  import static org.testng.Assert.assertTrue;
11  
12  import mockit.Mock;
13  import mockit.MockUp;
14  import mockit.Tested;
15  
16  import org.testng.annotations.BeforeMethod;
17  import org.testng.annotations.Test;
18  
19  import jmockit.loginExample.domain.userAccount.UserAccount;
20  
21  /**
22   * Equivalent to {@link LoginServiceNGTest}, but with minimal mocking and no redundant tests.
23   */
24  public final class LoginServiceIntegrationTest {
25  
26      /** The service. */
27      @Tested
28      LoginService service;
29  
30      /** The user id. */
31      String userId;
32  
33      /** The user password. */
34      String userPassword;
35  
36      /** The account. */
37      UserAccount account;
38  
39      /**
40       * Sets the up one account to be found.
41       */
42      @BeforeMethod
43      public void setUpOneAccountToBeFound() {
44          userId = "john";
45          userPassword = "password";
46          account = new UserAccount(userId, userPassword);
47  
48          new MockUp<UserAccount>() {
49              @Mock
50              UserAccount find(String accountId) {
51                  return account;
52              }
53          };
54      }
55  
56      /**
57       * Sets the account to logged in when password matches.
58       *
59       * @throws Exception
60       *             the exception
61       */
62      @Test
63      public void setAccountToLoggedInWhenPasswordMatches() throws Exception {
64          // Failed login attempts are inconsequential, provided they don't exceed the maximum number of attempts.
65          service.login(userId, "wrong password");
66          service.login(userId, "wrong password");
67          service.login(userId, userPassword);
68          service.login(userId, "wrong password");
69  
70          assertTrue(account.isLoggedIn());
71          assertFalse(account.isRevoked());
72      }
73  
74      /**
75       * Sets the account to revoked after three failed login attempts.
76       *
77       * @throws Exception
78       *             the exception
79       */
80      @Test
81      public void setAccountToRevokedAfterThreeFailedLoginAttempts() throws Exception {
82          service.login(userId, "wrong password");
83          service.login(userId, "wrong password");
84          service.login(userId, "wrong password");
85  
86          assertFalse(account.isLoggedIn());
87          assertTrue(account.isRevoked());
88      }
89  
90      /**
91       * Not revoke second account after two failed attempts on first account.
92       *
93       * @throws Exception
94       *             the exception
95       */
96      @Test
97      public void notRevokeSecondAccountAfterTwoFailedAttemptsOnFirstAccount() throws Exception {
98          UserAccount secondAccount = new UserAccount("roger", "password");
99          String accountId = account.getId();
100         account = secondAccount;
101 
102         service.login(accountId, "wrong password");
103         service.login(accountId, "wrong password");
104         service.login(secondAccount.getId(), "wrong password");
105 
106         assertFalse(secondAccount.isRevoked());
107     }
108 
109     /**
110      * Disallow concurrent logins.
111      */
112     @Test
113     public void disallowConcurrentLogins() {
114         account.setLoggedIn(true);
115 
116         assertThrows(AccountLoginLimitReachedException.class, () -> {
117             service.login(userId, userPassword);
118         });
119     }
120 
121     /**
122      * Throw exception if account not found.
123      */
124     @Test
125     public void throwExceptionIfAccountNotFound() {
126         account = null;
127 
128         assertThrows(UserAccountNotFoundException.class, () -> {
129             service.login("roger", "password");
130         });
131     }
132 
133     /**
134      * Disallow logging into revoked account.
135      */
136     @Test
137     public void disallowLoggingIntoRevokedAccount() {
138         account.setRevoked(true);
139 
140         assertThrows(UserAccountRevokedException.class, () -> {
141             service.login(userId, userPassword);
142         });
143     }
144 }