본문 바로가기
프로젝트/스프링 부트와 AWS로 혼자 구현하는 웹 서비스_실습

스프링 부트와 AWS로 혼자 구현하는 웹 서비스 08

by dudung__ 2023. 8. 15.

코드를  다 작성했으니 이어서 test 코드로 검증을 진행해볼 것 임 

 

 

*PostsApiControllerTest

import com.start.khw.springboot.domain.posts.Posts;
import com.start.khw.springboot.domain.posts.PostsRepository;
import com.start.khw.springboot.web.dto.PostsSaveRequestsDto;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PostsApiControllerTest {

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private PostsRepository postsRepository;

    @After
    public void tearDown() throws Exception {
        postsRepository.deleteAll();

    }

    @Test
    public void Posts_registered() throws Exception {
        //given
        String title = "title";
        String content = "content";
        PostsSaveRequestsDto requestsDto = PostsSaveRequestsDto
                .builder()
                .title(title)
                .content(content)
                .author("author")
                .build();

        String url = "http://localhost:" + port  + "/api/v1/posts";

        //when
        ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, requestsDto, Long.class);

        //then
        assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(responseEntity.getBody()).isGreaterThan(0L);

        List<Posts> all = postsRepository.findAll();
        assertThat(all.get(0).getTitle()).isEqualTo(title);
        assertThat(all.get(0).getContent()).isEqualTo(content);
        
    }
}

 

 

=> 등록 기능 완성

 

수정 / 조회 기능 작성

 

*PostApiController

PostApiController클래스에  추가

@PutMapping("/api/v1/posts/{id}")
public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) {
    return postsService.update(id, requestDto);
}


@GetMapping("/api/v1/posts/{id}")
public PostsResponseDto findById (@PathVariable Long id) {
    return postsService.findByID(id);
}

 

 

Dto 패키지 내에 작성 

 

*PostResponseDto

import com.start.khw.springboot.domain.posts.Posts;
import lombok.Getter;

@Getter
public class PostsResponseDto {

    private Long id ;
    private String title;
    private String content;
    private String author;


    public PostsResponseDto(Posts entity){
        this.id = entity.getId();
        this.title = entity.getTitle();
        this.content = entity.getContent();
        this.author = entity.getAuthor();
    }
}

PostResponse 클래스 코드 작성 

 

 

 

*PostsUpdateRequestDto

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class PostsUpdateRequestDto {
    private String title;
    private String content;

    @Builder
    public PostsUpdateRequestDto(String title, String content) {
        this.title = title;
        this.content = content;

    }
}

    

 update기능 관련된 코드 작성

 

 

Domain.posts

*Posts


public void update(String title, String content){
    this.title = title;
    this.content = content;
}

update코드 추가

 

 

service.posts에 
*PostsService 클래스에 

 


@Transactional
public Long update(Long id, PostsUpdateRequestDto requestDto) {
     Posts posts = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("There is no such post id =" + id));
     posts.update(requestDto.getTitle(), requestDto.getContent());
     return id;
}

public PostsResponseDto findByID (Long id) {
    Posts entity = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("There is no such post id =" + id));

    return new PostsResponseDto(entity);
}

코드 추가

 

이제 테스트코드를 작성해서 진행하면 됨