|
@@ -0,0 +1,202 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测(补充边界口径):消息通知开关 / 提醒时间设置(能力键 {@code notification.setting.config})。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>本类与同批已合入的 {@link NotificationSettingConfigContractTest} 互补:后者覆盖「载体落地 / 默认值 /
|
|
|
|
|
+ * 非法 HH:mm / 安全不可关闭 / 控制器 userId 隔离 / schema 建表」,本类补齐其未覆盖的三类契约口径:</p>
|
|
|
|
|
+ * <ol>
|
|
|
|
|
+ * <li><b>时间边界</b>:合法 HH:mm 应为 {@code 00:00}~{@code 23:59} 闭区间,{@code 00:00}/{@code 23:59}
|
|
|
|
|
+ * 必须被接受(防止校验把合法边界误杀)。</li>
|
|
|
|
|
+ * <li><b>开关取值</b>:{@code enabled} / {@code reminderEnabled} 为二值开关,仅接受 0/1,其它值(如 2)
|
|
|
|
|
+ * 必须拒绝。</li>
|
|
|
|
|
+ * <li><b>分发侧保底</b>:消息分发入口 {@code NotificationDispatchService} 必须依赖配置服务
|
|
|
|
|
+ * {@code NotificationSettingService},使「关闭全部通知后仍保关键安全通知」(SC-P-65/79) 真正落到发送链路,
|
|
|
|
|
+ * 而不只是停留在配置服务的一个孤立方法上。</li>
|
|
|
|
|
+ * </ol>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:本分支 src/main 尚无 {@code NotificationSettingService} / {@code NotificationSettingDTO}
|
|
|
|
|
+ * / {@code NotificationSettingController} / {@code notification_settings} 表,且 {@code NotificationDispatchService}
|
|
|
|
|
+ * 未注入配置服务。本测试以反射/结构探测断言失败(红),失败信息清晰可贴。纯 Mockito + 反射,
|
|
|
|
|
+ * 无 Spring 上下文 / 无 DB / 无外部服务。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class NotificationSettingConfigBoundaryContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String TAG = "RED[notification.setting.config/boundary] ";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String SERVICE = "com.etotem.cfc.service.NotificationSettingService";
|
|
|
|
|
+ private static final String DTO = "com.etotem.cfc.dto.NotificationSettingDTO";
|
|
|
|
|
+ private static final String DISPATCH = "com.etotem.cfc.service.NotificationDispatchService";
|
|
|
|
|
+
|
|
|
|
|
+ private static final Long USER_ID = 100L;
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界流:合法时间闭区间 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1:saveConfig 应接受合法下界时间 00:00")
|
|
|
|
|
+ void saveConfig_acceptsTime_00_00() throws Exception {
|
|
|
|
|
+ assertAcceptsTime("00:00");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-2:saveConfig 应接受合法上界时间 23:59")
|
|
|
|
|
+ void saveConfig_acceptsTime_23_59() throws Exception {
|
|
|
|
|
+ assertAcceptsTime("23:59");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 异常流:开关二值校验 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("异常流-1:saveConfig 应拒绝非二值 enabled(如 2)")
|
|
|
|
|
+ void saveConfig_rejectsInvalidEnabled() throws Exception {
|
|
|
|
|
+ assertRejectsSwitch("setEnabled", 2, "enabled");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("异常流-2:saveConfig 应拒绝非二值 reminderEnabled(如 2)")
|
|
|
|
|
+ void saveConfig_rejectsInvalidReminderEnabled() throws Exception {
|
|
|
|
|
+ assertRejectsSwitch("setReminderEnabled", 2, "reminderEnabled");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 分发侧保底:关闭后仍保关键安全通知 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("分发保底:NotificationDispatchService 应注入配置服务以保底安全通知")
|
|
|
|
|
+ void dispatchService_consultsSettingServiceForSafetyBypass() {
|
|
|
|
|
+ Class<?> dispatch = loadClass(DISPATCH);
|
|
|
|
|
+ assertNotNull(dispatch, TAG + "消息分发入口 " + simpleName(DISPATCH)
|
|
|
|
|
+ + " 不存在,无法验证「关闭后仍保关键安全通知」落到发送链路。");
|
|
|
|
|
+
|
|
|
|
|
+ Class<?> settingService = loadClass(SERVICE);
|
|
|
|
|
+ assertNotNull(settingService, TAG + "配置服务 " + simpleName(SERVICE)
|
|
|
|
|
+ + " 不存在:分发侧缺少可查询的开关/安全保底契约(SC-P-65/79)。");
|
|
|
|
|
+
|
|
|
|
|
+ boolean consults = false;
|
|
|
|
|
+ for (Field f : dispatch.getDeclaredFields()) {
|
|
|
|
|
+ if (settingService.equals(f.getType())) {
|
|
|
|
|
+ consults = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(consults, TAG + simpleName(DISPATCH) + " 应注入 " + simpleName(SERVICE)
|
|
|
|
|
+ + ":发送前按通知类型查询用户设置,type=safety 等关键安全通知在 enabled=0 时仍须放行"
|
|
|
|
|
+ + "(关闭后仍保关键安全通知,SC-P-65/79)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ private void assertAcceptsTime(String time) throws Exception {
|
|
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
|
|
+ Object dto = newDto();
|
|
|
|
|
+ setProp(dto, "setReminderTime", time);
|
|
|
|
|
+ setProp(dto, "setEnabled", 1);
|
|
|
|
|
+ setProp(dto, "setReminderEnabled", 1);
|
|
|
|
|
+ setProp(dto, "setSafetyEnabled", 1);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ invoke(service, "saveConfig", 2, USER_ID, dto);
|
|
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
|
|
+ fail(TAG + "合法边界时间 " + time + " 被误拒:HH:mm 合法区间应为 00:00~23:59,实际异常="
|
|
|
|
|
+ + e.getCause());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void assertRejectsSwitch(String setter, int badValue, String fieldName) throws Exception {
|
|
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
|
|
+ Object dto = newDto();
|
|
|
|
|
+ setProp(dto, "setReminderTime", "08:00");
|
|
|
|
|
+ setProp(dto, "setEnabled", 1);
|
|
|
|
|
+ setProp(dto, "setReminderEnabled", 1);
|
|
|
|
|
+ setProp(dto, "setSafetyEnabled", 1);
|
|
|
|
|
+ setProp(dto, setter, badValue);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ invoke(service, "saveConfig", 2, USER_ID, dto);
|
|
|
|
|
+ fail(TAG + fieldName + "=" + badValue + " 被接受:二值开关仅允许 0/1。"
|
|
|
|
|
+ + "(非法开关值须以 IllegalArgumentException 拒绝,TC-P-65-E/79-E)");
|
|
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
|
|
+ Throwable cause = e.getCause();
|
|
|
|
|
+ assertTrue(cause instanceof IllegalArgumentException,
|
|
|
|
|
+ TAG + fieldName + "=" + badValue + " 应抛 IllegalArgumentException,实际=" + cause);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 无配置用户:service 的 Mapper 字段全部注入 mock(selectOne 默认返回 null)。 */
|
|
|
|
|
+ private Object newServiceWithMockMapper() throws Exception {
|
|
|
|
|
+ Class<?> clazz = requireClass(SERVICE, "无法构造配置服务:服务类未落地。");
|
|
|
|
|
+ Object service = clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
|
|
+ if (f.getType().isInterface() && f.getType().getName().endsWith("Mapper")) {
|
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
|
+ f.set(service, mock(f.getType()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return service;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Object newDto() throws Exception {
|
|
|
|
|
+ Class<?> clazz = requireClass(DTO, "无法构造配置 DTO:DTO 未落地。");
|
|
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Class<?> requireClass(String fqcn, String reason) {
|
|
|
|
|
+ Class<?> clazz = loadClass(fqcn);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail(TAG + reason + " 期望类:" + fqcn);
|
|
|
|
|
+ }
|
|
|
|
|
+ return clazz;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Class<?> loadClass(String fqcn) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Class.forName(fqcn, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Method findMethod(Class<?> clazz, String name, int paramCount) {
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ if (m.getName().equals(name) && m.getParameterCount() == paramCount) {
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Object invoke(Object target, String name, int paramCount, Object... args) throws Exception {
|
|
|
|
|
+ Method m = findMethod(target.getClass(), name, paramCount);
|
|
|
|
|
+ assertNotNull(m, TAG + target.getClass().getName() + " 缺方法 " + name + "(参数 " + paramCount + ")。");
|
|
|
|
|
+ m.setAccessible(true);
|
|
|
|
|
+ return m.invoke(target, args);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void setProp(Object target, String setter, Object value) throws Exception {
|
|
|
|
|
+ Method found = null;
|
|
|
|
|
+ for (Method m : target.getClass().getMethods()) {
|
|
|
|
|
+ if (m.getName().equals(setter) && m.getParameterCount() == 1) {
|
|
|
|
|
+ found = m;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertNotNull(found, TAG + target.getClass().getName() + " 缺写入器 " + setter + "。");
|
|
|
|
|
+ found.invoke(target, value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String simpleName(String fqcn) {
|
|
|
|
|
+ int idx = fqcn.lastIndexOf('.');
|
|
|
|
|
+ return idx >= 0 ? fqcn.substring(idx + 1) : fqcn;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|