|
@@ -8,6 +8,8 @@ import com.zksy.common.exception.ServiceException;
|
|
|
import org.junit.jupiter.api.*;
|
|
import org.junit.jupiter.api.*;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.ZoneId;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
import static org.mockito.Mockito.*;
|
|
import static org.mockito.Mockito.*;
|
|
@@ -106,4 +108,45 @@ class AlarmOperationsServiceTest {
|
|
|
when(mapper.selectMap(eq(query),anyList())).thenReturn(Collections.nCopies(10001,alarm));
|
|
when(mapper.selectMap(eq(query),anyList())).thenReturn(Collections.nCopies(10001,alarm));
|
|
|
assertThrows(ServiceException.class, () -> service.map(query));
|
|
assertThrows(ServiceException.class, () -> service.map(query));
|
|
|
}
|
|
}
|
|
|
|
|
+ @Test void dashboardKeepsTotalsAndGroupsFourLevelsOnConsecutiveBeijingDates() {
|
|
|
|
|
+ query.setRootTypeId(1);
|
|
|
|
|
+ List<Integer> ids = Arrays.asList(1, 11);
|
|
|
|
|
+ when(scope.resolveOperations(query)).thenReturn(ids);
|
|
|
|
|
+ AlarmDashboardVO counts = new AlarmDashboardVO();
|
|
|
|
|
+ counts.setTotalCount(200); counts.setAlarmingDeviceCount(3);
|
|
|
|
|
+ when(mapper.selectDashboardCounts(query, ids, com.zksy.base.enums.AlarmStatusEnum.ALARM.getCode())).thenReturn(counts);
|
|
|
|
|
+ LocalDate today = LocalDate.now(ZoneId.of("Asia/Shanghai"));
|
|
|
|
|
+ Map<String,Object> yesterday = new HashMap<>();
|
|
|
|
|
+ yesterday.put("date", today.minusDays(1).toString()); yesterday.put("level", 2); yesterday.put("total", 9L);
|
|
|
|
|
+ Map<String,Object> unknownLevel = new HashMap<>(yesterday); unknownLevel.put("level", null);
|
|
|
|
|
+ when(mapper.selectDashboardTrend(eq(query),eq(ids),any(),any())).thenReturn(Arrays.asList(yesterday, unknownLevel));
|
|
|
|
|
+ List<Map<String,Object>> ratios = Collections.singletonList(Collections.singletonMap("count", 200L));
|
|
|
|
|
+ when(mapper.countByWarningType(query,ids)).thenReturn(ratios);
|
|
|
|
|
+ when(mapper.countByDeviceType(query,ids)).thenReturn(ratios);
|
|
|
|
|
+ AlarmDashboardVO result = service.dashboard(query, 7);
|
|
|
|
|
+ assertEquals(200,result.getTotalCount()); assertEquals(3,result.getAlarmingDeviceCount());
|
|
|
|
|
+ assertEquals(7,result.getTrend().size());
|
|
|
|
|
+ assertEquals(today.minusDays(6).toString(),result.getTrend().get(0).get("date"));
|
|
|
|
|
+ assertEquals(0L,result.getTrend().get(0).get("level1Count"));
|
|
|
|
|
+ assertEquals(9L,result.getTrend().get(5).get("level2Count"));
|
|
|
|
|
+ assertEquals(0L,result.getTrend().get(6).get("level4Count"));
|
|
|
|
|
+ assertSame(ratios,result.getWarningTypeRatio()); assertSame(ratios,result.getDeviceTypeRatio());
|
|
|
|
|
+ verify(mapper).selectDashboardTrend(query,ids,today.minusDays(6).atStartOfDay(),today.plusDays(1).atStartOfDay());
|
|
|
|
|
+ assertNull(query.getStartTime()); assertNull(query.getEndTime());
|
|
|
|
|
+ verify(scope).resolveOperations(query);
|
|
|
|
|
+ }
|
|
|
|
|
+ @Test void dashboardWithEmptyScopeReturnsZeroDaysWithoutUnscopedQueries() {
|
|
|
|
|
+ when(scope.resolveOperations(query)).thenReturn(Collections.emptyList());
|
|
|
|
|
+ AlarmDashboardVO result = service.dashboard(query, 1);
|
|
|
|
|
+ assertEquals(0,result.getTotalCount()); assertEquals(0,result.getAlarmingDeviceCount());
|
|
|
|
|
+ assertEquals(1,result.getTrend().size()); assertTrue(result.getWarningTypeRatio().isEmpty());
|
|
|
|
|
+ assertTrue(result.getDeviceTypeRatio().isEmpty()); verifyNoInteractions(mapper);
|
|
|
|
|
+ }
|
|
|
|
|
+ @Test void dashboardRejectsInvalidDaysAndUnauthorizedScopeBeforeReadingData() {
|
|
|
|
|
+ assertThrows(ServiceException.class, () -> service.dashboard(query, 0));
|
|
|
|
|
+ assertThrows(ServiceException.class, () -> service.dashboard(query, 32));
|
|
|
|
|
+ when(scope.resolveOperations(query)).thenThrow(new org.springframework.security.access.AccessDeniedException("denied"));
|
|
|
|
|
+ assertThrows(org.springframework.security.access.AccessDeniedException.class, () -> service.dashboard(query, 7));
|
|
|
|
|
+ verifyNoInteractions(mapper);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|