diff --git a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/controller/SysClientControllerTests.java b/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/controller/SysClientControllerTests.java deleted file mode 100644 index 69c979213..000000000 --- a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/controller/SysClientControllerTests.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2018-2026, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of pig4cloud.com nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.admin.controller; - -import com.pig4cloud.pigx.admin.api.dto.SysOauthClientDetailsDTO; -import com.pig4cloud.pigx.admin.api.entity.SysOauthClientDetails; -import com.pig4cloud.pigx.admin.service.SysOauthClientDetailsService; -import com.pig4cloud.pigx.common.core.util.R; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class SysClientControllerTests { - - @Test - void getByClientIdReturnsNullDataWhenClientDoesNotExist() { - SysOauthClientDetailsService clientDetailsService = mock(SysOauthClientDetailsService.class); - SysClientController controller = new SysClientController(clientDetailsService); - when(clientDetailsService.getOne(any(), eq(false))).thenReturn(null); - - R result = controller.getByClientId("missing-client"); - - assertThat(result.isOk()).isTrue(); - assertThat(result.getData()).isNull(); - } - - @Test - void getByClientIdHandlesNullAdditionalInformation() { - SysOauthClientDetailsService clientDetailsService = mock(SysOauthClientDetailsService.class); - SysClientController controller = new SysClientController(clientDetailsService); - SysOauthClientDetails details = new SysOauthClientDetails(); - details.setClientId("client-without-additional-information"); - when(clientDetailsService.getOne(any(), eq(false))).thenReturn(details); - - R result = controller.getByClientId(details.getClientId()); - - assertThat(result.isOk()).isTrue(); - assertThat(result.getData()).isNotNull(); - assertThat(result.getData().getClientId()).isEqualTo(details.getClientId()); - } - -} diff --git a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/handler/UmamiFetchTaskHandlerTests.java b/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/handler/UmamiFetchTaskHandlerTests.java deleted file mode 100644 index 2f31cc24b..000000000 --- a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/handler/UmamiFetchTaskHandlerTests.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.admin.handler; - -import cn.hutool.json.JSONUtil; -import com.pig4cloud.pigx.admin.api.constant.AnalyticsFetchStatus; -import com.pig4cloud.pigx.admin.api.entity.SysAnalyticsData; -import com.pig4cloud.pigx.admin.api.entity.SysSocialDetails; -import com.pig4cloud.pigx.admin.mapper.SysAnalyticsDataMapper; -import com.pig4cloud.pigx.admin.mapper.SysSocialDetailsMapper; -import com.pig4cloud.pigx.admin.service.SysPublicParamService; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpServer; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class UmamiFetchTaskHandlerTests { - - @Test - void failedCacheIsReusedAndExpandedMetricsUseVisits() throws IOException { - List requestedPaths = new CopyOnWriteArrayList<>(); - HttpServer server = startUmamiServer(requestedPaths); - try { - SysAnalyticsDataMapper analyticsDataMapper = mock(SysAnalyticsDataMapper.class); - SysSocialDetailsMapper socialDetailsMapper = mock(SysSocialDetailsMapper.class); - SysPublicParamService sysPublicParamService = mock(SysPublicParamService.class); - UmamiFetchTaskHandler handler = new UmamiFetchTaskHandler(analyticsDataMapper, socialDetailsMapper, - sysPublicParamService); - SysAnalyticsData failed = new SysAnalyticsData(); - failed.setId(99L); - failed.setProvider("umami"); - failed.setFetchStatus(AnalyticsFetchStatus.FAILED); - SysSocialDetails config = new SysSocialDetails(); - config.setAppId("admin"); - config.setAppSecret("password"); - - when(analyticsDataMapper.selectOne(any())).thenReturn(failed); - when(socialDetailsMapper.selectOne(any())).thenReturn(config); - when(sysPublicParamService.getSysPublicParamKeyToValue("SITE_UMAMI_HOST_URL")) - .thenReturn("http://127.0.0.1:" + server.getAddress().getPort()); - when(sysPublicParamService.getSysPublicParamKeyToValue("SITE_UMAMI_WEBSITE_ID")).thenReturn("site"); - - handler.fetchAndSave(1L, 1); - - ArgumentCaptor captor = ArgumentCaptor.forClass(SysAnalyticsData.class); - verify(analyticsDataMapper, never()).insert(any(SysAnalyticsData.class)); - verify(analyticsDataMapper, times(2)).updateById(captor.capture()); - assertThat(captor.getAllValues().get(0).getFetchStatus()).isEqualTo(AnalyticsFetchStatus.PENDING); - SysAnalyticsData success = captor.getAllValues().get(1); - assertThat(success.getId()).isEqualTo(99L); - assertThat(success.getFetchStatus()).isEqualTo(AnalyticsFetchStatus.SUCCESS); - assertThat(JSONUtil.parseObj(success.getDataJson()).getStr("topUrls")).contains("\"name\":\"path\"", - "\"value\":3"); - assertThat(requestedPaths).contains("/api/websites/site/metrics/expanded"); - assertThat(requestedPaths).doesNotContain("/api/websites/site/metrics"); - } - finally { - server.stop(0); - } - } - - private HttpServer startUmamiServer(List requestedPaths) throws IOException { - HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); - server.createContext("/", exchange -> { - String path = exchange.getRequestURI().getPath(); - requestedPaths.add(path); - if ("/api/auth/login".equals(path)) { - writeJson(exchange, 200, "{\"token\":\"token\"}"); - } - else if ("/api/websites/site/stats".equals(path)) { - writeJson(exchange, 200, - "{\"pageviews\":10,\"visits\":5,\"visitors\":4,\"bounces\":1,\"totaltime\":25}"); - } - else if ("/api/websites/site/metrics/expanded".equals(path)) { - String type = exchange.getRequestURI().getQuery().replaceAll("^.*type=([^&]+).*$", "$1"); - writeJson(exchange, 200, "[{\"name\":\"" + type - + "\",\"pageviews\":9,\"visitors\":4,\"visits\":3,\"bounces\":1,\"totaltime\":7}]"); - } - else { - writeJson(exchange, 404, "{}"); - } - }); - server.start(); - return server; - } - - private void writeJson(HttpExchange exchange, int status, String body) throws IOException { - byte[] bytes = body.getBytes(StandardCharsets.UTF_8); - exchange.getResponseHeaders().set("Content-Type", "application/json"); - exchange.sendResponseHeaders(status, bytes.length); - exchange.getResponseBody().write(bytes); - exchange.close(); - } - -} diff --git a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysAreaServiceImplTests.java b/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysAreaServiceImplTests.java deleted file mode 100644 index 27179313c..000000000 --- a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysAreaServiceImplTests.java +++ /dev/null @@ -1,180 +0,0 @@ -package com.pig4cloud.pigx.admin.service.impl; - -import com.baomidou.mybatisplus.core.conditions.Wrapper; -import com.pig4cloud.pigx.admin.api.dto.SysAreaSortDTO; -import com.pig4cloud.pigx.admin.api.dto.SysAreaUpdateDTO; -import com.pig4cloud.pigx.admin.api.entity.SysAreaEntity; -import com.pig4cloud.pigx.admin.api.vo.SysAreaChildCountVO; -import com.pig4cloud.pigx.admin.api.vo.SysAreaManageVO; -import com.pig4cloud.pigx.admin.mapper.SysAreaMapper; -import com.pig4cloud.pigx.common.core.util.R; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.springframework.test.util.ReflectionTestUtils; - -import java.util.Collection; -import java.util.List; -import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class SysAreaServiceImplTests { - - @Test - void saveAreaUsesSubmittedEntityWithoutExtraValidation() { - SysAreaServiceImpl service = spy(new SysAreaServiceImpl()); - SysAreaEntity area = area(3L, null, null); - doReturn(List.of()).when(service).list(any(Wrapper.class)); - doReturn(true).when(service).save(area); - doReturn(true).when(service).updateBatchById(any(Collection.class)); - - R result = service.saveArea(area); - - assertThat(result.isOk()).isTrue(); - assertThat(area.getPid()).isEqualTo(100000L); - assertThat(area.getAreaSort()).isZero(); - verify(service).save(area); - } - - @Test - void listManageChildrenLoadsChildCountsFromMapper() { - SysAreaServiceImpl service = new SysAreaServiceImpl(); - SysAreaMapper mapper = mock(SysAreaMapper.class); - ReflectionTestUtils.setField(service, "baseMapper", mapper); - SysAreaEntity root = area(1L, 100000L, 0L); - root.setName("全国"); - SysAreaChildCountVO childCount = new SysAreaChildCountVO(); - childCount.setPid(100000L); - childCount.setChildCount(32L); - when(mapper.selectList(any(Wrapper.class))).thenReturn(List.of(root)); - when(mapper.selectChildCounts(Set.of(100000L))).thenReturn(List.of(childCount)); - - List result = service.listManageChildren(0L); - - assertThat(result).hasSize(1); - assertThat(result.get(0).getChildCount()).isEqualTo(32L); - assertThat(result.get(0).getHasChildren()).isTrue(); - verify(mapper).selectChildCounts(Set.of(100000L)); - } - - @Test - void removeAreasRejectsEmptyIds() { - SysAreaServiceImpl service = new SysAreaServiceImpl(); - - R result = service.removeAreas(List.of()); - - assertThat(result.isOk()).isFalse(); - assertThat(result.getMsg()).contains("不能为空"); - } - - @Test - void updateAreaRejectsNationwideRoot() { - SysAreaServiceImpl service = new SysAreaServiceImpl(); - SysAreaMapper mapper = mock(SysAreaMapper.class); - ReflectionTestUtils.setField(service, "baseMapper", mapper); - when(mapper.selectById(1L)).thenReturn(area(1L, 100000L, 0L)); - SysAreaUpdateDTO update = new SysAreaUpdateDTO(); - update.setId(1L); - update.setName("全国"); - update.setAreaStatus("1"); - update.setHot("0"); - - R result = service.updateArea(update); - - assertThat(result.isOk()).isFalse(); - assertThat(result.getMsg()).contains("根节点"); - } - - @Test - void updateAreaWritesOnlyEditableFields() { - SysAreaServiceImpl service = new SysAreaServiceImpl(); - SysAreaMapper mapper = mock(SysAreaMapper.class); - ReflectionTestUtils.setField(service, "baseMapper", mapper); - when(mapper.selectById(2L)).thenReturn(area(2L, 110000L, 100000L)); - when(mapper.updateById(any(SysAreaEntity.class))).thenReturn(1); - SysAreaUpdateDTO update = new SysAreaUpdateDTO(); - update.setId(2L); - update.setName(" 北京市 "); - update.setAreaStatus("1"); - update.setHot("1"); - - R result = service.updateArea(update); - - ArgumentCaptor captor = ArgumentCaptor.forClass(SysAreaEntity.class); - verify(mapper).updateById(captor.capture()); - SysAreaEntity saved = captor.getValue(); - assertThat(result.isOk()).isTrue(); - assertThat(saved.getName()).isEqualTo("北京市"); - assertThat(saved.getAreaStatus()).isEqualTo("1"); - assertThat(saved.getHot()).isEqualTo("1"); - assertThat(saved.getPid()).isNull(); - assertThat(saved.getAdcode()).isNull(); - assertThat(saved.getAreaType()).isNull(); - assertThat(saved.getAreaSort()).isNull(); - } - - @Test - void updateAreaSortRejectsDuplicateIds() { - SysAreaServiceImpl service = new SysAreaServiceImpl(); - SysAreaSortDTO sortDTO = new SysAreaSortDTO(); - sortDTO.setPid(100000L); - sortDTO.setAreaIds(List.of(1L, 1L)); - - R result = service.updateAreaSort(sortDTO); - - assertThat(result.isOk()).isFalse(); - assertThat(result.getMsg()).contains("不能重复"); - } - - @Test - @SuppressWarnings("unchecked") - void updateAreaSortWritesDescendingValuesInVisualOrder() { - SysAreaServiceImpl service = spy(new SysAreaServiceImpl()); - SysAreaEntity first = area(1L, 110000L, 100000L); - SysAreaEntity second = area(2L, 120000L, 100000L); - doReturn(List.of(first, second)).when(service).list(any(Wrapper.class)); - doReturn(true).when(service).updateBatchById(any(Collection.class)); - - SysAreaSortDTO sortDTO = new SysAreaSortDTO(); - sortDTO.setPid(100000L); - sortDTO.setAreaIds(List.of(2L, 1L)); - - R result = service.updateAreaSort(sortDTO); - ArgumentCaptor> updatesCaptor = ArgumentCaptor.forClass(Collection.class); - verify(service).updateBatchById(updatesCaptor.capture()); - List updates = updatesCaptor.getValue().stream().toList(); - - assertThat(result.isOk()).isTrue(); - assertThat(result.getData()).isEqualTo(true); - assertThat(updates).extracting(SysAreaEntity::getId).containsExactly(2L, 1L); - assertThat(updates).extracting(SysAreaEntity::getAreaSort).containsExactly(2L, 1L); - } - - @Test - @SuppressWarnings("unchecked") - void removeAreasRejectsNationwideRoot() { - SysAreaServiceImpl service = spy(new SysAreaServiceImpl()); - SysAreaEntity root = area(1L, 100000L, 0L); - doReturn(List.of(root)).when(service).listByIds(any(Collection.class)); - - R result = service.removeAreas(List.of(1L)); - - assertThat(result.isOk()).isFalse(); - assertThat(result.getMsg()).contains("根节点"); - } - - private SysAreaEntity area(Long id, Long adcode, Long pid) { - SysAreaEntity area = new SysAreaEntity(); - area.setId(id); - area.setAdcode(adcode); - area.setPid(pid); - return area; - } - -} diff --git a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysLogServiceImplTests.java b/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysLogServiceImplTests.java deleted file mode 100644 index ffb2adfdd..000000000 --- a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysLogServiceImplTests.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.admin.service.impl; - -import com.pig4cloud.pigx.admin.mapper.SysLogMapper; -import org.junit.jupiter.api.Test; -import org.springframework.test.util.ReflectionTestUtils; - -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class SysLogServiceImplTests { - - @Test - void getLogSumUsesDatabaseAggregationAndKeepsResponseShape() { - SysLogMapper sysLogMapper = mock(SysLogMapper.class); - SysLogServiceImpl service = new SysLogServiceImpl(); - ReflectionTestUtils.setField(service, "baseMapper", sysLogMapper); - when(sysLogMapper.selectLogSum(any())) - .thenReturn(List.of(row("2026-07-13", "0", 12L), row("2026-07-13", "9", 3L), row("2026-07-14", "0", 8L))); - when(sysLogMapper.selectList(any())).thenThrow(new AssertionError("不应加载日志实体进行内存聚合")); - - List> result = service.getLogSum(); - - assertThat(result).containsExactly(Map.of("createTime", "2026-07-13", "0", 12, "9", 3), - Map.of("createTime", "2026-07-14", "0", 8)); - verify(sysLogMapper).selectLogSum(any()); - } - - private Map row(String createTime, String logType, long logCount) { - Map row = new LinkedHashMap<>(); - row.put("createTime", createTime); - row.put("logType", logType); - row.put("logCount", logCount); - return row; - } - -} diff --git a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysSystemInfoServiceImplTests.java b/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysSystemInfoServiceImplTests.java deleted file mode 100644 index f98f5e583..000000000 --- a/pigx-upms/pigx-upms-biz/src/test/java/com/pig4cloud/pigx/admin/service/impl/SysSystemInfoServiceImplTests.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.admin.service.impl; - -import com.pig4cloud.pigx.admin.api.constant.AnalyticsFetchStatus; -import com.pig4cloud.pigx.admin.api.entity.SysAnalyticsData; -import com.pig4cloud.pigx.admin.handler.ClarityFetchTaskHandler; -import com.pig4cloud.pigx.admin.handler.UmamiFetchTaskHandler; -import com.pig4cloud.pigx.admin.mapper.SysAnalyticsDataMapper; -import com.pig4cloud.pigx.admin.service.SysPublicParamService; -import com.pig4cloud.pigx.common.core.util.R; -import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder; -import org.junit.jupiter.api.Test; -import org.springframework.test.util.ReflectionTestUtils; - -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class SysSystemInfoServiceImplTests { - - @Test - @SuppressWarnings("unchecked") - void failedUmamiCacheTriggersRetryAndReturnsLoading() { - ClarityFetchTaskHandler clarityFetchTask = mock(ClarityFetchTaskHandler.class); - UmamiFetchTaskHandler umamiFetchTask = mock(UmamiFetchTaskHandler.class); - SysAnalyticsDataMapper analyticsDataMapper = mock(SysAnalyticsDataMapper.class); - SysPublicParamService sysPublicParamService = mock(SysPublicParamService.class); - SysSystemInfoServiceImpl service = new SysSystemInfoServiceImpl(clarityFetchTask, umamiFetchTask, - sysPublicParamService); - ReflectionTestUtils.setField(service, "baseMapper", analyticsDataMapper); - SysAnalyticsData failed = new SysAnalyticsData(); - failed.setProvider("umami"); - failed.setFetchStatus(AnalyticsFetchStatus.FAILED); - - when(sysPublicParamService.getSysPublicParamKeyToValue("SITE_ANALYTICS_PROVIDER")).thenReturn("umami"); - when(analyticsDataMapper.selectOne(any())).thenReturn(failed); - - TenantContextHolder.setTenantId(1L); - try { - R result = service.getAnalyticsData(1); - - assertThat(result.isOk()).isTrue(); - assertThat((Map) result.getData()).containsEntry("loading", true) - .containsEntry("provider", "umami"); - verify(umamiFetchTask).fetchAndSave(1L, 1); - } - finally { - TenantContextHolder.clear(); - } - } - -}