Tests on Utils and UserResponse
This commit is contained in:
parent
4e35aa4a75
commit
eeec726966
2 changed files with 69 additions and 0 deletions
|
@ -0,0 +1,21 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserResponse;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class UserResponseTests {
|
||||
@Test
|
||||
public void testUserResponse() {
|
||||
User u = new User();
|
||||
u.setEmail("email@example.com");
|
||||
u.setId(42L);
|
||||
u.setUsername("username");
|
||||
UserResponse us = UserResponse.fromUser(u);
|
||||
assertEquals(us.getName(), u.getName());
|
||||
assertEquals(us.getId(), u.getId());
|
||||
assertEquals(us.getUsername(), u.getUsername());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class UtilsTests {
|
||||
|
||||
@Test
|
||||
public void testToList() {
|
||||
List<String> hormannTitles = List.of("Prof.", "Dr.", "Kai (spiritual leader)");
|
||||
assertThat(Utils.toList(hormannTitles))
|
||||
.containsExactly(hormannTitles.get(0), hormannTitles.get(1), hormannTitles.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReturnIfGuest() {
|
||||
Principal principal = Mockito.mock(Principal.class);
|
||||
UserRepository userRepository = Mockito.mock(UserRepository.class);
|
||||
User host = new User();
|
||||
User guest = new User();
|
||||
host.getGuests().add(guest);
|
||||
|
||||
when(userRepository.findById(1L)).thenReturn(Optional.of(host));
|
||||
when(userRepository.findById(2L)).thenReturn(Optional.empty());
|
||||
when(userRepository.findByUsername("user")).thenReturn(guest);
|
||||
when(principal.getName()).thenReturn("user");
|
||||
|
||||
try {
|
||||
assertThat(Utils.returnIfGuest(userRepository, "toReturn", 1L, principal))
|
||||
.isEqualTo("toReturn");
|
||||
} catch (NotFoundException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
||||
assertThatThrownBy(() -> Utils.returnIfGuest(userRepository, "toReturn", 2L, principal))
|
||||
.isInstanceOf(NotFoundException.class);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue