Skip to content

Commit f25f60c

Browse files
committed
Made some improvements to clean design and testing
1 parent d1d566f commit f25f60c

File tree

4 files changed

+59
-45
lines changed

4 files changed

+59
-45
lines changed

accounts

719 Bytes
Binary file not shown.

src/main/java/entities/user_entities/User.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import interface_adapters.login_interface_adapters.Login;
77
import use_cases.user_attribute_modification_use_case.Changeable;
88
import interface_adapters.app_screen_interface_adapters.UserAppScreenGateway;
9-
import entities.chat.*;
109

1110
import java.io.File;
1211
import java.io.Serializable;
@@ -42,12 +41,16 @@ public ArrayList<Chat> getUserChats(){
4241
@Override
4342
// from Changeable
4443
public void changeFeature(String feature, String newFeature){
45-
if (feature == "Username"){
46-
this.username = newFeature;
47-
} else if (feature == "Password"){
48-
this.password = newFeature;
49-
} else if (feature == "Email"){
50-
this.email = newFeature;
44+
switch (feature) {
45+
case "Username":
46+
this.username = newFeature;
47+
break;
48+
case "Password":
49+
this.password = newFeature;
50+
break;
51+
case "Email":
52+
this.email = newFeature;
53+
break;
5154
}
5255
}
5356

src/main/java/screens/profile_update_screen/UserModificationUI.java

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
package screens.profile_update_screen; /**
2-
* Provides the UI elements
3-
*/
4-
import data_access.UserDatabase;
5-
import entities.user_entities.User;
6-
import interface_adapters.profile_modification_IA.ChangeController;
1+
package screens.profile_update_screen;
2+
import interface_adapters.profile_modification_IA.ChangeControllerClass;
73

84
import javax.swing.*;
95
import java.awt.*;
106
import java.awt.event.ActionEvent;
117
import java.awt.event.ActionListener;
8+
import java.util.Objects;
129

13-
public class UserModificationUI implements ChangeController {
14-
private JLabel label;
10+
11+
/**
12+
* Provides the UI elements
13+
*/
14+
public class UserModificationUI {
15+
ChangeControllerClass c = new ChangeControllerClass();
16+
private final JLabel label;
1517
public UserModificationUI() {
1618
final JFrame frame = new JFrame();
1719
frame.setSize(500, 300);
@@ -47,9 +49,9 @@ public UserModificationUI() {
4749
public void actionPerformed(ActionEvent e) {
4850
String username = (usernameField.getText());
4951
String password = (passwordField.getText());
50-
String item = cb.getSelectedItem().toString();
52+
String item = Objects.requireNonNull(cb.getSelectedItem()).toString();
5153
String newFeature = (newFeatureField.getText());
52-
boolean success = reportChange(username, password, item, newFeature);
54+
boolean success = c.reportChange(username, password, item, newFeature);
5355
if (success){
5456
label.setText("Your " + item + " was successfully changed.");
5557
}
@@ -74,28 +76,5 @@ public void actionPerformed(ActionEvent e) {
7476

7577
frame.setVisible(true);
7678
}
77-
/**
78-
* profile_modification_IA.ChangeController makes UI implement reportChange to invert the use-case --> UI dependency
79-
*/
80-
@Override
81-
public boolean reportChange(String username, String password, String feature, String newFeature) {
82-
UserDatabase db = new UserDatabase();
83-
if (db.UserExists(username)){
84-
User user = db.getUser(username);
85-
if (user.PasswordMatch(password) & user.getUsername().equals(username)){
86-
user.changeFeature(feature, newFeature);
87-
// this serializes the change
88-
db.modifyUser(username, user);
89-
return true;
90-
}
91-
}
92-
return false;
93-
}
94-
95-
// for trying out the code:
96-
// public static void main(String[] args) {
97-
// new profile_update_screen.UserModificationUI();
98-
//
99-
// }
10079

10180
}

src/test/java/TestUserChange.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,50 @@
11
import data_access.UserDatabase;
22
import org.junit.jupiter.api.Assertions;
3-
import screens.profile_update_screen.UserModificationUI;
4-
import org.junit.jupiter.api.Assertions;
3+
import interface_adapters.profile_modification_IA.ChangeControllerClass;
54
import org.junit.jupiter.api.Test;
65

6+
/**
7+
* Simple tests of User modification for all attribuites.
8+
*/
79
public class TestUserChange {
810
@Test
9-
public void correctChange() {
11+
public void emailChange() {
1012
UserDatabase db = new UserDatabase();
13+
ChangeControllerClass c = new ChangeControllerClass();
1114
db.createUser("parmism", "123", "[email protected]", "Basic");
12-
UserModificationUI ui = new UserModificationUI();
13-
ui.reportChange("parmism", "123", "Email", "[email protected]");
15+
c.reportChange("parmism", "123", "Email", "[email protected]");
1416
UserDatabase db2 = new UserDatabase();
1517
String actual = db2.getUser("parmism").getEmail();
1618
Assertions.assertEquals("[email protected]", actual);
1719
}
20+
21+
@Test
22+
public void passwordChange() {
23+
UserDatabase db = new UserDatabase();
24+
ChangeControllerClass c = new ChangeControllerClass();
25+
db.createUser("parmism", "123", "[email protected]", "Basic");
26+
c.reportChange("parmism", "123", "Password", "456");
27+
UserDatabase db2 = new UserDatabase();
28+
Assertions.assertTrue(db2.getUser("parmism").PasswordMatch("456"));
29+
}
30+
31+
@Test
32+
public void oldUsernameRemoved() {
33+
UserDatabase db = new UserDatabase();
34+
ChangeControllerClass c = new ChangeControllerClass();
35+
db.createUser("badUsername", "123", "[email protected]", "Basic");
36+
c.reportChange("badUsername", "123", "Username", "goodUsername");
37+
UserDatabase db2 = new UserDatabase();
38+
Assertions.assertFalse(db2.UserExists("badUsername"));
39+
}
40+
41+
@Test
42+
public void newUsernameInDB() {
43+
UserDatabase db = new UserDatabase();
44+
ChangeControllerClass c = new ChangeControllerClass();
45+
db.createUser("badUsername", "123", "[email protected]", "Basic");
46+
c.reportChange("badUsername", "123", "Username", "goodUsername");
47+
UserDatabase db2 = new UserDatabase();
48+
Assertions.assertTrue(db2.UserExists("goodUsername"));
49+
}
1850
}

0 commit comments

Comments
 (0)