728x90
반응형

1. UserUpdateDto

backend/module/user/repository/userUpdateDto

package web.backend.module.user.repository;

import lombok.Data;
import web.backend.module.user.User;

@Data
public class UserUpdateDto {
    private Long id;
    private User user;
}

 

2. Service

backend/module/user/UserService

package web.backend.module.user;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import web.backend.module.user.repository.UserQueryRepository;
import web.backend.module.user.repository.UserSpringJpaRepository;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserService {

    private final UserQueryRepository userQueryRepository;
    private final UserSpringJpaRepository userSpringJPARepository;

    public List<User> findAll() {
        return userSpringJPARepository.findAll();
    }

    public User findByIndexId(Long id) {
        return userSpringJPARepository.findById(id).get();
    }

    public String save(User user) {
        userSpringJPARepository.save(user);
        return "ok";
    }

    public String update(Long id, User user) {
        User userOne = userSpringJPARepository.findById(id).get();
        userOne.changeUser(user.getUserId());
        return "ok";
    }

    public String delete(Long id) {
        userSpringJPARepository.deleteById(id);
        return "ok";
    }


}

 

3. Controller

backend/module/user/UserController

package web.backend.module.user;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import web.backend.module.user.repository.UserUpdateDto;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    @GetMapping("/{userId}")
    public User findByIndexId(@PathVariable(value = "userId") Long userId ) {
        return userService.findByIndexId(userId);
    }

    @PostMapping
    public String save(@RequestBody User user) {
        return userService.save(user);
    }

    @PatchMapping
    public String update(@RequestBody UserUpdateDto user) {
        return userService.update(user.getId(), user.getUser());
    }

    @DeleteMapping
    public String delete(@RequestParam Long id) {
        return userService.delete(id);
    }

}

 

 


테스트 코드 작성

 

/src/test/java/web/backend/module/user/UserServiceTest

package web.backend.module.user;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import web.backend.module.user.repository.UserQueryRepository;
import web.backend.module.user.repository.UserSpringJpaRepository;

import javax.persistence.EntityManager;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Slf4j
class UserServiceTest {

    @Autowired
    EntityManager em;

    @Autowired
    UserSpringJpaRepository userSpringJpaRepository;

    @Autowired
    UserQueryRepository userQueryRepository;

    @BeforeEach
    public void before() {
        User user = new User("userId");
        User savedUser = userSpringJpaRepository.save(user);
    }

    @AfterEach
    public void after() {
        em.flush();
        em.clear();
    }


    @Test
    @Transactional
    void save() {
        Optional<User> findUser = userSpringJpaRepository.findByUserId("userId");
        String userId = findUser.get().getUserId();

        assertEquals("userId",userId);
    }

    @Test
    @Transactional
    void find() {

        List<User> findAll = userSpringJpaRepository.findAll();

        for( User userOne : findAll) {
            assertEquals("userId",userOne.getUserId());
        }

        User findOne = userSpringJpaRepository.findByUserId("userId").get();
        assertEquals("userId",findOne.getUserId());
    }

    @Test
    @Transactional
    void update() {
        Optional<User> findUser = userSpringJpaRepository.findByUserId("userId");
        User userOne = findUser.get();
        userOne.changeUser("newUserId");

        Optional<User> findNewUser = userSpringJpaRepository.findByUserId("newUserId");

        assertEquals("newUserId",findNewUser.get().getUserId());
    }

    @Test
    @Transactional
    void delete() {
        userSpringJpaRepository.deleteById(1L);

        assertEquals(0,userSpringJpaRepository.findAll().size());
    }


}

 

 

 

728x90
반응형

+ Recent posts