diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java new file mode 100644 index 0000000..2cf74db --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationServiceTests.java @@ -0,0 +1,74 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.util.ArrayList; +import java.util.Optional; +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.security.test.context.support.WithMockUser; + +@ExtendWith(MockitoExtension.class) +@WithMockUser(username = "user") +@DisplayName("SwitchableState controller test") +public class AutomationServiceTests { + @InjectMocks private AutomationService service; + + @Mock private TriggerRepository repository; + + @Mock private AutomationRepository automationRepository; + + @Mock private ConditionRepository conditionRepository; + + @Test + public void testFindTriggerByDeviceId() { + RangeTrigger trigger1 = new RangeTrigger(); + RangeTrigger trigger2 = new RangeTrigger(); + RangeTrigger trigger3 = new RangeTrigger(); + trigger1.setDeviceId(0L); + trigger2.setDeviceId(0L); + trigger3.setDeviceId(1L); + ArrayList list = new ArrayList<>(); + list.add(trigger1); + list.add(trigger1); + ArrayList> toPut = new ArrayList<>(); + when(repository.findAllByDeviceId(0L)).thenReturn(list); + service.findTriggersByDeviceId(0L, toPut); + assertThat(toPut.contains(trigger1)); + assertThat(toPut.contains(trigger2)); + } + + @Test + public void testFindByVerifiedId() { + Automation automation = new Automation(); + automation.setId(0L); + when(automationRepository.findById(eq(0L))).thenReturn(Optional.of(automation)); + Automation returned = service.findByVerifiedId(0L); + assertThat(returned).isSameAs(automation); + } + + @Test + public void testFindAllConditionByAutomationId() { + RangeCondition condition1 = new RangeCondition(); + RangeCondition condition2 = new RangeCondition(); + RangeCondition condition3 = new RangeCondition(); + condition1.setAutomationId(1L); + condition2.setAutomationId(0L); + condition3.setAutomationId(0L); + ArrayList list = new ArrayList<>(); + list.add(condition2); + list.add(condition3); + ArrayList> toPut = new ArrayList<>(); + when(conditionRepository.findAllByAutomationId(0L)).thenReturn(list); + service.findAllConditionsByAutomationId(0L, toPut); + assertThat(toPut).contains(condition2); + assertThat(toPut).contains(condition3); + } +}