Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 김수빈/EC2 인스턴스 도메인.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://ec2-43-201-121-159.ap-northeast-2.compute.amazonaws.com:8080/
60 changes: 60 additions & 0 deletions 김수빈/freelec-springboot2-websevice/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@


plugins {
id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.14.RELEASE'
id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

group = 'com.spring-study'
version = '0.0.1-SNAPSHOT'
// Java 소스를 컴파일할 때 사용할 Java 버전
sourceCompatibility = '14'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.projectlombok:lombok:1.18.22'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

//3장
//implementation 'org.springframework.boot:spring-boot-starter-web'
//implementation 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//implementation 'com.h2database:h2' 버전 수정하기
implementation 'com.h2database:h2:1.4.197'
//testImplementation 'org.springframework.boot:spring-boot-starter-test'

//4장
implementation('org.springframework.boot:spring-boot-starter-mustache')

//5장
// spring security
implementation('org.springframework.boot:spring-boot-starter-oauth2-client')
testImplementation('org.springframework.security:spring-security-test')

// session
implementation('org.springframework.session:spring-session-jdbc')

//8장(MaridDB 드라이버 등록)
implementation('org.mariadb.jdbc:mariadb-java-client')
}

test {
useJUnitPlatform()
}

//8장
//-plain.jar를 생성하지 않기 위해
jar {
enabled = false
}

2 changes: 2 additions & 0 deletions 김수빈/freelec-springboot2-websevice/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'freelec-springboot2-websevice'

Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
package com.book.springboot.web;

import com.book.springboot.web.dto.PostsSaveRequestDto;
import com.book.springboot.service.posts.PostsService;
import com.book.springboot.web.dto.PostsListResponseDto;
import com.book.springboot.web.dto.PostsResponseDto;
import com.book.springboot.web.dto.PostsUpdateRequestDto; //

import com.book.springboot.web.dto.PostsListResponseDto; //
import java.util.List; //

import com.book.springboot.web.dto.PostsSaveRequestDto;
import com.book.springboot.web.dto.PostsUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RequestMapping("/api/v1/posts")
@RestController
public class PostsApiController {

private final PostsService postsService;

@PostMapping("/api/v1/posts")
public Long save(@RequestBody PostsSaveRequestDto requestDto) {
@PostMapping
public Long save(@RequestBody PostsSaveRequestDto requestDto){
return postsService.save(requestDto);
}

@PostMapping("/api/v1/posts/{id}")
public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) {
@PutMapping("{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) {
@GetMapping("{id}")
public PostsResponseDto findById(@PathVariable Long id){
return postsService.findById(id);
}

@DeleteMapping("/api/v1/posts/{id}")
public Long delete(@PathVariable Long id) {
@DeleteMapping("{id}")
public Long delete(@PathVariable Long id){
postsService.delete(id);
return id;
}
//목록 얻어오기
@GetMapping("/api/v1/posts/")

@GetMapping
public List<PostsListResponseDto> getPostsList(){
return postsService.findAllDesc();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.profiles.include=oauth,real-db
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.session.store-type=jdbc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{{/userName}}
{{^userName}}
<a href="/oauth2/authorization/google" class="btn btn-success active" role="button">Google Login</a>
<a href="/oauth2/authorization/naver" class="btn btn-secondary active" role="button">Naver Login</a>
<!--<a href="/oauth2/authorization/naver" class="btn btn-secondary active" role="button">Naver Login</a>-->
{{/userName}}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
package com.book.springboot.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.book.springboot.domain.posts.Posts;
import com.book.springboot.web.dto.PostsUpdateRequestDto;

import com.book.springboot.domain.posts.PostsRepository;
import com.book.springboot.web.dto.PostsSaveRequestDto;
import com.book.springboot.web.dto.PostsUpdateRequestDto;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

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.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;

//import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.List;

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

//5장
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.BeforeEach;
import com.fasterxml.jackson.databind.ObjectMapper;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // 호스트가 사용하지 않는 랜덤포트를 사용
public class PostsApiControllerTest {

@LocalServerPort
Expand All @@ -53,11 +41,6 @@ public class PostsApiControllerTest {
@Autowired
private PostsRepository postsRepository;

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

@Autowired
private WebApplicationContext context;

Expand All @@ -71,9 +54,14 @@ public void setup(){
.build();
}

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

@Test
@WithMockUser(roles="USER")
public void Posts_등록된다() throws Exception {
@WithMockUser(roles="USER") // 인증된 모의 사용자를 만들어서 사용
public void Posts_등록된다() throws Exception{
//given
String title = "title";
String content = "content";
Expand All @@ -82,28 +70,24 @@ public void setup(){
.content(content)
.author("author")
.build();

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

//when
//ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, requestDto, Long.class);
mvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(new ObjectMapper().writeValueAsString(requestDto)))
.andExpect(status().isOk());

//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);

}

@Test
@WithMockUser(roles="USER")
public void Posts_수정된다() throws Exception {
public void Posts_수정된다() throws Exception{
//given
Posts savedPosts = postsRepository.save(Posts.builder()
.title("title")
Expand All @@ -122,23 +106,16 @@ public void setup(){

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

HttpEntity<PostsUpdateRequestDto> requestEntity = new HttpEntity<>(requestDto);

//when
//ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Long.class);
mvc.perform(put(url)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(new ObjectMapper().writeValueAsString(requestDto)))
.andExpect(status().isOk());
.andExpect(status().isOk());
//then
//assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
//assertThat(responseEntity.getBody()).isGreaterThan(0L);

List<Posts> all = postsRepository.findAll();
assertThat(all.get(0).getTitle()).isEqualTo(expectedTitle);
assertThat(all.get(0).getContent()).isEqualTo(expectedContent);

}


}
}