-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for users and weeks
- Loading branch information
Showing
3 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ name: Build & Test RepCounterBot | |
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
|
||
jobs: | ||
|
112 changes: 112 additions & 0 deletions
112
src/test/java/com/mykhailotiutiun/repcounterbot/services/UserServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<User> expectedUsers = Collections.singletonList(new User(1L, "Alice")); | ||
|
||
when(userRepository.findAll()).thenReturn(expectedUsers); | ||
|
||
List<User> 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); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |