diff --git a/Briefing-Api/src/main/java/com/example/briefingapi/member/business/MemberConverter.java b/Briefing-Api/src/main/java/com/example/briefingapi/member/business/MemberConverter.java index 3eca6d3..8e72224 100644 --- a/Briefing-Api/src/main/java/com/example/briefingapi/member/business/MemberConverter.java +++ b/Briefing-Api/src/main/java/com/example/briefingapi/member/business/MemberConverter.java @@ -37,12 +37,13 @@ public static Member toMember(GoogleUserInfo googleUserInfo) { .build(); } - public static Member toMember(String appleSocialId) { + public static Member toMember(String appleSocialId, String nickName) { return Member.builder() // .profileImgUrl(googleUserInfo.getPicture()) // .nickName(googleUserInfo.getName()) .socialId(appleSocialId) .socialType(SocialType.APPLE) + .nickName(nickName) .role(MemberRole.ROLE_USER) .status(MemberStatus.ACTIVE) .build(); diff --git a/Briefing-Api/src/main/java/com/example/briefingapi/member/implement/MemberCommandService.java b/Briefing-Api/src/main/java/com/example/briefingapi/member/implement/MemberCommandService.java index d615540..82b7ab0 100644 --- a/Briefing-Api/src/main/java/com/example/briefingapi/member/implement/MemberCommandService.java +++ b/Briefing-Api/src/main/java/com/example/briefingapi/member/implement/MemberCommandService.java @@ -5,6 +5,7 @@ import java.security.PublicKey; import java.security.spec.RSAPublicKeySpec; import java.util.Base64; +import java.util.List; import java.util.Optional; import com.example.briefingapi.member.business.MemberConverter; @@ -16,10 +17,11 @@ import com.example.briefingcommon.entity.FcmToken; import com.example.briefingcommon.entity.Member; import com.example.briefingcommon.entity.enums.SocialType; +import com.example.briefinginfra.feign.nickname.hwanmoo.adapter.NickNameGenerator; import com.example.briefinginfra.feign.oauth.apple.client.AppleOauth2Client; import com.example.briefinginfra.feign.oauth.apple.dto.ApplePublicKey; import com.example.briefinginfra.feign.oauth.apple.dto.ApplePublicKeyList; -import com.example.briefinginfra.feign.oauth.google.client.GoogleOauth2Client; + import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -27,7 +29,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; - import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import lombok.RequiredArgsConstructor; @@ -38,6 +39,7 @@ public class MemberCommandService { private final MemberRepository memberRepository; private final AppleOauth2Client appleOauth2Client; + private final NickNameGenerator nickNameGenerator; private final FcmTokenRepository fcmTokenRepository; @@ -87,8 +89,10 @@ private Member loginWithApple(MemberRequest.LoginDTO request) { Optional foundMember = memberRepository.findBySocialIdAndSocialType(appleSocialId, SocialType.APPLE); + String nickName = nickNameGenerator.getOneRandomNickName(); + return foundMember.isEmpty() - ? memberRepository.save(MemberConverter.toMember(appleSocialId)) + ? memberRepository.save(MemberConverter.toMember(appleSocialId, nickName)) : foundMember.get(); } diff --git a/core/Briefing-Common/src/main/java/com/example/briefingcommon/common/constant/BriefingStatic.java b/core/Briefing-Common/src/main/java/com/example/briefingcommon/common/constant/BriefingStatic.java new file mode 100644 index 0000000..246158a --- /dev/null +++ b/core/Briefing-Common/src/main/java/com/example/briefingcommon/common/constant/BriefingStatic.java @@ -0,0 +1,9 @@ +package com.example.briefingcommon.common.constant; + +public class BriefingStatic { + public static final String NICK_NAME_FORMAT = "json"; + public static final int NICK_NAME_COUNT = 1; + public static final int NICK_NAME_MAX_LENGTH = 8; + + public static final String DEFAULT_NICK_NAME = "하품하는 프로도"; +} diff --git a/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/adapter/NickNameGenerator.java b/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/adapter/NickNameGenerator.java new file mode 100644 index 0000000..37410ca --- /dev/null +++ b/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/adapter/NickNameGenerator.java @@ -0,0 +1,51 @@ +package com.example.briefinginfra.feign.nickname.hwanmoo.adapter; + +import com.example.briefinginfra.feign.nickname.hwanmoo.client.NickNameClient; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; + +import static com.example.briefingcommon.common.constant.BriefingStatic.*; +import static com.example.briefingcommon.common.constant.BriefingStatic.NICK_NAME_FORMAT; + +@Component +@RequiredArgsConstructor +public class NickNameGenerator { + private final NickNameClient nickNameClient; + + /** + * 기본 최대 길이를 사용하여 랜덤 닉네임을 생성하고 반환합니다. + * 이 메소드는 내부적으로 {@code getOneRandomNickNameWithDetails}를 호출합니다. + * + * @return 생성된 닉네임. 닉네임 생성에 실패한 경우 빈 문자열을 반환합니다. + */ + public String getOneRandomNickName() { + return getOneRandomNickNameWithDetails(NICK_NAME_MAX_LENGTH); + } + + /** + * 사용자가 지정한 최대 길이를 사용하여 랜덤 닉네임을 생성하고 반환합니다. + * 이 메소드는 내부적으로 {@code getOneRandomNickNameWithDetails}를 호출합니다. + * + * @param maxLength 생성할 닉네임의 최대 길이 + * @return 생성된 닉네임. 닉네임 생성에 실패한 경우 빈 문자열을 반환합니다. + */ + public String getOneRandomNickName(int maxLength) { + return getOneRandomNickNameWithDetails(maxLength); + } + + /** + * 지정된 최대 길이를 사용하여 랜덤 닉네임을 생성하고, 생성된 목록에서 첫 번째 닉네임을 반환합니다. + * 이 메소드는 내부적으로 {@code NickNameClient}를 사용하여 닉네임을 요청합니다. + * + * @param maxLength 생성할 닉네임의 최대 길이 + * @return 생성된 닉네임 중 첫 번째 닉네임. 닉네임 생성에 실패한 경우 빈 문자열을 반환합니다. + */ + private String getOneRandomNickNameWithDetails(int maxLength) { + List nickNameWords = nickNameClient.getNickName(NICK_NAME_FORMAT, NICK_NAME_COUNT, maxLength).getWords(); + if (!nickNameWords.isEmpty()) return nickNameWords.get(0); + return DEFAULT_NICK_NAME; + } +} + diff --git a/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/client/NickNameClient.java b/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/client/NickNameClient.java new file mode 100644 index 0000000..4120542 --- /dev/null +++ b/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/client/NickNameClient.java @@ -0,0 +1,17 @@ +package com.example.briefinginfra.feign.nickname.hwanmoo.client; + +import com.example.briefinginfra.feign.nickname.hwanmoo.dto.NickNameRes; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient( + name = "nickNameClient", + url = "https://nickname.hwanmoo.kr" +) +@Component +public interface NickNameClient { + @GetMapping(value = "/") + NickNameRes getNickName(@RequestParam(defaultValue = "json") String format, @RequestParam(defaultValue = "1") int count, @RequestParam(defaultValue = "8") int max_length); +} diff --git a/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/dto/NickNameRes.java b/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/dto/NickNameRes.java new file mode 100644 index 0000000..d0c04fa --- /dev/null +++ b/core/Briefing-Infra/src/main/java/com/example/briefinginfra/feign/nickname/hwanmoo/dto/NickNameRes.java @@ -0,0 +1,14 @@ +package com.example.briefinginfra.feign.nickname.hwanmoo.dto; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class NickNameRes { + private List words = new ArrayList<>(); + private String seed; +} diff --git a/core/Briefing-Infra/src/test/java/com/example/briefinginfra/config/TestConfig.java b/core/Briefing-Infra/src/test/java/com/example/briefinginfra/config/TestConfig.java new file mode 100644 index 0000000..2b4416f --- /dev/null +++ b/core/Briefing-Infra/src/test/java/com/example/briefinginfra/config/TestConfig.java @@ -0,0 +1,20 @@ +package com.example.briefinginfra.config; + +import com.example.briefinginfra.feign.nickname.hwanmoo.adapter.NickNameGenerator; +import com.example.briefinginfra.feign.nickname.hwanmoo.client.NickNameClient; +import org.mockito.Mockito; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +@TestConfiguration +public class TestConfig { + @Bean + public NickNameClient nickNameClient() { + return Mockito.mock(NickNameClient.class); + } + + @Bean + public NickNameGenerator nickNameGenerator(NickNameClient nickNameClient) { + return new NickNameGenerator(nickNameClient); + } +} diff --git a/core/Briefing-Infra/src/test/java/com/example/briefinginfra/feign/nickname/hwanmoo/adapter/NickNameGeneratorTest.java b/core/Briefing-Infra/src/test/java/com/example/briefinginfra/feign/nickname/hwanmoo/adapter/NickNameGeneratorTest.java new file mode 100644 index 0000000..5da84dc --- /dev/null +++ b/core/Briefing-Infra/src/test/java/com/example/briefinginfra/feign/nickname/hwanmoo/adapter/NickNameGeneratorTest.java @@ -0,0 +1,63 @@ +package com.example.briefinginfra.feign.nickname.hwanmoo.adapter; + +import com.example.briefingcommon.common.constant.BriefingStatic; +import com.example.briefinginfra.config.TestConfig; +import com.example.briefinginfra.feign.nickname.hwanmoo.client.NickNameClient; +import com.example.briefinginfra.feign.nickname.hwanmoo.dto.NickNameRes; +import org.junit.jupiter.api.DisplayName; +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.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.ArrayList; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = TestConfig.class) // 테스트 설정 클래스 지정 +class NickNameGeneratorTest { + @Autowired + private NickNameGenerator nickNameGenerator; + + @MockBean + private NickNameClient nickNameClient; + + @Test + @DisplayName("[NickNameGenerator] 랜덤 닉네임 생성") + void 랜덤_닉네임_생성() throws Exception { + // given + NickNameRes mockResponse = new NickNameRes(); + mockResponse.setWords(Arrays.asList("하품하는 프로도")); + when(nickNameClient.getNickName("json", 1, 8)).thenReturn(mockResponse); + + // when + String nickName = nickNameGenerator.getOneRandomNickName(); + + // then + assertThat(nickName).isNotNull(); + assertThat(nickName).isEqualTo("하품하는 프로도"); + } + + @Test + @DisplayName("[NickNameGenerator] 랜덤 닉네임 생성 - 빈 리스트 (기본 닉네임)") + void 랜덤_닉네임_생성_기본_닉네임() throws Exception { + // given + NickNameRes mockResponse = new NickNameRes(); + when(nickNameClient.getNickName("json", 1, 8)).thenReturn(mockResponse); + + // when + String nickName = nickNameGenerator.getOneRandomNickName(); + + // then + assertThat(nickName).isNotNull(); + assertThat(nickName).isEqualTo(BriefingStatic.DEFAULT_NICK_NAME); + } + +} \ No newline at end of file