Skip to content
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
23 changes: 23 additions & 0 deletions java-exceptions-template.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.5.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.18.10" level="project" />
</component>
</module>
1 change: 0 additions & 1 deletion src/main/java/com/epam/izh/rd/online/JavaExceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
public class JavaExceptions {

public static void main(String[] args) {

}

}
3 changes: 2 additions & 1 deletion src/main/java/com/epam/izh/rd/online/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class User {

private String password;

public User() {}
public User() {
}

public User(String login, String password) {
this.login = login;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.epam.izh.rd.online.exception;

public class NotAccessException extends RuntimeException {

public NotAccessException(String message) {
super(message);
}

public NotAccessException() {
}

public NotAccessException(String message, Throwable cause) {
super(message, cause);
}

public NotAccessException(Throwable cause) {
super(cause);
}

public NotAccessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.epam.izh.rd.online.exception;

public class NotCorrectPasswordException extends RuntimeException {
public NotCorrectPasswordException() {
}

public NotCorrectPasswordException(String message) {
super(message);
}

public NotCorrectPasswordException(String message, Throwable cause) {
super(message, cause);
}

public NotCorrectPasswordException(Throwable cause) {
super(cause);
}

public NotCorrectPasswordException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.epam.izh.rd.online.exception;

public class SimplePasswordException extends RuntimeException {
public SimplePasswordException(String message) {
super(message);
}

public SimplePasswordException() {
}

public SimplePasswordException(String message, Throwable cause) {
super(message, cause);
}

public SimplePasswordException(Throwable cause) {
super(cause);
}

public SimplePasswordException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.epam.izh.rd.online.exception;

public class UserAlreadyRegisteredException extends Exception {
public UserAlreadyRegisteredException(String message) {
super(message);
}

public UserAlreadyRegisteredException() {
}

public UserAlreadyRegisteredException(String message, Throwable cause) {
super(message, cause);
}

public UserAlreadyRegisteredException(Throwable cause) {
super(cause);
}

public UserAlreadyRegisteredException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.epam.izh.rd.online.exception;

public class UserNotFoundException extends Exception {
public UserNotFoundException() {
}

public UserNotFoundException(String message) {
super(message);
}

public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public UserNotFoundException(Throwable cause) {
super(cause);
}

public UserNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.NotCorrectPasswordException;
import com.epam.izh.rd.online.exception.UserNotFoundException;
import com.epam.izh.rd.online.repository.IUserRepository;

public class AuthenticationService implements IAuthenticationService {
Expand All @@ -25,15 +27,15 @@ public AuthenticationService(IUserRepository userRepository) {
* @param user - пользователь проходящий авторизацию
*/
@Override
public User login(User user) {
// Находим пользователя в базе
public User login(User user) throws UserNotFoundException {
User foundUser = userRepository.findByLogin(user.getLogin());
if (foundUser == null) {
throw new UserNotFoundException("Пользователь с таким логином не найден");
}
if (!foundUser.getPassword().equals(user.getPassword())) {
throw new NotCorrectPasswordException("Пароль введен неверно!");
}

//
// Здесь необходимо реализовать перечисленные выше проверки
//

// Устанавливаем найденного пользователя, который прошел все проверки, как вошедшего в систему.
CurrentUserManager.setCurrentLoggedInUser(foundUser);

return foundUser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.UserNotFoundException;

public interface IAuthenticationService {
User login(User user);
User login(User user) throws UserNotFoundException;

void logout();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.UserAlreadyRegisteredException;

public interface IUserService {

User register(User user);
User register(User user) throws UserAlreadyRegisteredException;

void delete(String login);
}
34 changes: 22 additions & 12 deletions src/main/java/com/epam/izh/rd/online/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.NotAccessException;
import com.epam.izh.rd.online.exception.SimplePasswordException;
import com.epam.izh.rd.online.exception.UserAlreadyRegisteredException;
import com.epam.izh.rd.online.repository.IUserRepository;
import com.epam.izh.rd.online.repository.UserRepository;

import java.util.regex.Pattern;

public class UserService implements IUserService {

private IUserRepository userRepository;
Expand All @@ -30,13 +35,18 @@ public UserService(IUserRepository userRepository) {
* @param user - даныне регистрирующегося пользователя
*/
@Override
public User register(User user) {

//
// Здесь необходимо реализовать перечисленные выше проверки
//
public User register(User user) throws UserAlreadyRegisteredException {
if (user.getLogin().isEmpty() || user.getPassword().isEmpty()) {
throw new IllegalArgumentException("Ошибка в заполнении полей");
}
if (user.equals(userRepository.findByLogin(user.getLogin()))) {
throw new UserAlreadyRegisteredException("Пользователь с логином "
+ user.getLogin() + " уже зарегистрирован");
}
if (Pattern.matches("\\d+", user.getPassword())) {
throw new SimplePasswordException("Пароль не соответствует требованиям безопасности");
}

// Если все проверки успешно пройдены, сохраняем пользователя в базу
return userRepository.save(user);
}

Expand All @@ -59,13 +69,13 @@ public User register(User user) {
* @param login
*/
public void delete(String login) {

// Здесь необходимо сделать доработку метод

try {
userRepository.deleteByLogin(login);

// Здесь необходимо сделать доработку метода

} catch (UnsupportedOperationException e) {
throw new NotAccessException("Недостаточно прав для выполнения операции");
}
}

}


4 changes: 2 additions & 2 deletions src/test/java/com/epam/izh/rd/online/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

public class Assert<T extends Throwable> {

public T assertThrowsWithClassName(String className, Executable executable, String message) {
public T assertThrowsWithClassName(String className, Executable executable, String message) throws ClassNotFoundException {
return assertThrows(forName(className), executable, message);
}

@SneakyThrows
private Class<T> forName(String className) {
private Class<T> forName(String className) throws ClassNotFoundException {
return (Class<T>) Class.forName("com.epam.izh.rd.online.exception." + className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ private void setup() {

@Test
@DisplayName("Тест метода IAuthenticationService.login(User user)")
void testRegisterCase1() {
void testRegisterCase1() throws ClassNotFoundException {
assertion.assertThrowsWithClassName("UserNotFoundException", () -> authenticationService.login(getUser()),
"Пользователь с таким логином не найден");
}

@Test
@DisplayName("Тест метода IAuthenticationService.login(User user)")
void testRegisterCase2() {
void testRegisterCase2() throws ClassNotFoundException {
User user = getUser();
userRepository.save(user);

Expand Down
15 changes: 10 additions & 5 deletions src/test/java/com/epam/izh/rd/online/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.epam.izh.rd.online;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.UserAlreadyRegisteredException;
import com.epam.izh.rd.online.repository.IUserRepository;
import com.epam.izh.rd.online.repository.UserRepository;
import com.epam.izh.rd.online.service.CurrentUserManager;
Expand Down Expand Up @@ -39,7 +40,7 @@ void testRegisterCase1(User user) {

@Test
@DisplayName("Тест метода IUserService.register(User user) кейс 2")
void testRegisterCase2() throws Exception {
void testRegisterCase2() throws Exception{
User user = Providers.getUser();

userService.register(user);
Expand All @@ -52,15 +53,19 @@ void testRegisterCase2() throws Exception {
void testRegisterCase3() {
User user = getUserWithNumberPassword();

assertion.assertThrowsWithClassName("SimplePasswordException", () -> userService.register(user),
"Пароль не соответствует требованиям безопасности"
);
try {
assertion.assertThrowsWithClassName("SimplePasswordException", () -> userService.register(user),
"Пароль не соответствует требованиям безопасности"
);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

@ParameterizedTest
@MethodSource("com.epam.izh.rd.online.Providers#testDelete")
@DisplayName("Тест метода IUserService.delete(String login)")
void testDelete(User user) {
void testDelete(User user) throws ClassNotFoundException {
CurrentUserManager.setCurrentLoggedInUser(user);
assertion.assertThrowsWithClassName("NotAccessException", () -> userService.delete("123"),
"Недостаточно прав для выполнения операции");
Expand Down