feat(appointments): discover external booking candidates through fleet boundary
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
package com.baiyue.lastleg.appointmentplanning;
|
||||
|
||||
import com.baiyue.common.security.AuthenticatedUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import java.util.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class AppointmentExternalCandidateAuthorizationTest {
|
||||
@Test void requiresBothPermissionsAndPlatformIdentity() {
|
||||
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
|
||||
var controller=context.getBean(AppointmentExternalCandidateController.class);
|
||||
var service=context.getBean(AppointmentExternalCandidateService.class);
|
||||
var permissions=List.of("platform:appointment:list","platform:overseas-warehouse:last-mile-order:list");
|
||||
var platform=new AuthenticatedUser(1L,0L,0L,1L,"staff","PLATFORM","WEB",Set.copyOf(permissions));
|
||||
for(var permission:permissions) {
|
||||
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(platform,"unused",List.of(new SimpleGrantedAuthority(permission))));
|
||||
assertThatThrownBy(()->controller.page(platform,1,20,null,null,null)).isInstanceOf(AccessDeniedException.class);
|
||||
}
|
||||
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(platform,"unused",permissions.stream().map(SimpleGrantedAuthority::new).toList()));
|
||||
var customer=new AuthenticatedUser(1L,9L,9L,1L,"customer","MERCHANT","WEB",Set.copyOf(permissions));
|
||||
assertThatThrownBy(()->controller.page(customer,1,20,null,null,null)).isInstanceOf(org.springframework.web.server.ResponseStatusException.class);
|
||||
verifyNoInteractions(service);
|
||||
controller.page(platform,1,20,null,null,null);verify(service).page(1,20,null,null,null);
|
||||
} finally {SecurityContextHolder.clearContext();}
|
||||
}
|
||||
@Configuration @EnableMethodSecurity static class Config {
|
||||
@Bean AppointmentExternalCandidateService service(){return mock(AppointmentExternalCandidateService.class);}
|
||||
@Bean AppointmentExternalCandidateController controller(AppointmentExternalCandidateService service){return new AppointmentExternalCandidateController(service);}
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baiyue.lastleg.appointmentplanning;
|
||||
|
||||
import com.baiyue.common.core.api.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class AppointmentExternalCandidateServiceTest {
|
||||
@Test void batchesResolutionAndCoverageWhileKeepingOriginalIdentityAndServerTotal() {
|
||||
var external=mock(AppointmentExternalSourceGateway.class);var warehouses=mock(AppointmentWarehouseClient.class);
|
||||
var sources=mock(AppointmentSourceService.class);var coverage=mock(AppointmentCoverageService.class);
|
||||
var service=new AppointmentExternalCandidateService(external,warehouses,sources,coverage,"token");
|
||||
when(warehouses.catalog("token")).thenReturn(ApiResponse.success(List.of(
|
||||
new AppointmentWarehouseClient.Warehouse(100L,"US","US","OVERSEAS","US"),
|
||||
new AppointmentWarehouseClient.Warehouse(200L,"CA","CA","OVERSEAS","CA"),
|
||||
new AppointmentWarehouseClient.Warehouse(300L,"ONT8","ONT8","FBA","US"))));
|
||||
var query=new AppointmentExternalSourceGateway.CandidateQuery(2,20,List.of("100"),List.of("ONT8"),null,null,"FB");
|
||||
when(external.candidates(query)).thenReturn(new PageResult<>(List.of(new AppointmentExternalSourceGateway.Candidate("C","Customer","FB-1","TR-1","FBA:ONT8")),2,20,21,2));
|
||||
var refs=List.of(new AppointmentCoverageService.SourceRef("C","BOOKING:FB-1","FBA:ONT8"));
|
||||
var source=new AppointmentSourceService.Source("BOOKING:FB-1","C",null,null,"FB-1",null,"100","US","FBA:ONT8","ONT8",null,"BOOKING:1",true,List.of());
|
||||
var covered=new AppointmentCoverageService.Coverage("C","BOOKING:FB-1","FBA:ONT8","UNSCHEDULED",List.of(),List.of(),"BOOKING:1",false);
|
||||
when(sources.resolveBatch(refs)).thenReturn(List.of(source));when(coverage.query(refs)).thenReturn(List.of(covered));
|
||||
var result=service.page(2,20,null,null," FB ");
|
||||
assertThat(result.total()).isEqualTo(21);assertThat(result.records().get(0).source()).isSameAs(source);
|
||||
assertThat(result.records().get(0).source().packages()).isNull();
|
||||
verify(external).candidates(query);verify(sources).resolveBatch(refs);verify(coverage).query(refs);
|
||||
verifyNoMoreInteractions(external,sources,coverage);
|
||||
assertThatThrownBy(()->service.page(1,20,"200",null,null)).hasMessageContaining("美国");
|
||||
when(warehouses.catalog("token")).thenThrow(new IllegalStateException());
|
||||
assertThatThrownBy(()->service.page(1,20,null,null,null)).hasMessageContaining("目录暂不可用");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user