Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tedx.tao #110

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Homework 5: Clean Architecture User Login System Example
+# Homework 5: Clean Architecture User Login System Example

## Assignment Preamble

Expand Down
1 change: 0 additions & 1 deletion src/main/java/app/MainWithDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static void main(String[] args) {
final LoggedInViewModel loggedInViewModel = new LoggedInViewModel();
final SignupViewModel signupViewModel = new SignupViewModel();

// TODO Task 1.1 in a copy of this file, change this line to use the in-memory DAO.
final DBUserDataAccessObject userDataAccessObject = new DBUserDataAccessObject(new CommonUserFactory());

final SignupView signupView = SignupUseCaseFactory.create(viewManagerModel, loginViewModel,
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/app/MainWithInMemory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package app;

import data_access.DBUserDataAccessObject;
import data_access.InMemoryUserDataAccessObject;
import entity.CommonUserFactory;
import interface_adapter.ViewManagerModel;
import interface_adapter.logged_in.LoggedInViewModel;
import interface_adapter.login.LoginViewModel;
import interface_adapter.signup.SignupViewModel;
import view.LoggedInView;
import view.LoginView;
import view.SignupView;
import view.ViewManager;

import javax.swing.*;
import java.awt.*;

/**
* The version of Main with an external database used to persist user data.
*/
public class MainWithInMemory {

/**
* The main method for starting the program with an external database used to persist user data.
* @param args input to main
*/
public static void main(String[] args) {
// Build the main program window, the main panel containing the
// various cards, and the layout, and stitch them together.

// The main application window.
final JFrame application = new JFrame("Login Example");
application.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

final CardLayout cardLayout = new CardLayout();

// The various View objects. Only one view is visible at a time.
final JPanel views = new JPanel(cardLayout);
application.add(views);

// This keeps track of and manages which view is currently showing.
final ViewManagerModel viewManagerModel = new ViewManagerModel();
new ViewManager(views, cardLayout, viewManagerModel);

// The data for the views, such as username and password, are in the ViewModels.
// This information will be changed by a presenter object that is reporting the
// results from the use case. The ViewModels are "observable", and will
// be "observed" by the Views.
final LoginViewModel loginViewModel = new LoginViewModel();
final LoggedInViewModel loggedInViewModel = new LoggedInViewModel();
final SignupViewModel signupViewModel = new SignupViewModel();

// TODO Task[done] 1.1 in a copy of this file, change this line to use the in-memory DAO.
final InMemoryUserDataAccessObject userDataAccessObject = new InMemoryUserDataAccessObject();

final SignupView signupView = SignupUseCaseFactory.create(viewManagerModel, loginViewModel,
signupViewModel, userDataAccessObject);
views.add(signupView, signupView.getViewName());

final LoginView loginView = LoginUseCaseFactory.create(viewManagerModel, loginViewModel,
loggedInViewModel, userDataAccessObject);
views.add(loginView, loginView.getViewName());

final LoggedInView loggedInView = ChangePasswordUseCaseFactory.create(viewManagerModel,
loggedInViewModel, userDataAccessObject);
views.add(loggedInView, loggedInView.getViewName());

viewManagerModel.setState(signupView.getViewName());
viewManagerModel.firePropertyChanged();

application.pack();
application.setVisible(true);
}
}
10 changes: 10 additions & 0 deletions src/main/java/data_access/DBUserDataAccessObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ public User get(String username) {
}
}

@Override
public void setCurrentUser(String name) {

}

@Override
public String getCurrentUser() {
return "";
}

@Override
public boolean existsByName(String username) {
final OkHttpClient client = new OkHttpClient().newBuilder()
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/data_access/FileUserDataAccessObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public User get(String username) {
return accounts.get(username);
}

@Override
public void setCurrentUser(String name) {

}

@Override
public String getCurrentUser() {
return "";
}

@Override
public boolean existsByName(String identifier) {
return accounts.containsKey(identifier);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/data_access/InMemoryUserDataAccessObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public User get(String username) {
return users.get(username);
}

@Override
public void setCurrentUser(String name) {
this.currentUser = name;
}

@Override
public String getCurrentUser() {
return this.currentUser;
}

@Override
public void changePassword(User user) {
// Replace the old entry with the new password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public LoggedInPresenter(ViewManagerModel viewManagerModel,

@Override
public void prepareSuccessView(ChangePasswordOutputData outputData) {
// TODO update the viewmodel!
// TODO[done] update the viewmodel!
loggedInViewModel.setPasswordChanged(true);
loggedInViewModel.firePropertyChanged("password");

}

@Override
public void prepareFailView(String error) {
// TODO update the viewmodel!
loggedInViewModel.setErrorMessage(error);
loggedInViewModel.firePropertyChanged("error");
}
}
17 changes: 17 additions & 0 deletions src/main/java/interface_adapter/logged_in/LoggedInViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@
* The View Model for the Logged In View.
*/
public class LoggedInViewModel extends ViewModel<LoggedInState> {
private boolean passwordChanged;
private String errorMessage;

public LoggedInViewModel() {
super("logged in");
setState(new LoggedInState());
}

public void setPasswordChanged(boolean changed) {
this.passwordChanged = changed;
}

public boolean isPasswordChanged() {
return this.passwordChanged;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public String getErrorMessage() {
return this.errorMessage;
}
}
2 changes: 1 addition & 1 deletion src/main/java/use_case/login/LoginInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void execute(LoginInputData loginInputData) {
else {

final User user = userDataAccessObject.get(loginInputData.getUsername());

userDataAccessObject.setCurrentUser(user.getName());
final LoginOutputData loginOutputData = new LoginOutputData(user.getName(), false);
loginPresenter.prepareSuccessView(loginOutputData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public interface LoginUserDataAccessInterface {
*/
User get(String username);

void setCurrentUser(String name);

String getCurrentUser();
}
28 changes: 28 additions & 0 deletions src/test/java/use_case/login/LoginInteractorTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package use_case.login;

import data_access.InMemoryUserDataAccessObject;
import entity.CommonUser;
import entity.CommonUserFactory;
import entity.User;
import entity.UserFactory;
Expand Down Expand Up @@ -95,4 +96,31 @@ public void prepareFailView(String error) {
LoginInputBoundary interactor = new LoginInteractor(userRepository, failurePresenter);
interactor.execute(inputData);
}

public void successUserLoggedInTest() {
LoginInputData inputData = new LoginInputData("Paul", "password");
InMemoryUserDataAccessObject userDataAccessObject = new InMemoryUserDataAccessObject();
userDataAccessObject.save(new CommonUser("Paul", "password"));

LoginOutputBoundary loginPresenter = new LoginOutputBoundary() {
@Override
public void prepareSuccessView(LoginOutputData outputData) {
assertEquals("Paul", outputData.getUsername());
}

@Override
public void prepareFailView(String error) {
fail("Expected successful login, but login failed with error: " + error);
}
};

LoginInteractor interactor = new LoginInteractor(userDataAccessObject, loginPresenter);

// Act: Before login, ensure no user is logged in, then execute login
assertNull(userDataAccessObject.getCurrentUser()); // Verify initial state (no user logged in)
interactor.execute(inputData); // Perform login for "Paul"

// Assert: Check that the current user is now "Paul"
assertEquals("Paul", userDataAccessObject.getCurrentUser());
}
}