Skip to content

Commit

Permalink
merge : Notification API 추가 #663
Browse files Browse the repository at this point in the history
Feat/#663 notification api 추가
  • Loading branch information
java-saeng authored Oct 5, 2023
2 parents 6f0073b + ec46e9c commit 325bcb3
Show file tree
Hide file tree
Showing 26 changed files with 1,188 additions and 265 deletions.
154 changes: 38 additions & 116 deletions backend/emm-sale/src/documentTest/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -403,122 +403,6 @@ include::{snippets}/create-fcmToken/request-fields.adoc[]
.HTTP request
include::{snippets}/create-fcmToken/http-request.adoc[]

== RequestNotification(함께 가기 알림)

=== `POST` : 함께 가기 알림 저장

.HTTP request 설명
include::{snippets}/create-request-notification/request-fields.adoc[]

.HTTP request
include::{snippets}/create-request-notification/http-request.adoc[]

.HTTP response 설명
include::{snippets}/create-request-notification/response-fields.adoc[]

.HTTP response
include::{snippets}/create-request-notification/http-response.adoc[]

==== `POST` : 차단된 사용자에게 알림 전송

.HTTP request 설명
include::{snippets}/create-blocked-member-requestNotification/request-fields.adoc[]

.HTTP request
include::{snippets}/create-blocked-member-requestNotification/http-request.adoc[]

.HTTP response
include::{snippets}/create-blocked-member-requestNotification/http-response.adoc[]

=== `PATCH` : 함께 가기 알림 상태 변경

.HTTP request 설명
include::{snippets}/modify-request-notification/request-fields.adoc[]

.HTTP request
include::{snippets}/modify-request-notification/http-request.adoc[]

include::{snippets}/modify-request-notification/path-parameters.adoc[]

=== `PATCH` : 함께 가기 알림 읽음 상태 변경

.HTTP request
include::{snippets}/read-request-notification/http-request.adoc[]

include::{snippets}/read-request-notification/path-parameters.adoc[]

=== `DELETE` : 함께 가기 알림 삭제

.HTTP request
include::{snippets}/delete-request-notification/http-request.adoc[]

include::{snippets}/delete-request-notification/path-parameters.adoc[]

=== `GET` : request notification id로 함께 가기 알림 조회

.HTTP request
include::{snippets}/find-request-notification/http-request.adoc[]

include::{snippets}/find-request-notification/path-parameters.adoc[]

.HTTP response 설명
include::{snippets}/find-request-notification/response-fields.adoc[]

.HTTP response
include::{snippets}/find-request-notification/http-response.adoc[]

=== `GET` : 사용자가 받은 모든 함께 가기 알림 목록 조회

.HTTP request
include::{snippets}/find-all-request-notification/http-request.adoc[]

.HTTP response 설명
include::{snippets}/find-all-request-notification/response-fields.adoc[]

.HTTP response
include::{snippets}/find-all-request-notification/http-response.adoc[]

=== `GET` : 사용자가 특정 행사에서 다른 사용자에게 함께 가기 알림을 보냈는지 확인

.HTTP request
include::{snippets}/already-existed-request-notification/http-request.adoc[]
include::{snippets}/already-existed-request-notification/request-parameters.adoc[]

.HTTP response 설명
include::{snippets}/already-existed-request-notification/http-response.adoc[]

== Update Notification(댓글 & 행사 알림)

=== `GET` : 댓글 & 행사 알림들 조회

.HTTP request 설명
include::{snippets}/get-update-notifications/request-parameters.adoc[]

include::{snippets}/get-update-notifications/http-request.adoc[]

.HTTP response
include::{snippets}/get-update-notifications/http-response.adoc[]

=== `PUT` : 댓글 & 행사 알림 읽음 상태 변경

.HTTP request 설명
include::{snippets}/put-update-notifications-read/path-parameters.adoc[]

include::{snippets}/put-update-notifications-read/http-request.adoc[]

.HTTP response
include::{snippets}/put-update-notifications-read/http-response.adoc[]

=== `DELETE` : 댓글 & 행사 알림 삭제 (단건 & 다건 가능)

.HTTP request 설명
include::{snippets}/delete-update-notifications/request-body.adoc[]

include::{snippets}/delete-update-notifications/http-request.adoc[]

.HTTP response
include::{snippets}/delete-update-notifications/http-response.adoc[]

== Block

=== `POST` : 사용자 차단 등록
Expand Down Expand Up @@ -772,3 +656,41 @@ include::{snippets}/delete-feed/http-request.adoc[]

.HTTP response
include::{snippets}/delete-feed/http-response.adoc[]

== 알림

=== `GET` : 알림 목록 조회

.HTTP request
include::{snippets}/get-all-notifications/http-request.adoc[]

.HTTP request 설명
include::{snippets}/get-all-notifications/request-parameters.adoc[]

.HTTP response
include::{snippets}/get-all-notifications/http-response.adoc[]

.HTTP response 설명
include::{snippets}/get-all-notifications/response-fields.adoc[]

=== `PATCH` : 알림 읽음 상태 변경

.HTTP request
include::{snippets}/patch-notification-read/http-request.adoc[]

.HTTP request 설명
include::{snippets}/patch-notification-read/path-parameters.adoc[]

.HTTP response
include::{snippets}/patch-notification-read/http-response.adoc[]

=== `DELETE` : 알림 삭제

.HTTP request
include::{snippets}/delete-notifications/http-request.adoc[]

.HTTP request 설명
include::{snippets}/delete-notifications/request-fields.adoc[]

