-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alustava arkkitehtuuri dokumentaatio, lisättiin käyttäjän luontisivu,…
… lisää testejä, jacocon pientä konfiguroimista
- Loading branch information
Showing
10 changed files
with
303 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Contactmanager/src/main/java/controllers/CreateUserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package controllers; | ||
|
||
import models.CreateUserModel; | ||
import utils.PathUtils; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
public class CreateUserController extends Controller<CreateUserModel> { | ||
public void createUser(String username) throws IOException { | ||
if (!Paths.get(PathUtils.getDataDir().toString(), username).toFile().exists() && !username.isEmpty()) { | ||
Files.createDirectory(Paths.get(PathUtils.getDataDir().toString(), username)); | ||
model.setUserCreated(true); | ||
model.setErrorMessage(""); | ||
} else { | ||
model.setUserCreated(false); | ||
model.setErrorMessage("Username is taken"); | ||
} | ||
|
||
model.renderView(); | ||
} | ||
|
||
public void exit() { | ||
model.setUserCreated(false); | ||
model.setErrorMessage(""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package models; | ||
|
||
import views.CreateUserView; | ||
|
||
public class CreateUserModel extends Model<CreateUserView> { | ||
private boolean userCreated = false; | ||
private String errorMessage = ""; | ||
|
||
public boolean isUserCreated() { | ||
return userCreated; | ||
} | ||
|
||
public void setUserCreated(boolean userCreated) { | ||
this.userCreated = userCreated; | ||
} | ||
|
||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
public void setErrorMessage(String errorMessage) { | ||
this.errorMessage = errorMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package views; | ||
|
||
import controllers.CreateUserController; | ||
import javafx.geometry.Insets; | ||
import javafx.geometry.Pos; | ||
import javafx.scene.Scene; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.layout.HBox; | ||
import javafx.scene.layout.VBox; | ||
import javafx.scene.paint.Color; | ||
import models.CreateUserModel; | ||
import routing.Router; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class CreateUserView extends View<CreateUserController, CreateUserModel> { | ||
private final Label errorLabel; | ||
private final TextField usernameTextField; | ||
private final Scene scene; | ||
|
||
public CreateUserView(Router router) { | ||
super(router); | ||
|
||
errorLabel = new Label(); | ||
errorLabel.setVisible(false); | ||
errorLabel.setTextFill(Color.RED); | ||
Label usernameLabel = new Label("Username"); | ||
usernameTextField = new TextField(); | ||
Button createUserButton = new Button("Create user"); | ||
Button cancelButton = new Button("Cancel"); | ||
HBox createUserButtonContainer = new HBox( | ||
createUserButton | ||
); | ||
createUserButtonContainer.setAlignment(Pos.CENTER); | ||
HBox cancelButtonContainer = new HBox( | ||
cancelButton | ||
); | ||
cancelButtonContainer.setAlignment(Pos.CENTER); | ||
VBox mainContainer = new VBox( | ||
errorLabel, | ||
usernameLabel, | ||
usernameTextField, | ||
createUserButtonContainer, | ||
cancelButtonContainer | ||
); | ||
mainContainer.setSpacing(2); | ||
mainContainer.setPadding(new Insets(10)); | ||
scene = new Scene( | ||
mainContainer | ||
); | ||
|
||
createUserButton.setOnMouseClicked(event -> { | ||
try { | ||
controller.createUser(usernameTextField.getText()); | ||
} catch (IOException exc) { | ||
exc.printStackTrace(); | ||
} | ||
}); | ||
cancelButton.setOnMouseClicked(event -> { | ||
controller.exit(); | ||
usernameTextField.clear(); | ||
render(); | ||
router.navigateTo(LoginView.class, Map.of()); | ||
}); | ||
} | ||
|
||
@Override | ||
public String getWindowTitleSuffix() { | ||
return "Create user"; | ||
} | ||
|
||
@Override | ||
public int getWindowWidth() { | ||
return 400; | ||
} | ||
|
||
@Override | ||
public int getWindowHeight() { | ||
return 200; | ||
} | ||
|
||
@Override | ||
public Scene getScene() { | ||
return scene; | ||
} | ||
|
||
@Override | ||
public void onNavigateTo(Map<String, Object> arguments) { | ||
} | ||
|
||
@Override | ||
public void render() { | ||
errorLabel.setText(model.getErrorMessage()); | ||
errorLabel.setVisible(!model.getErrorMessage().isEmpty()); | ||
|
||
if (model.isUserCreated()) { | ||
controller.exit(); | ||
usernameTextField.clear(); | ||
|
||
router.navigateTo(LoginView.class, Map.of()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import controllers.CreateUserController; | ||
import models.CreateUserModel; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import utils.PathUtils; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class CreateUserTest { | ||
private CreateUserController controller; | ||
private CreateUserModel model; | ||
|
||
@BeforeClass | ||
public static void environmentSetUp() throws IOException { | ||
PathUtils.ensureDataDirExists(); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
controller = new CreateUserController(); | ||
model = new CreateUserModel(); | ||
controller.setModel(model); | ||
|
||
controller.mock(); | ||
model.mock(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
Files.delete(Paths.get(PathUtils.getDataDir().toString(), "test")); | ||
} | ||
|
||
@Test | ||
public void creatingAUserWithAUsernameThatIsAlreadyTakenShouldWorkAsExpected() throws IOException { | ||
Path testUserPath = Paths.get(PathUtils.getDataDir().toString(), "test"); | ||
|
||
Files.createDirectory(testUserPath); | ||
|
||
controller.createUser("test"); | ||
assertEquals(model.getErrorMessage(), "Username is taken"); | ||
} | ||
|
||
@Test | ||
public void creatingAUserWithAUsernameThatIsNotAlreadyTakenShouldWorkAsExpected() throws IOException { | ||
controller.createUser("test"); | ||
|
||
assertEquals(model.getErrorMessage(), ""); | ||
assertTrue(model.isUserCreated()); | ||
assertTrue(Paths.get(PathUtils.getDataDir().toString(), "test").toFile().exists()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters