diff --git a/src/main/java/com/moment/the/improvement/service/ImprovementService.java b/src/main/java/com/moment/the/improvement/service/ImprovementService.java index 7c46b6f8..e72ce6a8 100644 --- a/src/main/java/com/moment/the/improvement/service/ImprovementService.java +++ b/src/main/java/com/moment/the/improvement/service/ImprovementService.java @@ -2,7 +2,8 @@ import com.moment.the.admin.AdminDomain; import com.moment.the.admin.repository.AdminRepository; -import com.moment.the.exceptionAdvice.exception.NoImprovementException; +import com.moment.the.admin.service.AdminServiceImpl; +import com.moment.the.exceptionAdvice.exception.AccessNotFoundException; import com.moment.the.exceptionAdvice.exception.UserNotFoundException; import com.moment.the.improvement.ImprovementDomain; import com.moment.the.improvement.dto.ImprovementDto; @@ -10,8 +11,6 @@ import com.moment.the.improvement.repository.ImprovementRepository; import lombok.RequiredArgsConstructor; import org.modelmapper.ModelMapper; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,10 +26,8 @@ public class ImprovementService { // Create improvement. @Transactional public ImprovementDomain save(ImprovementDto improvementDto){ - // 현재 user 정보를 가져오기 - String UserEmail = getUserEmail(); try { - AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail); + AdminDomain adminDomain = adminRepository.findByAdminId(AdminServiceImpl.getUserEmail()); return improvementRepository.save(improvementDto.ToEntity(adminDomain)); } catch (UserNotFoundException e){ throw new UserNotFoundException(); @@ -48,42 +45,23 @@ public List read(){ // Update improvement. @Transactional public void update(ImprovementDto improvementDto, Long improveIdx){ - // 현재 user 정보를 가져오기 - try { - String UserEmail = getUserEmail(); - AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail); - } catch (UserNotFoundException e){ - System.err.println("UserNotFoundException 이 발생했습니다."); - } // 개선 사례 가져오기 - try { - ImprovementDomain improvementDomain = improvementRepository.findByImproveIdx(improveIdx); + ImprovementDomain improvementDomain = improvementRepository.findByImproveIdx(improveIdx); + if(improvementDomain.getAdminDomain().getAdminId().equals(AdminServiceImpl.getUserEmail())){ improvementDomain.update(improvementDto); - } catch (NoImprovementException e){ - System.err.println("NoImprovementException 이 발생했습니다."); + } else { + throw new AccessNotFoundException(); } } // Delete improvement. @Transactional public void delete(Long improveIdx){ - try { - ImprovementDomain selectImprove = improvementRepository.findByImproveIdx(improveIdx); + ImprovementDomain selectImprove = improvementRepository.findByImproveIdx(improveIdx); + if(selectImprove.getAdminDomain().getAdminId().equals(AdminServiceImpl.getUserEmail())){ improvementRepository.delete(selectImprove); - } catch (NoImprovementException e){ - System.err.println("NoImprovementException 이 발생했습니다."); - } - } - - // Current UserEmail을 가져오기. - public String getUserEmail() { - String userEmail; - Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - if(principal instanceof UserDetails) { - userEmail = ((UserDetails) principal).getUsername(); } else { - userEmail = principal.toString(); + throw new AccessNotFoundException(); } - return userEmail; } } diff --git a/src/test/java/com/moment/the/improvement/service/ImprovementServiceTest.java b/src/test/java/com/moment/the/improvement/service/ImprovementServiceTest.java new file mode 100644 index 00000000..388dd29b --- /dev/null +++ b/src/test/java/com/moment/the/improvement/service/ImprovementServiceTest.java @@ -0,0 +1,81 @@ +package com.moment.the.improvement.service; + +import com.moment.the.admin.AdminDomain; +import com.moment.the.admin.dto.AdminDto; +import com.moment.the.admin.repository.AdminRepository; +import com.moment.the.admin.service.AdminService; +import com.moment.the.improvement.dto.ImprovementDto; +import com.moment.the.improvement.repository.ImprovementRepository; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.transaction.annotation.Transactional; + +import java.awt.*; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@Transactional +class ImprovementServiceTest { + @Autowired + private ImprovementRepository improvementRepository; + @Autowired + private ImprovementService improvementService; + @Autowired + private AdminService adminService; + @Autowired + private AdminRepository adminRepository; + @AfterEach + void deleteData(){ + improvementRepository.deleteAll(); + } + + // test 편의를 위한 회원가입 매서드 + void adminSignUp(String adminId, String password, String adminName) throws Exception { + AdminDto adminDto = new AdminDto(adminId, password, adminName); + adminService.signUp(adminDto); + } + + // test 편의를 위한 로그인 매서드 + AdminDomain adminLogin(String adminId, String password) throws Exception { + AdminDomain adminDomain = adminRepository.findByAdminId(adminId); + UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( + adminDomain.getAdminId(), + adminDomain.getAdminPwd(), + List.of(new SimpleGrantedAuthority("ROLE_USER"))); + SecurityContext context = SecurityContextHolder.getContext(); + context.setAuthentication(token); + + return adminDomain; + } + + @Test + void 실제개선사례작성() throws Exception { + //Given admin + String email = "asdf@gsm"; + String pw = "1234"; + String name = "jihwan"; + adminSignUp(email, pw, name); + adminLogin(email, pw); + + //Given improvement + ImprovementDto improvementDto = new ImprovementDto(); + improvementDto.setImproveContent("Hello world"); + improvementDto.setImproveContent("it's jihwan"); + + //when + improvementService.save(improvementDto); + + //then + assertEquals(false, improvementRepository.findByImproveContent("it's jihwan") == null); + assertEquals(true, improvementRepository.findByImproveContent("it's jihwan").getAdminDomain().getAdminId().equals("asdf@gsm")); + } + +} \ No newline at end of file