Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 503463be

Přidáno uživatelem Jakub Danek před více než 5 roky(ů)

re #29 split User into abstract class and RegisteredUser class

General user-info turned into interface in order to support multiple
user info sources (authentication methods)

Zobrazit rozdíly:

server/src/main/java/org/danekja/ymanager/business/auth/service/RegisteredUserService.java
1
package org.danekja.ymanager.business.auth.service;
2

  
3
import org.danekja.ymanager.repository.UserRepository;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.security.core.userdetails.UserDetails;
6
import org.springframework.security.core.userdetails.UserDetailsService;
7
import org.springframework.security.core.userdetails.UsernameNotFoundException;
8
import org.springframework.stereotype.Service;
9

  
10
@Service
11
public class RegisteredUserService implements UserDetailsService {
12

  
13
    private final UserRepository userRepository;
14

  
15
    @Autowired
16
    public RegisteredUserService(UserRepository userRepository) {
17
        this.userRepository = userRepository;
18
    }
19

  
20
    @Override
21
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
22
        UserDetails details = userRepository.getUser(username);
23

  
24
        //interface contract enforces this behavior
25
        if (details == null) {
26
            throw new UsernameNotFoundException("Username " + username + " not found!");
27
        }
28

  
29
        return details;
30
    }
31
}
server/src/main/java/org/danekja/ymanager/business/impl/DefaultUserManager.java
13 13
import org.slf4j.LoggerFactory;
14 14
import org.springframework.beans.factory.annotation.Autowired;
15 15
import org.springframework.dao.DataAccessException;
16
import org.springframework.security.core.userdetails.UserDetails;
17 16
import org.springframework.security.core.userdetails.UserDetailsService;
18
import org.springframework.security.core.userdetails.UsernameNotFoundException;
19 17
import org.springframework.stereotype.Service;
20 18

  
21 19
import java.time.LocalDate;
......
27 25
 * Also implements {@link UserDetailsService} to allow integration with Spring Security framework.
28 26
 */
29 27
@Service
30
public class DefaultUserManager implements UserManager, UserDetailsService {
28
public class DefaultUserManager implements UserManager {
31 29

  
32 30
    private static final Logger LOG = LoggerFactory.getLogger(DefaultUserManager.class);
33 31

  
......
67 65
        }
68 66
    }
69 67

  
70
    @Override
71
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
72
        UserDetails details = userRepository.getUser(username);
73

  
74
        //interface contract enforces this behavior
75
        if (details == null) {
76
            throw new UsernameNotFoundException("Username " + username + " not found!");
77
        }
78

  
79
        return details;
80
    }
81 68
}
server/src/main/java/org/danekja/ymanager/domain/RegisteredUser.java
1
package org.danekja.ymanager.domain;
2

  
3
import org.springframework.security.core.GrantedAuthority;
4
import org.springframework.security.core.authority.SimpleGrantedAuthority;
5
import org.springframework.security.core.userdetails.UserDetails;
6

  
7
import java.time.LocalDateTime;
8
import java.util.Collection;
9
import java.util.Collections;
10

  
11
public class RegisteredUser extends User implements UserDetails {
12

  
13
    /**
14
     * The maximal length of a name.
15
     */
16
    private static final int NAME_LENGTH = 45;
17

  
18
    /**
19
     * The maximal length of an email address.
20
     */
21
    private static final int EMAIL_ADDRESS_LENGTH = 100;
22

  
23
    /**
24
     * The user's first name.
25
     */
26
    private String firstName;
27

  
28
    /**
29
     * The user's last name.
30
     */
31
    private String lastName;
32

  
33
    /**
34
     * The user's email address.
35
     */
36
    private String email;
37

  
38
    /**
39
     * The URL of a user's photo.
40
     */
41
    private String photo;
42

  
43
    /**
44
     * The date and time of a user's creation.
45
     */
46
    private LocalDateTime creationDate;
47

  
48
    public RegisteredUser() {
49
    }
50

  
51
    public RegisteredUser(Long id, String firstName, String lastName, Float vacationCount, Integer totalSickDayCount, Integer takenSickDayCount, LocalDateTime notification, String token, String email, String photo, LocalDateTime creationDate, UserRole role, Status status) {
52
        super(id, vacationCount, totalSickDayCount, takenSickDayCount, notification, token, role, status);
53
        this.id = id;
54
        this.firstName = firstName;
55
        this.lastName = lastName;
56
        this.email = email;
57
        this.photo = photo;
58
        this.creationDate = creationDate;
59
        this.role = role;
60
        this.status = status;
61
    }
62

  
63
    /*
64
########################### UserDetails API #################################
65
 */
66
    @Override
67
    public Collection<? extends GrantedAuthority> getAuthorities() {
68
        //TODO do not create collection on each call
69
        return Collections.singleton(new SimpleGrantedAuthority(role.name()));
70
    }
71

  
72
    @Override
73
    public String getPassword() {
74
        return null;
75
    }
76

  
77
    @Override
78
    public String getUsername() {
79
        return getEmail();
80
    }
81

  
82
    @Override
83
    public boolean isAccountNonExpired() {
84
        //unsupported functionality
85
        return true;
86
    }
87

  
88
    @Override
89
    public boolean isAccountNonLocked() {
90
        //unsupported functionality
91
        return true;
92
    }
93

  
94
    @Override
95
    public boolean isCredentialsNonExpired() {
96
        //unsupported functionality
97
        return true;
98
    }
99

  
100
    @Override
101
    public boolean isEnabled() {
102
        return status == Status.ACCEPTED;
103
    }
104

  
105
    /*
106
    ################# UserDetails API (END) #########################
107
     */
108

  
109
    @Override
110
    public String getFirstName() {
111
        return firstName;
112
    }
113

  
114
    @Override
115
    public String getLastName() {
116
        return lastName;
117
    }
118

  
119
    @Override
120
    public String getEmail() {
121
        return email;
122
    }
123

  
124
    @Override
125
    public String getPhoto() {
126
        return photo;
127
    }
128

  
129
    @Override
130
    public LocalDateTime getCreationDate() {
131
        return creationDate;
132
    }
133

  
134
    /**
135
     * Replaces the user's first name with the new specified value.
136
     * If the given name is longer than the maximal allowed number of characters the method throws an exception.
137
     *
138
     * @param firstName the new first name
139
     * @throws IllegalArgumentException when the given name is longer than the maximal allowed number of characters
140
     */
141
    public void setFirstName(final String firstName) throws IllegalArgumentException {
142
        User.log.debug("Setting a new first name: {}", firstName);
143

  
144
        if (firstName == null) {
145
            User.log.warn("The given first name must not be null");
146
            throw new IllegalArgumentException("first.name.null.error");
147
        } else if (firstName.length() > NAME_LENGTH) {
148
            User.log.warn("The length of the given first name exceeded a limit");
149
            throw new IllegalArgumentException("name.length.error");
150
        }
151

  
152
        this.firstName = firstName;
153
    }
154

  
155
    /**
156
     * Replaces the user's last name with the given one.
157
     * If the given name is longer than the maximal allowed number of characters the method throws an exception.
158
     *
159
     * @param lastName the new last name
160
     * @throws IllegalArgumentException when the given name is longer than the maximal allowed number of characters
161
     */
162
    public void setLastName(final String lastName) throws IllegalArgumentException {
163
        User.log.debug("Setting a new last name: {}", lastName);
164

  
165
        if (lastName == null) {
166
            User.log.warn("The given last name must not be null");
167
            throw new IllegalArgumentException("last.name.null.error");
168
        } else if (lastName.length() > NAME_LENGTH) {
169
            User.log.warn("The length of the given last name exceeded a limit");
170
            throw new IllegalArgumentException("name.length.error");
171
        }
172

  
173
        this.lastName = lastName;
174
    }
175

  
176
    /**
177
     * Replaces the user's email address with the new given value.
178
     * If the given email is longer than the maximal allowed number of characters the method throws an exception.
179
     *
180
     * @param email the new email address
181
     * @throws IllegalArgumentException when the given email is longer than the maximal allowed number of characters
182
     */
183
    public void setEmail(final String email) throws IllegalArgumentException {
184
        User.log.debug("Setting a new email address: {}", email);
185

  
186
        if (email == null) {
187
            User.log.warn("The given email must not be null");
188
            throw new IllegalArgumentException("email.null.error");
189
        } else if (email.length() > EMAIL_ADDRESS_LENGTH) {
190
            User.log.warn("The length of the email address exceeded a limit");
191
            throw new IllegalArgumentException("email.length.error");
192
        }
193

  
194
        this.email = email;
195
    }
196

  
197
    /**
198
     * Replaces the URL of a user's photo with the given link.
199
     *
200
     * @param photo the new URL of the user's photo
201
     */
202
    public void setPhoto(final String photo) {
203
        User.log.debug("Setting a new url of a photo: {}", photo);
204

  
205
        this.photo = photo == null ? User.DEFAULT_PHOTO : photo;
206
    }
207

  
208
    /**
209
     * Replaces the user's creation date with the given date and time.
210
     *
211
     * @param creationDate the new creation date
212
     */
213
    public void setCreationDate(final LocalDateTime creationDate) {
214
        User.log.debug("Setting a new user's creation date: {}", creationDate);
215

  
216
        this.creationDate = creationDate;
217
    }
218
}
server/src/main/java/org/danekja/ymanager/domain/User.java
2 2

  
3 3
import org.slf4j.Logger;
4 4
import org.slf4j.LoggerFactory;
5
import org.springframework.security.core.GrantedAuthority;
6
import org.springframework.security.core.authority.SimpleGrantedAuthority;
7
import org.springframework.security.core.userdetails.UserDetails;
8 5

  
9 6
import java.time.LocalDateTime;
10 7
import java.time.LocalTime;
11
import java.util.Collection;
12
import java.util.Collections;
13 8

  
14 9
import static java.time.temporal.ChronoUnit.MINUTES;
15 10

  
......
17 12
 * The domain class {@code User} represents a single record in the 'end_user' table of a database.
