Skip to content

Commit

Permalink
Refactor StatisticsService class
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Oct 26, 2024
1 parent 6faba51 commit 513dbb6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/epam/reportportal/service/LaunchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* A basic Launch object implementation which does straight requests to ReportPortal.
*/
public class LaunchImpl extends Launch {
public static final String DISABLE_PROPERTY = "AGENT_NO_ANALYTICS";

private static final Map<ExecutorService, Scheduler> SCHEDULERS = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -263,7 +264,7 @@ protected Maybe<String> start(boolean statistics) {
*/
@Nonnull
public Maybe<String> start() {
return start(true);
return start(System.getenv(DISABLE_PROPERTY) == null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public class StatisticsService implements Closeable {
StandardCharsets.UTF_8
).split(":");

public static final String DISABLE_PROPERTY = "AGENT_NO_ANALYTICS";
private static final String CLIENT_PROPERTIES_FILE = "client.properties";
public static final String START_LAUNCH_EVENT_ACTION = "start_launch";
public static final String CLIENT_NAME_PARAM = "client_name";
Expand Down Expand Up @@ -88,10 +87,7 @@ public StatisticsService(ListenerParameters listenerParameters, Statistics clien

public StatisticsService(ListenerParameters listenerParameters) {
this(listenerParameters,
System.getenv(DISABLE_PROPERTY) != null ?
new DummyClient() :
new StatisticsClient(DECODED_CLIENT_INFO[0], DECODED_CLIENT_INFO[1], listenerParameters)
);
new StatisticsClient(DECODED_CLIENT_INFO[0], DECODED_CLIENT_INFO[1], listenerParameters));
}

protected Statistics getStatistics() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 EPAM Systems
*
* 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 com.epam.reportportal.service;

import com.epam.reportportal.service.statistics.StatisticsClient;
import com.epam.reportportal.service.statistics.StatisticsService;
import com.epam.reportportal.service.statistics.item.StatisticsItem;
import static com.epam.reportportal.util.test.CommonUtils.shutdownExecutorService;
import com.epam.reportportal.test.TestUtils;
import io.reactivex.Maybe;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class StatisticsRunnable {
public static final long DELAY = 1000;
public static final StatisticsClient STATISTICS_CLIENT = mock(StatisticsClient.class);
public static final StatisticsService STATISTICS_SERVICE =
new StatisticsService(TestUtils.standardParameters(), STATISTICS_CLIENT);
public static final Maybe<String> LAUNCH_ID = Maybe.just("launch_id");
public static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
public static final ReportPortalClient CLIENT = mock(ReportPortalClient.class);

public static class MyLaunch extends LaunchImpl {
public MyLaunch() {
super(CLIENT, TestUtils.standardParameters(), LAUNCH_ID, EXECUTOR_SERVICE);
}

@Override
StatisticsService getStatisticsService() {
return STATISTICS_SERVICE;
}
}

public static void main(String... args) {
MyLaunch launch = new MyLaunch();
//noinspection ReactiveStreamsUnusedPublisher
launch.start();
verify(STATISTICS_CLIENT, after(DELAY).times(Integer.parseInt(args[0]))).send(any(StatisticsItem.class));
shutdownExecutorService(EXECUTOR_SERVICE);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.epam.reportportal.service.statistics;

import com.epam.reportportal.service.LaunchImpl;
import com.epam.reportportal.service.StatisticsRunnable;
import com.epam.reportportal.util.test.ProcessUtils;
import org.junit.jupiter.api.Test;

Expand All @@ -31,15 +33,16 @@ public void test_statistics_property_off() throws IOException, InterruptedExcept
Process process = ProcessUtils.buildProcess(
true,
StatisticsRunnable.class,
Collections.singletonMap(StatisticsService.DISABLE_PROPERTY, "1"),
DummyClient.class.getCanonicalName()
Collections.singletonMap(LaunchImpl.DISABLE_PROPERTY, "1"),
"0"
);
assertThat("Exit code should be '0'", process.waitFor(), equalTo(0));
}

@Test
public void test_statistics_property_on() throws IOException, InterruptedException {
Process process = ProcessUtils.buildProcess(true, StatisticsRunnable.class, StatisticsClient.class.getCanonicalName());
Process process = ProcessUtils.buildProcess(true, StatisticsRunnable.class,
"1");
assertThat("Exit code should be '0'", process.waitFor(), equalTo(0));
}
}

0 comments on commit 513dbb6

Please sign in to comment.