Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for users and weeks #29

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Build & Test RepCounterBot

on:
push:
branches:
- master
pull_request:

jobs:
Expand Down
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);
}
}
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);
}

}