This commit is contained in:
2026-09-20 14:28:25 +08:00
parent b12e5c6ffa
commit 784f7ea6cd
@@ -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;
}
}