diff --git a/pom.xml b/pom.xml index fe6309b5..7e73f6a7 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,6 @@ true - org.projectlombok lombok true @@ -40,6 +39,10 @@ spring-boot-starter-test test + + com.google.code.gson + gson + mysql mysql-connector-java diff --git a/src/main/java/com/springboot/applypage/config/LocalDateTimeSerializer.java b/src/main/java/com/springboot/applypage/config/LocalDateTimeSerializer.java new file mode 100644 index 00000000..0cf1f256 --- /dev/null +++ b/src/main/java/com/springboot/applypage/config/LocalDateTimeSerializer.java @@ -0,0 +1,19 @@ +package com.springboot.applypage.config; + +import com.fasterxml.jackson.databind.JsonSerializable; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class LocalDateTimeSerializer implements JsonSerializer { + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + @Override + public JsonElement serialize(LocalDate localDate, Type type, JsonSerializationContext jsonSerializationContext) { + return new JsonPrimitive(formatter.format(localDate)); + } +} diff --git a/src/main/java/com/springboot/applypage/config/SwaggerConfiguration.java b/src/main/java/com/springboot/applypage/config/SwaggerConfiguration.java index 2820ec25..c7b401fb 100644 --- a/src/main/java/com/springboot/applypage/config/SwaggerConfiguration.java +++ b/src/main/java/com/springboot/applypage/config/SwaggerConfiguration.java @@ -10,8 +10,8 @@ import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; -//@Configuration -//@EnableSwagger2 +@Configuration +@EnableSwagger2 public class SwaggerConfiguration { @Bean diff --git a/src/main/java/com/springboot/applypage/controller/UserController.java b/src/main/java/com/springboot/applypage/controller/UserController.java new file mode 100644 index 00000000..a1a7d68b --- /dev/null +++ b/src/main/java/com/springboot/applypage/controller/UserController.java @@ -0,0 +1,37 @@ +package com.springboot.applypage.controller; + +import com.springboot.applypage.data.dto.UserDto; +import com.springboot.applypage.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/user") +public class UserController { + private final UserService userService; + @Autowired + public UserController(UserService userService){ + this.userService = userService; + } + + @GetMapping() + public ResponseEntity getUser(Long sid){ + UserDto userDto = userService.getUser(sid); + System.out.println(userDto); + return ResponseEntity.status(HttpStatus.OK).body(userDto); + } + + @PostMapping() + public ResponseEntity createUser(@RequestBody UserDto userDto){ + UserDto savedUser = userService.saveUser(userDto); + return ResponseEntity.status(HttpStatus.OK).body(savedUser); + } + + @DeleteMapping() + public ResponseEntity deleteUser(Long sid) throws Exception{ + userService.deleteUser(sid); + return ResponseEntity.status(HttpStatus.OK).body("정상적으로 삭제되었습니다."); + } +} diff --git a/src/main/java/com/springboot/applypage/data/dao/UserDAO.java b/src/main/java/com/springboot/applypage/data/dao/UserDAO.java new file mode 100644 index 00000000..fb597c59 --- /dev/null +++ b/src/main/java/com/springboot/applypage/data/dao/UserDAO.java @@ -0,0 +1,9 @@ +package com.springboot.applypage.data.dao; + +import com.springboot.applypage.data.entity.User; + +public interface UserDAO { + User insertUser(User user); + User selectUser(Long sid); + void deleteUser(Long sid) throws Exception; +} diff --git a/src/main/java/com/springboot/applypage/data/dao/impl/UserDAOImpl.java b/src/main/java/com/springboot/applypage/data/dao/impl/UserDAOImpl.java new file mode 100644 index 00000000..678a5478 --- /dev/null +++ b/src/main/java/com/springboot/applypage/data/dao/impl/UserDAOImpl.java @@ -0,0 +1,45 @@ +package com.springboot.applypage.data.dao.impl; + +import com.springboot.applypage.data.dao.UserDAO; +import com.springboot.applypage.data.entity.User; +import com.springboot.applypage.data.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +@Component +public class UserDAOImpl implements UserDAO { + + private final UserRepository userRepository; + @Autowired + public UserDAOImpl(UserRepository userRepository){ + this.userRepository = userRepository; + } + + @Override + public User insertUser(User user) { + + User savedUser = userRepository.save(user); + + return savedUser; + } + + @Override + public User selectUser(Long sid) { + User selectedUser = userRepository.getById(sid); + return selectedUser; + } + + @Override + public void deleteUser(Long sid) throws Exception { + Optional selectedUser = userRepository.findById(sid); + + if(selectedUser.isPresent()){ + User user = selectedUser.get(); + userRepository.delete(user); + }else{ + throw new Exception(); + } + } +} diff --git a/src/main/java/com/springboot/applypage/data/dto/UserDto.java b/src/main/java/com/springboot/applypage/data/dto/UserDto.java new file mode 100644 index 00000000..86db02af --- /dev/null +++ b/src/main/java/com/springboot/applypage/data/dto/UserDto.java @@ -0,0 +1,27 @@ +package com.springboot.applypage.data.dto; + +import com.springboot.applypage.data.enumdata.Role; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class UserDto { + + private Long sid; + + private String email; + + private String name; + + private LocalDate birthDay; + + private Role role; + +} diff --git a/src/main/java/com/springboot/applypage/data/entity/BaseEntity.java b/src/main/java/com/springboot/applypage/data/entity/BaseEntity.java index a886dd90..539082d5 100644 --- a/src/main/java/com/springboot/applypage/data/entity/BaseEntity.java +++ b/src/main/java/com/springboot/applypage/data/entity/BaseEntity.java @@ -4,11 +4,9 @@ import lombok.Setter; import lombok.ToString; import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; -import javax.persistence.Column; import javax.persistence.EntityListeners; import javax.persistence.MappedSuperclass; import java.time.LocalDateTime; @@ -21,7 +19,7 @@ public class BaseEntity { @CreatedDate - @Column(updatable = false) + //@Column(updatable = false) private LocalDateTime createdAt; @LastModifiedDate diff --git a/src/main/java/com/springboot/applypage/data/entity/User.java b/src/main/java/com/springboot/applypage/data/entity/User.java new file mode 100644 index 00000000..711414ee --- /dev/null +++ b/src/main/java/com/springboot/applypage/data/entity/User.java @@ -0,0 +1,37 @@ +package com.springboot.applypage.data.entity; + +import com.springboot.applypage.data.enumdata.Role; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.ColumnDefault; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.time.LocalDate; + +@Entity +@Data +@AllArgsConstructor +@NoArgsConstructor +@Table(name="user") +public class User extends BaseEntity{ + + @Id + private Long sid; + + @Column(nullable = false) + private String email; + + @Column(nullable = false) + private String name; + + @Column(nullable = false) + private LocalDate birthDay; + + @Column(nullable = false) + @ColumnDefault("BABY") + private Role role; +} diff --git a/src/main/java/com/springboot/applypage/data/enumdata/Role.java b/src/main/java/com/springboot/applypage/data/enumdata/Role.java new file mode 100644 index 00000000..3b61da1b --- /dev/null +++ b/src/main/java/com/springboot/applypage/data/enumdata/Role.java @@ -0,0 +1,5 @@ +package com.springboot.applypage.data.enumdata; + +public enum Role { + ROOT, REPR, MANA, BABY +} diff --git a/src/main/java/com/springboot/applypage/data/repository/UserRepository.java b/src/main/java/com/springboot/applypage/data/repository/UserRepository.java new file mode 100644 index 00000000..5f85849b --- /dev/null +++ b/src/main/java/com/springboot/applypage/data/repository/UserRepository.java @@ -0,0 +1,7 @@ +package com.springboot.applypage.data.repository; + +import com.springboot.applypage.data.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/src/main/java/com/springboot/applypage/service/UserService.java b/src/main/java/com/springboot/applypage/service/UserService.java new file mode 100644 index 00000000..a2010e03 --- /dev/null +++ b/src/main/java/com/springboot/applypage/service/UserService.java @@ -0,0 +1,9 @@ +package com.springboot.applypage.service; + +import com.springboot.applypage.data.dto.UserDto; + +public interface UserService { + UserDto getUser(Long sid); + UserDto saveUser(UserDto user); + void deleteUser(Long sid) throws Exception; +} diff --git a/src/main/java/com/springboot/applypage/service/impl/UserServiceImpl.java b/src/main/java/com/springboot/applypage/service/impl/UserServiceImpl.java new file mode 100644 index 00000000..5b5a7f34 --- /dev/null +++ b/src/main/java/com/springboot/applypage/service/impl/UserServiceImpl.java @@ -0,0 +1,62 @@ +package com.springboot.applypage.service.impl; + +import com.springboot.applypage.data.dao.UserDAO; +import com.springboot.applypage.data.dto.UserDto; +import com.springboot.applypage.data.entity.User; +import com.springboot.applypage.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserServiceImpl implements UserService { + + private final UserDAO userDAO; + + @Autowired + public UserServiceImpl(UserDAO userDAO){ + this.userDAO = userDAO; + } + + @Override + public UserDto getUser(Long sid) { + User selectedUser = userDAO.selectUser(sid); + + UserDto responseUser = new UserDto(); + + responseUser.setName(selectedUser.getName()); + responseUser.setRole(selectedUser.getRole()); + responseUser.setEmail(selectedUser.getEmail()); + responseUser.setSid(selectedUser.getSid()); + responseUser.setBirthDay(selectedUser.getBirthDay()); + + + return responseUser; + } + + @Override + public UserDto saveUser(UserDto user) { + //User savedUser = userDAO.insertUser(user); + User savedUser = new User(); + savedUser.setEmail(user.getEmail()); + savedUser.setBirthDay(user.getBirthDay()); + savedUser.setName(user.getName()); + savedUser.setRole(user.getRole()); + savedUser.setSid(user.getSid()); + + savedUser = userDAO.insertUser(savedUser); + + UserDto responseUser = new UserDto(); + responseUser.setRole(savedUser.getRole()); + responseUser.setEmail(savedUser.getEmail()); + responseUser.setSid(savedUser.getSid()); + responseUser.setBirthDay(savedUser.getBirthDay()); + responseUser.setName(savedUser.getName()); + + return responseUser; + } + + @Override + public void deleteUser(Long sid) throws Exception{ + userDAO.deleteUser(sid); + } +} diff --git a/src/test/java/com/springboot/applypage/controller/UserControllerTest.java b/src/test/java/com/springboot/applypage/controller/UserControllerTest.java new file mode 100644 index 00000000..1dd2bdc5 --- /dev/null +++ b/src/test/java/com/springboot/applypage/controller/UserControllerTest.java @@ -0,0 +1,109 @@ +package com.springboot.applypage.controller; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.springboot.applypage.config.LocalDateTimeSerializer; +import com.springboot.applypage.data.dto.UserDto; +import com.springboot.applypage.data.enumdata.Role; +import com.springboot.applypage.service.impl.UserServiceImpl; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDate; +import java.time.Month; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(UserController.class) //UserController 클래스 만을 가지고 온다 +public class UserControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean //가짜 객체를 주입받는다. + UserServiceImpl userService; + + @Test + @DisplayName("UserController getUser method Test") + void getUserTest() throws Exception{ + given(userService.getUser(201904055L)).willReturn( + new UserDto(201904055L + , "scg9268@kangnam.ac.kr" + , "성창규" + , LocalDate.of(2020, Month.JANUARY, 8) + , Role.ROOT) + ); + + String sid = "201904055"; + + mockMvc.perform( + get("/user?sid=" + sid)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.sid").exists()) + .andExpect(jsonPath("$.email").exists()) + .andExpect(jsonPath("$.name").exists()) + .andExpect(jsonPath("$.birthDay").exists()) + .andExpect(jsonPath("$.role").exists()) + .andDo(print()); + + verify(userService).getUser(201904055L); + } + + @Test + @DisplayName("UserController createUser method Test") + void createUserTest() throws Exception{ + given(userService.saveUser(new UserDto(201904055L + , "scg9268@kangnam.ac.kr" + , "성창규" + , LocalDate.of(2020, Month.JANUARY, 8) + , Role.ROOT))) + .willReturn(new UserDto(201904055L + , "scg9268@kangnam.ac.kr" + , "성창규" + , LocalDate.of(2020, Month.JANUARY, 8) + , Role.ROOT)); + + UserDto userDto = UserDto.builder() + .sid(201904055L) + .email("scg9268@kangnam.ac.kr") + .name("성창규") + .birthDay(LocalDate.of(2020, Month.JANUARY, 8)) + .role(Role.ROOT) + .build(); + + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateTimeSerializer()); + + Gson gson = gsonBuilder.setPrettyPrinting().create(); + String content = gson.toJson(userDto); + + mockMvc.perform( + post("/user") + .content(content) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.sid").exists()) + .andExpect(jsonPath("$.email").exists()) + .andExpect(jsonPath("$.name").exists()) + .andExpect(jsonPath("$.birthDay").exists()) + .andExpect(jsonPath("$.role").exists()) + .andDo(print()); + + verify(userService).saveUser(new UserDto(201904055L + , "scg9268@kangnam.ac.kr" + , "성창규" + , LocalDate.of(2020, Month.JANUARY, 8) + , Role.ROOT)); + } +} diff --git a/src/test/java/com/springboot/applypage/service/impl/UserServiceTest.java b/src/test/java/com/springboot/applypage/service/impl/UserServiceTest.java new file mode 100644 index 00000000..48f14624 --- /dev/null +++ b/src/test/java/com/springboot/applypage/service/impl/UserServiceTest.java @@ -0,0 +1,72 @@ +package com.springboot.applypage.service.impl; + +import com.springboot.applypage.data.dao.UserDAO; +import com.springboot.applypage.data.dao.impl.UserDAOImpl; +import com.springboot.applypage.data.dto.UserDto; +import com.springboot.applypage.data.entity.User; +import com.springboot.applypage.data.enumdata.Role; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.time.LocalDate; + +import static org.mockito.AdditionalAnswers.returnsFirstArg; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +public class UserServiceTest { + private UserDAO userDAO = Mockito.mock(UserDAO.class); + private UserServiceImpl userService; + + @BeforeEach + public void setUpTest(){ + userService = new UserServiceImpl(userDAO); + } + + @Test + void getUserTest(){ + + User user = new User(); + user.setSid(201904055L); + user.setRole(Role.BABY); + user.setName("성창규"); + user.setBirthDay(LocalDate.of(2000, 01, 25)); + user.setEmail("scg9268@naver.com"); + + Mockito.when(userDAO.selectUser(201904055L)) + .thenReturn(user); + + UserDto userDto = userService.getUser(201904055L); + + Assertions.assertEquals(user.getEmail(), userDto.getEmail()); + Assertions.assertEquals(user.getName(), userDto.getName()); + Assertions.assertEquals(user.getRole(), userDto.getRole()); + Assertions.assertEquals(user.getSid(), userDto.getSid()); + Assertions.assertEquals(user.getBirthDay(), userDto.getBirthDay()); + + verify(userDAO).selectUser(201904055L); + } + + @Test + void saveUserTest(){ + Mockito.when(userDAO.insertUser(any(User.class))) + .then(returnsFirstArg()); + + UserDto userDto = userService.saveUser( + new UserDto( + 201904055L + , "scg9268@kangnam.ac.kr" + , "성창규" + ,LocalDate.of(2000, 01, 25) + ,Role.ROOT) + ); + + Assertions.assertEquals(userDto.getBirthDay(),LocalDate.of(2000, 01, 25)); + Assertions.assertEquals(userDto.getEmail(), "scg9268@kangnam.ac.kr"); + Assertions.assertEquals(userDto.getSid(), 201904055L); + Assertions.assertEquals(userDto.getName(), "성창규"); + Assertions.assertEquals(userDto.getRole(), Role.ROOT); + } +}