Skip to content

Commit 84d0140

Browse files
authored
Merge pull request #843 from amvanbaren/admin-report-unittests
Add unittests for admin stats job to find negative downloads bug
2 parents 97e5758 + 4b8528f commit 84d0140

File tree

2 files changed

+163
-4
lines changed

2 files changed

+163
-4
lines changed

server/src/main/java/org/eclipse/openvsx/entities/AdminStatistics.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import org.eclipse.openvsx.json.AdminStatisticsJson;
1313

1414
import jakarta.persistence.*;
15-
import java.util.ArrayList;
16-
import java.util.Comparator;
17-
import java.util.List;
18-
import java.util.Map;
15+
16+
import java.util.*;
1917
import java.util.stream.Collectors;
2018

2119
@Entity
@@ -327,4 +325,17 @@ public Map<String, Long> getTopMostDownloadedExtensions() {
327325
public void setTopMostDownloadedExtensions(Map<String, Long> topMostDownloadedExtensions) {
328326
this.topMostDownloadedExtensions = topMostDownloadedExtensions;
329327
}
328+
329+
@Override
330+
public boolean equals(Object o) {
331+
if (this == o) return true;
332+
if (o == null || getClass() != o.getClass()) return false;
333+
AdminStatistics that = (AdminStatistics) o;
334+
return id == that.id && year == that.year && month == that.month && extensions == that.extensions && downloads == that.downloads && downloadsTotal == that.downloadsTotal && publishers == that.publishers && Double.compare(that.averageReviewsPerExtension, averageReviewsPerExtension) == 0 && namespaceOwners == that.namespaceOwners && Objects.equals(extensionsByRating, that.extensionsByRating) && Objects.equals(publishersByExtensionsPublished, that.publishersByExtensionsPublished) && Objects.equals(topMostActivePublishingUsers, that.topMostActivePublishingUsers) && Objects.equals(topNamespaceExtensions, that.topNamespaceExtensions) && Objects.equals(topNamespaceExtensionVersions, that.topNamespaceExtensionVersions) && Objects.equals(topMostDownloadedExtensions, that.topMostDownloadedExtensions);
335+
}
336+
337+
@Override
338+
public int hashCode() {
339+
return Objects.hash(id, year, month, extensions, downloads, downloadsTotal, publishers, averageReviewsPerExtension, namespaceOwners, extensionsByRating, publishersByExtensionsPublished, topMostActivePublishingUsers, topNamespaceExtensions, topNamespaceExtensionVersions, topMostDownloadedExtensions);
340+
}
330341
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/** ******************************************************************************
2+
* Copyright (c) 2023 Precies. Software Ltd and others
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
* ****************************************************************************** */
10+
package org.eclipse.openvsx.admin;
11+
12+
import jakarta.persistence.Column;
13+
import jakarta.persistence.ElementCollection;
14+
import jakarta.persistence.FetchType;
15+
import jakarta.persistence.MapKeyColumn;
16+
import org.eclipse.openvsx.entities.AdminStatistics;
17+
import org.eclipse.openvsx.repositories.RepositoryService;
18+
import org.eclipse.openvsx.util.VersionService;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.mockito.Mock;
22+
import org.mockito.Mockito;
23+
import org.mockito.junit.jupiter.MockitoExtension;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.context.TestConfiguration;
26+
import org.springframework.boot.test.mock.mockito.MockBean;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
29+
import org.testcontainers.shaded.org.apache.commons.lang3.NotImplementedException;
30+
31+
import java.util.Map;
32+
33+
@ExtendWith(SpringExtension.class)
34+
public class AdminStatisticsJobRequestHandlerTest {
35+
36+
@MockBean
37+
RepositoryService repositories;
38+
39+
@MockBean
40+
AdminStatisticsService service;
41+
42+
@Autowired
43+
AdminStatisticsJobRequestHandler handler;
44+
45+
@Test
46+
public void testAdminStatisticsJobRequestHandler() throws Exception {
47+
var expectedStatistics = mockAdminStatistics();
48+
49+
var request = new AdminStatisticsJobRequest(2023, 11);
50+
handler.run(request);
51+
Mockito.verify(service).saveAdminStatistics(expectedStatistics);
52+
}
53+
54+
@Test
55+
public void testAdminStatisticsJobRequestHandlerWithPreviousStatistics() throws Exception {
56+
var expectedStatistics = mockAdminStatistics();
57+
expectedStatistics.setDownloads(678L);
58+
59+
var prevStatistics = new AdminStatistics();
60+
prevStatistics.setDownloadsTotal(5000);
61+
Mockito.when(repositories.findAdminStatisticsByYearAndMonth(2023, 10)).thenReturn(prevStatistics);
62+
63+
var request = new AdminStatisticsJobRequest(2023, 11);
64+
handler.run(request);
65+
Mockito.verify(service).saveAdminStatistics(expectedStatistics);
66+
}
67+
68+
@TestConfiguration
69+
static class TestConfig {
70+
@Bean
71+
AdminStatisticsJobRequestHandler adminStatisticsJobRequestHandler() {
72+
return new AdminStatisticsJobRequestHandler();
73+
}
74+
}
75+
76+
private AdminStatistics mockAdminStatistics() {
77+
var year = 2023;
78+
var month = 11;
79+
var extensions = 1234L;
80+
var downloadsTotal = 5678L;
81+
var publishers = 579L;
82+
var averageReviewsPerExtension = 2.5;
83+
var namespaceOwners = 268L;
84+
var extensionsByRating = Map.of(
85+
1, 34,
86+
2, 100,
87+
3, 700,
88+
4, 150,
89+
5, 250
90+
);
91+
var publishersByExtensionsPublished = Map.of(
92+
1, 500,
93+
3, 70,
94+
10, 9
95+
);
96+
var topMostActivePublishingUsers = Map.of(
97+
"foo", 400,
98+
"bar", 150,
99+
"baz", 29
100+
);
101+
var topNamespaceExtensions = Map.of(
102+
"lorum", 800,
103+
"ipsum", 400,
104+
"dolar", 34
105+
);
106+
var topNamespaceExtensionVersions = Map.of(
107+
"lorum", 8000,
108+
"ipsum", 2000,
109+
"dolar", 68
110+
);
111+
var topMostDownloadedExtensions = Map.of(
112+
"lorum.alpha", 1200L,
113+
"ipsum.beta", 450L,
114+
"dolar.omega", 300L
115+
);
116+
117+
var expectedStatistics = new AdminStatistics();
118+
expectedStatistics.setYear(year);
119+
expectedStatistics.setMonth(month);
120+
expectedStatistics.setExtensions(extensions);
121+
expectedStatistics.setDownloads(downloadsTotal);
122+
expectedStatistics.setDownloadsTotal(downloadsTotal);
123+
expectedStatistics.setPublishers(publishers);
124+
expectedStatistics.setAverageReviewsPerExtension(averageReviewsPerExtension);
125+
expectedStatistics.setNamespaceOwners(namespaceOwners);
126+
expectedStatistics.setExtensionsByRating(extensionsByRating);
127+
expectedStatistics.setPublishersByExtensionsPublished(publishersByExtensionsPublished);
128+
expectedStatistics.setTopMostActivePublishingUsers(topMostActivePublishingUsers);
129+
expectedStatistics.setTopNamespaceExtensions(topNamespaceExtensions);
130+
expectedStatistics.setTopNamespaceExtensionVersions(topNamespaceExtensionVersions);
131+
expectedStatistics.setTopMostDownloadedExtensions(topMostDownloadedExtensions);
132+
133+
Mockito.when(repositories.countActiveExtensions()).thenReturn(extensions);
134+
Mockito.when(repositories.downloadsTotal()).thenReturn(downloadsTotal);
135+
Mockito.when(repositories.countActiveExtensionPublishers()).thenReturn(publishers);
136+
Mockito.when(repositories.averageNumberOfActiveReviewsPerActiveExtension()).thenReturn(averageReviewsPerExtension);
137+
Mockito.when(repositories.countPublishersThatClaimedNamespaceOwnership()).thenReturn(namespaceOwners);
138+
Mockito.when(repositories.countActiveExtensionsGroupedByExtensionReviewRating()).thenReturn(extensionsByRating);
139+
Mockito.when(repositories.countActiveExtensionPublishersGroupedByExtensionsPublished()).thenReturn(publishersByExtensionsPublished);
140+
var limit = 10;
141+
Mockito.when(repositories.topMostActivePublishingUsers(limit)).thenReturn(topMostActivePublishingUsers);
142+
Mockito.when(repositories.topNamespaceExtensions(limit)).thenReturn(topNamespaceExtensions);
143+
Mockito.when(repositories.topNamespaceExtensionVersions(limit)).thenReturn(topNamespaceExtensionVersions);
144+
Mockito.when(repositories.topMostDownloadedExtensions(limit)).thenReturn(topMostDownloadedExtensions);
145+
146+
return expectedStatistics;
147+
}
148+
}

0 commit comments

Comments
 (0)