18 13
 * User holds all informations about a user which is logged to the application.
19 14
 */
20
public class User implements UserDetails {
21
    /**
22
     * The maximal length of a name.
23
     */
24
    private static final int NAME_LENGTH = 45;
25

  
26
    /**
27
     * The maximal length of an email address.
28
     */
29
    private static final int EMAIL_ADDRESS_LENGTH = 100;
15
public abstract class User {
30 16

  
31 17
    /**
32 18
     * The url of a generic photo.
33 19
     */
34
    private static final String DEFAULT_PHOTO = "https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg";
20
    protected static final String DEFAULT_PHOTO = "https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg";
35 21

  
36 22
    /**
37 23
     * The logger.
38 24
     */
39
    private static final Logger log = LoggerFactory.getLogger(User.class);
25
    protected static final Logger log = LoggerFactory.getLogger(User.class);
40 26

  
41 27
    /**
42 28
     * The user's ID.
43 29
     */
44
    private Long id;
45

  
46
    /**
47
     * The user's first name.
48
     */
49
    private String firstName;
50

  
51
    /**
52
     * The user's last name.
53
     */
54
    private String lastName;
30
    protected Long id;
55 31

  
56 32
    /**
57 33
     * The number of user's remaining hours of an overtime.
58 34
     */
59
    private Float vacationCount;
35
    protected Float vacationCount;
60 36

  
61 37
    /**
62 38
     * The number of user's sick days available during a year.
63 39
     */
64
    private Integer totalSickDayCount;
40
    protected Integer totalSickDayCount;
65 41

  
66 42
    /**
67 43
     * The number of user's taken sick days.
68 44
     */
69
    private Integer takenSickDayCount;
45
    protected Integer takenSickDayCount;
70 46

  
71 47
    /**
72 48
     * The date and time of sending an email warning about an incoming reset of remaining overtimes and sick days.
73 49
     */
74
    private LocalDateTime notification;
50
    protected LocalDateTime notification;
75 51

  
76 52
    /**
77 53
     * The token for the Google oAuth.
78 54
     */
79
    private String token;
80

  
81
    /**
82
     * The user's email address.
83
     */
84
    private String email;
85

  
86
    /**
87
     * The URL of a user's photo.
88
     */
89
    private String photo;
90

  
91
    /**
92
     * The date and time of a user's creation.
93
     */
94
    private LocalDateTime creationDate;
55
    protected String token;
95 56

  
96 57
    /**
97 58
     * The user's role.
98 59
     */
99
    private UserRole role;
60
    protected UserRole role;
100 61

  
101 62
    /**
102 63
     * The user's authorization status.
103 64
     */
104
    private Status status;
65
    protected Status status;
105 66

  
106 67
    public User() {
107 68
    }
108 69

  
109
    public User(Long id, String firstName, String lastName, Float vacationCount, Integer totalSickDayCount, Integer takenSickDayCount, LocalDateTime notification, String token, String email, String photo, LocalDateTime creationDate, UserRole role, Status status) {
70
    public User(Long id, Float vacationCount, Integer totalSickDayCount, Integer takenSickDayCount, LocalDateTime notification, String token, UserRole role, Status status) {
110 71
        this.id = id;
111
        this.firstName = firstName;
112
        this.lastName = lastName;
113 72
        this.vacationCount = vacationCount;
114 73
        this.totalSickDayCount = totalSickDayCount;
115 74
        this.takenSickDayCount = takenSickDayCount;
116 75
        this.notification = notification;
117 76
        this.token = token;
118
        this.email = email;
119
        this.photo = photo;
120
        this.creationDate = creationDate;
121 77
        this.role = role;
122 78
        this.status = status;
123 79
    }
124 80

  
125
    /*
126
    ########################### UserDetails API #################################
127
     */
128
    @Override
129
    public Collection<? extends GrantedAuthority> getAuthorities() {
130
        //TODO do not create collection on each call
131
        return Collections.singleton(new SimpleGrantedAuthority(role.name()));
132
    }
133

  
134
    @Override
135
    public String getPassword() {
136
        return null;
137
    }
138

  
139
    @Override
140
    public String getUsername() {
141
        return getEmail();
142
    }
143

  
144
    @Override
145
    public boolean isAccountNonExpired() {
146
        //unsupported functionality
147
        return true;
148
    }
149

  
150
    @Override
151
    public boolean isAccountNonLocked() {
152
        //unsupported functionality
153
        return true;
154
    }
155

  
156
    @Override
157
    public boolean isCredentialsNonExpired() {
158
        //unsupported functionality
159
        return true;
160
    }
161

  
162
    @Override
163
    public boolean isEnabled() {
164
        return status == Status.ACCEPTED;
165
    }
166

  
167
    /*
168
    ################# UserDetails API (END) #########################
169
     */
170

  
171

  
172 81
    /**
173 82
     * Returns the user's ID.
174 83
     *
......
194 103
     *
195 104
     * @return the user's first name
196 105
     */
197
    public String getFirstName() {
198
        return this.firstName;
199
    }
200

  
201
    /**
202
     * Replaces the user's first name with the new specified value.
203
     * If the given name is longer than the maximal allowed number of characters the method throws an exception.
204
     *
205
     * @param firstName the new first name
206
     * @throws IllegalArgumentException when the given name is longer than the maximal allowed number of characters
207
     */
208
    public void setFirstName(final String firstName) throws IllegalArgumentException {
209
        User.log.debug("Setting a new first name: {}", firstName);
210

  
211
        if (firstName == null) {
212
            User.log.warn("The given first name must not be null");
213
            throw new IllegalArgumentException("first.name.null.error");
214
        } else if (firstName.length() > User.NAME_LENGTH) {
215
            User.log.warn("The length of the given first name exceeded a limit");
216
            throw new IllegalArgumentException("name.length.error");
217
        }
218

  
219
        this.firstName = firstName;
220
    }
106
    public abstract String getFirstName();
221 107

  
222 108
    /**
223 109
     * Returns the user's last name.
224 110
     *
225 111
     * @return the user's last name
226 112
     */
227
    public String getLastName() {
228
        return this.lastName;
229
    }
230

  
231
    /**
232
     * Replaces the user's last name with the given one.
233
     * If the given name is longer than the maximal allowed number of characters the method throws an exception.
234
     *
235
     * @param lastName the new last name
236
     * @throws IllegalArgumentException when the given name is longer than the maximal allowed number of characters
237
     */
238
    public void setLastName(final String lastName) throws IllegalArgumentException {
239
        User.log.debug("Setting a new last name: {}", lastName);
240

  
241
        if(lastName == null) {
242
            User.log.warn("The given last name must not be null");
243
            throw new IllegalArgumentException("last.name.null.error");
244
        } else if (lastName.length() > User.NAME_LENGTH) {
245
            User.log.warn("The length of the given last name exceeded a limit");
246
            throw new IllegalArgumentException("name.length.error");
247
        }
248

  
249
        this.lastName = lastName;
250
    }
113
    public abstract String getLastName();
251 114

  
252 115
    /**
253 116
     * Returns the number of user's remaining hours of an overtime.
......
461 324
     *
462 325
     * @return the user's email address
463 326
     */
464
    public String getEmail() {
465
        return this.email;
466
    }
467

  
468
    /**
469
     * Replaces the user's email address with the new given value.
470
     * If the given email is longer than the maximal allowed number of characters the method throws an exception.
471
     *
472
     * @param email the new email address
473
     * @throws IllegalArgumentException when the given email is longer than the maximal allowed number of characters
474
     */
475
    public void setEmail(final String email) throws IllegalArgumentException {
476
        User.log.debug("Setting a new email address: {}", email);
477

  
478
        if (email == null) {
479
            User.log.warn("The given email must not be null");
480
            throw new IllegalArgumentException("email.null.error");
481
        }else if (email.length() > User.EMAIL_ADDRESS_LENGTH) {
482
            User.log.warn("The length of the email address exceeded a limit");
483
            throw new IllegalArgumentException("email.length.error");
484
        }
485

  
486
        this.email = email;
487
    }
327
    public abstract String getEmail();
488 328

  
489 329
    /**
490 330
     * Returns the URL of a user's photo.
......
492 332
     * @return the URL of the user's photo
493 333
     */
494 334
    public String getPhoto() {
495
        return this.photo;
335
        return DEFAULT_PHOTO;
496 336
    }
497 337

  
498
    /**
499
     * Replaces the URL of a user's photo with the given link.
500
     *
501
     * @param photo the new URL of the user's photo
502
     */
503
    public void setPhoto(final String photo) {
504
        User.log.debug("Setting a new url of a photo: {}", photo);
338
    ;
505 339

  
506
        this.photo = photo == null ? User.DEFAULT_PHOTO : photo;
507
    }
508 340

  
509 341
    /**
510 342
     * Returns the date and time of a user's creation.
511 343
     *
512 344
     * @return the date and time of the user's creation
513 345
     */
514
    public LocalDateTime getCreationDate() {
515
        return this.creationDate;
516
    }
517

  
518
    /**
519
     * Replaces the user's creation date with the given date and time.
520
     *
521
     * @param creationDate the new creation date
522
     */
523
    public void setCreationDate(final LocalDateTime creationDate) {
524
        User.log.debug("Setting a new user's creation date: {}", creationDate);
525

  
526
        this.creationDate = creationDate;
527
    }
346
    public abstract LocalDateTime getCreationDate();
528 347

  
529 348
    /**
530 349
     * Returns the user's role.
......
640 459
    @Override
641 460
    public String toString() {
642 461
        return "User{" +
643
                "id=" + this.id +
644
                ", firstName='" + this.firstName + '\'' +
645
                ", lastName='" + this.lastName + '\'' +
646
                ", vacationCount=" + this.vacationCount +
647
                ", totalSickDayCount=" + this.totalSickDayCount +
648
                ", takenSickDayCount=" + this.takenSickDayCount +
649
                ", notification=" + this.notification +
650
                ", token='" + this.token + '\'' +
651
                ", email='" + this.email + '\'' +
652
                ", photo='" + this.photo + '\'' +
653
                ", creationDate=" + this.creationDate +
654
                ", role=" + this.role +
655
                ", status=" + this.status +
462
                "id=" + id +
463
                ", vacationCount=" + vacationCount +
464
                ", totalSickDayCount=" + totalSickDayCount +
465
                ", takenSickDayCount=" + takenSickDayCount +
466
                ", notification=" + notification +
467
                ", token='" + token + '\'' +
468
                ", role=" + role +
469
                ", status=" + status +
656 470
                '}';
657 471
    }
658 472
}
server/src/main/java/org/danekja/ymanager/repository/UserRepository.java
1 1
package org.danekja.ymanager.repository;
2 2

  
3
import org.danekja.ymanager.domain.RegisteredUser;
3 4
import org.danekja.ymanager.domain.Status;
4 5
import org.danekja.ymanager.domain.User;
5 6
import org.danekja.ymanager.domain.UserRole;
6
import org.danekja.ymanager.dto.*;
7
import org.danekja.ymanager.dto.BasicProfileUser;
8
import org.danekja.ymanager.dto.DefaultSettings;
9
import org.danekja.ymanager.dto.FullUserProfile;
7 10
import org.danekja.ymanager.repository.jdbc.mappers.UserRowMapper;
8 11
import org.slf4j.Logger;
9 12
import org.slf4j.LoggerFactory;
......
28 31
@Repository
29 32
public class UserRepository {
30 33

  
31
    private final RowMapper<User> USER_MAPPER = new UserRowMapper();
34
    private final RowMapper<RegisteredUser> USER_MAPPER = new UserRowMapper();
32 35

  
33 36
    /**
34 37
     * The mapper maps a row from a result of a query to an BasicProfileUser.
......
246 249
     * @param email email value, used as search key
247 250
     * @return found user object or null (if not found)
248 251
     */
249
    public User getUser(final String email) {
250
        List<User> users = this.jdbc.query("SELECT * FROM end_user WHERE email = ?", USER_MAPPER, email);
252
    public RegisteredUser getUser(final String email) {
253
        List<RegisteredUser> users = this.jdbc.query("SELECT * FROM end_user WHERE email = ?", USER_MAPPER, email);
251 254

  
252 255
        return RepositoryUtils.singleResult(users);
253 256
    }
......
258 261
     * @param id id value, used as search key
259 262
     * @return found user object or null (if not found)
260 263
     */
261
    public User getUser(final long id) {
262
        List<User> users = this.jdbc.query("SELECT * FROM end_user WHERE id = ?", USER_MAPPER, id);
264
    public RegisteredUser getUser(final long id) {
265
        List<RegisteredUser> users = this.jdbc.query("SELECT * FROM end_user WHERE id = ?", USER_MAPPER, id);
263 266

  
264 267
        return RepositoryUtils.singleResult(users);
265 268
    }
server/src/main/java/org/danekja/ymanager/repository/VacationRepository.java
1 1
package org.danekja.ymanager.repository;
2 2

  
3
import org.danekja.ymanager.domain.User;
4
import org.danekja.ymanager.domain.Vacation;
5 3
import org.danekja.ymanager.domain.Status;
6
import org.danekja.ymanager.domain.UserRole;
7
import org.danekja.ymanager.dto.VacationDay;
4
import org.danekja.ymanager.domain.Vacation;
8 5
import org.danekja.ymanager.domain.VacationType;
6
import org.danekja.ymanager.dto.VacationDay;
9 7
import org.slf4j.Logger;
10 8
import org.slf4j.LoggerFactory;
11 9
import org.springframework.beans.factory.annotation.Autowired;
......
163 161
        jdbc.update("DELETE FROM vacation_day WHERE id=?", id);
164 162
    }
165 163

  
166
    public Optional<User> findUserByVacationID(final long id) {
167
        return ofNullable(jdbc.queryForObject(
168
                "SELECT u.id,u.first_name, u.last_name, u.no_vacations," +
169
                        "u.no_sick_days, u.taken_sick_days, u.alert, u.token, u.email," +
170
                        "u.photo, u.creation_date, u.user_role, u.status " +
171
                "FROM vacation_day v INNER JOIN end_user u ON v.user_id = u.id " +
172
                "WHERE v.id = ?", new Object[]{id}, (ResultSet rs, int rowNum)->
173
        {
174
            User user = new User();
175
            user.setId(rs.getLong("u.id"));
176
            user.setFirstName(rs.getString("u.first_name"));
177
            user.setLastName(rs.getString("u.last_name"));
178
            user.setVacationCount(rs.getFloat("u.no_vacations"));
179
            user.setTotalSickDayCount(rs.getInt("u.no_sick_days"));
180
            user.setTakenSickDayCount(rs.getInt("u.taken_sick_days"));
181
            user.setNotification(rs.getTimestamp("u.alert").toLocalDateTime());
182
            user.setToken(rs.getString("u.token"));
183
            user.setEmail(rs.getString("u.email"));
184
            user.setPhoto(rs.getString("u.photo"));
185
            user.setCreationDate(rs.getTimestamp("u.creation_date").toLocalDateTime());
186
            user.setRole(UserRole.getUserRole(rs.getString("u.user_role")));
187
            user.setStatus(getStatus(rs.getString("u.status")));
188
            return user;
189
        }));
190
    }
191 164

  
192 165
    public boolean isExistVacationForUser(Long userId, LocalDate date) {
193 166
        return jdbc.queryForObject("SELECT count(id) AS exist FROM vacation_day WHERE vacation_date = ? AND user_id = ?",
server/src/main/java/org/danekja/ymanager/repository/jdbc/mappers/UserRowMapper.java
1 1
package org.danekja.ymanager.repository.jdbc.mappers;
2 2

  
3
import org.danekja.ymanager.domain.User;
3
import org.danekja.ymanager.domain.RegisteredUser;
4 4
import org.danekja.ymanager.domain.Status;
5 5
import org.danekja.ymanager.domain.UserRole;
6 6
import org.springframework.jdbc.core.RowMapper;
......
13 13
/**
14 14
 * Maps "end_user" query rows into User object.
15 15
 */
16
public class UserRowMapper implements RowMapper<User> {
16
public class UserRowMapper implements RowMapper<RegisteredUser> {
17 17

  
18 18
    @Override
19
    public User mapRow(ResultSet resultSet, int i) throws SQLException {
19
    public RegisteredUser mapRow(ResultSet resultSet, int i) throws SQLException {
20 20
        Long id = resultSet.getLong("id");
21 21
        String firstName = resultSet.getString("first_name");
22 22
        String lastName = resultSet.getString("last_name");
......
36 36
        UserRole role = UserRole.valueOf(resultSet.getString("user_role"));
37 37
        Status status = Status.valueOf(resultSet.getString("status"));
38 38

  
39
        return new User(id, firstName, lastName, vacations, sickDays, takenSickDays, alert, token, email, photo, creationDate, role, status);
39
        return new RegisteredUser(id, firstName, lastName, vacations, sickDays, takenSickDays, alert, token, email, photo, creationDate, role, status);
40 40
    }
41 41
}
server/src/test/java/org/danekja/ymanager/domain/RegisteredUserTest.java
1
package org.danekja.ymanager.domain;
2

  
3
import org.junit.jupiter.api.BeforeEach;
4
import org.junit.jupiter.api.Test;
5

  
6
import java.time.LocalDateTime;
7
import java.time.LocalTime;
8

  
9
import static org.junit.jupiter.api.Assertions.*;
10

  
11
/**
12
 * This class tests methods of the {@code RegisteredUser} class.
13
 * @see RegisteredUser
14
 */
15
class RegisteredUserTest {
16

  
17
    /**
18
     * The empty instance of the {@code RegisteredUser}.
19
     */
20
    private RegisteredUser user;
21

  
22
    /**
23
     * Prepares the instance of the {@code RegisteredUser}.
24
     */
25
    @BeforeEach
26
    void setUp() {
27
        this.user = new RegisteredUser();
28
        this.user.setVacationCount(0f);
29
        this.user.setTotalSickDayCount(0);
30
        this.user.setTakenSickDayCount(0);
31
    }
32

  
33
    /**
34
     * Tests the method {@code setFirstName} with common values where no problem should occur.
35
     */
36
    @Test
37
    void setFirstName_valid() {
38
        this.user.setFirstName("aaaaaa");
39
        assertEquals("aaaaaa", this.user.getFirstName());
40
    }
41

  
42
    /**
43
     * Tests the method {@code setFirstName} with the maximal length of a name.
44
     */
45
    @Test
46
    void setFirstName_maxLength_valid() {
47
        this.user.setFirstName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
48
        assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", this.user.getFirstName());
49
    }
50

  
51
    /**
52
     * Tests the method {@code setFirstName} with name that exceeds maximal length which should throw an exception.
53
     */
54
    @Test
55
    void setFirstName_tooLong() {
56
        assertThrows(IllegalArgumentException.class, () -> this.user.setFirstName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
57
    }
58

  
59
    /**
60
     * Tests the method {@code setFirstName} with null input which should throw an exception.
61
     */
62
    @Test
63
    void setFirstName_nullInput() {
64
        assertThrows(IllegalArgumentException.class, () -> this.user.setFirstName(null));
65
    }
66

  
67
    /**
68
     * Tests the method {@code setLastName} with common values where no problem should occur.
69
     */
70
    @Test
71
    void setLastName_valid() {
72
        this.user.setLastName("aaaaaa");
73
        assertEquals("aaaaaa", this.user.getLastName());
74
    }
75

  
76
    /**
77
     * Tests the method {@code setLastName} with the maximal length of a name.
78
     */
79
    @Test
80
    void setLastName_maxLength_valid() {
81
        this.user.setLastName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
82
        assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", this.user.getLastName());
83
    }
84

  
85
    /**
86
     * Tests the method {@code setLastName} with name that exceeds maximal length which should throw an exception.
87
     */
88
    @Test
89
    void setLastName_tooLong() {
90
        assertThrows(IllegalArgumentException.class, () -> this.user.setLastName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
91
    }
92

  
93
    /**
94
     * Tests the method {@code setLastName} with null input which should throw an exception.
95
     */
96
    @Test
97
    void setLastName_nullInput() {
98
        assertThrows(IllegalArgumentException.class, () -> this.user.setLastName(null));
99
    }
100

  
101
    /**
102
     * Tests the method {@code setVacationCount} with common values where no problem should occur.
103
     */
104
    @Test
105
    void setVacationCount_valid() {
106
        this.user.setVacationCount(10f);
107
        assertEquals(10f, this.user.getVacationCount().floatValue());
108
    }
109

  
110
    /**
111
     * Tests the method {@code setVacationCount} with zero which is a threshold value.
112
     */
113
    @Test
114
    void setVacationCount_zeroInput_valid() {
115
        this.user.setVacationCount(0f);
116
        assertEquals(0f, this.user.getVacationCount().floatValue());
117
    }
118

  
119
    /**
120
     * Tests the method {@code setVacationCount} with negative one which is a threshold value.
121
     */
122
    @Test
123
    void setVacationCount_negativeOneInput() {
124
        assertThrows(IllegalArgumentException.class, () -> this.user.setVacationCount(-1f));
125
    }
126

  
127
    /**
128
     * Tests the method {@code setVacationCount} with negative value.
129
     */
130
    @Test
131
    void setVacationCount_negativeInput() {
132
        assertThrows(IllegalArgumentException.class, () -> this.user.setVacationCount(-10f));
133
    }
134

  
135
    /**
136
     * Tests the method {@code setVacationCount} with null value which should throw an exception.
137
     */
138
    @Test
139
    void setVacationCount_nullInput() {
140
        assertThrows(IllegalArgumentException.class, () -> this.user.setVacationCount(null));
141
    }
142

  
143
    /**
144
     * Tests the method {@code addVacationCount} with common values where no problem should occur.
145
     */
146
    @Test
147
    void addVacationCount_floatInput_valid() {
148
        this.user.addVacationCount(10f);
149
        assertEquals(10f, this.user.getVacationCount().floatValue());
150
    }
151

  
152
    /**
153
     * Tests the method {@code addVacationCount} with common values where no problem should occur.
154
     */
155
    @Test
156
    void addVacationCount_addFloatInput_valid() {
157
        this.user.setVacationCount(2f);
158
        this.user.addVacationCount(10f);
159
        assertEquals(12f, this.user.getVacationCount().floatValue());
160
    }
161

  
162
    /**
163
     * Tests the method {@code addVacationCount} with common values where no problem should occur.
164
     */
165
    @Test
166
    void addVacationCount_subtractFloatInput_valid() {
167
        this.user.setVacationCount(20f);
168
        this.user.addVacationCount(-10f);
169
        assertEquals(10f, this.user.getVacationCount().floatValue());
170
    }
171

  
172
    /**
173
     * Tests the method {@code addVacationCount} with negative result of the operation which should throw an exception.
174
     */
175
    @Test
176
    void addVacationCount_floatInput_negativeResult() {
177
        assertThrows(IllegalArgumentException.class, () -> this.user.addVacationCount(-10f));
178
    }
179

  
180
    /**
181
     * Tests the method {@code addVacationCount} with null value which should throw an exception.
182
     */
183
    @Test
184
    void addVacationCount_nullFloatInput() {
185
        assertThrows(IllegalArgumentException.class, () -> this.user.addVacationCount(null));
186
    }
187

  
188
    /**
189
     * Tests the method {@code addVacationCount} with common values where no problem should occur.
190
     */
191
    @Test
192
    void addVacationCount_timeInput_valid() {
193
        LocalTime from = LocalTime.of(10,0);
194
        LocalTime to = LocalTime.of(20,0);
195
        this.user.addVacationCount(from, to);
196
        assertEquals(10f, this.user.getVacationCount().floatValue());
197
    }
198

  
199
    /**
200
     * Tests the method {@code addVacationCount} with wrong order of parameters which should throw an exception.
201
     */
202
    @Test
203
    void addVacationCount_timeInput_order() {
204
        LocalTime from = LocalTime.of(10,0);
205
        LocalTime to = LocalTime.of(20,0);
206
        assertThrows(IllegalArgumentException.class, () -> this.user.addVacationCount(to, from));
207
    }
208

  
209
    /**
210
     * Tests the method {@code addVacationCount} with null from parameter which should throw an exception.
211
     */
212
    @Test
213
    void addVacationCount_timeInput_nullFrom() {
214
        LocalTime to = LocalTime.of(20,0);
215
        assertThrows(IllegalArgumentException.class, () -> this.user.addVacationCount(null, to));
216
    }
217

  
218
    /**
219
     * Tests the method {@code addVacationCount} with null to parameter which should throw an exception.
220
     */
221
    @Test
222
    void addVacationCount_timeInput_nullTo() {
223
        LocalTime from = LocalTime.of(20,0);
224
        assertThrows(IllegalArgumentException.class, () -> this.user.addVacationCount(from, null));
225
    }
226

  
227
    /**
228
     * Tests the method {@code addVacationCount} with null parameters which should throw an exception.
229
     */
230
    @Test
231
    void addVacationCount_timeNullInput() {
232
        assertThrows(IllegalArgumentException.class, () -> this.user.addVacationCount(null, null));
233
    }
234

  
235
    /**
236
     * Tests the method {@code setTotalSickDayCount} with common values where no problem should occur.
237
     */
238
    @Test
239
    void setTotalSickDayCount_valid() {
240
        this.user.setTotalSickDayCount(10);
241
        assertEquals(10, this.user.getTotalSickDayCount().intValue());
242
    }
243

  
244
    /**
245
     * Tests the method {@code setTotalSickDayCount} with zero which is a threshold value.
246
     */
247
    @Test
248
    void setTotalSickDayCount_zeroInput_valid() {
249
        this.user.setTotalSickDayCount(0);
250
        assertEquals(0, this.user.getTotalSickDayCount().intValue());
251
    }
252

  
253
    /**
254
     * Tests the method {@code setTotalSickDayCount} with negative one which is a threshold value.
255
     */
256
    @Test
257
    void setTotalSickDayCount_negativeOneInput() {
258
        assertThrows(IllegalArgumentException.class, () -> this.user.setTotalSickDayCount(-1));
259
    }
260

  
261
    /**
262
     * Tests the method {@code setTotalSickDayCount} with negative value.
263
     */
264
    @Test
265
    void setTotalSickDayCount_negativeInput() {
266
        assertThrows(IllegalArgumentException.class, () -> this.user.setTotalSickDayCount(-10));
267
    }
268

  
269
    /**
270
     * Tests the method {@code setTotalSickDayCount} with null value.
271
     */
272
    @Test
273
    void setTotalSickDayCount_nullInput_valid() {
274
        this.user.setTotalSickDayCount(null);
275
        assertNull(this.user.getTotalSickDayCount());
276
    }
277

  
278
    /**
279
     * Tests the method {@code setTakenSickDayCount} with common values where no problem should occur.
280
     */
281
    @Test
282
    void setTakenSickDayCount_valid() {
283
        this.user.setTotalSickDayCount(20);
284
        this.user.setTakenSickDayCount(10);
285
        assertEquals(10, this.user.getTakenSickDayCount().intValue());
286
    }
287

  
288
    /**
289
     * Tests the method {@code setTakenSickDayCount} with zero which is a threshold value.
290
     */
291
    @Test
292
    void setTakenSickDayCount_zeroInput_valid() {
293
        this.user.setTakenSickDayCount(0);
294
        assertEquals(0, this.user.getTakenSickDayCount().intValue());
295
    }
296

  
297
    /**
298
     * Tests the method {@code setTakenSickDayCount} with negative one which is a threshold value.
299
     */
300
    @Test
301
    void setTakenSickDayCount_negativeOneInput() {
302
        assertThrows(IllegalArgumentException.class, () -> this.user.setTakenSickDayCount(-1));
303
    }
304

  
305
    /**
306
     * Tests the method {@code setTakenSickDayCount} with negative value.
307
     */
308
    @Test
309
    void setTakenSickDayCount_negativeInput() {
310
        this.user.setTotalSickDayCount(10);
311
        assertThrows(IllegalArgumentException.class, () -> this.user.setTakenSickDayCount(-10));
312
    }
313

  
314
    /**
315
     * Tests the method {@code setTakenSickDayCount} with a value that is greater than total sick days.
316
     */
317
    @Test
318
    void setTakenSickDayCount_over() {
319
        this.user.setTotalSickDayCount(10);
320
        assertThrows(IllegalArgumentException.class, () -> this.user.setTakenSickDayCount(20));
321
    }
322

  
323
    /**
324
     * Tests the method {@code setTakenSickDayCount} with a value that is equals the total sick days.
325
     */
326
    @Test
327
    void setTakenSickDayCount_same() {
328
        this.user.setTotalSickDayCount(10);
329
        this.user.setTakenSickDayCount(10);
330
        assertEquals(10, this.user.getTakenSickDayCount().intValue());
331
    }
332

  
333
    /**
334
     * Tests the method {@code setTakenSickDayCount} with null value.
335
     */
336
    @Test
337
    void setTakenSickDayCount_nullInput() {
338
        assertThrows(IllegalArgumentException.class, () -> this.user.setTakenSickDayCount(null));
339
    }
340

  
341
    /**
342
     * Tests the method {@code addTakenSickDayCount} with common values where no problem should occur.
343
     */
344
    @Test
345
    void addTakenSickDayCount_valid() {
346
        this.user.setTotalSickDayCount(50);
347
        this.user.addTakenSickDayCount(10);
348
        assertEquals(10, this.user.getTakenSickDayCount().intValue());
349
    }
350

  
351
    /**
352
     * Tests the method {@code addTakenSickDayCount} with common values where no problem should occur.
353
     */
354
    @Test
355
    void addTakenSickDayCount_add_valid() {
356
        this.user.setTotalSickDayCount(50);
357
        this.user.setTakenSickDayCount(2);
358
        this.user.addTakenSickDayCount(10);
359
        assertEquals(12f, this.user.getTakenSickDayCount().intValue());
360
    }
361

  
362
    /**
363
     * Tests the method {@code addTakenSickDayCount} with common values where no problem should occur.
364
     */
365
    @Test
366
    void addTakenSickDayCount_subtract_valid() {
367
        this.user.setTotalSickDayCount(50);
368
        this.user.setTakenSickDayCount(20);
369
        this.user.addTakenSickDayCount(-10);
370
        assertEquals(10f, this.user.getTakenSickDayCount().intValue());
371
    }
372

  
373
    /**
374
     * Tests the method {@code addTakenSickDayCount} with negative result of the operation which should throw an exception.
375
     */
376
    @Test
377
    void addTakenSickDayCount_negativeResult() {
378
        assertThrows(IllegalArgumentException.class, () -> this.user.addTakenSickDayCount(-10));
379
    }
380

  
381
    /**
382
     * Tests the method {@code addTakenSickDayCount} with null value which should throw an exception.
383
     */
384
    @Test
385
    void addTakenSickDayCount_nullInput() {
386
        assertThrows(IllegalArgumentException.class, () -> this.user.addTakenSickDayCount(null));
387
    }
388

  
389
    /**
390
     * Tests the method {@code addTakenSickDayCount} with a value that is greater than total sick days.
391
     */
392
    @Test
393
    void addTakenSickDayCount_over() {
394
        this.user.setTotalSickDayCount(10);
395
        assertThrows(IllegalArgumentException.class, () -> this.user.addTakenSickDayCount(20));
396
    }
397

  
398
    /**
399
     * Tests the method {@code addTakenSickDayCount} with a value that is equals the total sick days.
400
     */
401
    @Test
402
    void addTakenSickDayCount_same() {
403
        this.user.setTotalSickDayCount(10);
404
        this.user.addTakenSickDayCount(10);
405
        assertEquals(10, this.user.getTakenSickDayCount().intValue());
406
    }
407

  
408

  
409
    /**
410
     * Tests the method {@code setNotification} with common values where no problem should occur.
411
     */
412
    @Test
413
    void setNotification_valid() {
414
        this.user.setNotification(LocalDateTime.of(2010,5,1,20,0));
415
        assertEquals(LocalDateTime.of(2010,5,1,20,0), this.user.getNotification());
416
    }
417

  
418
    /**
419
     * Tests the method {@code setNotification} with null value.
420
     */
421
    @Test
422
    void setNotification_nullInput_valid() {
423
        this.user.setNotification(null);
424
        assertNull(this.user.getNotification());
425
    }
426

  
427
    /**
428
     * Tests the method {@code setToken} with common values where no problem should occur.
429
     */
430
    @Test
431
    void setToken_valid() {
432
        this.user.setToken("aaaaa");
433
        assertEquals("aaaaa", this.user.getToken());
434
    }
435

  
436
    /**
437
     * Tests the method {@code setToken} with null value.
438
     */
439
    @Test
440
    void setToken_nullInput() {
441
        assertThrows(IllegalArgumentException.class, () -> this.user.setToken(null));
442
    }
443

  
444
    /**
445
     * Tests the method {@code setEmail} with common values where no problem should occur.
446
     */
447
    @Test
448
    void setEmail_valid() {
449
        this.user.setEmail("aaaaaa");
450
        assertEquals("aaaaaa", this.user.getEmail());
451
    }
452

  
453
    /**
454
     * Tests the method {@code setEmail} with the maximal length of an email address.
455
     */
456
    @Test
457
    void setEmail_maxLength() {
458
        this.user.setEmail("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
459
        assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", this.user.getEmail());
460
    }
461

  
462
    /**
463
     * Tests the method {@code setEmail} with email address that exceeds maximal length which should throw an exception.
464
     */
465
    @Test
466
    void setEmail_tooLong() {
467
        assertThrows(IllegalArgumentException.class, () -> this.user.setEmail("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
468
    }
469

  
470
    /**
471
     * Tests the method {@code setEmail} with null value.
472
     */
473
    @Test
474
    void setEmail_nullInput() {
475
        assertThrows(IllegalArgumentException.class, () -> this.user.setEmail(null));
476
    }
477

  
478
    /**
479
     * Tests the method {@code setPhoto} with common values where no problem should occur.
480
     */
481
    @Test
482
    void setPhoto_valid() {
483
        this.user.setPhoto("aaaaa");
484
        assertEquals("aaaaa", this.user.getPhoto());
485
    }
486

  
487
    /**
488
     * Tests the method {@code setPhoto} with null value.
489
     */
490
    @Test
491
    void setPhoto_nullInput_valid() {
492
        this.user.setPhoto(null);
493
        assertEquals("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg", this.user.getPhoto());
494
    }
495

  
496
    /**
497
     * Tests the method {@code setRole} with common values where no problem should occur.
498
     */
499
    @Test
500
    void setRole_valid() {
501
        this.user.setRole(UserRole.EMPLOYER);
502
        assertEquals(UserRole.EMPLOYER, this.user.getRole());
503
    }
504

  
505
    /**
506
     * Tests the method {@code setRole} with null value.
507
     */
508
    @Test
509
    void setRole_nullInput() {
510
        assertThrows(IllegalArgumentException.class, () -> this.user.setRole(null));
511
    }
512

  
513
    /**
514
     * Tests the method {@code setStatus} with common values where no problem should occur.
515
     */
516
    @Test
517
    void setStatus_valid() {
518
        this.user.setStatus(Status.ACCEPTED);
519
        assertEquals(Status.ACCEPTED, this.user.getStatus());
520
    }
521

  
522
    /**
523
     * Tests the method {@code setStatus} with null value.
524
     */
525
    @Test
526
    void setStatus_nullInput() {
527
        assertThrows(IllegalArgumentException.class, () -> this.user.setStatus(null));
528
    }
529

  
530
    /**
531
     * Tests the method {@code takeVacation} with common values where no problem should occur.
532
     */
533
    @Test
534
    void takeVacation_valid() {
535
        this.user.setVacationCount(10f);
536
        this.user.takeVacation(LocalTime.of(15,0), LocalTime.of(20, 0));
537
        assertEquals(5, this.user.getVacationCount().intValue());
538
    }
539

  
540
    /**
541
     * Tests the method {@code takeVacation} when there is not enough vacations.
542
     */
543
    @Test
544
    void takeVacation_notEnough() {
545
        assertThrows(IllegalArgumentException.class, () -> this.user.takeVacation(LocalTime.of(15,0), LocalTime.of(20, 0)));
546
    }
547

  
548
    /**
549
     * Tests the method {@code takeVacation} with switched from and to parameters.
550
     */
551
    @Test
552
    void takeVacation_order() {
553
        assertThrows(IllegalArgumentException.class, () -> this.user.takeVacation( LocalTime.of(20,0), LocalTime.of(10,0)));
554
    }
555

  
556
    /**
557
     * Tests the method {@code takeVacation} with from argument null.
558
     */
559
    @Test
560
    void takeVacation_nullFrom() {
561
        assertThrows(IllegalArgumentException.class, () -> this.user.takeVacation(null, LocalTime.of(15,0)));
562
    }
563

  
564
    /**
565
     * Tests the method {@code takeVacation} with to argument null.
566
     */
567
    @Test
568
    void takeVacation_nullTo() {
569
        assertThrows(IllegalArgumentException.class, () -> this.user.takeVacation(LocalTime.of(15,0), null));
570
    }
571

  
572
    /**
573
     * Tests the method {@code takeVacation } with both arguments null.
574
     */
575
    @Test
576
    void takeVacation_nullBoth() {
577
        assertThrows(IllegalArgumentException.class, () -> this.user.takeVacation(null, null));
578
    }
579

  
580
    /**
581
     * Tests the method {@code takeVacation} with common values where no problem should occur.
582
     */
583
    @Test
584
    void takeSickDay_valid() {
585
        this.user.setTotalSickDayCount(5);
586
        this.user.takeSickDay();
587
        assertEquals(1, this.user.getTakenSickDayCount().intValue());
588
    }
589

  
590
    /**
591
     * Tests the method {@code takeVacation} when there is not enough available sick days.
592
     */
593
    @Test
594
    void takeSickDay_notEnough() {
595
        assertThrows(IllegalArgumentException.class, () -> this.user.takeSickDay());
596
    }
597

  
598
}
server/src/test/java/org/danekja/ymanager/domain/UserTest.java
1
package org.danekja.ymanager.domain;
2

  
3
import org.junit.jupiter.api.BeforeEach;
4
import org.junit.jupiter.api.Test;
5

  
6
import java.time.LocalDateTime;
7
import java.time.LocalTime;
8

  
9
import static org.junit.jupiter.api.Assertions.*;
10

  
11
/**
12
 * This class tests methods of the {@code User} class.
13
 * @see User
14
 */
15
class UserTest {
16

  
17
    /**
18
     * The empty instance of the {@code User}.
19
     */
20
    private User user;
21

  
22
    /**
23
     * Prepares the instance of the {@code User}.
24
     */
25
    @BeforeEach
26
    void setUp() {
27
        this.user = new User();
28
        this.user.setVacationCount(0f);
29
        this.user.setTotalSickDayCount(0);
30
        this.user.setTakenSickDayCount(0);
31
    }
32

  
33
    /**
34
     * Tests the method {@code setFirstName} with common values where no problem should occur.
35
     */
36
    @Test
37
    void setFirstName_valid() {
38
        this.user.setFirstName("aaaaaa");
39
        assertEquals("aaaaaa", this.user.getFirstName());
40
    }
41

  
42
    /**
43
     * Tests the method {@code setFirstName} with the maximal length of a name.
44
     */
45
    @Test
46
    void setFirstName_maxLength_valid() {
47
        this.user.setFirstName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
48
        assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", this.user.getFirstName());
49
    }
50

  
51
    /**
52
     * Tests the method {@code setFirstName} with name that exceeds maximal length which should throw an exception.
53
     */
54
    @Test
55
    void setFirstName_tooLong() {
56
        assertThrows(IllegalArgumentException.class, () -> this.user.setFirstName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
57
    }
58

  
59
    /**
60
     * Tests the method {@code setFirstName} with null input which should throw an exception.
61
     */
62
    @Test
63
    void setFirstName_nullInput() {
64
        assertThrows(IllegalArgumentException.class, () -> this.user.setFirstName(null));
65
    }
66

  
67
    /**
68
     * Tests the method {@code setLastName} with common values where no problem should occur.
69
     */
70
    @Test
71
    void setLastName_valid() {
72
        this.user.setLastName("aaaaaa");
73
        assertEquals("aaaaaa", this.user.getLastName());
74
    }
75

  
76
    /**
77
     * Tests the method {@code setLastName} with the maximal length of a name.
78
     */
79
    @Test
80
    void setLastName_maxLength_valid() {
81
        this.user.setLastName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
82
        assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", this.user.getLastName());
83
    }
84

  
85
    /**
86
     * Tests the method {@code setLastName} with name that exceeds maximal length which should throw an exception.
87
     */
88
    @Test
89
    void setLastName_tooLong() {
90
        assertThrows(IllegalArgumentException.class, () -> this.user.setLastName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
91
    }
92

  
93
    /**
... Rozdílový soubor je zkrácen, protože jeho délka přesahuje max. limit.

Také k dispozici: Unified diff