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

[Category] Fix/category #130

Merged
merged 21 commits into from
Sep 25, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.data.domain.Page;
Expand All @@ -29,6 +30,9 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;




@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
Expand All @@ -41,7 +45,9 @@ public class CategoryServiceImpl implements CategoryService {
public CategoryResponseDto createCategory(CategoryCreateDto categoryCreateDto) {
Category category = categoryMapper.toCategory(categoryCreateDto);
category.setStatus(EStatus.ACTIVE);
category.setOwner(userRepository.findById(getCurrentUserId()).orElseThrow());
User owner = userRepository.findById(getCurrentUserId())
.orElseThrow(() -> new NoSuchElementException("Owner not found"));
category.setOwner(owner);
category = categoryRepository.save(category);
return categoryMapper.toCategoryResponseDto(category);
}
Expand Down Expand Up @@ -163,11 +169,17 @@ private Specification<Category> findAllByUser() {
});
}

private Long getCurrentUserId() {
public Long getCurrentUserId() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
org.springframework.security.core.userdetails.User user =
org.springframework.security.core.userdetails.User securityUser =
(org.springframework.security.core.userdetails.User) auth.getPrincipal();
User dbUser = userRepository.findByEmailOrPhone(user.getUsername());

User dbUser = userRepository.findByEmailOrPhone(securityUser.getUsername());

if (dbUser == null) {
throw new NoSuchElementException("User not found");
}

return dbUser.getId();
}

Expand Down
141 changes: 127 additions & 14 deletions src/test/java/com/fjb/sunrise/services/CategoryServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
package com.fjb.sunrise.services;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;


import com.fjb.sunrise.dtos.base.DataTableInputDTO;
import com.fjb.sunrise.dtos.requests.CategoryCreateDto;

import com.fjb.sunrise.dtos.requests.CategoryUpdateDto;
import com.fjb.sunrise.dtos.responses.CategoryResponseDto;
import com.fjb.sunrise.enums.ERole;
import com.fjb.sunrise.enums.EStatus;
import com.fjb.sunrise.exceptions.NotFoundException;
import com.fjb.sunrise.mappers.CategoryMapper;
import com.fjb.sunrise.models.Category;
import com.fjb.sunrise.models.User;
import com.fjb.sunrise.repositories.CategoryRepository;
import com.fjb.sunrise.repositories.UserRepository;
import com.fjb.sunrise.services.impl.CategoryServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

import java.util.Optional;


