Skip to content

Commit

Permalink
Merge pull request #166 from theMomentTeam/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
siwony authored Jun 17, 2021
2 parents 26257d2 + f011e43 commit 1060d1e
Show file tree
Hide file tree
Showing 9 changed files with 908 additions and 507 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ImprovementController {
@ApiImplicitParam(name = "RefreshToken", value = "로그인 성공 후 refresh_token", required = false, dataType = "String", paramType = "header")
})
public CommonResult save(@Valid @RequestBody ImprovementDto improvementDto){
improvementService.create(improvementDto);
improvementService.save(improvementDto);
return responseService.getSuccessResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
@Repository
public interface AdminRepository extends JpaRepository<AdminDomain, Long> {
AdminDomain findByAdminId(String adminId);
AdminDomain findByAdminIdAndAdminPwd(String adminId, String password);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
public interface ImprovementRepository extends JpaRepository<ImprovementDomain, Long> {
// 해당 idx 찾기.
ImprovementDomain findByImproveIdx(Long improveIdx);
// 개시글 제목으로 찾기.
ImprovementDomain findByImproveContent(String content);
// 해당 idx 삭제하기.
void deleteAllByImproveIdx(Long improveIdx);
// 모든 idx 최신순으로 조회하기.
List<ImprovementDomain> findAllByOrderByImproveIdxDesc();
}
}
44 changes: 23 additions & 21 deletions src/main/java/com/moment/the/service/ImprovementService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public class ImprovementService {

// Create improvement.
@Transactional
public ImprovementDomain create(ImprovementDto improvementDto){
public ImprovementDomain save(ImprovementDto improvementDto){
// 현재 user 정보를 가져오기
String UserEmail = GetUserEmail();
AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail);
if(adminDomain == null){
String UserEmail = getUserEmail();
try {
AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail);
return improvementRepository.save(improvementDto.ToEntity(adminDomain));
} catch (UserNotFoundException e){
throw new UserNotFoundException();
}

return improvementRepository.save(improvementDto.ToEntity(adminDomain));
}

// Read improvement.
Expand All @@ -49,36 +49,38 @@ public List<ImprovementViewAllDto> read(){
@Transactional
public void update(ImprovementDto improvementDto, Long improveIdx){
// 현재 user 정보를 가져오기
String UserEmail = GetUserEmail();
AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail);
if(adminDomain == null){
throw new UserNotFoundException();
try {
String UserEmail = getUserEmail();
AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail);
} catch (UserNotFoundException e){
System.err.println("UserNotFoundException 이 발생했습니다.");
}
// 개선 사례 가져오기
ImprovementDomain improvementDomain = improvementRepository.findByImproveIdx(improveIdx);
if (improvementDomain == null){
throw new NoImprovementException();
try {
ImprovementDomain improvementDomain = improvementRepository.findByImproveIdx(improveIdx);
improvementDomain.update(improvementDto);
} catch (NoImprovementException e){
System.err.println("NoImprovementException 이 발생했습니다.");
}
// 개선 사례 업데이트 하기
improvementDomain.update(improvementDto);
}

// Delete improvement.
@Transactional
public void delete(Long improveIdx){
ImprovementDomain improvementDomain = improvementRepository.findByImproveIdx(improveIdx);
if (improvementDomain == null){
throw new NoImprovementException();
try {
ImprovementDomain selectImprove = improvementRepository.findByImproveIdx(improveIdx);
improvementRepository.delete(selectImprove);
} catch (NoImprovementException e){
System.err.println("NoImprovementException 이 발생했습니다.");
}
improvementRepository.deleteAllByImproveIdx(improvementDomain.getImproveIdx());
}

// Current UserEmail을 가져오기.
public String GetUserEmail() {
public String getUserEmail() {
String userEmail;
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(principal instanceof UserDetails) {
userEmail = ((UserDetails)principal).getUsername();
userEmail = ((UserDetails) principal).getUsername();
} else {
userEmail = principal.toString();
}
Expand Down
236 changes: 4 additions & 232 deletions src/test/java/com/moment/the/TheApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import com.moment.the.advice.exception.UserAlreadyExistsException;
import com.moment.the.advice.exception.UserNotFoundException;
import com.moment.the.domain.AdminDomain;
import com.moment.the.domain.ImprovementDomain;
import com.moment.the.dto.AdminDto;
import com.moment.the.dto.ImprovementDto;
import com.moment.the.dto.SignInDto;
import com.moment.the.repository.AdminRepository;
import com.moment.the.repository.ImprovementRepository;
import com.moment.the.service.AdminService;
import com.moment.the.service.AdminServiceImpl;
import com.moment.the.service.ImprovementService;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
Expand All @@ -33,236 +37,4 @@
@SpringBootTest
@Transactional
class TheApplicationTests {

// test 편의를 위한 회원가입 매서드
void adminSignUp(String adminId, String password, String adminName) throws Exception {
AdminDto adminDto = new AdminDto(adminId, password, adminName);
adminService.signUp(adminDto);
}

@AfterEach
public void dataClean(){
adminRepository.deleteAll();
}

@Test
void pageable_값_검증() {
//Given
int expected = (186-5)/6;
//When
int actual = 30;
//then
assertEquals(expected, actual);
System.out.println("expected: " + expected + " actual: " + actual);
}

@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AdminRepository adminRepository;
@Autowired
private AdminService adminService;
@Autowired
private AdminServiceImpl adminServiceImpl;

@Test
void 회원가입(){
//Given
AdminDto adminDto = new AdminDto();
String email = "[email protected]";
String adminName = "jihwan";
String pw = "1234";

//when
adminDto.setAdminPwd(passwordEncoder.encode(pw));
adminDto.setAdminId(email);
adminDto.setAdminName(adminName);
adminRepository.save(adminDto.toEntity());

//then
assertEquals(adminDto.getAdminId(), email);
assertEquals(passwordEncoder.matches(pw,adminDto.getAdminPwd()), true);
assertEquals(adminDto.getAdminName(), "jihwan");
}

@Test
void 이_사용자가_있나요(){
//Given
AdminDto adminDto = new AdminDto();
String alreadyEmail = "asdf@asdf";
String email = "asdf@asdf";
adminDto.setAdminId(alreadyEmail);

//when
adminRepository.save(adminDto.toEntity());

//then
assertEquals(adminRepository.findByAdminId(email) == null , false);

}

@Test
void 로그인_하겠습니다(){
//Given
AdminDto adminDto = new AdminDto();

String id = "s20062@gsm";
adminDto.setAdminId(id);

String pw = "1234";
adminDto.setAdminPwd(passwordEncoder.encode(pw));

adminRepository.save(adminDto.toEntity());

//when
if(adminRepository.findByAdminId(id) == null){
throw new UserNotFoundException();
} else {
// then
assertEquals(passwordEncoder.matches(pw, adminDto.getAdminPwd()), true);
}
}

@Test
void GetUserEmail(){
//Given
AdminDto adminDto = new AdminDto();
String userEmail = "s20062@gsm";
String pw = "1234";
adminDto.setAdminId(userEmail);
adminDto.setAdminPwd(passwordEncoder.encode(pw));
adminRepository.save(adminDto.toEntity());
System.out.println("======== saved =========");

// when login session 발급
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
adminDto.getAdminId(),
adminDto.getAdminPwd(),
List.of(new SimpleGrantedAuthority("ROLE_USER")));
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(token);
System.out.println("=================================");
System.out.println(context);

//then
String currentUserEmail = adminServiceImpl.getUserEmail();
assertEquals(currentUserEmail, "s20062@gsm");
}

@Test
void 서비스_회원가입() throws Exception {
//Given
AdminDto adminDto = new AdminDto();
adminDto.setAdminId("s20062@gsm");
adminDto.setAdminPwd("1234");
adminDto.setAdminName("jihwan");

//when
adminService.signUp(adminDto);

//then
assertEquals(adminRepository.findByAdminId("s20062@gsm") != null, true);
}

@Test
void 서비스_로그인() throws Exception {
//Given
AdminDto adminDto = new AdminDto();
adminDto.setAdminId("s20062@gsmasdf");
adminDto.setAdminPwd(passwordEncoder.encode("1234"));
adminDto.setAdminName("jihwan");

//when
adminRepository.save(adminDto.toEntity());

//then
assertEquals(adminService.loginUser("s20062@gsmasdf","1234") == null, false);
}

@Test
void 회원탈퇴() throws Exception {
// Given 회원가입
AdminDto adminDto = new AdminDto();
adminDto.setAdminName("jihwan");
adminDto.setAdminId("s20062@gsm");
adminDto.setAdminPwd(passwordEncoder.encode("1234"));
adminRepository.save(adminDto.toEntity());
System.out.println("=========is saved=========");

// Given SignInDto
SignInDto signInDto = new SignInDto();
signInDto.setAdminId("s20062@gsm");
signInDto.setAdminPwd("1234");
System.out.println("======== is set ========");

// when login session 발급
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
adminDto.getAdminId(),
adminDto.getAdminPwd(),
List.of(new SimpleGrantedAuthority("ROLE_USER")));
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(token);
System.out.println("=================================");
System.out.println(context);
// when 회원탈퇴를 실행 했을 때.
adminService.withdrawal(signInDto);
}

@Test
void 시원이가_안믿는_매치스(){
//Given
String pw = "1234";
//when
String encodePw = passwordEncoder.encode(pw);
System.out.println("====================");
System.out.println(encodePw);
//then
assertEquals(passwordEncoder.matches(pw, encodePw), true);
}

@Test
void 서비스_토큰_발급(){
//Given
boolean exceptionCatched = false;

AdminDto adminDto = new AdminDto();
adminDto.setAdminId("admin@admin");
adminDto.setAdminPwd(passwordEncoder.encode("1234"));
adminRepository.save(adminDto.toEntity());

//When
try {
adminServiceImpl.loginUser("admin@admin", "134");
} catch (UserNotFoundException e) {
exceptionCatched = true;
}

//Then
assertTrue(exceptionCatched);
}

@Test
void 로그아웃(){
//Given
AdminDto adminDto = new AdminDto();
String userEmail = "s20062@gsm";
String pw = "1234";
adminDto.setAdminId(userEmail);
adminDto.setAdminPwd(passwordEncoder.encode(pw));
adminRepository.save(adminDto.toEntity());
System.out.println("======== saved =========");

// when login session 발급
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
adminDto.getAdminId(),
adminDto.getAdminPwd(),
List.of(new SimpleGrantedAuthority("ROLE_USER")));
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(token);
System.out.println("=================================");
System.out.println(context);

// When logout
adminServiceImpl.logout();
}
}
Loading

0 comments on commit 1060d1e

Please sign in to comment.