diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a19085a..4cdc176 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,6 +2,8 @@ name: Build & Test RepCounterBot on: push: + branches: + - master pull_request: jobs: diff --git a/src/test/java/com/mykhailotiutiun/repcounterbot/services/UserServiceImplTest.java b/src/test/java/com/mykhailotiutiun/repcounterbot/services/UserServiceImplTest.java new file mode 100644 index 0000000..d10b0f6 --- /dev/null +++ b/src/test/java/com/mykhailotiutiun/repcounterbot/services/UserServiceImplTest.java @@ -0,0 +1,112 @@ +package com.mykhailotiutiun.repcounterbot.services; + +import com.mykhailotiutiun.repcounterbot.cache.ChatDataCache; +import com.mykhailotiutiun.repcounterbot.model.User; +import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; +import com.mykhailotiutiun.repcounterbot.repository.UserRepository; +import com.mykhailotiutiun.repcounterbot.service.Impl.UserServiceImpl; +import com.mykhailotiutiun.repcounterbot.service.LocalDateWeekService; +import com.mykhailotiutiun.repcounterbot.service.WorkoutWeekService; +import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class UserServiceImplTest { + + + @Mock + private UserRepository userRepository; + + @Mock + private WorkoutWeekService workoutWeekService; + + @Mock + private LocalDateWeekService localDateWeekService; + + @Mock + private ChatDataCache chatDataCache; + + @InjectMocks + private UserServiceImpl userService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testGetUserById() { + Long userId = 1L; + User expectedUser = new User(userId, "John Doe"); + + when(userRepository.findById(userId)).thenReturn(Optional.of(expectedUser)); + + User actualUser = userService.getUserById(userId); + + assertEquals(expectedUser, actualUser); + } + + @Test + public void testGetAllUsers() { + List expectedUsers = Collections.singletonList(new User(1L, "Alice")); + + when(userRepository.findAll()).thenReturn(expectedUsers); + + List actualUsers = userService.getAllUsers(); + + assertEquals(expectedUsers, actualUsers); + } + + @Test + public void testCreateUser() { + User newUser = new User(2L, "Bob"); + + when(userRepository.existsById(newUser.getId())).thenReturn(false); + + userService.create(newUser); + + verify(userRepository).save(newUser); + verify(workoutWeekService).create(any(WorkoutWeek.class)); + } + + @Test + public void testSetUserLang() { + String userId = "3"; + String localTag = "en_US"; + + User user = new User(Long.valueOf(userId), "Eva"); + + when(userRepository.findById(Long.valueOf(userId))).thenReturn(Optional.of(user)); + + userService.setUserLang(userId, localTag); + + verify(userRepository).save(user); + verify(chatDataCache).setUserSelectedLanguage(userId, localTag); + } + + @Test + public void testDeleteUserById() { + Long userId = 4L; + + userService.deleteById(userId); + + verify(userRepository).deleteById(userId); + } +} \ No newline at end of file diff --git a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java new file mode 100644 index 0000000..b586c36 --- /dev/null +++ b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java @@ -0,0 +1,112 @@ +package com.mykhailotiutiun.repcounterbot.services; + +import com.mykhailotiutiun.repcounterbot.model.User; +import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; +import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutWeekRepository; +import com.mykhailotiutiun.repcounterbot.service.Impl.WorkoutWeekServiceImpl; +import com.mykhailotiutiun.repcounterbot.service.LocalDateWeekService; +import com.mykhailotiutiun.repcounterbot.service.LocaleMessageService; +import com.mykhailotiutiun.repcounterbot.service.WorkoutDayService; +import org.junit.Assert; +import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.LocalDate; +import java.util.Objects; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class WorkoutWeekServiceImplTest { + + @Mock + private WorkoutWeekRepository workoutWeekRepository; + + @Mock + private WorkoutDayService workoutDayService; + + @Mock + private LocalDateWeekService localDateWeekService; + + @Mock + private LocaleMessageService localeMessageService; + + @InjectMocks + private WorkoutWeekServiceImpl workoutWeekService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testGetWorkoutWeekById() { + String workoutWeekId = "1"; + WorkoutWeek expectedWorkoutWeek = new WorkoutWeek(workoutWeekId, new User(1L, "John"), true, LocalDate.of(20024, 4, 14), LocalDate.of(20024, 4, 20)); + + when(workoutWeekRepository.findById(workoutWeekId)).thenReturn(Optional.of(expectedWorkoutWeek)); + + WorkoutWeek actualWorkoutWeek = workoutWeekService.getWorkoutWeekById(workoutWeekId); + + assertEquals(expectedWorkoutWeek, actualWorkoutWeek); + } + + @Test + public void testGetCurrentWorkoutWeekByUserId() { + Long userId = 123L; + WorkoutWeek expectedWorkoutWeek = new WorkoutWeek("2", new User(userId, "John"), true, LocalDate.of(20024, 4, 14), LocalDate.of(20024, 4, 20)); + + when(workoutWeekRepository.findByUserIdAndCurrent(userId, true)).thenReturn(Optional.of(expectedWorkoutWeek)); + when(localDateWeekService.isCurrentWeek(any(), any())).thenReturn(true); + + WorkoutWeek actualWorkoutWeek = workoutWeekService.getCurrentWorkoutWeekByUserId(userId); + + assertEquals(expectedWorkoutWeek, actualWorkoutWeek); + } + + @Test + public void testCreateWorkoutWeek() { + WorkoutWeek newWorkoutWeek = new WorkoutWeek("3", new User(1L, "John"), true, LocalDate.of(20024, 4, 14), LocalDate.of(20024, 4, 20)); + + when(workoutWeekRepository.existsById(newWorkoutWeek.getId())).thenReturn(false); + + workoutWeekService.create(newWorkoutWeek); + + verify(workoutWeekRepository).save(newWorkoutWeek); + verify(workoutDayService, times(7)).save(any(WorkoutDay.class)); + } + + @Test + public void testCreateFromOldWorkoutWeek() { + WorkoutWeek oldWorkoutWeek = new WorkoutWeek("4", new User(142L, "John"), true, LocalDate.of(20024, 4, 14), LocalDate.of(20024, 4, 20)); + + when(localDateWeekService.getFirstDateOfWeekFromDate(any())).thenReturn(LocalDate.now()); + when(localDateWeekService.getLastDateOfWeekFromDate(any())).thenReturn(LocalDate.now()); + + workoutWeekService.create(oldWorkoutWeek); + workoutWeekService.createFromOldWorkoutWeek(oldWorkoutWeek); + + verify(workoutWeekRepository, times(3)).save(any(WorkoutWeek.class)); + } + + @Test + public void testDeleteWorkoutWeekById() { + String workoutWeekId = "6"; + + workoutWeekService.deleteById(workoutWeekId); + + verify(workoutWeekRepository).deleteById(workoutWeekId); + } + +} \ No newline at end of file