feat: company sign-in, company scope and super-admin Companies 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
91a0f339a5
commit
69d3ebc58c
16 files changed
+702
-12
No files matched your search
+59
@@ -0,0 +1,59 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AdminCompanyServiceTest {
|
||||
|
||||
private final RestTemplateCustomUtil rest = mock(RestTemplateCustomUtil.class);
|
||||
private final AdminCompanyService service = new AdminCompanyService(rest);
|
||||
|
||||
private static HttpClientErrorException upstream(HttpStatus s, String body) {
|
||||
return HttpClientErrorException.create(s, s.name(), null, body.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private void assertCoded(Runnable call, HttpStatus status, String message) {
|
||||
assertThatThrownBy(call::run).isInstanceOf(CodedResponseStatusException.class).satisfies(ex -> {
|
||||
CodedResponseStatusException c = (CodedResponseStatusException) ex;
|
||||
assertThat(c.getStatusCode()).isEqualTo(status);
|
||||
assertThat(c.getReason()).isEqualTo(message);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void upstream400_409_404_becomeCodedExceptions() {
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
when(rest.postForEntity(any(String.class), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(upstream(HttpStatus.BAD_REQUEST, "{\"message\":\"Name is required\"}"));
|
||||
assertCoded(() -> service.save(Map.of("name", "")), HttpStatus.BAD_REQUEST, "Name is required");
|
||||
|
||||
when(rest.postForEntity(any(String.class), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(upstream(HttpStatus.CONFLICT, ""));
|
||||
assertCoded(() -> service.renameCode(1L, "taken"), HttpStatus.CONFLICT, "Code is already taken");
|
||||
|
||||
when(rest.getForEntity(any(String.class), any(ParameterizedTypeReference.class))).thenThrow(upstream(HttpStatus.NOT_FOUND, ""));
|
||||
assertCoded(() -> service.get(9L), HttpStatus.NOT_FOUND, "Company not found");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankOrMissingCode_is400_beforeAnyDownstreamCall() {
|
||||
assertCoded(() -> service.renameCode(1L, null), HttpStatus.BAD_REQUEST, "Code is required");
|
||||
assertCoded(() -> service.renameCode(1L, " "), HttpStatus.BAD_REQUEST, "Code is required");
|
||||
verifyNoMoreInteractions(rest);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user