Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Nov 15, 2024
1 parent 703b445 commit aace1e6
Showing 1 changed file with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.mockito.Mock;
import retrofit2.Response;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -42,7 +43,6 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.matchesRegex;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.*;

Expand All @@ -67,7 +67,6 @@ protected Statistics getStatistics() {

private static final String SEMANTIC_VERSION_PATTERN = "(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?";


@Mock
private StatisticsApiClient httpClient;

Expand All @@ -84,7 +83,10 @@ protected Statistics getStatistics() {
private ListenerParameters parameters;

private void stubSend() {
when(statistics.send(any())).thenReturn(Maybe.create(e -> e.onSuccess(Response.success(ResponseBody.create("", MediaType.get("text/plain"))))));
when(statistics.send(any())).thenReturn(Maybe.create(e -> e.onSuccess(Response.success(ResponseBody.create(
"",
MediaType.get("text/plain")
)))));
}

@BeforeEach
Expand All @@ -98,10 +100,7 @@ public void setup() {
public void test_statistics_send_event_with_agent() {
stubSend();
StartLaunchRQ launchRq = TestUtils.standardLaunchRequest(parameters);
launchRq.setAttributes(Collections.singleton(new ItemAttributesRQ("agent",
"agent-java-testng|test-version-1",
true
)));
launchRq.setAttributes(Collections.singleton(new ItemAttributesRQ("agent", "agent-java-testng|test-version-1", true)));

service.sendEvent(launchMaybe, launchRq);
service.close();
Expand All @@ -118,12 +117,13 @@ public void test_statistics_send_event_with_agent() {

Map<String, Object> params = event.getParams();
assertThat(params.get(StatisticsService.CLIENT_NAME_PARAM), anyOf(equalTo("${name}"), equalTo("client-java")));
assertThat(params.get(StatisticsService.CLIENT_VERSION_PARAM).toString(), anyOf(equalTo("${version}"), matchesRegex(SEMANTIC_VERSION_PATTERN)));
assertThat(
params.get(StatisticsService.CLIENT_VERSION_PARAM).toString(),
anyOf(equalTo("${version}"), matchesRegex(SEMANTIC_VERSION_PATTERN))
);
assertThat(params.get(StatisticsService.AGENT_NAME_PARAM), equalTo("agent-java-testng"));
assertThat(params.get(StatisticsService.AGENT_VERSION_PARAM), equalTo("test-version-1"));
assertThat(params.get(StatisticsService.INTERPRETER_PARAM),
equalTo("Java " + System.getProperty("java.version"))
);
assertThat(params.get(StatisticsService.INTERPRETER_PARAM), equalTo("Java " + System.getProperty("java.version")));
}

@Test
Expand All @@ -147,12 +147,13 @@ public void test_statistics_send_event_no_agent_record() {
Map<String, Object> params = event.getParams();

assertThat(params.get(StatisticsService.CLIENT_NAME_PARAM), anyOf(equalTo("${name}"), equalTo("client-java")));
assertThat(params.get(StatisticsService.CLIENT_VERSION_PARAM).toString(), anyOf(equalTo("${version}"), matchesRegex(SEMANTIC_VERSION_PATTERN)));
assertThat(
params.get(StatisticsService.CLIENT_VERSION_PARAM).toString(),
anyOf(equalTo("${version}"), matchesRegex(SEMANTIC_VERSION_PATTERN))
);
assertThat(params, not(hasKey(StatisticsService.AGENT_NAME_PARAM)));
assertThat(params, not(hasKey(StatisticsService.AGENT_VERSION_PARAM)));
assertThat(params.get(StatisticsService.INTERPRETER_PARAM),
equalTo("Java " + System.getProperty("java.version"))
);
assertThat(params.get(StatisticsService.INTERPRETER_PARAM), equalTo("Java " + System.getProperty("java.version")));
}

@Test
Expand All @@ -165,8 +166,8 @@ public void test_statistics_send_event_async() {

@Test
public void verify_service_sends_same_client_id() {
when(httpClient.send(anyString(), anyString(), anyString(), any(StatisticsItem.class))).thenReturn(Maybe.create(
e -> e.onSuccess(Response.success(ResponseBody.create("", MediaType.get("text/plain"))))));
when(httpClient.send(anyString(), anyString(), anyString(), any(StatisticsItem.class))).thenReturn(Maybe.create(e -> e.onSuccess(
Response.success(ResponseBody.create("", MediaType.get("text/plain"))))));
String cid;
try (StatisticsClient client = new StatisticsClient("id", "secret", httpClient)) {
try (StatisticsService service = new StatisticsService(TestUtils.standardParameters(), client)) {
Expand All @@ -179,8 +180,8 @@ public void verify_service_sends_same_client_id() {
cid = item1.getClientId();
}
StatisticsApiClient secondClient = mock(StatisticsApiClient.class);
when(secondClient.send(anyString(), anyString(), anyString(), any())).thenReturn(Maybe.create(e -> e.onSuccess(
Response.success(ResponseBody.create("", MediaType.get("text/plain"))))));
when(secondClient.send(anyString(), anyString(), anyString(), any())).thenReturn(Maybe.create(e -> e.onSuccess(Response.success(
ResponseBody.create("", MediaType.get("text/plain"))))));

try (StatisticsClient client = new StatisticsClient("id", "secret", secondClient)) {
try (StatisticsService service = new StatisticsService(TestUtils.standardParameters(), client)) {
Expand All @@ -197,16 +198,19 @@ public void verify_service_sends_same_client_id() {
}

@Test
public void verify_service_sends_same_client_id_for_processes()
throws IOException, InterruptedException {
Process process = ProcessUtils.buildProcess(false, StatisticsIdsRunnable.class);
public void verify_service_sends_same_client_id_for_processes() throws IOException, InterruptedException {
Map<String, String> homeProperty = Collections.singletonMap(
"user.home",
System.getProperty("user.dir") + File.separator + "same_client_id_test"
);
Process process = ProcessUtils.buildProcess(false, StatisticsIdsRunnable.class, null, homeProperty);
assertThat("Exit code should be '0'", process.waitFor(), equalTo(0));
String result = Utils.readInputStreamToString(process.getInputStream());
process.destroyForcibly();
Map<String, String> values = Arrays.stream(result.split(System.lineSeparator()))
.collect(Collectors.toMap(k -> k.substring(0, k.indexOf("=")), v -> v.substring(v.indexOf("=") + 1)));

Process process2 = ProcessUtils.buildProcess(false, StatisticsIdsRunnable.class);
Process process2 = ProcessUtils.buildProcess(false, StatisticsIdsRunnable.class, null, homeProperty);
assertThat("Exit code should be '0'", process2.waitFor(), equalTo(0));
String result2 = Utils.readInputStreamToString(process2.getInputStream());
Map<String, String> values2 = Arrays.stream(result2.split(System.lineSeparator()))
Expand Down

0 comments on commit aace1e6

Please sign in to comment.