chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
e396568cc3
commit
5ee51d1913
2918 files changed
+302373
-3
No files matched your search
+61
@@ -0,0 +1,61 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.model.AppBanner;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppBannerServiceTest {
|
||||
|
||||
@Mock
|
||||
private RestTemplateCustomUtil restTemplateUtil;
|
||||
|
||||
private AppBannerService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new AppBannerService(restTemplateUtil);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findActiveReturnsBannerList() {
|
||||
AppBanner banner = new AppBanner(1L, "Grand sale", "/store", "PROMO", 0);
|
||||
when(restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_APP_BANNER_FIND_ACTIVE))
|
||||
.thenReturn("http://system/appBanner/findActive");
|
||||
when(restTemplateUtil.<List<AppBanner>>getForEntity(eq("http://system/appBanner/findActive"), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(ResponseEntity.ok(List.of(banner)));
|
||||
|
||||
List<AppBanner> result = service.findActive();
|
||||
|
||||
assertThat(result).extracting(AppBanner::getMessage).containsExactly("Grand sale");
|
||||
assertThat(result).extracting(AppBanner::getStyle).containsExactly("PROMO");
|
||||
}
|
||||
|
||||
@Test
|
||||
void findActiveReturnsEmptyOnNonOkResponse() {
|
||||
when(restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_APP_BANNER_FIND_ACTIVE))
|
||||
.thenReturn("http://system/appBanner/findActive");
|
||||
when(restTemplateUtil.<List<AppBanner>>getForEntity(any(String.class), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
|
||||
List<AppBanner> result = service.findActive();
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.model.AppPromo;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppPromoServiceTest {
|
||||
|
||||
@Mock
|
||||
private RestTemplateCustomUtil restTemplateUtil;
|
||||
|
||||
private AppPromoService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new AppPromoService(restTemplateUtil);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findActiveReturnsPromoList() {
|
||||
AppPromo promo = new AppPromo(1L, "IMAGE", "https://cdn/app-promo/a.jpg", "/store", "Grand sale",
|
||||
"Grand sale banner", LocalDateTime.now().minusDays(1), LocalDateTime.now().plusDays(1), true, 0);
|
||||
when(restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_APP_PROMO_FIND_ACTIVE))
|
||||
.thenReturn("http://system/appPromo/findActive");
|
||||
when(restTemplateUtil.<List<AppPromo>>getForEntity(eq("http://system/appPromo/findActive"), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(ResponseEntity.ok(List.of(promo)));
|
||||
|
||||
List<AppPromo> result = service.findActive();
|
||||
|
||||
assertThat(result).extracting(AppPromo::getMediaUrl).containsExactly("https://cdn/app-promo/a.jpg");
|
||||
assertThat(result).extracting(AppPromo::getCaption).containsExactly("Grand sale");
|
||||
}
|
||||
|
||||
@Test
|
||||
void findActiveReturnsEmptyOnNonOkResponse() {
|
||||
when(restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_APP_PROMO_FIND_ACTIVE))
|
||||
.thenReturn("http://system/appPromo/findActive");
|
||||
when(restTemplateUtil.<List<AppPromo>>getForEntity(any(String.class), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
|
||||
List<AppPromo> result = service.findActive();
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void findActiveReturnsEmptyOnNullBody() {
|
||||
when(restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_APP_PROMO_FIND_ACTIVE))
|
||||
.thenReturn("http://system/appPromo/findActive");
|
||||
when(restTemplateUtil.<List<AppPromo>>getForEntity(any(String.class), any(ParameterizedTypeReference.class)))
|
||||
.thenReturn(ResponseEntity.ok(null));
|
||||
|
||||
List<AppPromo> result = service.findActive();
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import com.solvway.mobile.api.util.redis.RedisUtil;
|
||||
import com.solvway.mobile.api.util.redis.TokenCacheKeyUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AuthenticationServiceCacheKeyTest {
|
||||
|
||||
private static final String TOKEN = "eyJhbGciOiJIUzUxMiJ9.payload.signature";
|
||||
|
||||
@Mock private MemberService memberService;
|
||||
@Mock private RestTemplateCustomUtil restTemplateUtil;
|
||||
@Mock private RedisUtil<String> redisUtil;
|
||||
|
||||
@InjectMocks private AuthenticationService authenticationService;
|
||||
|
||||
@Test
|
||||
void usernameLookupUsesHashedTokenAsCacheKey() {
|
||||
String expectedKey = "mobile-api:auth:token-username:" + TokenCacheKeyUtil.hashToken(TOKEN);
|
||||
when(redisUtil.checkKeyExist(expectedKey)).thenReturn(true);
|
||||
when(redisUtil.getValue(expectedKey)).thenReturn("alice");
|
||||
|
||||
assertThat(authenticationService.getUsernameFromToken(TOKEN)).isEqualTo("alice");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cacheKeyNeverContainsTheRawToken() {
|
||||
String key = "mobile-api:auth:token-username:" + TokenCacheKeyUtil.hashToken(TOKEN);
|
||||
assertThat(key).doesNotContain(TOKEN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void memberCodeLookupUsesHashedTokenAsCacheKey() {
|
||||
String expectedKey = "mobile-api:auth:token-member-code:" + TokenCacheKeyUtil.hashToken(TOKEN);
|
||||
when(redisUtil.checkKeyExist(expectedKey)).thenReturn(true);
|
||||
when(redisUtil.getValue(expectedKey)).thenReturn("M001");
|
||||
|
||||
assertThat(authenticationService.getMemberCodeFromToken(TOKEN)).isEqualTo("M001");
|
||||
}
|
||||
|
||||
@Test
|
||||
void memberUuidLookupUsesHashedTokenAsCacheKey() {
|
||||
String expectedKey = "mobile-api:auth:token-member-uuid:" + TokenCacheKeyUtil.hashToken(TOKEN);
|
||||
when(redisUtil.checkKeyExist(expectedKey)).thenReturn(true);
|
||||
when(redisUtil.getValue(expectedKey)).thenReturn("uuid-1");
|
||||
|
||||
assertThat(authenticationService.getMemberUuidFromToken(TOKEN)).isEqualTo("uuid-1");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.google.cloud.spring.pubsub.core.PubSubTemplate;
|
||||
import com.solvway.core.model.Email;
|
||||
import com.solvway.core.utils.PubSubUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EmailServiceTest {
|
||||
|
||||
@Mock
|
||||
private PubSubTemplate pubSubTemplate;
|
||||
|
||||
@Mock
|
||||
private PubSubUtil pubSubUtil;
|
||||
|
||||
@InjectMocks
|
||||
private EmailService emailService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ReflectionTestUtils.setField(emailService, "emailSaveRequestTopic", "email-save-request-topic");
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_publishesEmailToSaveRequestTopicAndReturnsEmail() {
|
||||
Email email = new Email();
|
||||
email.setSubject("Test Subject");
|
||||
|
||||
Optional<Email> result = emailService.send(email);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
verify(pubSubUtil).publishMessageWithCustomObjectMapper(
|
||||
eq(pubSubTemplate), eq("email-save-request-topic"), eq(email));
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_returnsEmptyWhenPublishThrows() {
|
||||
Email email = new Email();
|
||||
doThrow(new RuntimeException("publish failed"))
|
||||
.when(pubSubUtil).publishMessageWithCustomObjectMapper(any(), any(), any());
|
||||
|
||||
Optional<Email> result = emailService.send(email);
|
||||
|
||||
assertFalse(result.isPresent());
|
||||
}
|
||||
}
|
||||
+370
@@ -0,0 +1,370 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.core.model.GenericHierarchy;
|
||||
import com.solvway.core.model.GenericPostContent;
|
||||
import com.solvway.core.model.HelpCenterPost;
|
||||
import com.solvway.mobile.api.cache.MessageLabelCache;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.dto.HelpCenterContentResponse;
|
||||
import com.solvway.mobile.api.dto.HelpCenterNode;
|
||||
import com.solvway.mobile.api.dto.HelpCenterTreeResponse;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class HelpCenterServiceTest {
|
||||
|
||||
@Mock
|
||||
RestTemplateCustomUtil restTemplateUtil;
|
||||
|
||||
@Mock
|
||||
MessageLabelCache messageLabelCache;
|
||||
|
||||
@InjectMocks
|
||||
HelpCenterService helpCenterService;
|
||||
|
||||
private static final BigInteger UUID = BigInteger.valueOf(42);
|
||||
private static final String LOCALE = "en";
|
||||
|
||||
@Test
|
||||
void getLeafContent_returnsContent_whenLocalizedPostContentExists() {
|
||||
when(restTemplateUtil.getURLforApiService(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
HelpCenterPost post = new HelpCenterPost();
|
||||
post.setGenericPostUuid(BigInteger.valueOf(99));
|
||||
post.setRedirectUrl(null);
|
||||
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_HELP_CENTER_POST_FIND_BY_HIERARCHY_UUID),
|
||||
eq(HelpCenterPost.class), eq(UUID)))
|
||||
.thenReturn(ResponseEntity.ok(post));
|
||||
|
||||
GenericPostContent content = new GenericPostContent();
|
||||
content.setContent("Some help content");
|
||||
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_GENERIC_POST_CONTENT_FIND_BY_UUID_AND_LOCALE),
|
||||
eq(GenericPostContent.class), eq(BigInteger.valueOf(99)), eq(LOCALE)))
|
||||
.thenReturn(ResponseEntity.ok(content));
|
||||
|
||||
Optional<HelpCenterContentResponse> result = helpCenterService.getLeafContent(UUID, LOCALE);
|
||||
|
||||
assertThat(result).isPresent();
|
||||
assertThat(result.get().content()).isEqualTo("Some help content");
|
||||
assertThat(result.get().redirectUrl()).isNull();
|
||||
assertThat(result.get().uuid()).isEqualTo(UUID);
|
||||
assertThat(result.get().locale()).isEqualTo(LOCALE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLeafContent_returnsRedirectUrl_whenPostHasRedirect() {
|
||||
when(restTemplateUtil.getURLforApiService(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
HelpCenterPost post = new HelpCenterPost();
|
||||
post.setRedirectUrl("https://example.com/redirect");
|
||||
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_HELP_CENTER_POST_FIND_BY_HIERARCHY_UUID),
|
||||
eq(HelpCenterPost.class), eq(UUID)))
|
||||
.thenReturn(ResponseEntity.ok(post));
|
||||
|
||||
Optional<HelpCenterContentResponse> result = helpCenterService.getLeafContent(UUID, LOCALE);
|
||||
|
||||
assertThat(result).isPresent();
|
||||
assertThat(result.get().redirectUrl()).isEqualTo("https://example.com/redirect");
|
||||
assertThat(result.get().content()).isNull();
|
||||
|
||||
verify(restTemplateUtil, never()).getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_GENERIC_POST_CONTENT_FIND_BY_UUID_AND_LOCALE),
|
||||
eq(GenericPostContent.class), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLeafContent_returnsEmpty_whenPostNotFound() {
|
||||
when(restTemplateUtil.getURLforApiService(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_HELP_CENTER_POST_FIND_BY_HIERARCHY_UUID),
|
||||
eq(HelpCenterPost.class), eq(UUID)))
|
||||
.thenReturn(ResponseEntity.ok().build());
|
||||
|
||||
Optional<HelpCenterContentResponse> result = helpCenterService.getLeafContent(UUID, LOCALE);
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLeafContent_returnsEmpty_whenPostNotOk() {
|
||||
when(restTemplateUtil.getURLforApiService(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_HELP_CENTER_POST_FIND_BY_HIERARCHY_UUID),
|
||||
eq(HelpCenterPost.class), eq(UUID)))
|
||||
.thenReturn(ResponseEntity.status(HttpStatus.NOT_FOUND).build());
|
||||
|
||||
Optional<HelpCenterContentResponse> result = helpCenterService.getLeafContent(UUID, LOCALE);
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLeafContent_returnsEmpty_whenNoLocalizedContent() {
|
||||
when(restTemplateUtil.getURLforApiService(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
HelpCenterPost post = new HelpCenterPost();
|
||||
post.setGenericPostUuid(BigInteger.valueOf(99));
|
||||
post.setRedirectUrl(null);
|
||||
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_HELP_CENTER_POST_FIND_BY_HIERARCHY_UUID),
|
||||
eq(HelpCenterPost.class), eq(UUID)))
|
||||
.thenReturn(ResponseEntity.ok(post));
|
||||
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_GENERIC_POST_CONTENT_FIND_BY_UUID_AND_LOCALE),
|
||||
eq(GenericPostContent.class), eq(BigInteger.valueOf(99)), eq(LOCALE)))
|
||||
.thenReturn(ResponseEntity.ok().build());
|
||||
|
||||
Optional<HelpCenterContentResponse> result = helpCenterService.getLeafContent(UUID, LOCALE);
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
|
||||
// ─── getTree helpers ───────────────────────────────────────────────────────
|
||||
|
||||
private static GenericHierarchy node(long uuid, long parentUuid, String name, String tag,
|
||||
int sortIndex, String status, String sysManaged,
|
||||
List<GenericHierarchy> children) {
|
||||
GenericHierarchy gh = new GenericHierarchy();
|
||||
gh.setUuid(BigInteger.valueOf(uuid));
|
||||
gh.setParentUuid(BigInteger.valueOf(parentUuid));
|
||||
gh.setName(name);
|
||||
gh.setMessageTag(tag);
|
||||
gh.setSortIndex(sortIndex);
|
||||
gh.setStatus(GeneralEnum.FlagYesNo.valueOf(status));
|
||||
gh.setSysManaged(GeneralEnum.FlagYesNo.valueOf(sysManaged));
|
||||
gh.setHierarchyType(GeneralEnum.GenericHierarchyType.HC);
|
||||
gh.setChildList(children);
|
||||
return gh;
|
||||
}
|
||||
|
||||
private void stubUrlPassthrough() {
|
||||
when(restTemplateUtil.getURLforApiService(anyString())).thenAnswer(inv -> inv.getArgument(0));
|
||||
}
|
||||
|
||||
private void stubRoot(GenericHierarchy root) {
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_GENERIC_HIERARCHY_FIND_ROOT_BY_TYPE),
|
||||
eq(GenericHierarchy.class), eq(GeneralEnum.GenericHierarchyType.HC)))
|
||||
.thenReturn(ResponseEntity.ok(root));
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_GENERIC_HIERARCHY_FIND_FULL_BY_TYPE_AND_UUID),
|
||||
eq(GenericHierarchy.class), eq(GeneralEnum.GenericHierarchyType.HC), eq(root.getUuid())))
|
||||
.thenReturn(ResponseEntity.ok(root));
|
||||
}
|
||||
|
||||
private void stubLabel(String code, String locale, String label) {
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_MESSAGE_FIND_BY_CODE_AND_LOCALE),
|
||||
eq(String.class), eq(code), eq(locale)))
|
||||
.thenReturn(ResponseEntity.ok(label));
|
||||
}
|
||||
|
||||
private void stubLabelNotFound(String code, String locale) {
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_MESSAGE_FIND_BY_CODE_AND_LOCALE),
|
||||
eq(String.class), eq(code), eq(locale)))
|
||||
.thenReturn(ResponseEntity.status(HttpStatus.NOT_FOUND).build());
|
||||
}
|
||||
|
||||
// ─── getTree tests ─────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void getTree_returnsEmptyCategories_whenNoHcRoot() {
|
||||
stubUrlPassthrough();
|
||||
when(restTemplateUtil.getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_GENERIC_HIERARCHY_FIND_ROOT_BY_TYPE),
|
||||
eq(GenericHierarchy.class), eq(GeneralEnum.GenericHierarchyType.HC)))
|
||||
.thenReturn(ResponseEntity.ok(null));
|
||||
|
||||
HelpCenterTreeResponse result = helpCenterService.getTree("en_US");
|
||||
|
||||
assertThat(result.locale()).isEqualTo("en_US");
|
||||
assertThat(result.categories()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTree_stripsRoot_returnsTopLevelChildrenSortedBySortIndex() {
|
||||
stubUrlPassthrough();
|
||||
|
||||
GenericHierarchy leaf1 = node(11, 1, "Cat B", "TAG.CAT_B", 2, "Y", "N", List.of());
|
||||
GenericHierarchy leaf2 = node(12, 1, "Cat A", "TAG.CAT_A", 1, "Y", "N", List.of());
|
||||
GenericHierarchy root = node(1, 0, "HC Root", null, 0, "Y", "Y", List.of(leaf1, leaf2));
|
||||
|
||||
stubRoot(root);
|
||||
|
||||
when(messageLabelCache.get("TAG.CAT_B", "en_US")).thenReturn(Optional.empty());
|
||||
when(messageLabelCache.get("TAG.CAT_A", "en_US")).thenReturn(Optional.empty());
|
||||
|
||||
stubLabel("TAG.CAT_B", "en_US", "Category B");
|
||||
stubLabel("TAG.CAT_A", "en_US", "Category A");
|
||||
|
||||
HelpCenterTreeResponse result = helpCenterService.getTree("en_US");
|
||||
|
||||
assertThat(result.locale()).isEqualTo("en_US");
|
||||
assertThat(result.categories()).hasSize(2);
|
||||
// sorted by sortIndex: leaf2 (1) before leaf1 (2)
|
||||
assertThat(result.categories().get(0).label()).isEqualTo("Category A");
|
||||
assertThat(result.categories().get(1).label()).isEqualTo("Category B");
|
||||
|
||||
verify(messageLabelCache).put("TAG.CAT_A", "en_US", "Category A");
|
||||
verify(messageLabelCache).put("TAG.CAT_B", "en_US", "Category B");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTree_recursesArbitraryDepth() {
|
||||
stubUrlPassthrough();
|
||||
|
||||
GenericHierarchy leaf = node(30, 20, "Leaf", "TAG.LEAF", 1, "Y", "N", List.of());
|
||||
GenericHierarchy mid = node(20, 10, "Mid", "TAG.MID", 1, "Y", "N", List.of(leaf));
|
||||
GenericHierarchy top = node(10, 1, "Top", "TAG.TOP", 1, "Y", "N", List.of(mid));
|
||||
GenericHierarchy root = node(1, 0, "HC Root", null, 0, "Y", "Y", List.of(top));
|
||||
|
||||
stubRoot(root);
|
||||
|
||||
when(messageLabelCache.get("TAG.TOP", "en_US")).thenReturn(Optional.empty());
|
||||
when(messageLabelCache.get("TAG.MID", "en_US")).thenReturn(Optional.empty());
|
||||
when(messageLabelCache.get("TAG.LEAF", "en_US")).thenReturn(Optional.empty());
|
||||
|
||||
stubLabel("TAG.TOP", "en_US", "Top Category");
|
||||
stubLabel("TAG.MID", "en_US", "Mid Category");
|
||||
stubLabel("TAG.LEAF", "en_US", "Leaf Article");
|
||||
|
||||
HelpCenterTreeResponse result = helpCenterService.getTree("en_US");
|
||||
|
||||
assertThat(result.categories()).hasSize(1);
|
||||
HelpCenterNode topNode = result.categories().get(0);
|
||||
assertThat(topNode.label()).isEqualTo("Top Category");
|
||||
assertThat(topNode.children()).hasSize(1);
|
||||
|
||||
HelpCenterNode midNode = topNode.children().get(0);
|
||||
assertThat(midNode.label()).isEqualTo("Mid Category");
|
||||
assertThat(midNode.children()).hasSize(1);
|
||||
|
||||
HelpCenterNode leafNode = midNode.children().get(0);
|
||||
assertThat(leafNode.label()).isEqualTo("Leaf Article");
|
||||
assertThat(leafNode.children()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTree_excludesInactiveNodesAndTheirDescendants() {
|
||||
stubUrlPassthrough();
|
||||
|
||||
GenericHierarchy activeLeaf = node(21, 10, "Active Leaf", "TAG.ACTIVE_LEAF", 1, "Y", "N", List.of());
|
||||
GenericHierarchy activeCat = node(10, 1, "Active Cat", "TAG.ACTIVE_CAT", 1, "Y", "N", List.of(activeLeaf));
|
||||
|
||||
GenericHierarchy hiddenChild = node(31, 20, "Hidden Child", "TAG.HIDDEN_CHILD", 1, "Y", "N", List.of());
|
||||
GenericHierarchy hiddenCat = node(20, 1, "Hidden Cat", "TAG.HIDDEN_CAT", 2, "N", "N", List.of(hiddenChild));
|
||||
|
||||
GenericHierarchy root = node(1, 0, "HC Root", null, 0, "Y", "Y", List.of(activeCat, hiddenCat));
|
||||
|
||||
stubRoot(root);
|
||||
|
||||
when(messageLabelCache.get("TAG.ACTIVE_CAT", "en_US")).thenReturn(Optional.empty());
|
||||
when(messageLabelCache.get("TAG.ACTIVE_LEAF", "en_US")).thenReturn(Optional.empty());
|
||||
|
||||
stubLabel("TAG.ACTIVE_CAT", "en_US", "Active Category");
|
||||
stubLabel("TAG.ACTIVE_LEAF", "en_US", "Active Leaf");
|
||||
|
||||
HelpCenterTreeResponse result = helpCenterService.getTree("en_US");
|
||||
|
||||
assertThat(result.categories()).hasSize(1);
|
||||
assertThat(result.categories().get(0).label()).isEqualTo("Active Category");
|
||||
assertThat(result.categories().get(0).children()).hasSize(1);
|
||||
assertThat(result.categories().get(0).children().get(0).label()).isEqualTo("Active Leaf");
|
||||
|
||||
verify(restTemplateUtil, never()).getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_MESSAGE_FIND_BY_CODE_AND_LOCALE),
|
||||
eq(String.class), eq("TAG.HIDDEN_CAT"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTree_fallsBackToName_whenMessageTagMissingOrUnresolved() {
|
||||
stubUrlPassthrough();
|
||||
|
||||
GenericHierarchy blankTag = node(11, 1, "Blank Tag Name", "", 1, "Y", "N", List.of());
|
||||
GenericHierarchy nullTag = node(12, 1, "Null Tag Name", null, 2, "Y", "N", List.of());
|
||||
GenericHierarchy notFoundTag = node(13, 1, "Not Found Name", "TAG.NOT_FOUND", 3, "Y", "N", List.of());
|
||||
GenericHierarchy root = node(1, 0, "HC Root", null, 0, "Y", "Y", List.of(blankTag, nullTag, notFoundTag));
|
||||
|
||||
stubRoot(root);
|
||||
|
||||
when(messageLabelCache.get("TAG.NOT_FOUND", "en_US")).thenReturn(Optional.empty());
|
||||
stubLabelNotFound("TAG.NOT_FOUND", "en_US");
|
||||
|
||||
HelpCenterTreeResponse result = helpCenterService.getTree("en_US");
|
||||
|
||||
assertThat(result.categories()).hasSize(3);
|
||||
assertThat(result.categories().get(0).label()).isEqualTo("Blank Tag Name");
|
||||
assertThat(result.categories().get(1).label()).isEqualTo("Null Tag Name");
|
||||
assertThat(result.categories().get(2).label()).isEqualTo("Not Found Name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTree_usesCacheForHits_andCachesFreshFetches() {
|
||||
stubUrlPassthrough();
|
||||
|
||||
GenericHierarchy catA = node(11, 1, "Cat A Name", "TAG.A", 1, "Y", "N", List.of());
|
||||
GenericHierarchy catB = node(12, 1, "Cat B Name", "TAG.B", 2, "Y", "N", List.of());
|
||||
GenericHierarchy root = node(1, 0, "HC Root", null, 0, "Y", "Y", List.of(catA, catB));
|
||||
|
||||
stubRoot(root);
|
||||
|
||||
// TAG.A is a cache hit
|
||||
when(messageLabelCache.get("TAG.A", "en_US")).thenReturn(Optional.of("Cached A Label"));
|
||||
// TAG.B is a cache miss
|
||||
when(messageLabelCache.get("TAG.B", "en_US")).thenReturn(Optional.empty());
|
||||
|
||||
// Only stub TAG.B label endpoint
|
||||
stubLabel("TAG.B", "en_US", "Fresh B Label");
|
||||
|
||||
HelpCenterTreeResponse result = helpCenterService.getTree("en_US");
|
||||
|
||||
assertThat(result.categories()).hasSize(2);
|
||||
assertThat(result.categories().get(0).label()).isEqualTo("Cached A Label");
|
||||
assertThat(result.categories().get(1).label()).isEqualTo("Fresh B Label");
|
||||
|
||||
// TAG.A should never hit the REST endpoint
|
||||
verify(restTemplateUtil, never()).getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_MESSAGE_FIND_BY_CODE_AND_LOCALE),
|
||||
eq(String.class), eq("TAG.A"), anyString());
|
||||
|
||||
// TAG.B should be fetched and cached
|
||||
verify(restTemplateUtil).getForEntity(
|
||||
eq(RestApiSystemServiceConstants.API_REST_MESSAGE_FIND_BY_CODE_AND_LOCALE),
|
||||
eq(String.class), eq("TAG.B"), eq("en_US"));
|
||||
verify(messageLabelCache).put("TAG.B", "en_US", "Fresh B Label");
|
||||
|
||||
// TAG.A should NOT be put in cache (it was a hit)
|
||||
verify(messageLabelCache, never()).put(eq("TAG.A"), anyString(), anyString());
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.constant.api.RestApiMemberServiceConstants;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class MemberServiceTest {
|
||||
|
||||
@Mock
|
||||
private RestTemplateCustomUtil restTemplateUtil;
|
||||
|
||||
private MemberService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new MemberService(restTemplateUtil);
|
||||
}
|
||||
|
||||
@Test
|
||||
void existsActiveByMemberCodeReturnsTrueWhenOkAndBodyTrue() {
|
||||
when(restTemplateUtil.getURLforApiService(RestApiMemberServiceConstants.API_REST_MEMBER_MEMBER_EXISTS_ACTIVE_BY_MEMBER_CODE))
|
||||
.thenReturn("http://member/member/existsActiveByMemberCode");
|
||||
when(restTemplateUtil.getForEntity(eq("http://member/member/existsActiveByMemberCode"), eq(Boolean.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(true));
|
||||
|
||||
boolean result = service.existsActiveByMemberCode("M1");
|
||||
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void existsActiveByMemberCodeReturnsFalseWhenOkAndBodyFalse() {
|
||||
when(restTemplateUtil.getURLforApiService(RestApiMemberServiceConstants.API_REST_MEMBER_MEMBER_EXISTS_ACTIVE_BY_MEMBER_CODE))
|
||||
.thenReturn("http://member/member/existsActiveByMemberCode");
|
||||
when(restTemplateUtil.getForEntity(eq("http://member/member/existsActiveByMemberCode"), eq(Boolean.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(false));
|
||||
|
||||
boolean result = service.existsActiveByMemberCode("M1");
|
||||
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void existsActiveByMemberCodeReturnsFalseWhenOkAndBodyNull() {
|
||||
when(restTemplateUtil.getURLforApiService(RestApiMemberServiceConstants.API_REST_MEMBER_MEMBER_EXISTS_ACTIVE_BY_MEMBER_CODE))
|
||||
.thenReturn("http://member/member/existsActiveByMemberCode");
|
||||
when(restTemplateUtil.getForEntity(eq("http://member/member/existsActiveByMemberCode"), eq(Boolean.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(null));
|
||||
|
||||
boolean result = service.existsActiveByMemberCode("M1");
|
||||
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void existsActiveByMemberCodeReturnsFalseOnNonOkStatus() {
|
||||
when(restTemplateUtil.getURLforApiService(RestApiMemberServiceConstants.API_REST_MEMBER_MEMBER_EXISTS_ACTIVE_BY_MEMBER_CODE))
|
||||
.thenReturn("http://member/member/existsActiveByMemberCode");
|
||||
when(restTemplateUtil.getForEntity(eq("http://member/member/existsActiveByMemberCode"), eq(Boolean.class), any()))
|
||||
.thenReturn(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
|
||||
boolean result = service.existsActiveByMemberCode("M1");
|
||||
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user