|
@@ -0,0 +1,451 @@
|
|
|
|
|
+package com.zksy.web.controller.video;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
|
|
+import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
|
|
|
+import com.zksy.base.mapper.EquipmentVideoBindingMapper;
|
|
|
|
|
+import com.zksy.base.mapper.VideoSessionMapper;
|
|
|
|
|
+import com.zksy.base.service.EquipmentBaseService;
|
|
|
|
|
+import com.zksy.base.video.VideoCollectorClient;
|
|
|
|
|
+import com.zksy.base.video.VideoDeviceRecord;
|
|
|
|
|
+import com.zksy.base.video.VideoDeviceService;
|
|
|
|
|
+import com.zksy.base.video.VideoSession;
|
|
|
|
|
+import com.zksy.common.core.domain.entity.SysUser;
|
|
|
|
|
+import com.zksy.common.core.domain.model.LoginUser;
|
|
|
|
|
+import com.zksy.common.core.redis.RedisCache;
|
|
|
|
|
+import com.zksy.framework.config.ResourcesConfig;
|
|
|
|
|
+import com.zksy.framework.config.SecurityConfig;
|
|
|
|
|
+import com.zksy.framework.config.properties.PermitAllUrlProperties;
|
|
|
|
|
+import com.zksy.framework.security.filter.JwtAuthenticationTokenFilter;
|
|
|
|
|
+import com.zksy.framework.security.handle.AuthenticationEntryPointImpl;
|
|
|
|
|
+import com.zksy.framework.security.handle.CockpitAuthenticationEntryPoint;
|
|
|
|
|
+import com.zksy.framework.security.handle.LogoutSuccessHandlerImpl;
|
|
|
|
|
+import com.zksy.framework.web.exception.GlobalExceptionHandler;
|
|
|
|
|
+import com.zksy.framework.web.service.PermissionService;
|
|
|
|
|
+import com.zksy.framework.web.service.TokenService;
|
|
|
|
|
+import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
|
+import org.junit.jupiter.params.provider.CsvSource;
|
|
|
|
|
+import org.junit.jupiter.params.provider.EnumSource;
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.context.annotation.Import;
|
|
|
|
|
+import org.springframework.core.env.MapPropertySource;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
|
+import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
|
|
+import org.springframework.mock.web.MockServletContext;
|
|
|
|
|
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
+import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
|
|
+import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
|
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
|
+import org.springframework.test.web.servlet.ResultActions;
|
|
|
|
|
+import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
|
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
+import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
|
|
|
|
+import org.springframework.web.filter.CorsFilter;
|
|
|
|
|
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.Filter;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.time.Clock;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.Locale;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
|
|
+import static org.mockito.Mockito.clearInvocations;
|
|
|
|
|
+import static org.mockito.Mockito.doAnswer;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.verifyNoInteractions;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
|
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * The agreed seam is the existing /api/video HTTP contract, not controller metadata.
|
|
|
|
|
+ * Uses the real controller, SecurityConfig, method authorization, PermissionService,
|
|
|
|
|
+ * JWT filter, TokenService, RedisCache, exception handlers and VideoDeviceService.
|
|
|
|
|
+ * Only database/media boundaries and RedisTemplate storage are replaced in memory.
|
|
|
|
|
+ * No preinstalled Authentication, security-test request postprocessor, service stub,
|
|
|
|
|
+ * private-method assertion or source-text assertion stands in for those behaviors.
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>This is an isolated single-session contract test: it does not verify SQL,
|
|
|
|
|
+ * transaction locking, scheduled expiry or old/cockpit mixed-session reference counts.
|
|
|
|
|
+ * The mapper's count is zero because each fixture has no competing session. The
|
|
|
|
|
+ * service is constructed by a bean factory, not component-scanned: its scheduling
|
|
|
|
|
+ * configuration is intentionally not bootstrapped. No Boot application, Nacos,
|
|
|
|
|
+ * servlet server, database connection or media/Redis network client is started.
|
|
|
|
|
+ */
|
|
|
|
|
+class VideoDeviceCompatibilityTest {
|
|
|
|
|
+ private static final long OWNER = 41L;
|
|
|
|
|
+ private static final long OTHER_USER = 42L;
|
|
|
|
|
+ private static final String DEVICE_ID = "legacy-video-01";
|
|
|
|
|
+ private static final String GB_DEVICE = "34020000002000000001";
|
|
|
|
|
+ private static final String GB_CHANNEL = "34020000001320000001";
|
|
|
|
|
+ private static final String STREAM = "legacy_stream_001";
|
|
|
|
|
+ private static final String MEDIA_URL = "https://media.invalid/legacy.flv";
|
|
|
|
|
+ private static final String START_TIME = "2026-10-01 10:00:00";
|
|
|
|
|
+ private static final String END_TIME = "2026-10-01 10:30:00";
|
|
|
|
|
+ private static final String RANGE_JSON = "{\"startTime\":\"2026-10-01 10:00:00\","
|
|
|
|
|
+ + "\"endTime\":\"2026-10-01 10:30:00\"}";
|
|
|
|
|
+ private static final String CONTROL_JSON = "{\"action\":\"pause\"}";
|
|
|
|
|
+
|
|
|
|
|
+ private AnnotationConfigWebApplicationContext context;
|
|
|
|
|
+ private MockMvc mvc;
|
|
|
|
|
+ private TokenService tokens;
|
|
|
|
|
+ private EquipmentBaseMapper devices;
|
|
|
|
|
+ private EquipmentVideoBindingMapper bindings;
|
|
|
|
|
+ private VideoSessionMapper sessions;
|
|
|
|
|
+ private VideoCollectorClient collector;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
|
|
+ RequestContextHolder.resetRequestAttributes();
|
|
|
|
|
+ context = new AnnotationConfigWebApplicationContext();
|
|
|
|
|
+ context.setServletContext(new MockServletContext());
|
|
|
|
|
+ context.getEnvironment().getPropertySources().addFirst(new MapPropertySource("video-test", Map.of(
|
|
|
|
|
+ "token.header", "Authorization",
|
|
|
|
|
+ "token.secret", "dmlkZW8tY29tcGF0aWJpbGl0eS10ZXN0LW9ubHktbm90LWEtcHJvZHVjdGlvbi1zZWNyZXQtdmlkZW8tY29tcGF0aWJpbGl0eS10ZXN0",
|
|
|
|
|
+ "token.expireTime", "60")));
|
|
|
|
|
+ context.register(HttpContractConfiguration.class);
|
|
|
|
|
+ context.refresh();
|
|
|
|
|
+ // Mount the actual FilterChainProxy; spring-security-test is not required.
|
|
|
|
|
+ mvc = MockMvcBuilders.webAppContextSetup(context)
|
|
|
|
|
+ .addFilters(context.getBean("springSecurityFilterChain", Filter.class)).build();
|
|
|
|
|
+ tokens = context.getBean(TokenService.class);
|
|
|
|
|
+ devices = context.getBean(EquipmentBaseMapper.class);
|
|
|
|
|
+ bindings = context.getBean(EquipmentVideoBindingMapper.class);
|
|
|
|
|
+ sessions = context.getBean(VideoSessionMapper.class);
|
|
|
|
|
+ collector = context.getBean(VideoCollectorClient.class);
|
|
|
|
|
+
|
|
|
|
|
+ VideoDeviceRecord device = new VideoDeviceRecord();
|
|
|
|
|
+ device.setEquipmentId(DEVICE_ID);
|
|
|
|
|
+ device.setVideoId(DEVICE_ID);
|
|
|
|
|
+ device.setSourceType("GB28181");
|
|
|
|
|
+ device.setGbDeviceId(GB_DEVICE);
|
|
|
|
|
+ device.setGbChannelId(GB_CHANNEL);
|
|
|
|
|
+ when(bindings.find(DEVICE_ID)).thenReturn(device);
|
|
|
|
|
+ Map<String, Object> playback = Map.of("stream", STREAM, "url", MEDIA_URL,
|
|
|
|
|
+ "proxyKey", "internal-media-proxy-key");
|
|
|
|
|
+ when(collector.get("/api/play/start/" + GB_DEVICE + "/" + GB_CHANNEL, Map.of()))
|
|
|
|
|
+ .thenReturn(playback);
|
|
|
|
|
+ when(collector.get("/api/playback/start", rangeParameters())).thenReturn(playback);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, VideoSession> rows = new ConcurrentHashMap<>();
|
|
|
|
|
+ when(sessions.insert(any(VideoSession.class))).thenAnswer(invocation -> {
|
|
|
|
|
+ VideoSession row = invocation.getArgument(0);
|
|
|
|
|
+ rows.put(row.getSessionId(), copy(row));
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ });
|
|
|
|
|
+ when(sessions.selectById(anyString())).thenAnswer(invocation -> copy(rows.get(invocation.getArgument(0))));
|
|
|
|
|
+ when(sessions.updateById(any(VideoSession.class))).thenAnswer(invocation -> {
|
|
|
|
|
+ VideoSession row = invocation.getArgument(0);
|
|
|
|
|
+ return rows.replace(row.getSessionId(), copy(row)) == null ? 0 : 1;
|
|
|
|
|
+ });
|
|
|
|
|
+ when(sessions.selectCount(any())).thenReturn(0L);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @AfterEach
|
|
|
|
|
+ void tearDown() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (context != null) {
|
|
|
|
|
+ context.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
|
|
+ RequestContextHolder.resetRequestAttributes();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void playPermissionStartsLiveWithTheLegacyResponseFields() throws Exception {
|
|
|
|
|
+ startSession(Operation.LIVE, token(OWNER, "video:device:play"));
|
|
|
|
|
+ verify(collector).get("/api/play/start/" + GB_DEVICE + "/" + GB_CHANNEL, Map.of());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void recordPermissionStartsPlaybackWithTheLegacyRangeFields() throws Exception {
|
|
|
|
|
+ startSession(Operation.PLAYBACK, token(OWNER, "video:device:record"));
|
|
|
|
|
+ verify(collector).get("/api/playback/start", rangeParameters());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ParameterizedTest(name = "{0} rejects unrelated and empty permission sets")
|
|
|
|
|
+ @EnumSource(Operation.class)
|
|
|
|
|
+ void loggedInUsersWithoutTheRequiredPermissionAreRejectedBeforeBusinessAccess(Operation operation)
|
|
|
|
|
+ throws Exception {
|
|
|
|
|
+ for (String bearer : Arrays.asList(token(OWNER, "video:device:list"), token(OWNER))) {
|
|
|
|
|
+ perform(operation, "not-reached", bearer)
|
|
|
|
|
+ .andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(403))
|
|
|
|
|
+ .andExpect(jsonPath("$.msg").value("没有权限,请联系管理员授权"));
|
|
|
|
|
+ }
|
|
|
|
|
+ verifyNoInteractions(devices, bindings, sessions, collector);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ParameterizedTest(name = "{0} does not accept {1}")
|
|
|
|
|
+ @CsvSource({
|
|
|
|
|
+ "LIVE, video:device:record",
|
|
|
|
|
+ "PLAYBACK, video:device:play",
|
|
|
|
|
+ "CONTROL, video:device:play"
|
|
|
|
|
+ })
|
|
|
|
|
+ void playAndRecordPermissionsAreNotInterchangeableForStartAndControl(Operation operation, String permission)
|
|
|
|
|
+ throws Exception {
|
|
|
|
|
+ perform(operation, "not-reached", token(OWNER, permission))
|
|
|
|
|
+ .andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(403))
|
|
|
|
|
+ .andExpect(jsonPath("$.msg").value("没有权限,请联系管理员授权"));
|
|
|
|
|
+ verifyNoInteractions(devices, bindings, sessions, collector);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ParameterizedTest(name = "{0} on {1}: {2} allows only the session owner")
|
|
|
|
|
+ @CsvSource({
|
|
|
|
|
+ "HEARTBEAT, LIVE, video:device:play",
|
|
|
|
|
+ "HEARTBEAT, LIVE, video:device:record",
|
|
|
|
|
+ "HEARTBEAT, PLAYBACK, video:device:play",
|
|
|
|
|
+ "HEARTBEAT, PLAYBACK, video:device:record",
|
|
|
|
|
+ "STOP, LIVE, video:device:play",
|
|
|
|
|
+ "STOP, LIVE, video:device:record",
|
|
|
|
|
+ "STOP, PLAYBACK, video:device:play",
|
|
|
|
|
+ "STOP, PLAYBACK, video:device:record",
|
|
|
|
|
+ "CONTROL, PLAYBACK, video:device:record"
|
|
|
|
|
+ })
|
|
|
|
|
+ void sessionActionsKeepAnyPermissionSemanticsButStillRequireTheRealSessionOwner(
|
|
|
|
|
+ Operation operation, Operation start, String permission) throws Exception {
|
|
|
|
|
+ // Create ownership via HTTP and the JWT principal, never by seeding an owner in a service stub.
|
|
|
|
|
+ String sessionId = startSession(start, token(OWNER, "video:device:play", "video:device:record"));
|
|
|
|
|
+ clearInvocations(devices, bindings, sessions, collector);
|
|
|
|
|
+
|
|
|
|
|
+ perform(operation, sessionId, token(OTHER_USER, permission))
|
|
|
|
|
+ .andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(500))
|
|
|
|
|
+ .andExpect(jsonPath("$.msg").value("播放会话不存在或无权操作"));
|
|
|
|
|
+ verifyNoInteractions(devices, bindings, collector);
|
|
|
|
|
+ verify(sessions, never()).updateById(any(VideoSession.class));
|
|
|
|
|
+ verify(sessions, never()).lockVideo(anyString());
|
|
|
|
|
+
|
|
|
|
|
+ success(perform(operation, sessionId, token(OWNER, permission)));
|
|
|
|
|
+ if (operation == Operation.CONTROL) {
|
|
|
|
|
+ verify(collector).get("/api/playback/pause/" + STREAM, Map.of());
|
|
|
|
|
+ } else if (operation == Operation.STOP) {
|
|
|
|
|
+ String path = start == Operation.LIVE
|
|
|
|
|
+ ? "/api/play/stop/" + GB_DEVICE + "/" + GB_CHANNEL
|
|
|
|
|
+ : "/api/playback/stop/" + GB_DEVICE + "/" + GB_CHANNEL + "/" + STREAM;
|
|
|
|
|
+ verify(collector).get(path, Map.of());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ verify(sessions).updateById(any(VideoSession.class));
|
|
|
|
|
+ verifyNoInteractions(collector);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ParameterizedTest(name = "legacy control action {0}")
|
|
|
|
|
+ @CsvSource({
|
|
|
|
|
+ "pause, 0, /api/playback/pause/legacy_stream_001",
|
|
|
|
|
+ "resume, 0, /api/playback/resume/legacy_stream_001",
|
|
|
|
|
+ "seek, 45, /api/playback/seek/legacy_stream_001/45",
|
|
|
|
|
+ "speed, 2, /api/playback/speed/legacy_stream_001/2.0"
|
|
|
|
|
+ })
|
|
|
|
|
+ void recordPermissionKeepsLegacyPlaybackControlBodyAndMediaCommand(
|
|
|
|
|
+ String action, double value, String mediaPath) throws Exception {
|
|
|
|
|
+ String bearer = token(OWNER, "video:device:record");
|
|
|
|
|
+ String sessionId = startSession(Operation.PLAYBACK, bearer);
|
|
|
|
|
+ clearInvocations(collector);
|
|
|
|
|
+ success(mvc.perform(post("/api/video/sessions/{id}/control", sessionId)
|
|
|
|
|
+ .header("Authorization", bearer).contentType(MediaType.APPLICATION_JSON)
|
|
|
|
|
+ .content(JSON.toJSONString(Map.of("action", action, "value", value)))));
|
|
|
|
|
+ verify(collector).get(mediaPath, Map.of());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ParameterizedTest(name = "anonymous {0} keeps legacy code 401, not cockpit HTTP 401")
|
|
|
|
|
+ @EnumSource(Operation.class)
|
|
|
|
|
+ void anonymousRequestsAreRejectedByTheRealSecurityChain(Operation operation) throws Exception {
|
|
|
|
|
+ MockHttpServletRequestBuilder request = request(operation, "not-reached");
|
|
|
|
|
+ mvc.perform(request).andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(401))
|
|
|
|
|
+ .andExpect(jsonPath("$.msg").value(
|
|
|
|
|
+ "请求访问:" + path(operation, "not-reached") + ",认证失败,无法访问系统资源"));
|
|
|
|
|
+ verifyNoInteractions(devices, bindings, sessions, collector);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ParameterizedTest(name = "{0} cannot authenticate with an invalid or revoked JWT")
|
|
|
|
|
+ @EnumSource(Operation.class)
|
|
|
|
|
+ void malformedAndRevokedTokensCannotBypassAuthentication(Operation operation) throws Exception {
|
|
|
|
|
+ String revoked = token(OWNER, "video:device:play", "video:device:record");
|
|
|
|
|
+ LoginUser login = tokens.getLoginUserByToken(revoked);
|
|
|
|
|
+ tokens.delLoginUser(login.getToken());
|
|
|
|
|
+ for (String bearer : Arrays.asList("Bearer not-a-jwt", revoked)) {
|
|
|
|
|
+ perform(operation, "not-reached", bearer)
|
|
|
|
|
+ .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(401));
|
|
|
|
|
+ }
|
|
|
|
|
+ verifyNoInteractions(devices, bindings, sessions, collector);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ParameterizedTest(name = "missing session is not accessible through {0}")
|
|
|
|
|
+ @EnumSource(value = Operation.class, names = {"HEARTBEAT", "STOP", "CONTROL"})
|
|
|
|
|
+ void missingSessionUsesTheSameLegacyOwnershipFailure(Operation operation) throws Exception {
|
|
|
|
|
+ perform(operation, "missing-session", token(OWNER, "video:device:play", "video:device:record"))
|
|
|
|
|
+ .andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(500))
|
|
|
|
|
+ .andExpect(jsonPath("$.msg").value("播放会话不存在或无权操作"));
|
|
|
|
|
+ verifyNoInteractions(devices, bindings, collector);
|
|
|
|
|
+ verify(sessions, never()).updateById(any(VideoSession.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String startSession(Operation operation, String bearer) throws Exception {
|
|
|
|
|
+ String mode = operation == Operation.LIVE ? "LIVE" : "PLAYBACK";
|
|
|
|
|
+ String body = success(perform(operation, DEVICE_ID, bearer))
|
|
|
|
|
+ .andExpect(jsonPath("$.data.sessionId").isNotEmpty())
|
|
|
|
|
+ .andExpect(jsonPath("$.data.mode").value(mode))
|
|
|
|
|
+ .andExpect(jsonPath("$.data.stream").value(STREAM))
|
|
|
|
|
+ .andExpect(jsonPath("$.data.url").value(MEDIA_URL))
|
|
|
|
|
+ .andExpect(jsonPath("$.data.proxyKey").doesNotExist())
|
|
|
|
|
+ .andReturn().getResponse().getContentAsString(StandardCharsets.UTF_8);
|
|
|
|
|
+ return JSON.parseObject(body).getJSONObject("data").getString("sessionId");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private ResultActions success(ResultActions result) throws Exception {
|
|
|
|
|
+ return result.andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
|
|
+ .andExpect(jsonPath("$.msg").value("操作成功"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private ResultActions perform(Operation operation, String sessionId, String bearer) throws Exception {
|
|
|
|
|
+ return mvc.perform(request(operation, sessionId).header("Authorization", bearer));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private MockHttpServletRequestBuilder request(Operation operation, String sessionId) {
|
|
|
|
|
+ MockHttpServletRequestBuilder request = post(path(operation, sessionId));
|
|
|
|
|
+ if (operation == Operation.PLAYBACK) {
|
|
|
|
|
+ request.contentType(MediaType.APPLICATION_JSON).content(RANGE_JSON);
|
|
|
|
|
+ } else if (operation == Operation.CONTROL) {
|
|
|
|
|
+ request.contentType(MediaType.APPLICATION_JSON).content(CONTROL_JSON);
|
|
|
|
|
+ }
|
|
|
|
|
+ return request;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String path(Operation operation, String sessionId) {
|
|
|
|
|
+ String resource = operation == Operation.LIVE || operation == Operation.PLAYBACK
|
|
|
|
|
+ ? "/devices/" + DEVICE_ID : "/sessions/" + sessionId;
|
|
|
|
|
+ return "/api/video" + resource + "/" + operation.name().toLowerCase(Locale.ROOT);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String token(long userId, String... permissions) {
|
|
|
|
|
+ SysUser user = new SysUser();
|
|
|
|
|
+ user.setUserId(userId);
|
|
|
|
|
+ user.setUserName("video-user-" + userId);
|
|
|
|
|
+ LoginUser login = new LoginUser(userId, 101L, user, new HashSet<>(Arrays.asList(permissions)));
|
|
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
|
+ request.setRemoteAddr("127.0.0.1"); // TokenService's location lookup short-circuits for an internal IP.
|
|
|
|
|
+ request.addHeader("User-Agent", "VideoDeviceCompatibilityTest");
|
|
|
|
|
+ ServletRequestAttributes attributes = new ServletRequestAttributes(request);
|
|
|
|
|
+ RequestContextHolder.setRequestAttributes(attributes);
|
|
|
|
|
+ try {
|
|
|
|
|
+ return "Bearer " + tokens.createToken(login);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ attributes.requestCompleted();
|
|
|
|
|
+ RequestContextHolder.resetRequestAttributes();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Map<String, Object> rangeParameters() {
|
|
|
|
|
+ return Map.of("deviceId", GB_DEVICE, "channelId", GB_CHANNEL,
|
|
|
|
|
+ "startTime", START_TIME, "endTime", END_TIME, "type", "all");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static VideoSession copy(VideoSession source) {
|
|
|
|
|
+ if (source == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ VideoSession copy = new VideoSession();
|
|
|
|
|
+ BeanUtils.copyProperties(source, copy);
|
|
|
|
|
+ return copy;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ enum Operation { LIVE, PLAYBACK, HEARTBEAT, STOP, CONTROL }
|
|
|
|
|
+
|
|
|
|
|
+ @Configuration
|
|
|
|
|
+ @EnableWebMvc
|
|
|
|
|
+ @EnableWebSecurity
|
|
|
|
|
+ @Import({SecurityConfig.class, PermitAllUrlProperties.class, PermissionService.class,
|
|
|
|
|
+ JwtAuthenticationTokenFilter.class, TokenService.class, RedisCache.class,
|
|
|
|
|
+ AuthenticationEntryPointImpl.class, CockpitAuthenticationEntryPoint.class,
|
|
|
|
|
+ LogoutSuccessHandlerImpl.class, GlobalExceptionHandler.class, VideoDeviceController.class})
|
|
|
|
|
+ static class HttpContractConfiguration {
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ Clock clock() {
|
|
|
|
|
+ return Clock.systemUTC();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ UserDetailsService userDetailsService() {
|
|
|
|
|
+ return new InMemoryUserDetailsManager();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ CorsFilter corsFilter() {
|
|
|
|
|
+ // Reuse the real CORS bean factory, without importing unrelated resource/interceptor setup.
|
|
|
|
|
+ return new ResourcesConfig().corsFilter();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ EquipmentBaseMapper equipmentBaseMapper() {
|
|
|
|
|
+ return mock(EquipmentBaseMapper.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ EquipmentVideoBindingMapper equipmentVideoBindingMapper() {
|
|
|
|
|
+ return mock(EquipmentVideoBindingMapper.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ VideoSessionMapper videoSessionMapper() {
|
|
|
|
|
+ return mock(VideoSessionMapper.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ VideoCollectorClient videoCollectorClient() {
|
|
|
|
|
+ return mock(VideoCollectorClient.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ VideoDeviceService videoDeviceService(EquipmentBaseMapper devices, VideoCollectorClient collector,
|
|
|
|
|
+ VideoSessionMapper sessions, EquipmentVideoBindingMapper bindings) {
|
|
|
|
|
+ // Equipment writes are outside these five endpoints; no ledger service graph is bootstrapped.
|
|
|
|
|
+ return new VideoDeviceService(devices, collector, sessions, mock(EquipmentBaseService.class), bindings);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ RedisTemplate<String, Object> redisTemplate() {
|
|
|
|
|
+ Map<String, Object> values = new ConcurrentHashMap<>();
|
|
|
|
|
+ RedisTemplate<String, Object> template = mock(RedisTemplate.class);
|
|
|
|
|
+ ValueOperations<String, Object> operations = mock(ValueOperations.class);
|
|
|
|
|
+ when(template.opsForValue()).thenReturn(operations);
|
|
|
|
|
+ when(operations.get(anyString())).thenAnswer(invocation -> values.get(invocation.getArgument(0)));
|
|
|
|
|
+ doAnswer(invocation -> {
|
|
|
|
|
+ values.put(invocation.getArgument(0), invocation.getArgument(1));
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }).when(operations).set(anyString(), any(), anyLong(), any(TimeUnit.class));
|
|
|
|
|
+ when(template.delete(anyString())).thenAnswer(invocation -> values.remove(invocation.getArgument(0)) != null);
|
|
|
|
|
+ return template;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|