Skip to content

Commit e783770

Browse files
committed
test : controller test ์ถ”๊ฐ€
1 parent a272f17 commit e783770

8 files changed

Lines changed: 257 additions & 7 deletions

File tree

โ€Žsrc/docs/asciidoc/index.adocโ€Ž

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,42 @@ include::{snippets}/bookshelf-controller-slice-test/find-summary-bookshelf-by-us
427427

428428
include::{snippets}/bookshelf-controller-slice-test/find-summary-bookshelf-by-user-id/response-fields.adoc[]
429429

430+
=== ์ฑ…์žฅ ์ข‹์•„์š” ์ถ”๊ฐ€
431+
432+
==== Request
433+
434+
include::{snippets}/bookshelf-like-controller-slice-test/create-like/http-request.adoc[]
435+
436+
==== Path Parameter
437+
438+
include::{snippets}/bookshelf-like-controller-slice-test/create-like/path-parameters.adoc[]
439+
440+
==== Request Header
441+
442+
include::{snippets}/bookshelf-like-controller-slice-test/create-like/request-headers.adoc[]
443+
444+
==== Response
445+
446+
include::{snippets}/bookshelf-like-controller-test/create-like/http-response.adoc[]
447+
448+
=== ์ฑ…์žฅ ์ข‹์•„์š” ์ทจ์†Œ
449+
450+
==== Request
451+
452+
include::{snippets}/bookshelf-like-controller-slice-test/delete-like/http-request.adoc[]
453+
454+
==== Path Parameter
455+
456+
include::{snippets}/bookshelf-like-controller-slice-test/delete-like/path-parameters.adoc[]
457+
458+
==== Request Header
459+
460+
include::{snippets}/bookshelf-like-controller-slice-test/delete-like/request-headers.adoc[]
461+
462+
==== Response
463+
464+
include::{snippets}/bookshelf-like-controller-test/delete-like/http-response.adoc[]
465+
430466
== Book - ์ฑ…
431467

432468
=== ์ฑ… ๊ฒ€์ƒ‰
@@ -473,7 +509,6 @@ include::{snippets}/book-controller-slice-test/test-find-recent-query/http-respo
473509

474510
include::{snippets}/book-controller-slice-test/test-find-recent-query/response-fields.adoc[]
475511

476-
477512
=== ์ฑ… ์ƒ์„ธ ์ •๋ณด
478513

479514
==== Request
@@ -888,13 +923,14 @@ include::{snippets}/book-group-comment-controller-slice-test/delete-book-group-c
888923

889924
include::{snippets}/book-group-comment-controller-slice-test/delete-book-group-comment_-should-return-ok/path-parameters.adoc[]
890925

891-
892926
=== ๋ชจ์ž„ ๊ฒ€์ƒ‰
893927

894928
==== Request
929+
895930
include::{snippets}/book-group-controller-slice-test/find-all-book-groups-by-query/http-request.adoc[]
896931

897932
==== Request Header
933+
898934
include::{snippets}/book-group-controller-slice-test/find-all-book-groups-by-query/request-headers.adoc[]
899935

900936
==== Request Parameter

โ€Žsrc/main/java/com/dadok/gaerval/domain/bookshelf/api/BookshelfLikeController.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import lombok.RequiredArgsConstructor;
1616

