File size: 9,002 Bytes
6a6be6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.dalab.autodelete.controller;

import com.dalab.autodelete.dto.*;
import com.dalab.autodelete.service.IDeleteAgentDashboardService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class DeleteAgentDashboardControllerTest {

    @Mock
    private IDeleteAgentDashboardService deleteAgentDashboardService;

    @InjectMocks
    private DeleteAgentDashboardController controller;

    private DeleteKpisDTO mockKpis;
    private List<StorageReclaimedTrendDTO> mockTrends;
    private List<DeletionByPolicyDTO> mockPolicyData;
    private List<RecentActivityDTO> mockActivities;
    private List<RiskAssetDTO> mockRiskAssets;

    @BeforeEach
    void setUp() {
        // Setup mock data
        mockKpis = DeleteKpisDTO.builder()
                .activePolicies(12)
                .assetsDeleted(5284)
                .assetsDeletedLast24h(47)
                .pendingDeletion(187)
                .failedDeletion(15)
                .storageReclaimed("2.8 TB")
                .estimatedMonthlySavings(8950.0)
                .totalSavingsToDate(67320.0)
                .complianceScore(99.2)
                .averageDeletionTime(12.5)
                .lastPolicyRun("30 minutes ago")
                .build();

        mockTrends = List.of(
                StorageReclaimedTrendDTO.builder()
                        .date(LocalDate.now().minusWeeks(1))
                        .retention(125.5)
                        .gdpr(28.3)
                        .duplicateData(45.7)
                        .qualityIssues(18.2)
                        .storageOptimization(32.1)
                        .totalReclaimed(249.8)
                        .build()
        );

        mockPolicyData = List.of(
                DeletionByPolicyDTO.builder()
                        .name("Retention Policies")
                        .value(3547)
                        .color("#3b82f6")
                        .build()
        );

        mockActivities = List.of(
                RecentActivityDTO.builder()
                        .id("1")
                        .event("Asset Deleted")
                        .assetName("test.parquet")
                        .policy("Data Retention Policy")
                        .assetSize("4.7 GB")
                        .reason("Exceeded retention period")
                        .status("Success")
                        .timestamp(LocalDateTime.now().minusMinutes(8))
                        .details("Successfully deleted")
                        .build()
        );

        mockRiskAssets = List.of(
                RiskAssetDTO.builder()
                        .name("Test Asset")
                        .type("Documents")
                        .size("12.4 GB")
                        .lastAccessed(LocalDateTime.now().minusYears(2))
                        .risk("High")
                        .scheduledDeletion(LocalDateTime.of(2024, 12, 25, 0, 0))
                        .reason("Exceeded retention period")
                        .build()
        );
    }

    @Test
    void getDeleteKpis_ShouldReturnKpis() {
        // Given
        when(deleteAgentDashboardService.getDeleteKpis()).thenReturn(mockKpis);

        // When
        ResponseEntity<DeleteKpisDTO> response = controller.getDeleteKpis();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(12, response.getBody().getActivePolicies());
        assertEquals(5284, response.getBody().getAssetsDeleted());
        verify(deleteAgentDashboardService).getDeleteKpis();
    }

    @Test
    void getStorageReclaimedTrends_ShouldReturnTrends() {
        // Given
        when(deleteAgentDashboardService.getStorageReclaimedTrends(7)).thenReturn(mockTrends);

        // When
        ResponseEntity<List<StorageReclaimedTrendDTO>> response = controller.getStorageReclaimedTrends(7);

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(1, response.getBody().size());
        assertEquals(125.5, response.getBody().get(0).getRetention());
        verify(deleteAgentDashboardService).getStorageReclaimedTrends(7);
    }

    @Test
    void getStorageReclaimedTrends_WithDefaultWeeks_ShouldUseDefaultValue() {
        // Given
        when(deleteAgentDashboardService.getStorageReclaimedTrends(7)).thenReturn(mockTrends);

        // When
        ResponseEntity<List<StorageReclaimedTrendDTO>> response = controller.getStorageReclaimedTrends(7);

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        verify(deleteAgentDashboardService).getStorageReclaimedTrends(7);
    }

    @Test
    void getDeletionByPolicyData_ShouldReturnPolicyData() {
        // Given
        when(deleteAgentDashboardService.getDeletionByPolicyData()).thenReturn(mockPolicyData);

        // When
        ResponseEntity<List<DeletionByPolicyDTO>> response = controller.getDeletionByPolicyData();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(1, response.getBody().size());
        assertEquals("Retention Policies", response.getBody().get(0).getName());
        verify(deleteAgentDashboardService).getDeletionByPolicyData();
    }

    @Test
    void getRecentActivities_ShouldReturnActivities() {
        // Given
        when(deleteAgentDashboardService.getRecentActivities(10)).thenReturn(mockActivities);

        // When
        ResponseEntity<List<RecentActivityDTO>> response = controller.getRecentActivities(10);

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(1, response.getBody().size());
        assertEquals("Asset Deleted", response.getBody().get(0).getEvent());
        verify(deleteAgentDashboardService).getRecentActivities(10);
    }

    @Test
    void getRecentActivities_WithDefaultLimit_ShouldUseDefaultValue() {
        // Given
        when(deleteAgentDashboardService.getRecentActivities(10)).thenReturn(mockActivities);

        // When
        ResponseEntity<List<RecentActivityDTO>> response = controller.getRecentActivities(10);

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        verify(deleteAgentDashboardService).getRecentActivities(10);
    }

    @Test
    void getRiskAssets_ShouldReturnRiskAssets() {
        // Given
        when(deleteAgentDashboardService.getRiskAssets(10)).thenReturn(mockRiskAssets);

        // When
        ResponseEntity<List<RiskAssetDTO>> response = controller.getRiskAssets(10);

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(1, response.getBody().size());
        assertEquals("Test Asset", response.getBody().get(0).getName());
        verify(deleteAgentDashboardService).getRiskAssets(10);
    }

    @Test
    void getRiskAssets_WithDefaultLimit_ShouldUseDefaultValue() {
        // Given
        when(deleteAgentDashboardService.getRiskAssets(10)).thenReturn(mockRiskAssets);

        // When
        ResponseEntity<List<RiskAssetDTO>> response = controller.getRiskAssets(10);

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        verify(deleteAgentDashboardService).getRiskAssets(10);
    }

    @Test
    void triggerManualDeletionRun_WhenSuccessful_ShouldReturnSuccess() {
        // Given
        when(deleteAgentDashboardService.triggerManualDeletionRun()).thenReturn(true);

        // When
        ResponseEntity<String> response = controller.triggerManualDeletionRun();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertEquals("Manual deletion run triggered successfully", response.getBody());
        verify(deleteAgentDashboardService).triggerManualDeletionRun();
    }

    @Test
    void triggerManualDeletionRun_WhenFailed_ShouldReturnError() {
        // Given
        when(deleteAgentDashboardService.triggerManualDeletionRun()).thenReturn(false);

        // When
        ResponseEntity<String> response = controller.triggerManualDeletionRun();

        // Then
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertEquals("Failed to trigger manual deletion run", response.getBody());
        verify(deleteAgentDashboardService).triggerManualDeletionRun();
    }
}