|  | 
|  | 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