Skip to content

Commit

Permalink
test: 테스트 및 문서 보완
Browse files Browse the repository at this point in the history
  • Loading branch information
Choi-JJunho committed Oct 11, 2023
1 parent 327fc63 commit 7d820d7
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 5 deletions.
1 change: 1 addition & 0 deletions backend/pium/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ include::petPlant.adoc[]
include::reminder.adoc[]
include::history.adoc[]
include::garden.adoc[]
include::member.adoc[]
72 changes: 72 additions & 0 deletions backend/pium/src/docs/asciidoc/member.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
== 사용자(Member)

=== 로그인

==== Request

include::{snippets}/auth/login/http-request.adoc[]

==== Response

include::{snippets}/auth/login/http-response.adoc[]

=== 로그아웃

==== Request

include::{snippets}/auth/logout/http-request.adoc[]

==== Response

include::{snippets}/auth/logout/http-response.adoc[]

=== 세션확인

==== Request

include::{snippets}/member/checkSession/http-request.adoc[]

==== Response

include::{snippets}/member/checkSession/http-response.adoc[]

=== 회원탈퇴

==== Request

include::{snippets}/member/withdraw/http-request.adoc[]

==== Response

include::{snippets}/member/withdraw/http-response.adoc[]

=== 알림구독

==== Request

include::{snippets}/member/subscribeNotification/http-request.adoc[]

==== Response

include::{snippets}/member/subscribeNotification/http-response.adoc[]

=== 알림구독해지

==== Request

include::{snippets}/member/unSubscribeNotification/http-request.adoc[]

==== Response

include::{snippets}/member/unSubscribeNotification/http-response.adoc[]

=== 알림 구독상태 확인

==== Request

include::{snippets}/member/checkNotification/http-request.adoc[]

==== Response

include::{snippets}/member/checkNotification/http-response.adoc[]
include::{snippets}/member/checkNotification/response-body.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public ResponseEntity<Void> checkNotificationStatus(@Auth Member member, @Reques
@DeleteMapping("/notification")
public ResponseEntity<Void> delete(@Auth Member member) {
memberService.unSubscribeNotification(member);
return ResponseEntity.ok().build();
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public class Member extends BaseEntity {
private String deviceToken;

@Builder
public Member(Long kakaoId) {
public Member(Long kakaoId, String deviceToken) {
this.kakaoId = kakaoId;
this.deviceToken = deviceToken;
}

public void updateDeviceToken(String deviceToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ public NotificationCheckResponse checkNotification(Member member) {
.build();
}

@Transactional
public void subscribeNotification(Member member, NotificationSubscribeRequest request) {
member.updateDeviceToken(request.getDeviceToken());
}

@Transactional
public void unSubscribeNotification(Member member) {
member.updateDeviceToken(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class 로그인_ {
mockMvc.perform(post("/login")
.queryParam("code", "authorization code")
.contentType(APPLICATION_JSON_VALUE))
.andDo(document("auth/login.html/",
.andDo(document("auth/login/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
queryParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.Mockito.doNothing;
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
Expand All @@ -13,9 +14,11 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.official.pium.UITest;
import com.official.pium.domain.Member;
import com.official.pium.service.MemberService;
import com.official.pium.service.dto.NotificationSubscribeRequest;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Nested;
Expand All @@ -38,6 +41,9 @@ class MemberControllerTest extends UITest {
@MockBean
private MemberService memberService;

@Autowired
private ObjectMapper objectMapper;

@Nested
class 회원_탈퇴_ {

Expand All @@ -48,7 +54,7 @@ class 회원_탈퇴_ {
mockMvc.perform(delete("/members/withdraw")
.session(session)
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andDo(document("auth/withdraw/",
.andDo(document("member/withdraw/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestCookies()
Expand Down Expand Up @@ -80,7 +86,7 @@ class 사용자_세션_ {
mockMvc.perform(get("/members/me")
.session(session)
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andDo(document("auth/checkSession/",
.andDo(document("member/checkSession/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestCookies()
Expand All @@ -102,4 +108,60 @@ class 사용자_세션_ {
.andDo(print());
}
}

@Nested
class 알림_구독_ {

@Test
void 확인_정상_요청__200_반환() throws Exception {
mockMvc.perform(get("/members/notification")
.session(session)
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andDo(document("member/checkNotification/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestCookies()
)
)
.andExpect(status().isOk())
.andDo(print());
}

@Test
void 등록_정상_요청__200_반환() throws Exception {
doNothing().when(memberService).subscribeNotification(any(Member.class), any());

mockMvc.perform(post("/members/notification")
.session(session)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(NotificationSubscribeRequest.builder()
.deviceToken("deviceToken")
.build())))
.andDo(document("member/subscribeNotification/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestCookies()
)
)
.andExpect(status().isOk())
.andDo(print());
}

@Test
void 삭제_정상_요청__204_반환() throws Exception {
doNothing().when(memberService).unSubscribeNotification(any(Member.class));

mockMvc.perform(delete("/members/notification")
.session(session)
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andDo(document("member/unSubscribeNotification/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestCookies()
)
)
.andExpect(status().isNoContent())
.andDo(print());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import com.official.pium.config.DatabaseClearExtension;
import com.official.pium.domain.Member;
import com.official.pium.repository.MemberRepository;
import com.official.pium.repository.PetPlantRepository;
import com.official.pium.service.dto.NotificationCheckResponse;
import com.official.pium.service.dto.NotificationSubscribeRequest;
import com.official.pium.support.PetPlantSupport;
import java.util.Optional;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
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.context.SpringBootTest.WebEnvironment;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
@ExtendWith(DatabaseClearExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
class MemberServiceTest {

Expand Down Expand Up @@ -65,4 +70,56 @@ class MemberServiceTest {
softly.assertThat(petPlantRepository.findById(petPlant3)).isEmpty();
});
}

@Test
void 알림_구독__사용자의_디바이스_토큰에_값이_존재한다() {
Member member = Member.builder()
.kakaoId(123451L)
.build();
Member saveMember = memberRepository.save(member);

memberService.subscribeNotification(saveMember, NotificationSubscribeRequest.builder()
.deviceToken("deviceToken")
.build());

assertThat(saveMember.getDeviceToken()).isEqualTo("deviceToken");
}

@Test
void 알림_해지__사용자의_디바이스_토큰에_값이_존재한다() {
Member member = Member.builder()
.kakaoId(123451L)
.deviceToken("deviceToken")
.build();
Member saveMember = memberRepository.save(member);

memberService.unSubscribeNotification(saveMember);

assertThat(saveMember.getDeviceToken()).isNull();
}

@Test
void 사용자가_알림_구독중이면_True() {
Member member = Member.builder()
.kakaoId(123451L)
.deviceToken("deviceToken")
.build();
Member saveMember = memberRepository.save(member);

NotificationCheckResponse notificationCheckResponse = memberService.checkNotification(saveMember);

assertThat(notificationCheckResponse.isSubscribe()).isTrue();
}

@Test
void 사용자가_알림_구독중이_아니면_False() {
Member member = Member.builder()
.kakaoId(123451L)
.build();
Member saveMember = memberRepository.save(member);

NotificationCheckResponse notificationCheckResponse = memberService.checkNotification(saveMember);

assertThat(notificationCheckResponse.isSubscribe()).isFalse();
}
}

0 comments on commit 7d820d7

Please sign in to comment.