From 6dedf593322edd891507b67892fa5a38e380c155 Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Sat, 23 May 2020 19:46:35 +0200 Subject: [PATCH] added a couple of tests to guestcontrollertests --- .../controller/GuestControllerTests.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java index 26af2b6..6170571 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java @@ -1,13 +1,19 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestPermissionsRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestsUpdateRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.EagerUserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import java.security.Principal; +import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -89,4 +95,50 @@ public class GuestControllerTests { when(userRepository.findByUsername("user")).thenReturn(user); assertThat(guestController.findGuests(mockPrincipal).isEmpty()); } + + @Test + public void testUpdatePermission() { + GuestPermissionsRequest permission = new GuestPermissionsRequest(); + permission.setCameraEnabled(true); + when(mockPrincipal.getName()).thenReturn("user"); + User user = new User(); + user.setCameraEnabled(true); + when(userRepository.findByUsername("user")).thenReturn(user); + when(userRepository.save(user)).thenReturn(user); + assertTrue(guestController.updatePermissions(permission, mockPrincipal).isCameraEnabled()); + } + + @Test + public void testSetGuests() { + when(mockPrincipal.getName()).thenReturn("user"); + ArrayList users = new ArrayList<>(); + User user1 = new User(); + user1.setId(1L); + user1.getGuests().add(new User()); + user1.getGuests().add(new User()); + user1.getGuests().add(new User()); + User user2 = new User(); + user2.setId(2L); + User user3 = new User(); + user3.setId(3L); + users.add(user1); + users.add(user2); + users.add(user3); + ArrayList ids = new ArrayList<>(); + ids.add(1L); + ids.add(2L); + ids.add(3L); + GuestsUpdateRequest request = new GuestsUpdateRequest(); + request.setIds(ids); + when(userRepository.findAllById(request.getIds())).thenReturn(users); + when(userRepository.findByUsername("user")).thenReturn(user1); + when(userRepository.saveAll(Set.copyOf(user1.getGuests()))).thenReturn(null); + when(userRepository.save(any(User.class))).thenReturn(null); + when(userRepository.saveAll(users)).thenReturn(users); + List returned = guestController.setGuests(request, mockPrincipal); + assertThat(returned.size()).isEqualTo(3); + assertTrue(returned.contains(user1)); + assertTrue(returned.contains(user2)); + assertTrue(returned.contains(user3)); + } }