From 784f7ea6cd71c807c2d7f7ac88029fe047f9643a Mon Sep 17 00:00:00 2001 From: ouyangxq <775153912@qq.com> Date: Sun, 20 Sep 2026 14:28:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/service/PdaReleaseServiceTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 file/src/test/java/com/baiyue/file/service/PdaReleaseServiceTest.java diff --git a/file/src/test/java/com/baiyue/file/service/PdaReleaseServiceTest.java b/file/src/test/java/com/baiyue/file/service/PdaReleaseServiceTest.java new file mode 100644 index 000000000..146db4720 --- /dev/null +++ b/file/src/test/java/com/baiyue/file/service/PdaReleaseServiceTest.java @@ -0,0 +1,82 @@ +package com.baiyue.file.service; + +import org.junit.jupiter.api.Test; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.Timestamp; +import java.time.LocalDateTime; +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.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class PdaReleaseServiceTest { + private static final String APK = "11111111-1111-1111-1111-111111111111"; + private static final String WGT = "22222222-2222-2222-2222-222222222222"; + + @Test + void selectsApkForOldBaseThenWgtForCompatibleBase() throws Exception { + var jdbc = mock(JdbcTemplate.class); + stubQueries(jdbc); + var service = new PdaReleaseService(jdbc, mock(PdaPackageStorage.class), + "https://updates.example.com", "domestic-app", "overseas-app"); + + assertThat(service.latest("domestic", "domestic-app", "android", 1, "1.0.0")) + .containsEntry("packageType", "apk").containsEntry("appid", "domestic-app"); + assertThat(service.latest("domestic", "domestic-app", "android", 2, "1.0.0")) + .containsEntry("packageType", "wgt").containsEntry("minAppVersionCode", 2); + assertThat(service.latest("domestic", "domestic-app", "android", 2, "1.0.10")) + .isEqualTo(Map.of("update", false)); + assertThatThrownBy(() -> service.latest("domestic", "overseas-app", "android", 2, "1.0.0")) + .hasMessageContaining("参数无效"); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private static void stubQueries(JdbcTemplate jdbc) throws Exception { + when(jdbc.query(anyString(), any(RowMapper.class), any(Object[].class))).thenAnswer(invocation -> { + String sql = invocation.getArgument(0); + RowMapper mapper = invocation.getArgument(1); + Object[] params = invocation.getArgument(2); + if (sql.contains("file_pda_release_channel")) { + ResultSet rs = mock(ResultSet.class); + when(rs.getString(1)).thenReturn("domestic-app"); + when(rs.getString(2)).thenReturn(APK); + when(rs.getString(3)).thenReturn(WGT); + return List.of(mapper.mapRow(rs, 0)); + } + if (sql.contains("file_pda_release") && params.length == 1) { + String id = (String) params[0]; + ResultSet rs = releaseRow(id); + return List.of(mapper.mapRow(rs, 0)); + } + return List.of(); + }); + } + + private static ResultSet releaseRow(String id) throws Exception { + ResultSet rs = mock(ResultSet.class); + boolean apk = id.equals(APK); + when(rs.getString("id")).thenReturn(id); + when(rs.getString("app_code")).thenReturn("domestic"); + when(rs.getString("app_id")).thenReturn("domestic-app"); + when(rs.getString("package_type")).thenReturn(apk ? "apk" : "wgt"); + when(rs.getString("version_name")).thenReturn(apk ? "1.0.0" : "1.0.10"); + when(rs.getInt("version_code")).thenReturn(2); + when(rs.getInt("min_app_version_code")).thenReturn(2); + when(rs.getString("release_notes")).thenReturn("Update"); + when(rs.getString("status")).thenReturn("PUBLISHED"); + when(rs.getString("object_key")).thenReturn("domestic/" + id + (apk ? ".apk" : ".wgt")); + when(rs.getString("sha256")).thenReturn("a".repeat(64)); + when(rs.getLong("size_bytes")).thenReturn(256L); + when(rs.getTimestamp("created_at")).thenReturn(Timestamp.valueOf(LocalDateTime.of(2026, 9, 20, 1, 0))); + when(rs.getTimestamp("published_at")).thenReturn(Timestamp.valueOf(LocalDateTime.of(2026, 9, 20, 2, 0))); + return rs; + } +}