mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-15 09:46:16 +01:00
Use Optional for optional attributes
This commit is contained in:
@@ -10,6 +10,7 @@ import javax.ws.rs.core.UriInfo;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public abstract class User2UserDtoMapper {
|
public abstract class User2UserDtoMapper {
|
||||||
@@ -35,8 +36,16 @@ public abstract class User2UserDtoMapper {
|
|||||||
target.setLinks(links);
|
target.setLinks(links);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mappings({@Mapping(target = "lastModified"), @Mapping(target = "creationDate")})
|
@Mapping(target = "creationDate")
|
||||||
Instant mapTime(Long epochMilli) {
|
Instant mapTime(Long epochMilli) {
|
||||||
return epochMilli == null? null: Instant.ofEpochMilli(epochMilli);
|
// TODO assert parameter not null
|
||||||
|
return Instant.ofEpochMilli(epochMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mapping(target = "lastModified")
|
||||||
|
Optional<Instant> mapOptionalTime(Long epochMilli) {
|
||||||
|
return Optional
|
||||||
|
.ofNullable(epochMilli)
|
||||||
|
.map(Instant::ofEpochMilli);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import lombok.Data;
|
|||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class UserDto {
|
public class UserDto {
|
||||||
@@ -12,7 +13,7 @@ public class UserDto {
|
|||||||
private boolean admin;
|
private boolean admin;
|
||||||
private Instant creationDate;
|
private Instant creationDate;
|
||||||
private String displayName;
|
private String displayName;
|
||||||
private Instant lastModified;
|
private Optional<Instant> lastModified;
|
||||||
private String mail;
|
private String mail;
|
||||||
private String name;
|
private String name;
|
||||||
private String password;
|
private String password;
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ package sonia.scm.api.v2.resources;
|
|||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import org.apache.shiro.authc.credential.PasswordService;
|
import org.apache.shiro.authc.credential.PasswordService;
|
||||||
import org.mapstruct.*;
|
import org.mapstruct.Context;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Named;
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static sonia.scm.api.rest.resources.UserResource.DUMMY_PASSWORT;
|
import static sonia.scm.api.rest.resources.UserResource.DUMMY_PASSWORT;
|
||||||
|
|
||||||
@@ -31,8 +35,14 @@ public abstract class UserDto2UserMapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mappings({@Mapping(target = "lastModified"), @Mapping(target = "creationDate")})
|
@Mapping(target = "creationDate")
|
||||||
Long mapTime(Instant instant) {
|
Long mapTime(Instant instant) {
|
||||||
return instant == null? null: instant.toEpochMilli();
|
// TODO assert parameter not null
|
||||||
|
return instant.toEpochMilli();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mapping(target = "lastModified")
|
||||||
|
Long mapOptionalTime(Optional<Instant> instant) {
|
||||||
|
return instant.map(Instant::toEpochMilli).orElse(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,8 +39,7 @@ public class User2UserDtoMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldMapLinks_forAdmin() {
|
public void shouldMapLinks_forAdmin() {
|
||||||
User user = new User();
|
User user = createDefaultUser();
|
||||||
user.setName("abc");
|
|
||||||
when(subject.hasRole("admin")).thenReturn(true);
|
when(subject.hasRole("admin")).thenReturn(true);
|
||||||
|
|
||||||
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
||||||
@@ -51,10 +50,16 @@ public class User2UserDtoMapperTest {
|
|||||||
assertEquals("expected map with create baseUri", expextedBaseUri, userDto.getLinks().get("create").getHref());
|
assertEquals("expected map with create baseUri", expextedBaseUri, userDto.getLinks().get("create").getHref());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private User createDefaultUser() {
|
||||||
public void shouldMapLinks_forNormalUser() {
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName("abc");
|
user.setName("abc");
|
||||||
|
user.setCreationDate(1L);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldMapLinks_forNormalUser() {
|
||||||
|
User user = createDefaultUser();
|
||||||
when(subject.hasRole("user")).thenReturn(true);
|
when(subject.hasRole("user")).thenReturn(true);
|
||||||
|
|
||||||
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
||||||
@@ -67,8 +72,7 @@ public class User2UserDtoMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldMapFields() {
|
public void shouldMapFields() {
|
||||||
User user = new User();
|
User user = createDefaultUser();
|
||||||
user.setName("abc");
|
|
||||||
|
|
||||||
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
||||||
|
|
||||||
@@ -77,9 +81,8 @@ public class User2UserDtoMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRemovePassword() {
|
public void shouldRemovePassword() {
|
||||||
User user = new User();
|
User user = createDefaultUser();
|
||||||
user.setPassword("password");
|
user.setPassword("password");
|
||||||
user.setName("abc");
|
|
||||||
|
|
||||||
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
||||||
|
|
||||||
@@ -88,8 +91,7 @@ public class User2UserDtoMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldMapTimes() {
|
public void shouldMapTimes() {
|
||||||
User user = new User();
|
User user = createDefaultUser();
|
||||||
user.setName("abc");
|
|
||||||
Instant expectedCreationDate = Instant.ofEpochSecond(6666666);
|
Instant expectedCreationDate = Instant.ofEpochSecond(6666666);
|
||||||
Instant expectedModificationDate = expectedCreationDate.plusSeconds(1);
|
Instant expectedModificationDate = expectedCreationDate.plusSeconds(1);
|
||||||
user.setCreationDate(expectedCreationDate.toEpochMilli());
|
user.setCreationDate(expectedCreationDate.toEpochMilli());
|
||||||
@@ -98,6 +100,6 @@ public class User2UserDtoMapperTest {
|
|||||||
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
||||||
|
|
||||||
assertEquals(expectedCreationDate, userDto.getCreationDate());
|
assertEquals(expectedCreationDate, userDto.getCreationDate());
|
||||||
assertEquals(expectedModificationDate, userDto.getLastModified());
|
assertEquals(expectedModificationDate, userDto.getLastModified().get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ import org.mockito.Mock;
|
|||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.mockito.MockitoAnnotations.initMocks;
|
import static org.mockito.MockitoAnnotations.initMocks;
|
||||||
|
|
||||||
@@ -22,8 +24,7 @@ public class UserDto2UserMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldMapFields() {
|
public void shouldMapFields() {
|
||||||
UserDto dto = new UserDto();
|
UserDto dto = createDefaultDto();
|
||||||
dto.setName("abc");
|
|
||||||
User user = mapper.userDtoToUser(dto, "original password");
|
User user = mapper.userDtoToUser(dto, "original password");
|
||||||
assertEquals("abc" , user.getName());
|
assertEquals("abc" , user.getName());
|
||||||
}
|
}
|
||||||
@@ -32,7 +33,7 @@ public class UserDto2UserMapperTest {
|
|||||||
public void shouldEncodePassword() {
|
public void shouldEncodePassword() {
|
||||||
when(passwordService.encryptPassword("unencrypted")).thenReturn("encrypted");
|
when(passwordService.encryptPassword("unencrypted")).thenReturn("encrypted");
|
||||||
|
|
||||||
UserDto dto = new UserDto();
|
UserDto dto = createDefaultDto();
|
||||||
dto.setPassword("unencrypted");
|
dto.setPassword("unencrypted");
|
||||||
User user = mapper.userDtoToUser(dto, "original password");
|
User user = mapper.userDtoToUser(dto, "original password");
|
||||||
assertEquals("encrypted" , user.getPassword());
|
assertEquals("encrypted" , user.getPassword());
|
||||||
@@ -40,21 +41,28 @@ public class UserDto2UserMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldMapTimes() {
|
public void shouldMapTimes() {
|
||||||
UserDto dto = new UserDto();
|
UserDto dto = createDefaultDto();
|
||||||
dto.setName("abc");
|
|
||||||
Instant expectedCreationDate = Instant.ofEpochMilli(66666660000L);
|
Instant expectedCreationDate = Instant.ofEpochMilli(66666660000L);
|
||||||
Instant expectedModificationDate = null;
|
Optional<Instant> expectedModificationDate = Optional.empty();
|
||||||
dto.setCreationDate(expectedCreationDate);
|
dto.setCreationDate(expectedCreationDate);
|
||||||
dto.setLastModified(expectedModificationDate);
|
dto.setLastModified(expectedModificationDate);
|
||||||
|
|
||||||
User user = mapper.userDtoToUser(dto, "original password");
|
User user = mapper.userDtoToUser(dto, "original password");
|
||||||
|
|
||||||
assertEquals((Long) expectedCreationDate.toEpochMilli(), user.getCreationDate());
|
assertEquals((Long) expectedCreationDate.toEpochMilli(), user.getCreationDate());
|
||||||
assertEquals(null, user.getLastModified());
|
assertNull(user.getLastModified());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
initMocks(this);
|
initMocks(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private UserDto createDefaultDto() {
|
||||||
|
UserDto dto = new UserDto();
|
||||||
|
dto.setName("abc");
|
||||||
|
dto.setCreationDate(Instant.now());
|
||||||
|
dto.setLastModified(Optional.empty());
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ public class UserV2ResourceTest {
|
|||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName("Neo");
|
user.setName("Neo");
|
||||||
user.setPassword("redpill");
|
user.setPassword("redpill");
|
||||||
|
user.setCreationDate(System.currentTimeMillis());
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user