.HTTP response
include::{snippets}/delete-notifications/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.emmsale.message_room.application.MessageCommandService;
import com.emmsale.message_room.application.RoomQueryService;
import com.emmsale.notification.application.FcmTokenRegisterService;
import com.emmsale.notification.application.NotificationCommandService;
import com.emmsale.notification.application.NotificationQueryService;
import com.emmsale.notification.application.RequestNotificationCommandService;
import com.emmsale.notification.application.RequestNotificationQueryService;
import com.emmsale.notification.application.UpdateNotificationCommandService;
Expand Down Expand Up @@ -114,6 +116,10 @@ abstract class MockMvcTestHelper {
protected FeedQueryService feedQueryService;
@MockBean
private MemberArgumentResolver memberArgumentResolver;
@MockBean
protected NotificationQueryService notificationQueryService;
@MockBean
protected NotificationCommandService notificationCommandService;

@BeforeEach
void setUp(final WebApplicationContext applicationContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.emmsale;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.emmsale.notification.api.NotificationApi;
import com.emmsale.notification.application.dto.NotificationDetailResponse;
import com.emmsale.notification.application.dto.NotificationDeleteRequest;
import com.emmsale.notification.domain.NotificationType;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.RequestFieldsSnippet;
import org.springframework.restdocs.payload.ResponseFieldsSnippet;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.restdocs.request.RequestParametersSnippet;

@WebMvcTest(NotificationApi.class)
class NotificationApiTest extends MockMvcTestHelper {

private String commentJsonData1, commentJsonData2, eventJsonData1;

@BeforeEach
void setUp() {
commentJsonData1 = "{"
+ "\"content\":\"content\","
+ "\"writer\":\"writer\","
+ "\"writerImageUrl\":\"imageUrl\""
+ "}";

eventJsonData1 = "{"
+ "\"title\":\"title\""
+ "}";

commentJsonData2 = "{"
+ "\"content\":\"content\","
+ "\"writer\":\"writer\","
+ "\"writerImageUrl\":\"imageUrl\""
+ "}";
}

@Test
@DisplayName("find() : 현재 로그인 한 사용자가 받은 알림들을 성공적으로 조회한다면 200 OK를 반환할 수 있다.")
void test_find() throws Exception {
//given
final String accessToken = "Bearer Token";

final RequestParametersSnippet requestParam = requestParameters(
parameterWithName("member-id").description("알림을 조회할 멤버 ID"));

final ResponseFieldsSnippet responseFields = responseFields(
fieldWithPath("[].notificationId").description("알림 ID"),
fieldWithPath("[].type").description("알림 종류"),
fieldWithPath("[].notificationInformation").description("알림 정보(JSON 형태)"),
fieldWithPath("[].isRead").description("사용자가 알림을 읽었는지 유무"),
fieldWithPath("[].redirectId").description("알림을 생성한 곳을 리다이렉트 하기 위한 ID"),
fieldWithPath("[].receiverId").description("알림 받는 사람 ID"),
fieldWithPath("[].createdAt").description("알림 생성 시간")
);

final List<NotificationDetailResponse> responses = List.of(
new NotificationDetailResponse(
1L,
NotificationType.COMMENT,
commentJsonData1,
false,
1L,
225L,
LocalDateTime.now()
),
new NotificationDetailResponse(
1L,
NotificationType.EVENT,
eventJsonData1,
false,
2L,
225L,
LocalDateTime.now()
)
);

//when
when(notificationQueryService.findAllByMemberId(any(), anyLong()))
.thenReturn(responses);

//then
mockMvc.perform(get("/notifications")
.queryParam("member-id", "1")
.header("Authorization", accessToken))
.andExpect(status().isOk())
.andDo(print())
.andDo(document("get-all-notifications", requestParam, responseFields));
}

@Test
@DisplayName("read() : 알림 읽음 상태를 성공적으로 변경시켰다면 204 No Content를 반환할 수 있다.")
void test_read() throws Exception {
final PathParametersSnippet pathParams = pathParameters(
parameterWithName("notifications-id").description("읽음 상태 변경 시킬 알림 ID")
);

final String accessToken = "Bearer Token";

//when & then
mockMvc.perform(patch("/notifications/{notifications-id}/read", 1L)
.header("Authorization", accessToken))
.andExpect(status().isNoContent())
.andDo(print())
.andDo(document("patch-notification-read", pathParams));
}

@Test
@DisplayName("deleteBatch() : 알림을 성공적으로 삭제했다면 204 No Content 를 반환할 수 있다.")
void test_deleteBatch() throws Exception {
//given
final RequestFieldsSnippet requestFields = requestFields(
fieldWithPath("deleteIds").description("삭제할 알림 ID들")
);

final NotificationDeleteRequest request =
new NotificationDeleteRequest(List.of(1L, 2L, 3L));

final String accessToken = "Bearer Token";

//when & then
mockMvc.perform(delete("/notifications")
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", accessToken))
.andExpect(status().isNoContent())
.andDo(print())
.andDo(document("delete-notifications", requestFields));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public class CommentNotificationEvent {
public class CommentNotificationEvent extends NotificationEvent {

private static final String UPDATE_NOTIFICATION_COMMENT_TYPE = "COMMENT";

private final Long receiverId;
private final Long redirectId;
private final LocalDateTime createdAt;
private final String notificationType;
private final String content;
private final String writer;
private final String writerImageUrl;

public CommentNotificationEvent(
final Long receiverId, final Long redirectId,
final LocalDateTime createdAt, final String notificationType,
final String content, final String writer,
final String writerImageUrl
) {
super(receiverId, redirectId, createdAt, notificationType);
this.content = content;
this.writer = writer;
this.writerImageUrl = writerImageUrl;
}

public static CommentNotificationEvent of(final Comment comment, final Comment trigger) {
final Member member = comment.getMember();
final Member triggerMember = trigger.getMember();
Expand Down
Loading

0 comments on commit 325bcb3

Please sign in to comment.