check session
This commit is contained in:
@@ -12,6 +12,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import com.example.oneul.domain.post.dao.command.PostCommandRepository;
|
||||
import com.example.oneul.domain.post.domain.Post;
|
||||
import com.example.oneul.domain.user.domain.UserEntity;
|
||||
import com.example.oneul.global.error.exception.ExpiredSessionException;
|
||||
import com.example.oneul.global.error.exception.NotFoundException;
|
||||
import com.example.oneul.infra.dto.PostMessage;
|
||||
import com.example.oneul.infra.kafka.KafkaPublisher;
|
||||
@@ -85,7 +86,10 @@ public class PostCommnadServiceImpl implements PostCommandService{
|
||||
public void deletePost(Long id, HttpSession httpSession){
|
||||
// TODO: 이 때 세션이 만기되면 어떡함
|
||||
UserEntity userEntity = (UserEntity)httpSession.getAttribute("user");
|
||||
|
||||
if(userEntity == null){
|
||||
throw new ExpiredSessionException("만료된 세션");
|
||||
}
|
||||
|
||||
postCommandRepository.deleteByIdAndWriter(id, userEntity);
|
||||
|
||||
kafkaPublisher.sendMessage(
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.example.oneul.global.error;
|
||||
|
||||
import com.example.oneul.domain.user.exception.UserAlreadyExistException;
|
||||
import com.example.oneul.domain.user.exception.WrongUsernameAndPasswordException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -10,6 +7,10 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
import com.example.oneul.domain.user.exception.UserAlreadyExistException;
|
||||
import com.example.oneul.domain.user.exception.WrongUsernameAndPasswordException;
|
||||
import com.example.oneul.global.error.exception.ExpiredSessionException;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
@@ -25,4 +26,10 @@ public class GlobalExceptionHandler {
|
||||
log.info(e.getMessage());
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ExpiredSessionException.class)
|
||||
protected ResponseEntity<String> handleExpiredSessionException(ExpiredSessionException e){
|
||||
log.info(e.getMessage());
|
||||
return new ResponseEntity<>(e.getMessage(),HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.oneul.global.error.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(code = HttpStatus.NOT_ACCEPTABLE, reason = "session is expired")
|
||||
public class ExpiredSessionException extends RuntimeException {
|
||||
public ExpiredSessionException(String message){
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,58 @@
|
||||
package com.example.oneul.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import com.example.oneul.domain.post.dao.command.PostCommandRepository;
|
||||
import com.example.oneul.domain.post.domain.Post;
|
||||
import com.example.oneul.domain.post.service.command.PostCommandService;
|
||||
import com.example.oneul.domain.post.service.command.PostCommnadServiceImpl;
|
||||
import com.example.oneul.infra.kafka.KafkaPublisher;
|
||||
|
||||
@ActiveProfiles("test")
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class PostCommandSerivceTest {
|
||||
|
||||
private PostCommandService postCommandService;
|
||||
@Mock private PostCommandRepository postCommandRepository;
|
||||
@Mock private KafkaPublisher kafkaPublisher;
|
||||
protected MockHttpSession httpSession;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
postCommandService = new PostCommnadServiceImpl(postCommandRepository, kafkaPublisher);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createPostTest() throws Exception {
|
||||
// given
|
||||
Long mockPostId = 1L;
|
||||
Post post = mockPost(mockPostId);
|
||||
ReflectionTestUtils.setField(post, "id", mockPostId);
|
||||
|
||||
// mocking
|
||||
given(postCommandRepository.save(post)).willReturn(post);
|
||||
given(postCommandRepository.findById(mockPostId)).willReturn(Optional.ofNullable(post));
|
||||
|
||||
// when
|
||||
Post createdPost = postCommandService.createPost(post, httpSession);
|
||||
|
||||
// then
|
||||
assertEquals(post.getContent(), createdPost.getContent());
|
||||
}
|
||||
|
||||
private Post mockPost(Long id) {
|
||||
return Post.builder().content("mocking post")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,11 +28,8 @@ import com.example.oneul.domain.user.service.UserServiceImpl;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class UserServiceTest {
|
||||
private UserService userService;
|
||||
@Mock
|
||||
private UserRepository userRepository;
|
||||
@Mock
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Mock private UserRepository userRepository;
|
||||
@Mock private PasswordEncoder passwordEncoder;
|
||||
protected MockHttpSession httpSession;
|
||||
|
||||
@BeforeEach
|
||||
|
||||
Reference in New Issue
Block a user