17-
@RequestMapping("/api/bookshelf/{bookshelfId}/like")
17+
@RequestMapping("/api/bookshelves/{bookshelfId}/like")
1818
@RestController
1919
@RequiredArgsConstructor
2020
public class BookshelfLikeController {

โ€Žsrc/main/java/com/dadok/gaerval/domain/bookshelf/entity/Bookshelf.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class Bookshelf extends BaseTimeColumn {
5858

5959
@JsonManagedReference
6060
@OneToMany(mappedBy = "bookshelf", cascade = CascadeType.PERSIST, orphanRemoval = true)
61-
private final Set<BookshelfLike> bookshelfLikes = new HashSet<>(); //set ๋ฎ์–ด์“ฐ๊ธฐ?
61+
private final Set<BookshelfLike> bookshelfLikes = new HashSet<>();
6262

6363
@Column(name = "job_id", nullable = true)
6464
private Long jobId;

โ€Žsrc/main/java/com/dadok/gaerval/domain/bookshelf/entity/BookshelfItem.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import lombok.NoArgsConstructor;
2828

2929
@Entity
30-
@Table(name = "Bookshelf_items",
30+
@Table(name = "Bookshelf_item",
3131
uniqueConstraints = {
3232
@UniqueConstraint(name = "bookshelf_id_book_id_unique_key",
3333
columnNames = {"bookshelf_id", "book_id"})

โ€Žsrc/main/java/com/dadok/gaerval/domain/bookshelf/entity/BookshelfLike.javaโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import lombok.Getter;
2323
import lombok.NoArgsConstructor;
2424

25-
@Entity
26-
@Table(name = "Bookshelf_likes",
25+
@Entity(name = "bookshelf_likes")
26+
@Table(
2727
uniqueConstraints = {
2828
@UniqueConstraint(name = "bookshelf_id_user_id_unique_key",
2929
columnNames = {"bookshelf_id", "user_id"})

โ€Žsrc/main/resources/sql/schema.sqlโ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ drop table if exists book_comments cascade;
22

33
drop table if exists bookshelf_item cascade;
44

5+
drop table if exists bookshelf_likes cascade;
6+
57
drop table if exists bookshelves cascade;
68

79
drop table if exists group_comments cascade;
@@ -174,6 +176,19 @@ create table if not exists bookshelf_item
174176
foreign key (book_id) references books (id)
175177
);
176178

179+
create table if not exists bookshelf_likes
180+
(
181+
id bigint auto_increment
182+
primary key,
183+
created_at datetime(6) not null,
184+
modified_at datetime(6) null,
185+
user_id bigint not null,
186+
bookshelf_id bigint not null,
187+
foreign key (bookshelf_id) references bookshelves (id),
188+
foreign key (user_id) references users (id),
189+
unique (user_id, bookshelf_id)
190+
);
191+
177192
create index job_id_index
178193
on bookshelves (job_id);
179194

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.dadok.gaerval.domain.bookshelf.api;
2+
3+
import static com.dadok.gaerval.global.config.security.jwt.AuthService.*;
4+
import static org.mockito.Mockito.*;
5+
import static org.springframework.restdocs.headers.HeaderDocumentation.*;
6+
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
7+
import static org.springframework.restdocs.request.RequestDocumentation.*;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
10+
11+
import java.nio.charset.StandardCharsets;
12+
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
15+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
16+
import org.springframework.boot.test.mock.mockito.MockBean;
17+
import org.springframework.http.MediaType;
18+
19+
import com.dadok.gaerval.controller.ControllerSliceTest;
20+
import com.dadok.gaerval.domain.bookshelf.service.BookshelfLikeService;
21+
import com.dadok.gaerval.testutil.WithMockCustomOAuth2LoginUser;
22+
import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper;
23+
import com.epages.restdocs.apispec.ResourceDocumentation;
24+
import com.epages.restdocs.apispec.ResourceSnippetParameters;
25+
26+
@WebMvcTest(controllers = BookshelfLikeController.class)
27+
@WithMockCustomOAuth2LoginUser
28+
class BookshelfLikeControllerSliceTest extends ControllerSliceTest {
29+
30+
@MockBean
31+
private BookshelfLikeService bookshelfLikeService;
32+
33+
@DisplayName("createLike - ์ฑ…์žฅ ์ข‹์•„์š” ์ƒ์„ฑ")
34+
@Test
35+
void createLike() throws Exception {
36+
// Given
37+
doNothing().when(bookshelfLikeService).createBookshelfLike(any(), eq(2L));
38+
39+
// When // Then
40+
mockMvc.perform(post("/api/bookshelves/{bookshelvesId}/like", 2L)
41+
.contentType(MediaType.APPLICATION_JSON)
42+
.header(ACCESS_TOKEN_HEADER_NAME, MOCK_ACCESS_TOKEN)
43+
.characterEncoding(StandardCharsets.UTF_8)
44+
).andExpect(status().isOk())
45+
.andDo(print())
46+
.andDo(this.restDocs.document(
47+
requestHeaders(
48+
headerWithName(ACCESS_TOKEN_HEADER_NAME).description(ACCESS_TOKEN_HEADER_NAME_DESCRIPTION)
49+
),
50+
pathParameters(
51+
parameterWithName("bookshelvesId").description("์ฒต์žฅ Id")
52+
)
53+
))
54+
.andDo(MockMvcRestDocumentationWrapper.document("{class-name}/{method-name}",
55+
ResourceDocumentation.resource(ResourceSnippetParameters.builder()
56+
.requestHeaders(
57+
headerWithName(ACCESS_TOKEN_HEADER_NAME).description(ACCESS_TOKEN_HEADER_NAME_DESCRIPTION))
58+
.pathParameters(
59+
parameterWithName("bookshelvesId").description("์ฒต์žฅ Id")
60+
)
61+
.build()
62+
)));
63+
}
64+
65+
@DisplayName("deleteLike - ์ฑ…์žฅ ์ข‹์•„์š” ์‚ญ์ œ")
66+
@Test
67+
void deleteLike() throws Exception {
68+
// Given
69+
doNothing().when(bookshelfLikeService).deleteBookshelfLike(any(), eq(2L));
70+
71+
// When // Then
72+
mockMvc.perform(delete("/api/bookshelves/{bookshelvesId}/like", 2L)
73+
.contentType(MediaType.APPLICATION_JSON)
74+
.header(ACCESS_TOKEN_HEADER_NAME, MOCK_ACCESS_TOKEN)
75+
.characterEncoding(StandardCharsets.UTF_8)
76+
).andExpect(status().isOk())
77+
.andDo(print())
78+
.andDo(this.restDocs.document(
79+
requestHeaders(
80+
headerWithName(ACCESS_TOKEN_HEADER_NAME).description(ACCESS_TOKEN_HEADER_NAME_DESCRIPTION)
81+
),
82+
pathParameters(
83+
parameterWithName("bookshelvesId").description("์ฒต์žฅ Id")
84+
)
85+
))
86+
.andDo(MockMvcRestDocumentationWrapper.document("{class-name}/{method-name}",
87+
ResourceDocumentation.resource(ResourceSnippetParameters.builder()
88+
.requestHeaders(
89+
headerWithName(ACCESS_TOKEN_HEADER_NAME).description(ACCESS_TOKEN_HEADER_NAME_DESCRIPTION))
90+
.pathParameters(
91+
parameterWithName("bookshelvesId").description("์ฒต์žฅ Id")
92+
)
93+
.build()
94+
)));
95+
}
96+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,108 @@
11
package com.dadok.gaerval.domain.bookshelf.service;
22

3+
import static org.assertj.core.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import static org.mockito.BDDMockito.*;
6+
7+
import java.util.Optional;
8+
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.mockito.InjectMocks;
12+
import org.mockito.Mock;
13+
import org.mockito.junit.jupiter.MockitoExtension;
14+
import org.springframework.test.util.ReflectionTestUtils;
15+
16+
import com.dadok.gaerval.domain.bookshelf.entity.Bookshelf;
17+
import com.dadok.gaerval.domain.bookshelf.entity.BookshelfLike;
18+
import com.dadok.gaerval.domain.bookshelf.exception.AlreadyExistsBookshelfLikeException;
19+
import com.dadok.gaerval.domain.bookshelf.repository.BookshelfLikeRepository;
20+
import com.dadok.gaerval.domain.user.entity.User;
21+
import com.dadok.gaerval.domain.user.service.UserService;
22+
import com.dadok.gaerval.global.error.exception.ResourceNotfoundException;
23+
import com.dadok.gaerval.testutil.JobObjectProvider;
24+
import com.dadok.gaerval.testutil.UserObjectProvider;
25+
26+
@ExtendWith(MockitoExtension.class)
327
class DefaultBookshelfLikeServiceTest {
428

29+
@InjectMocks
30+
private DefaultBookshelfLikeService bookshelfLikeService;
31+
32+
@Mock
33+
private BookshelfLikeRepository bookshelfLikeRepository;
34+
35+
@Mock
36+
private BookshelfService bookshelfService;
37+
38+
@Mock
39+
private UserService userService;
40+
41+
private final User user = UserObjectProvider.createKakaoUser(JobObjectProvider.backendJob());
42+
private final Bookshelf bookshelf = Bookshelf.create(UserObjectProvider.createNaverUser());
43+
44+
@Test
45+
void createBookshelfLike_success() {
46+
// Given
47+
ReflectionTestUtils.setField(user, "id", 1L);
48+
given(userService.getById(1L))
49+
.willReturn(user);
50+
given(bookshelfService.getById(2L))
51+
.willReturn(bookshelf);
52+
given(bookshelfLikeRepository.existsByBookshelfIdAndUserId(2L, 1L))
53+
.willReturn(Boolean.FALSE);
54+
55+
// When
56+
assertDoesNotThrow(() -> {
57+
bookshelfLikeService.createBookshelfLike(1L, 2L);
58+
});
59+
60+
// Then
61+
assertThat(bookshelf.getBookshelfLikes().size()).isEqualTo(1);
62+
}
63+
64+
@Test
65+
void createBookshelfLike_alreadyExist_fail() {
66+
// Given
67+
ReflectionTestUtils.setField(user, "id", 1L);
68+
given(userService.getById(1L))
69+
.willReturn(user);
70+
given(bookshelfService.getById(2L))
71+
.willReturn(bookshelf);
72+
given(bookshelfLikeRepository.existsByBookshelfIdAndUserId(2L, 1L))
73+
.willReturn(Boolean.TRUE);
74+
75+
// When // Then
76+
assertThrows(AlreadyExistsBookshelfLikeException.class, () -> {
77+
bookshelfLikeService.createBookshelfLike(1L, 2L);
78+
});
79+
}
80+
81+
@Test
82+
void deleteBookshelfLike_success() {
83+
// Given
84+
ReflectionTestUtils.setField(user, "id", 1L);
85+
BookshelfLike bookshelfLike = BookshelfLike.create(user, bookshelf);
86+
87+
given(bookshelfLikeRepository.findByUserIdAndBookshelfId(1L, 2L))
88+
.willReturn(Optional.of(bookshelfLike));
89+
90+
// When // Then
91+
assertDoesNotThrow(() -> {
92+
bookshelfLikeService.deleteBookshelfLike(1L, 2L);
93+
});
94+
}
95+
96+
@Test
97+
void deleteBookshelfLike_notExist_fail() {
98+
// Given
99+
given(bookshelfLikeRepository.findByUserIdAndBookshelfId(1L, 2L))
100+
.willReturn(Optional.empty());
101+
102+
// When // Then
103+
assertThrows(ResourceNotfoundException.class, () -> {
104+
bookshelfLikeService.deleteBookshelfLike(1L, 2L);
105+
});
106+
}
107+
5108
}

0 commit comments

Comments
ย (0)