mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
Enhance unit tests and use guice for injection
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package sonia.scm.api.rest.resources;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
public class MapperModule extends AbstractModule {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(UserDto2UserMapper.class).to(Mappers.getMapper(UserDto2UserMapper.class).getClass());
|
||||
bind(User2UserDtoMapper.class).to(Mappers.getMapper(User2UserDtoMapper.class).getClass());
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import org.mapstruct.AfterMapping;
|
||||
import org.mapstruct.Context;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingTarget;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import sonia.scm.user.User;
|
||||
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
@@ -13,17 +12,16 @@ import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public abstract class User2UserDtoMapper {
|
||||
public static User2UserDtoMapper INSTANCE = Mappers.getMapper(User2UserDtoMapper.class);
|
||||
|
||||
abstract public UserDto userToUserDto(User user, @Context UriInfo uriInfo);
|
||||
public abstract UserDto userToUserDto(User user, @Context UriInfo uriInfo);
|
||||
|
||||
@AfterMapping
|
||||
public void removePassword(User source, @MappingTarget UserDto target, @Context UriInfo uriInfo) {
|
||||
void removePassword(@MappingTarget UserDto target) {
|
||||
target.setPassword(UserResource.DUMMY_PASSWORT);
|
||||
}
|
||||
|
||||
@AfterMapping
|
||||
public void appendLinks(User source, @MappingTarget UserDto target, @Context UriInfo uriInfo) {
|
||||
void appendLinks(@MappingTarget UserDto target, @Context UriInfo uriInfo) {
|
||||
Map<String, Link> links = new LinkedHashMap<>();
|
||||
links.put("self", new Link(uriInfo.getAbsolutePath()));
|
||||
target.setLinks(links);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package sonia.scm.api.rest.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import org.apache.shiro.authc.credential.PasswordService;
|
||||
import org.mapstruct.Context;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Named;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import sonia.scm.user.User;
|
||||
|
||||
import static sonia.scm.api.rest.resources.UserResource.DUMMY_PASSWORT;
|
||||
@@ -13,13 +13,14 @@ import static sonia.scm.api.rest.resources.UserResource.DUMMY_PASSWORT;
|
||||
@Mapper
|
||||
public abstract class UserDto2UserMapper {
|
||||
|
||||
public static UserDto2UserMapper INSTANCE = Mappers.getMapper(UserDto2UserMapper.class);
|
||||
@Inject
|
||||
private PasswordService passwordService;
|
||||
|
||||
@Mapping(source = "password", target = "password", qualifiedByName = "encrypt")
|
||||
abstract public User userDtoToUser(UserDto userDto, @Context String originalPassword, @Context PasswordService passwordService);
|
||||
public abstract User userDtoToUser(UserDto userDto, @Context String originalPassword);
|
||||
|
||||
@Named("encrypt")
|
||||
public String encrypt(String password, @Context String originalPassword, @Context PasswordService passwordService) {
|
||||
String encrypt(String password, @Context String originalPassword) {
|
||||
|
||||
if (DUMMY_PASSWORT.equals(password))
|
||||
{
|
||||
|
||||
@@ -7,7 +7,6 @@ import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.credential.PasswordService;
|
||||
import sonia.scm.security.Role;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserException;
|
||||
@@ -26,12 +25,14 @@ public class UserNewResource extends AbstractManagerResource<User, UserException
|
||||
/** Field description */
|
||||
public static final String PATH_PART = "usersnew";
|
||||
|
||||
private final PasswordService passwordService;
|
||||
private final UserDto2UserMapper dtoToUserMapper;
|
||||
private final User2UserDtoMapper userToDtoMapper;
|
||||
|
||||
@Inject
|
||||
public UserNewResource(UserManager userManager, PasswordService passwordService) {
|
||||
public UserNewResource(UserManager userManager, UserDto2UserMapper dtoToUserMapper, User2UserDtoMapper userToDtoMapper) {
|
||||
super(userManager);
|
||||
this.passwordService = passwordService;
|
||||
this.dtoToUserMapper = dtoToUserMapper;
|
||||
this.userToDtoMapper = userToDtoMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,7 +65,7 @@ public class UserNewResource extends AbstractManagerResource<User, UserException
|
||||
if (SecurityUtils.getSubject().hasRole(Role.ADMIN))
|
||||
{
|
||||
User user = manager.get(id);
|
||||
UserDto userDto = User2UserDtoMapper.INSTANCE.userToUserDto(user, uriInfo);
|
||||
UserDto userDto = userToDtoMapper.userToUserDto(user, uriInfo);
|
||||
return Response.ok(userDto).build();
|
||||
}
|
||||
else
|
||||
@@ -99,7 +100,7 @@ public class UserNewResource extends AbstractManagerResource<User, UserException
|
||||
@QueryParam("desc") boolean desc)
|
||||
{
|
||||
Collection<User> items = fetchItems(sortby, desc, start, limit);
|
||||
items.stream().map(user -> User2UserDtoMapper.INSTANCE.userToUserDto(user, uriInfo)).collect(Collectors.toList());
|
||||
items.stream().map(user -> userToDtoMapper.userToUserDto(user, uriInfo)).collect(Collectors.toList());
|
||||
return Response.ok(new GenericEntity<Collection<User>>(items) {}).build();
|
||||
}
|
||||
|
||||
@@ -116,7 +117,7 @@ public class UserNewResource extends AbstractManagerResource<User, UserException
|
||||
@PathParam("id") String name, UserDto userDto)
|
||||
{
|
||||
User o = manager.get(name);
|
||||
User user = UserDto2UserMapper.INSTANCE.userDtoToUser(userDto, o.getPassword(), passwordService);
|
||||
User user = dtoToUserMapper.userDtoToUser(userDto, o.getPassword());
|
||||
return super.update(name, user);
|
||||
}
|
||||
|
||||
@@ -132,7 +133,7 @@ public class UserNewResource extends AbstractManagerResource<User, UserException
|
||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
public Response create(@Context UriInfo uriInfo, UserDto userDto)
|
||||
{
|
||||
User user = UserDto2UserMapper.INSTANCE.userDtoToUser(userDto, "", passwordService);
|
||||
User user = dtoToUserMapper.userDtoToUser(userDto, "");
|
||||
return super.create(uriInfo, user);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user