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

C.kashiwabara #120

Open
wants to merge 3 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
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 java.awt.CardLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import data_access.InMemoryUserDataAccessObject;
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;

/**
* 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();

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 currentUser;
}

@Override
public void changePassword(User user) {
// Replace the old entry with the new password
Expand Down
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();
}
30 changes: 28 additions & 2 deletions src/test/java/use_case/login/LoginInteractorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

public class LoginInteractorTest {

// TODO Task 2.2: make a copy of this test method and follow the instructions in the readme to test your
// code from Task 2.1..
@Test
public void successTest() {
LoginInputData inputData = new LoginInputData("Paul", "password");
Expand Down Expand Up @@ -41,6 +39,34 @@ public void prepareFailView(String error) {
interactor.execute(inputData);
}

@Test
public void successUserLoggedInTest() {
LoginInputData inputData = new LoginInputData("Paul", "password");
LoginUserDataAccessInterface userRepository = new InMemoryUserDataAccessObject();

// For the success test, we need to add Paul to the data access repository before we log in.
UserFactory factory = new CommonUserFactory();
User user = factory.create("Paul", "password");
userRepository.save(user);

// This creates a successPresenter that tests whether the test case is as we expect.
LoginOutputBoundary successPresenter = new LoginOutputBoundary() {
@Override
public void prepareSuccessView(LoginOutputData user) {
assertEquals("Paul", userRepository.getCurrentUser());
}

@Override
public void prepareFailView(String error) {
fail("Use case failure is unexpected.");
}
};

LoginInputBoundary interactor = new LoginInteractor(userRepository, successPresenter);
assertNull(userRepository.getCurrentUser());
interactor.execute(inputData);
}


@Test
public void failurePasswordMismatchTest() {
Expand Down