• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torresj / community-api / 28

16 Jun 2025 10:53AM UTC coverage: 74.063% (-8.8%) from 82.813%
28

push

circleci

Jaime Torres Benavente
fixing test errors

237 of 320 relevant lines covered (74.06%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.07
/src/main/java/com/torresj/community/services/impl/UserServiceImpl.java
1
package com.torresj.community.services.impl;
2

3
import com.torresj.community.dtos.CommunityDto;
4
import com.torresj.community.dtos.UserDto;
5
import com.torresj.community.entities.UserEntity;
6
import com.torresj.community.enums.UserRole;
7
import com.torresj.community.exceptions.CommunityNotFoundException;
8
import com.torresj.community.exceptions.UserNotFoundException;
9
import com.torresj.community.mappers.UserMapper;
10
import com.torresj.community.repositories.UserRepository;
11
import com.torresj.community.security.CustomUserDetails;
12
import com.torresj.community.services.CommunityService;
13
import com.torresj.community.services.UserService;
14
import lombok.RequiredArgsConstructor;
15
import lombok.extern.slf4j.Slf4j;
16
import org.springframework.security.core.userdetails.UserDetails;
17
import org.springframework.security.core.userdetails.UserDetailsService;
18
import org.springframework.security.core.userdetails.UsernameNotFoundException;
19
import org.springframework.stereotype.Service;
20

21
import java.util.ArrayList;
22
import java.util.List;
23

24
@Service
25
@RequiredArgsConstructor
26
@Slf4j
1✔
27
public class UserServiceImpl implements UserService, UserDetailsService {
28
    private final UserRepository repository;
29
    private final UserMapper userMapper;
30
    private final CommunityService communityService;
31

32
    @Override
33
    public UserDto create(Long communityId, String name, String password, UserRole role)
34
            throws CommunityNotFoundException {
35
        CommunityDto communityDto = null;
1✔
36
        if (communityId != null) {
1✔
37
            communityDto = communityService.get(communityId);
1✔
38
        }
39

40
        return userMapper.toUserDto(
1✔
41
                repository.save(
1✔
42
                        UserEntity.builder()
1✔
43
                                .communityId(communityId)
1✔
44
                                .role(role)
1✔
45
                                .name(name)
1✔
46
                                .password(password)
1✔
47
                                .build()),
1✔
48
                communityDto);
49
    }
50

51
    @Override
52
    public UserDto get(long userId) throws UserNotFoundException, CommunityNotFoundException {
53
        UserEntity userEntity =
1✔
54
                repository.findById(userId).orElseThrow(() -> new UserNotFoundException(userId));
1✔
55
        CommunityDto communityDto = userEntity.getCommunityId() == null
1✔
56
                ? null
×
57
                : communityService.get(userEntity.getCommunityId());
1✔
58
        return userMapper.toUserDto(userEntity, communityDto);
1✔
59
    }
60

61
    @Override
62
    public List<UserDto> get() throws CommunityNotFoundException {
63
        List<UserEntity> entities = repository.findAll();
1✔
64
        List<UserDto> users = new ArrayList<>();
1✔
65
        for (UserEntity entity : entities) {
1✔
66
            CommunityDto communityDto = entity.getCommunityId() == null
1✔
67
                    ? null
1✔
68
                    : communityService.get(entity.getCommunityId());
1✔
69
            users.add(userMapper.toUserDto(entity, communityDto));
1✔
70
        }
1✔
71
        return users;
1✔
72
    }
73

74
    @Override
75
    public UserDto get(String name) throws UserNotFoundException, CommunityNotFoundException {
76
        UserEntity userEntity =
1✔
77
                repository.findByName(name).orElseThrow(() -> new UserNotFoundException(name));
1✔
78
        CommunityDto communityDto = communityService.get(userEntity.getCommunityId());
1✔
79
        return userMapper.toUserDto(userEntity, communityDto);
1✔
80
    }
81

82
    @Override
83
    public UserEntity getEntity(String name) throws UserNotFoundException {
84
        return repository.findByName(name).orElseThrow(() -> new UserNotFoundException(name));
×
85
    }
86

87
    @Override
88
    public void update(long userId, String newPassword) throws UserNotFoundException {
89
        UserEntity userEntity =
1✔
90
                repository.findById(userId).orElseThrow(() -> new UserNotFoundException(userId));
1✔
91
        repository.save(
×
92
                userEntity
93
                        .toBuilder()
×
94
                        .id(userId)
×
95
                        .name(userEntity.getName())
×
96
                        .role(userEntity.getRole())
×
97
                        .communityId(userEntity.getCommunityId())
×
98
                        .password(newPassword)
×
99
                        .build()
×
100
        );
101
    }
×
102

103
    @Override
104
    public void update(long userId, UserRole newRole) throws UserNotFoundException {
105
        UserEntity userEntity =
1✔
106
                repository.findById(userId).orElseThrow(() -> new UserNotFoundException(userId));
1✔
107
        repository.save(
×
108
                userEntity
109
                        .toBuilder()
×
110
                        .id(userId)
×
111
                        .name(userEntity.getName())
×
112
                        .role(newRole)
×
113
                        .communityId(userEntity.getCommunityId())
×
114
                        .password(userEntity.getPassword())
×
115
                        .build()
×
116
        );
117
    }
×
118

119
    @Override
120
    public void delete(long userId) {
121
        repository.deleteById(userId);
×
122
    }
×
123

124
    @Override
125
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
126
        UserEntity user = repository.findByName(username).orElseThrow(() -> new UsernameNotFoundException(username));
1✔
127
        return new CustomUserDetails(user);
1✔
128
    }
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc