This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-24 23:10:16 +02:00
parent 2372d51efc
commit fe45151dd1
2 changed files with 208 additions and 0 deletions

View file

@ -0,0 +1,104 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanConditionRepository;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@DisplayName("BooleanConditionController tests")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class BooleanConditionControllerTests {
@InjectMocks private BooleanConditionController controller;
@Mock private BooleanConditionRepository repository;
@Mock private Principal principal;
@Test
public void testCreate() {
BooleanCondition condition = new BooleanCondition();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(any())).thenAnswer(i -> i.getArgument(0));
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
BooleanCondition returned = controller.create(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
BooleanCondition condition = new BooleanCondition();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(condition)).thenReturn(condition);
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setId(34L);
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
when(repository.findById(34L)).thenReturn(Optional.of(condition));
BooleanCondition returned = controller.update(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
public void testGetAll() {
BooleanCondition condition1 = new BooleanCondition();
condition1.setAutomationId(1L);
BooleanCondition condition2 = new BooleanCondition();
condition2.setAutomationId(1L);
BooleanCondition condition3 = new BooleanCondition();
condition3.setAutomationId(1L);
ArrayList<BooleanCondition> list = new ArrayList<>();
list.add(condition1);
list.add(condition2);
list.add(condition3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<BooleanCondition> returned = controller.getAll(42L);
assertThat(returned).contains(condition1);
assertThat(returned).contains(condition2);
assertThat(returned).contains(condition3);
assertThat(returned.size()).isEqualTo(3);
}
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}

View file

@ -0,0 +1,104 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTriggerRepository;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@DisplayName("BooleanTriggerController tests")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class BooleanTriggerControllerTests {
@InjectMocks private BooleanTriggerController controller;
@Mock private BooleanTriggerRepository repository;
@Mock private Principal principal;
@Test
public void testCreate() {
BooleanTrigger condition = new BooleanTrigger();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(any())).thenAnswer(i -> i.getArgument(0));
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
BooleanTrigger returned = controller.create(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
@SneakyThrows(NotFoundException.class)
public void testUpdate() {
BooleanTrigger condition = new BooleanTrigger();
condition.setOn(true);
condition.setDeviceId(1L);
condition.setAutomationId(2L);
when(repository.save(condition)).thenReturn(condition);
BooleanConditionOrTriggerSaveRequest toSend = new BooleanConditionOrTriggerSaveRequest();
toSend.setId(34L);
toSend.setAutomationId(2L);
toSend.setDeviceId(1L);
toSend.setOn(false);
when(repository.findById(34L)).thenReturn(Optional.of(condition));
BooleanTrigger returned = controller.update(toSend);
assertThat(returned.isOn()).isEqualTo(toSend.isOn());
assertThat(returned.getAutomationId()).isEqualTo(toSend.getAutomationId());
assertThat(returned.getDeviceId()).isEqualTo(toSend.getDeviceId());
}
@Test
public void testGetAll() {
BooleanTrigger condition1 = new BooleanTrigger();
condition1.setAutomationId(1L);
BooleanTrigger condition2 = new BooleanTrigger();
condition2.setAutomationId(1L);
BooleanTrigger condition3 = new BooleanTrigger();
condition3.setAutomationId(1L);
ArrayList<BooleanTrigger> list = new ArrayList<>();
list.add(condition1);
list.add(condition2);
list.add(condition3);
when(repository.findAllByAutomationId(42L)).thenReturn(list);
List<BooleanTrigger> returned = controller.getAll(42L);
assertThat(returned).contains(condition1);
assertThat(returned).contains(condition2);
assertThat(returned).contains(condition3);
assertThat(returned.size()).isEqualTo(3);
}
@Test
public void testDelete() {
doNothing().when(repository).deleteById(eq(42L));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
Assertions.assertDoesNotThrow(() -> controller.delete(42L));
}
}