Skip to content

Commit 5ec3195

Browse files
committed
Almost completely tested UserExistsInteractor and UserVerificationInteractor, and refactored the code to solve dependency issues
1 parent c29211c commit 5ec3195

File tree

9 files changed

+185
-67
lines changed

9 files changed

+185
-67
lines changed

src/main/java/screens/login_screen/UserLoginUI.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/** This is the screen on which the user enters his credentials in order to login **/
1010
public class UserLoginUI implements ActionListener, UserVerificationOutputBoundary {
1111

12-
private UserLoginInputBoundary loginInteractor;
12+
private final UserLoginInputBoundary loginInteractor;
1313
JTextField credentialText;
1414
JPasswordField passwordText;
1515

@@ -65,11 +65,6 @@ public void cannotVerify() {
6565

6666
}
6767

68-
@Override
69-
public void setInputBoundary(UserLoginInputBoundary loginInteractor) {
70-
this.loginInteractor = loginInteractor;
71-
}
72-
7368
@Override
7469
public void actionPerformed(ActionEvent e) {
7570
String username = credentialText.getText();

src/main/java/screens/user_registration_screen/UserRegistrationUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static void main(String[] args){
8686
UserVerificationOutputBoundary loginUI = new UserLoginUI(userLoginInteractor);
8787
UserVerificationInputBoundary verificationInteractor = new UserVerificationInteractor(testDB, loginUI);
8888
UserExistsOutputBoundary verificationScreen = new UserVerificationScreen(verificationInteractor);
89-
UserExistsInputBoundary existsInteractor = new UserExistsInteractor(testDB, verificationScreen);
89+
UserExistsInputBoundary existsInteractor = new UserExistsInteractor(testDB, verificationScreen, new verificationMethodFactory());
9090
UserRegistrationUI testUI = new UserRegistrationUI(existsInteractor);
9191
testUI.getUserCredentials();
9292

src/main/java/screens/user_registration_screen/UserVerificationScreen.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
package screens.user_registration_screen;
2-
3-
import screens.login_screen.UserLoginUI;
4-
import use_cases.user_login_use_cases.UserLoginInputBoundary;
52
import use_cases.user_registration_use_cases.*;
63

74
import javax.swing.*;
@@ -18,7 +15,7 @@ public class UserVerificationScreen implements UserExistsOutputBoundary, ActionL
1815

1916
public UserVerificationScreen(UserVerificationInputBoundary verificationInputBoundary){
2017
this.verificationInputBoundary = verificationInputBoundary;
21-
};
18+
}
2219

2320
@Override
2421
public void getVerificationCredentials() {
@@ -76,7 +73,7 @@ public void actionPerformed(ActionEvent e) {
7673
int code = Integer.parseInt(verText.getText());
7774
verificationInputBoundary.setCode(this.code);
7875
verificationInputBoundary.setCredentials(username, password, email);
79-
verificationInputBoundary.verify("Email", code);
76+
verificationInputBoundary.verify(code);
8077
}
8178

8279
}

src/main/java/use_cases/user_registration_use_cases/UserExistsInteractor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package use_cases.user_registration_use_cases;
22

33
import data_access.Database;
4-
import screens.user_registration_screen.UserVerificationScreen;
54

65
import java.util.Random;
76

87
public class UserExistsInteractor implements UserExistsInputBoundary{
8+
private final createMailMan mailManFactory;
99
//May need to refactor this using facade design pattern since this class has too many responsibilities.
1010
Database database;
1111
UserExistsOutputBoundary existsOutputBoundary;
1212

1313
private ISendVerificationCode codeMailMan;
1414

15-
public UserExistsInteractor(Database database, UserExistsOutputBoundary existsOutputBoundary){
15+
public UserExistsInteractor(Database database, UserExistsOutputBoundary existsOutputBoundary, createMailMan mailMan){
1616
this.database = database;
1717
this.existsOutputBoundary = existsOutputBoundary;
18+
this.mailManFactory = mailMan;
1819
}
1920
@Override
2021
public void register(String username, String password, String email) {
@@ -31,7 +32,7 @@ public void register(String username, String password, String email) {
3132

3233
@Override
3334
public void setCodeDeliveryMethod(String type) {
34-
this.codeMailMan = new verificationMethodFactory().getVerificationMethod(type);
35+
this.codeMailMan = mailManFactory.getVerificationMethod(type);
3536
}
3637

3738
@Override

src/main/java/use_cases/user_registration_use_cases/UserVerificationInputBoundary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
public interface UserVerificationInputBoundary {
44
void setCode(int code);
55
void setCredentials(String username, String password, String email);
6-
void verify(String type, int code);
6+
void verify(int code);
77
}

src/main/java/use_cases/user_registration_use_cases/UserVerificationInteractor.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package use_cases.user_registration_use_cases;
22

33
import data_access.Database;
4-
import screens.login_screen.UserLoginUI;
5-
import use_cases.user_login_use_cases.UserLoginInputBoundary;
6-
import use_cases.user_login_use_cases.UserLoginInteractor;
7-
8-
import java.util.Random;
94

105
public class UserVerificationInteractor implements UserVerificationInputBoundary{
116
private final Database database;
@@ -23,14 +18,12 @@ public UserVerificationInteractor(Database database, UserVerificationOutputBound
2318
}
2419

2520
@Override
26-
public void verify(String type, int code) {
21+
public void verify(int code) {
2722
System.out.println(this.code);
2823
if(code == this.code){
2924
database.createUser(this.username, this.password, this.email, "Basic");
30-
System.out.println("verified");
3125
verificationOutputBoundary.getLoginCredentials();
3226
}else{
33-
System.out.println("failure");
3427
verificationOutputBoundary.cannotVerify();
3528
}
3629

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package use_cases.user_registration_use_cases;
22

3-
import use_cases.user_login_use_cases.UserLoginInputBoundary;
4-
53
public interface UserVerificationOutputBoundary {
64
void getLoginCredentials();
75
void cannotVerify();
86

9-
void setInputBoundary(UserLoginInputBoundary loginInteractor2);
107
}

src/test/java/test_user_registration/TestUserExistsInputBoundary.java

Lines changed: 107 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,127 @@
22

33
import data_access.Database;
44
import entities.user_entities.User;
5+
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.Test;
7+
import use_cases.user_registration_use_cases.ISendVerificationCode;
8+
import use_cases.user_registration_use_cases.UserExistsInteractor;
69
import use_cases.user_registration_use_cases.UserExistsOutputBoundary;
10+
import use_cases.user_registration_use_cases.createMailMan;
711

812
public class TestUserExistsInputBoundary {
9-
@Test
10-
public void invalidCredentials(){
11-
//Anonymous class
12-
Database testDB = new Database() {
13-
@Override
14-
public User getUser(String username) {
15-
return null;
16-
}
13+
//Objects used for testing
14+
// The below anonymous database is created to test cases where the user does not exist.
15+
Database userDontExist = new Database() {
16+
@Override
17+
public User getUser(String username) {
18+
return null;
19+
}
1720

18-
@Override
19-
public void createUser(String username, String password, String email, String type) {
20-
}
21+
@Override
22+
public void createUser(String username, String password, String email, String type) {
2123

22-
@Override
23-
public boolean UserExists(String username, String email) {
24-
return false;
25-
}
24+
}
2625

27-
@Override
28-
public boolean UserExists(String username) {
29-
return false;
30-
}
31-
};
32-
//Anonymous class for outputBoundary
33-
UserExistsOutputBoundary outputBoundary = new UserExistsOutputBoundary() {
34-
public int x;
35-
@Override
36-
public void getVerificationCredentials() {
37-
x += 1;
38-
}
26+
@Override
27+
public boolean UserExists(String username, String email) {
28+
return false;
29+
}
30+
@Override
31+
public boolean UserExists(String username) {
32+
return false;
33+
}
34+
};
35+
//The below anonymous database for testing cases where the user exists
36+
Database userExists = new Database() {
37+
@Override
38+
public User getUser(String username) {
39+
return null;
40+
}
3941

40-
@Override
41-
public void presentUserExistsMessage() {
42-
x += 3;
43-
}
42+
@Override
43+
public void createUser(String username, String password, String email, String type) {
4444

45-
@Override
46-
public void getCode(int code) {
47-
x += 5;
48-
}
45+
}
4946

50-
@Override
51-
public void getUserCredentials(String username, String password, String email) {
52-
x += 6;
53-
}
54-
};
47+
@Override
48+
public boolean UserExists(String username, String email) {
49+
return true;
50+
}
51+
52+
@Override
53+
public boolean UserExists(String username) {
54+
return true;
55+
}
56+
};
57+
//This output boundary used in test cases
58+
private class TestOutputBoundary implements UserExistsOutputBoundary{
59+
public int x = 0;
60+
@Override
61+
public void getVerificationCredentials() {
62+
this.x += 1;
63+
}
5564

65+
@Override
66+
public void presentUserExistsMessage() {
67+
this.x += 3;
68+
}
5669

70+
@Override
71+
public void getCode(int code) {
72+
this.x += 5;
73+
}
5774

75+
@Override
76+
public void getUserCredentials(String username, String password, String email) {
77+
this.x += 7;
78+
}
79+
}
80+
TestOutputBoundary userExistsOutputBoundary = new TestOutputBoundary();
81+
//The verification streams, and verification Stream factory used for testing
82+
ISendVerificationCode verStream1 = (email, code) -> {
83+
84+
};
85+
ISendVerificationCode verStream2 = (email, code) -> {
86+
87+
};
88+
private class TestCreateMailMan implements createMailMan{
89+
public int x;
90+
@Override
91+
public ISendVerificationCode getVerificationMethod(String type) {
92+
if(type.equals("0")){
93+
x = 100;
94+
return verStream1;
95+
}else{
96+
x = 200;
97+
return verStream2;
98+
}
99+
}
100+
}
101+
TestCreateMailMan mailManFactory = new TestCreateMailMan();
58102

103+
//In the first test, the user exists in the database, and we will set verStream1 as the verification method
104+
@Test
105+
public void userExistsInDatabase1(){
106+
UserExistsInteractor uInteractor = new UserExistsInteractor(userExists,
107+
userExistsOutputBoundary, mailManFactory);
108+
uInteractor.setCodeDeliveryMethod("0");
109+
uInteractor.register("a", "b", "c");
110+
//The quantity below should be 103
111+
Assertions.assertEquals(userExistsOutputBoundary.x + mailManFactory.x, 103);
59112

60113
}
114+
//In this test, the user does not exist in the database, and we will set verStream2 as the verification method
115+
@Test
116+
public void userExistsInDatabase2(){
117+
TestOutputBoundary userExists2 = new TestOutputBoundary();
118+
TestCreateMailMan createMailMan2 = new TestCreateMailMan();
119+
UserExistsInteractor uInteractor2 = new UserExistsInteractor(userDontExist, userExists2, createMailMan2);
120+
uInteractor2.setCodeDeliveryMethod("1");
121+
uInteractor2.register("a", "b", "c");
122+
//The quantity below should be 213
123+
Assertions.assertEquals(userExists2.x + createMailMan2.x, 213);
124+
125+
}
126+
127+
//No other combinations of the method calls, could arrive at those values, so the above tests passing suffices
61128
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package test_user_registration;
2+
3+
import data_access.Database;
4+
import entities.user_entities.User;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import use_cases.user_registration_use_cases.UserVerificationInteractor;
8+
import use_cases.user_registration_use_cases.UserVerificationOutputBoundary;
9+
10+
public class TestUserVerificationInputBoundary {
11+
private class testDatabase implements Database{
12+
public int x;
13+
@Override
14+
public User getUser(String username) {
15+
return null;
16+
}
17+
18+
@Override
19+
public void createUser(String username, String password, String email, String type) {
20+
x += 1;
21+
}
22+
23+
@Override
24+
public boolean UserExists(String username, String email) {
25+
return false;
26+
}
27+
28+
@Override
29+
public boolean UserExists(String username) {
30+
return false;
31+
}
32+
}
33+
private class testUserVerificationOutputBdy implements UserVerificationOutputBoundary{
34+
public int x;
35+
@Override
36+
public void getLoginCredentials() {
37+
x += 20;
38+
}
39+
40+
@Override
41+
public void cannotVerify() {
42+
x+= 30;
43+
}
44+
}
45+
//objects used for test
46+
testDatabase testDB = new testDatabase();
47+
testUserVerificationOutputBdy userVerificationOutputBdy = new testUserVerificationOutputBdy();
48+
//The below test is to see if the verify function works in the case that the code is not right
49+
@Test
50+
public void testCodeNotRight(){
51+
UserVerificationInteractor testInteractor = new UserVerificationInteractor(testDB, userVerificationOutputBdy);
52+
53+
testInteractor.setCode(123);
54+
testInteractor.setCredentials("a", "b", "c");
55+
testInteractor.verify(135);
56+
Assertions.assertEquals(testDB.x + userVerificationOutputBdy.x, 30);
57+
}
58+
//The below test is to see if the verify function works in the case that the code is right
59+
@Test
60+
public void testCodeRight(){
61+
UserVerificationInteractor testInteractor = new UserVerificationInteractor(testDB, userVerificationOutputBdy);
62+
63+
testInteractor.setCode(123);
64+
testInteractor.setCredentials("a", "b", "c");
65+
testInteractor.verify(123);
66+
Assertions.assertEquals(testDB.x + userVerificationOutputBdy.x, 21);
67+
}
68+
}

0 commit comments

Comments
 (0)