From 11f362568410d5b3983b1658d33e7f11c36edad6 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 14 Sep 2020 12:39:25 +0200 Subject: [PATCH] User service added login and returning token --- .../java/com/plannaplan/entities/User.java | 26 ++++++++ .../exceptions/UserNotFoundException.java | 13 ++++ .../repositories/UserRepository.java | 5 +- .../com/plannaplan/services/UserService.java | 22 +++++++ .../java/com/plannaplan/types/UserRoles.java | 2 +- .../plannaplan/services/UserServiceTest.java | 63 +++++++++++++++++++ .../src/main/java/com/plannaplan/App.java | 23 ++++++- 7 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 buisnesslogic/src/main/java/com/plannaplan/exceptions/UserNotFoundException.java create mode 100644 buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java index c40d984..e675415 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java @@ -1,5 +1,7 @@ package com.plannaplan.entities; +import java.sql.Date; + import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -14,11 +16,35 @@ public class User { private Long id; private String name; private String surname; + private String email; private UserRoles role; + private String token; + private Date tokenCreatedDate; public User() { } + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Date getTokenCreatedDate() { + return tokenCreatedDate; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.tokenCreatedDate = new Date(System.currentTimeMillis()); + this.token = token; + } + public String getName() { return name; } diff --git a/buisnesslogic/src/main/java/com/plannaplan/exceptions/UserNotFoundException.java b/buisnesslogic/src/main/java/com/plannaplan/exceptions/UserNotFoundException.java new file mode 100644 index 0000000..d56283f --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/exceptions/UserNotFoundException.java @@ -0,0 +1,13 @@ +package com.plannaplan.exceptions; + +public class UserNotFoundException extends Exception { + /** + * + */ + private static final long serialVersionUID = 1L; + + public UserNotFoundException(String message) { + super(message); + } + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java index 19f1ac6..a341ecb 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java @@ -3,9 +3,12 @@ package com.plannaplan.repositories; import com.plannaplan.entities.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository { - + @Query("FROM User WHERE email = ?1") + User getByAuthority(@Param("authority") String authority); } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index ffa133a..270aac2 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -1,6 +1,10 @@ package com.plannaplan.services; +import java.util.UUID; + import com.plannaplan.abstracts.EventWatcher; +import com.plannaplan.entities.User; +import com.plannaplan.exceptions.UserNotFoundException; import com.plannaplan.repositories.UserRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -15,4 +19,22 @@ public class UserService extends EventWatcher { super(); } + public String login(String authority) throws UserNotFoundException { + User user = this.repo.getByAuthority(authority); + if (user == null) { + throw new UserNotFoundException("Can not find user with given authority"); + } + String token = UUID.randomUUID().toString(); + user.setToken(token); + return token; + } + + public void save(User user) { + this.repo.save(user); + } + + public User getUserByEmail(String email) { + return this.repo.getByAuthority(email); + } + } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/types/UserRoles.java b/buisnesslogic/src/main/java/com/plannaplan/types/UserRoles.java index f56d2c5..845fddc 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/types/UserRoles.java +++ b/buisnesslogic/src/main/java/com/plannaplan/types/UserRoles.java @@ -1,5 +1,5 @@ package com.plannaplan.types; public enum UserRoles { - STUDENT, DEANERY, ADMIN + STUDENT, DEANERY, ADMIN, TEST_USER } \ No newline at end of file diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java new file mode 100644 index 0000000..e21bf45 --- /dev/null +++ b/buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java @@ -0,0 +1,63 @@ +package com.plannaplan.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.plannaplan.entities.User; +import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.types.UserRoles; + +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.runner.RunWith; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration +@TestMethodOrder(OrderAnnotation.class) +public class UserServiceTest { + public static String TEST_USER_MAIL = "noteexisitingmail@notexistingdomain.com"; + public static String TEST_USER_NAME = "Tom"; + public static String TEST_USER_SUERNAME = "Kovalsky"; + + @Autowired + UserService userService; + + @Before + public void initialize() { + User testUser = new User(); + testUser.setEmail(TEST_USER_MAIL); + testUser.setName(TEST_USER_NAME); + testUser.setSurname(TEST_USER_SUERNAME); + testUser.setRole(UserRoles.TEST_USER); + this.userService.save(testUser); + } + + @Test + public void shouldReturnToken() { + try { + String token = this.userService.login(TEST_USER_MAIL); + System.out.println("Returned token: " + token); + assertTrue(token != null); + } catch (UserNotFoundException e) { + e.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void shouldThrowException() { + try { + this.userService.login("thiseamilisnotindatabase@gmail.com"); + assertTrue(false); + } catch (UserNotFoundException e) { + assertTrue(true); + } + } +} diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 137a7bd..50d5f57 100644 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -1,15 +1,36 @@ package com.plannaplan; +import com.plannaplan.entities.User; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; @SpringBootApplication public class App { + @Autowired + UserService userService; + public static void main(String[] args) { Logo logo = new Logo("beta"); System.out.println(logo.getLogo()); - System.out.println("|=============================================================================================|"); + System.out.println( + "|=============================================================================================|"); SpringApplication.run(App.class, args); } + + // @EventListener(ApplicationReadyEvent.class) + // public void importData() { + // User testUser = new User(); + // testUser.setEmail("noteexisitingmail@notexistingdomain.com"); + // testUser.setName("Tom"); + // testUser.setSurname("Kovalsky"); + // testUser.setRole(UserRoles.TEST_USER); + // this.userService.save(testUser); + // } }