-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb2fdbc
commit 17bd3f8
Showing
7 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/polzzak/domain/pushtoken/controller/PushTokenController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.polzzak.domain.pushtoken.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.polzzak.domain.pushtoken.model.CreatePushToken; | ||
import com.polzzak.domain.pushtoken.service.PushTokenService; | ||
import com.polzzak.global.common.ApiResponse; | ||
import com.polzzak.global.security.LoginId; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/v1/push-token") | ||
public class PushTokenController { | ||
|
||
private final PushTokenService pushTokenService; | ||
|
||
@PostMapping | ||
public ResponseEntity<ApiResponse<Void>> createStampBoard( | ||
@LoginId Long memberId, @RequestBody @Valid CreatePushToken createPushToken | ||
) { | ||
pushTokenService.addToken(memberId, createPushToken.token()); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.created()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/polzzak/domain/pushtoken/model/CreatePushToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.polzzak.domain.pushtoken.model; | ||
|
||
public record CreatePushToken(String token) { | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/polzzak/domain/pushtoken/model/PushToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.polzzak.domain.pushtoken.model; | ||
|
||
import com.polzzak.domain.model.BaseEntity; | ||
import com.polzzak.domain.user.entity.Member; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor | ||
@Table(name = "push_token") | ||
public class PushToken extends BaseEntity { | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@Column(nullable = false) | ||
private String token; | ||
|
||
@Builder(builderMethodName = "createPushToken") | ||
public PushToken(Member member, String token) { | ||
this.member = member; | ||
this.token = token; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/polzzak/domain/pushtoken/repository/PushTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.polzzak.domain.pushtoken.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.polzzak.domain.pushtoken.model.PushToken; | ||
|
||
public interface PushTokenRepository extends JpaRepository<PushToken, Long> { | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/polzzak/domain/pushtoken/service/PushTokenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.polzzak.domain.pushtoken.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.polzzak.domain.pushtoken.model.PushToken; | ||
import com.polzzak.domain.pushtoken.repository.PushTokenRepository; | ||
import com.polzzak.domain.user.entity.Member; | ||
import com.polzzak.domain.user.service.UserService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PushTokenService { | ||
|
||
private final UserService userService; | ||
private final PushTokenRepository pushTokenRepository; | ||
|
||
public void addToken(Long memberId, String token) { | ||
Member member = userService.findMemberByMemberId(memberId); | ||
|
||
PushToken pushToken = PushToken.createPushToken() | ||
.member(member) | ||
.token(token) | ||
.build(); | ||
|
||
pushTokenRepository.save(pushToken); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/com/polzzak/domain/pushtoken/controller/PushTokenControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.polzzak.domain.pushtoken.controller; | ||
|
||
import static com.polzzak.support.TokenFixtures.*; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.restdocs.headers.HeaderDocumentation.*; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*; | ||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
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.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
|
||
import com.polzzak.domain.pushtoken.service.PushTokenService; | ||
import com.polzzak.domain.user.service.UserService; | ||
import com.polzzak.support.PushTokenFixtures; | ||
import com.polzzak.support.test.ControllerTestHelper; | ||
|
||
@WebMvcTest(PushTokenController.class) | ||
class PushTokenControllerTest extends ControllerTestHelper { | ||
|
||
@MockBean | ||
private PushTokenService pushTokenService; | ||
@MockBean | ||
private UserService userService; | ||
|
||
private static final String BASE_URL = "/api/v1/push-token"; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
when(userService.findMemberByMemberId(anyLong())).thenReturn(PushTokenFixtures.MEMBER); | ||
} | ||
|
||
@Test | ||
@DisplayName("푸시 토큰 생성 테스트") | ||
void createStampBoardTest() throws Exception { | ||
doNothing().when(pushTokenService).addToken(anyLong(), anyString()); | ||
|
||
mockMvc.perform( | ||
post(BASE_URL) | ||
.header(HttpHeaders.AUTHORIZATION, TOKEN_TYPE + USER_ACCESS_TOKEN) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectToString(PushTokenFixtures.CREATE_PUSH_TOKEN))) | ||
.andDo(print()) | ||
.andExpect(status().isCreated()) | ||
.andDo(document("push/push_token-create-success", | ||
requestHeaders( | ||
headerWithName(HttpHeaders.AUTHORIZATION).description("엑세스 토큰") | ||
), | ||
requestFields( | ||
fieldWithPath("token").description("푸시 토큰") | ||
), | ||
responseFields( | ||
fieldWithPath("code").description("응답 코드"), | ||
fieldWithPath("messages").description("응답 메시지").optional(), | ||
fieldWithPath("data").description("응답 데이터").optional() | ||
))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.polzzak.support; | ||
|
||
import com.polzzak.domain.membertype.entity.MemberType; | ||
import com.polzzak.domain.membertype.entity.MemberTypeDetail; | ||
import com.polzzak.domain.pushtoken.model.CreatePushToken; | ||
import com.polzzak.domain.user.entity.Member; | ||
|
||
public class PushTokenFixtures { | ||
|
||
public static final Member MEMBER = new Member("NICK", "profileKEY", new MemberTypeDetail(MemberType.KID, "kid")); | ||
public static final CreatePushToken CREATE_PUSH_TOKEN = new CreatePushToken("token"); | ||
} |