/* * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.usageStatistics.impl.providers; import java.util.*; import jetbrains.buildServer.clouds.server.CloudStatisticsProvider; import jetbrains.buildServer.groups.UserGroupManager; import jetbrains.buildServer.serverSide.BuildAgentManager; import jetbrains.buildServer.serverSide.db.TeamCityDatabaseManager; import jetbrains.buildServer.serverSide.impl.BaseServerTestCase; import jetbrains.buildServer.usageStatistics.UsageStatisticsProvider; import jetbrains.buildServer.usageStatistics.UsageStatisticsPublisher; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @Test public class UsageStatisticsProvidersTest extends BaseServerTestCase { private Collection myProviders; @BeforeMethod @Override protected void setUp() throws Exception { super.setUp(); final TeamCityDatabaseManager dbManager = new TeamCityDatabaseManager(); myProviders = new ArrayList(); myProviders.add(new IDEUsageStatisticsProvider(myServer, myFixture.getServerPaths(), myFixture.getXmlRpcDispatcher())); myProviders.add(new NotificatorUsageStatisticsProvider(myServer, myFixture.getNotificatorRegistry(), myFixture.getNotificationRulesManager())); myProviders.add(new RunnerUsageStatisticsProvider(myServer)); myProviders.add(new ServerConfigurationUsageStatisticsProvider(dbManager, myFixture.getLoginConfiguration(), myFixture.getServer())); myProviders.add(getStaticServerUsageProvider()); myProviders.add(new TriggerUsageStatisticsProvider(myServer)); myProviders.add(new VCSUsageStatisticsProvider(myServer)); } private StaticServerUsageStatisticsProvider getStaticServerUsageProvider() { final CloudStatisticsProvider mockCloudProvider = new CloudStatisticsProvider() { public int getNumberOfProfiles() { return 2; } public int getNumberOfImages() { return 6; } public int getNumberOfRunningInstances() { return 9; } }; myProviders.add(new AgentsJavaUsageStatisticsProvider(myServer)); myProviders.add(new ServerLoadUsageStatisticsProvider(myServer, new WebUsersProvider() { @NotNull public Set getWebUsers(final long fromTimestamp) { return Collections.emptySet(); } }, new IDEUsersProvider() { @NotNull public Set getIDEUsers(final long fromTimestamp) { return Collections.emptySet(); } })); return new StaticServerUsageStatisticsProvider(myServer, myFixture.getUserGroupManager(), myFixture.getAgentPoolManager(), mockCloudProvider); } public void provider_should_not_fail_on_publishing_statistics() { final UsageStatisticsPublisher publisher = new UsageStatisticsPublisher() { public void publishStatistic(@NotNull final String id, @Nullable final Object value) { // do nothing } }; for (final BaseUsageStatisticsProvider provider : myProviders) { provider.setIdFormat(""); provider.setGroupName(""); provider.accept(publisher); } } public void static_server_usage_statistics_provider_test() { final StaticServerUsageStatisticsProvider provider = getStaticServerUsageProvider(); provider.setIdFormat("%s"); final Map statistics = collectStatisticsByProvider(provider); assertEquals(myServer.getProjectManager().getNumberOfProjects(), statistics.get("projectNumber")); assertEquals(myServer.getProjectManager().getArchivedProjects().size(), statistics.get("archivedProjectNumber")); assertEquals(myServer.getProjectManager().getNumberOfBuildTypes(), statistics.get("buildTypeNumber")); final BuildAgentManager buildAgentManager = myServer.getBuildAgentManager(); assertEquals(buildAgentManager.getRegisteredAgents(true).size(), statistics.get("allRegisteredAgentNumber")); assertEquals(buildAgentManager.getRegisteredAgents(false).size(), statistics.get("authorizedRegisteredAgentNumber")); assertEquals(myServer.getVcsManager().getAllRegisteredVcsRoots().size(), statistics.get("vcsRootNumber")); assertEquals(myServer.getUserModel().getNumberOfRegisteredUsers(), statistics.get("userNumber")); final UserGroupManager userGroupManager = myServer.getSingletonService(UserGroupManager.class); assertEquals(userGroupManager.getUserGroups().size(), statistics.get("userGroupNumber")); } /* private Collection getAllProviders() { return Arrays.asList( myServer.getSingletonService(AgentsJavaUsageStatisticsProvider.class), myServer.getSingletonService(ServerLoadUsageStatisticsProvider.class), myServer.getSingletonService(IDEUsageStatisticsProvider.class), myServer.getSingletonService(NotificatorUsageStatisticsProvider.class), myServer.getSingletonService(RunnerUsageStatisticsProvider.class), myServer.getSingletonService(ServerConfigurationUsageStatisticsProvider.class), myServer.getSingletonService(StaticServerUsageStatisticsProvider.class), myServer.getSingletonService(TriggerUsageStatisticsProvider.class), myServer.getSingletonService(VCSUsageStatisticsProvider.class) // myServer.getSingletonService(WebPagesUsageStatisticsProvider.class) ); } */ private Map collectStatisticsByProvider(@NotNull final UsageStatisticsProvider provider) { final Map statistics = new HashMap(); provider.accept(new UsageStatisticsPublisher() { public void publishStatistic(@NotNull final String id, @Nullable final Object value) { statistics.put(id, value); } }); return statistics; } }