feat(mobile-api): admin interests API
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
48507979bd
commit
5d768b1566
7 files changed
+387
-1
No files matched your search
+151
@@ -0,0 +1,151 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.admin.AdminInterestRequest;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminInterestServiceTest {
|
||||
|
||||
private static final String LIST = "/counter/interest/findByCompany/{companyId}";
|
||||
private static final String SAVE = "/counter/interest/save";
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminInterestService service;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
AdminAuthenticationService auth = mock(AdminAuthenticationService.class);
|
||||
service = new AdminInterestService(rest, auth);
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
User caller = new User();
|
||||
caller.setUsername("boss");
|
||||
caller.setCompanyId(5L);
|
||||
when(auth.loadUserByToken("tok")).thenReturn(caller);
|
||||
when(rest.postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any()))
|
||||
.thenAnswer(i -> ResponseEntity.ok(i.getArgument(2)));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private static Map<String, Object> row(long id, String name, boolean active, int sort) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("companyId", 5);
|
||||
m.put("name", name);
|
||||
m.put("active", active);
|
||||
m.put("sortOrder", sort);
|
||||
return m;
|
||||
}
|
||||
|
||||
private void stubList(Map<String, Object>... rows) {
|
||||
when(rest.getForEntity(eq(LIST), any(ParameterizedTypeReference.class), eq(5L))).thenReturn(ResponseEntity.ok(List.of(rows)));
|
||||
}
|
||||
|
||||
private Map<String, Object> saved() {
|
||||
ArgumentCaptor<Object> c = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), c.capture());
|
||||
return (Map<String, Object>) c.getValue();
|
||||
}
|
||||
|
||||
private static void assertCoded(Runnable r, HttpStatus status, String code, String field) {
|
||||
assertThatThrownBy(r::run).isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(status);
|
||||
assertThat(e.getCode()).isEqualTo(code);
|
||||
if (field != null) assertThat((Map<String, Object>) e.getBody().getProperties().get("details")).containsEntry("field", field);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void listIsTheCallersCompanyInterests() {
|
||||
stubList(row(1, "Cookware", true, 0), row(2, "Old", false, 1));
|
||||
|
||||
assertThat(service.list()).extracting(m -> m.get("name")).containsExactly("Cookware", "Old");
|
||||
assertThat(service.list().get(1)).containsEntry("active", false).doesNotContainKey("companyId");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSendsTrimmedActiveInterestForTheCallersCompany() {
|
||||
service.create(new AdminInterestRequest(" Kitchen appliances ", null, null));
|
||||
|
||||
assertThat(saved()).containsEntry("companyId", 5L).containsEntry("name", "Kitchen appliances")
|
||||
.containsEntry("active", true).containsEntry("sortOrder", 0).doesNotContainKey("id");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nameIsRequired() {
|
||||
assertCoded(() -> service.create(new AdminInterestRequest(" ", null, null)), HttpStatus.BAD_REQUEST, "REQUIRED_FIELD", "name");
|
||||
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nameAtMostHundredCharacters() {
|
||||
assertCoded(() -> service.create(new AdminInterestRequest("x".repeat(101), null, null)), HttpStatus.BAD_REQUEST, "TOO_LONG", "name");
|
||||
service.create(new AdminInterestRequest("x".repeat(100), null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNameIsAConflictOnTheNameField() {
|
||||
when(rest.postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.CONFLICT, "Conflict", null, null, null));
|
||||
|
||||
assertCoded(() -> service.create(new AdminInterestRequest("kitchen APPLIANCES", null, null)), HttpStatus.CONFLICT, "DUPLICATE_NAME", "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateDeactivatesKeepingNameAndOrder() {
|
||||
stubList(row(2, "Cookware", true, 3));
|
||||
|
||||
service.update(2L, new AdminInterestRequest(null, false, null));
|
||||
|
||||
assertThat(saved()).containsEntry("id", 2L).containsEntry("companyId", 5L).containsEntry("name", "Cookware")
|
||||
.containsEntry("active", false).containsEntry("sortOrder", 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateRenamesAndReorders() {
|
||||
stubList(row(2, "Cookware", true, 3));
|
||||
|
||||
service.update(2L, new AdminInterestRequest(" Pots ", null, 1));
|
||||
|
||||
assertThat(saved()).containsEntry("name", "Pots").containsEntry("sortOrder", 1).containsEntry("active", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateOfAnInterestNotInTheCallersCompanyIsNotFound() {
|
||||
stubList(row(2, "Cookware", true, 3));
|
||||
|
||||
assertCoded(() -> service.update(99L, new AdminInterestRequest("X", true, 0)), HttpStatus.NOT_FOUND, "INTEREST_NOT_FOUND", null);
|
||||
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user