class CategoryServiceTest {

// The class to test, this annotation to inject all mock below to this
Expand All @@ -37,13 +55,20 @@ class CategoryServiceTest {
private CategoryRepository categoryRepository;


@Mock
private UserRepository userRepository;

// Class for re-use in test
private Category category;

// private CategoryCreateDto categoryCreateDto;
private com.fjb.sunrise.models.User user;
private CategoryCreateDto categoryCreateDto;
private CategoryResponseDto categoryResponseDto;

// private DataTableInputDTO dataTableInputDTO;
private DataTableInputDTO dataTableInputDTO;


// Class for re-use in test

private CategoryUpdateDto categoryUpdateDto;

Expand All @@ -65,18 +90,38 @@ public void init() {
.status(EStatus.ACTIVE)
.build();

// categoryCreateDto = new CategoryCreateDto();
// categoryCreateDto.setName("Category-Test");
categoryCreateDto = new CategoryCreateDto();
categoryCreateDto.setName("Category-Test");

user = new User();
user.setId(1L);
user.setRole(ERole.USER);

UserDetails securityUser = new org.springframework.security.core.userdetails.User("user@example.com", "password", new ArrayList<>());
Authentication auth = mock(Authentication.class);
when(auth.getPrincipal()).thenReturn(securityUser);

SecurityContext securityContext = mock(SecurityContext.class);
when(securityContext.getAuthentication()).thenReturn(auth);
SecurityContextHolder.setContext(securityContext);

categoryUpdateDto = new CategoryUpdateDto();
categoryUpdateDto.setId(1L);
categoryUpdateDto.setName("Category-Test");

// dataTableInputDTO = new DataTableInputDTO();
// dataTableInputDTO.setStart(0);
// dataTableInputDTO.setLength(10);
// dataTableInputDTO.setSearch(Map.of("value", "Category-Test"));
// dataTableInputDTO.setOrder(List.of(Map.of("colName", "name", "dir", "asc")));

dataTableInputDTO = new DataTableInputDTO();
dataTableInputDTO.setStart(0);
dataTableInputDTO.setLength(10);
dataTableInputDTO.setSearch(Map.of("value", "Category-Test"));
dataTableInputDTO.setOrder(List.of(Map.of("colName", "name", "dir", "asc")));

dataTableInputDTO = new DataTableInputDTO();
dataTableInputDTO.setStart(0);
dataTableInputDTO.setLength(10);
dataTableInputDTO.setSearch(Map.of("value", "Category-Test"));
dataTableInputDTO.setOrder(List.of(Map.of("colName", "name", "dir", "asc")));


}

Expand Down Expand Up @@ -110,6 +155,33 @@ void getCategoryById_shouldReturn404_whenNotFound() {
}


@Test
void getCurrentUserId_shouldThrowException_whenUserNotFound() {
// Mô phỏng Authentication
Authentication auth = mock(Authentication.class);
UserDetails securityUser =
new org.springframework.security.core.userdetails.User("user@example.com", "password", new ArrayList<>());
when(auth.getPrincipal()).thenReturn(securityUser);
SecurityContextHolder.getContext().setAuthentication(auth);

// Mô phỏng repository không tìm thấy người dùng
when(userRepository.findByEmailOrPhone(securityUser.getUsername())).thenReturn(null);

// Kiểm tra ngoại lệ
assertThrows(NoSuchElementException.class, () -> categoryService.getCurrentUserId());
}


@Test
void createCategory_shouldThrowException_whenMapperFails() {
// Mô phỏng lỗi khi mapper không thành công
when(categoryMapper.toCategory(categoryCreateDto)).thenThrow(new RuntimeException("Mapping error"));

// Kiểm tra ngoại lệ
assertThrows(RuntimeException.class, () -> categoryService.createCategory(categoryCreateDto));
}


@Nested
class UpdateCategoryTests {
@Test
Expand Down Expand Up @@ -211,4 +283,45 @@ void enableCategory_shouldDoNothing_whenCategoryNotFound() {
}


@Test
public void testGetCategoryList() {
// Giả lập hành vi cho repository
when(categoryRepository.findAll(any(Specification.class), any(Pageable.class)))
.thenReturn(new PageImpl<>(List.of(category)));

// Gọi phương thức
Page<Category> result = categoryService.getCategoryList(dataTableInputDTO);

// Kiểm tra kết quả
assertNotNull(result);
assertEquals(1, result.getContent().size());
assertEquals("Category-Test", result.getContent().get(0).getName());
}

@Test
public void testGetAllCategories() {
List<Category> categories = List.of(category);
when(categoryRepository.findAll()).thenReturn(categories);
when(categoryMapper.toCategoryResponseDto(any(Category.class))).thenReturn(categoryResponseDto);

// Gọi phương thức
List<CategoryResponseDto> result = categoryService.getAllCategories();

// Kiểm tra kết quả
assertEquals(1, result.size());
assertEquals("Category-Test", result.get(0).getName());
}

@Test
public void testFindCategoryByAdminAndUser() {
List<Category> categories = List.of(category);
when(categoryRepository.findAll(any(Specification.class), any(Sort.class))).thenReturn(categories);

// Gọi phương thức
List<Category> result = categoryService.findCategoryByAdminAndUser();

// Kiểm tra kết quả
assertEquals(1, result.size());
assertEquals("Category-Test", result.get(0).getName());
}
}
Loading