|
| 1 | +/* |
| 2 | + * SonarLint Core - Implementation |
| 3 | + * Copyright (C) 2016-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonarsource.sonarlint.core.telemetry; |
| 21 | + |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.UUID; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.mockito.ArgumentCaptor; |
| 27 | +import org.sonarsource.sonarlint.core.monitoring.MonitoringUserIdStore; |
| 28 | +import org.sonarsource.sonarlint.core.rpc.protocol.backend.initialize.InitializeParams; |
| 29 | +import org.sonarsource.sonarlint.core.rpc.protocol.backend.initialize.TelemetryClientConstantAttributesDto; |
| 30 | +import org.sonarsource.sonarlint.core.telemetry.gessie.GessieConnectionInfo; |
| 31 | +import org.sonarsource.sonarlint.core.telemetry.gessie.GessieConnectionInfo.ConnectionType; |
| 32 | +import org.sonarsource.sonarlint.core.telemetry.gessie.GessieEventPublisher; |
| 33 | +import org.sonarsource.sonarlint.core.telemetry.gessie.GessieService; |
| 34 | +import org.sonarsource.sonarlint.core.telemetry.gessie.event.payload.IDESupportedLanguageViewedPayload; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.mockito.ArgumentMatchers.any; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.never; |
| 40 | +import static org.mockito.Mockito.verify; |
| 41 | +import static org.mockito.Mockito.when; |
| 42 | + |
| 43 | +class GessieEventPublisherTests { |
| 44 | + |
| 45 | + private GessieService gessieService; |
| 46 | + private MonitoringUserIdStore monitoringUserIdStore; |
| 47 | + private TelemetryServerAttributesProvider telemetryServerAttributesProvider; |
| 48 | + |
| 49 | + private GessieEventPublisher underTest; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setUp() { |
| 53 | + gessieService = mock(GessieService.class); |
| 54 | + monitoringUserIdStore = mock(MonitoringUserIdStore.class); |
| 55 | + telemetryServerAttributesProvider = mock(TelemetryServerAttributesProvider.class); |
| 56 | + |
| 57 | + var telemetryConstantAttributes = mock(TelemetryClientConstantAttributesDto.class); |
| 58 | + when(telemetryConstantAttributes.getProductKey()).thenReturn("vscode"); |
| 59 | + when(telemetryConstantAttributes.getProductVersion()).thenReturn("1.0.0"); |
| 60 | + |
| 61 | + var init = mock(InitializeParams.class); |
| 62 | + when(init.getTelemetryConstantAttributes()).thenReturn(telemetryConstantAttributes); |
| 63 | + |
| 64 | + underTest = new GessieEventPublisher(gessieService, monitoringUserIdStore, telemetryServerAttributesProvider, init); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void supportedLanguageViewed_does_nothing_when_gessie_disabled() { |
| 69 | + when(gessieService.isEnabled()).thenReturn(false); |
| 70 | + |
| 71 | + underTest.supportedLanguageViewed("scopeId"); |
| 72 | + |
| 73 | + verify(gessieService, never()).supportedLanguageViewed(any()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void supportedLanguageViewed_does_nothing_when_local_user_id_unavailable() { |
| 78 | + when(gessieService.isEnabled()).thenReturn(true); |
| 79 | + when(monitoringUserIdStore.getOrCreate()).thenReturn(Optional.empty()); |
| 80 | + |
| 81 | + underTest.supportedLanguageViewed("scopeId"); |
| 82 | + |
| 83 | + verify(gessieService, never()).supportedLanguageViewed(any()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void supportedLanguageViewed_sends_event_without_connection_info_for_unbound_scope() { |
| 88 | + when(gessieService.isEnabled()).thenReturn(true); |
| 89 | + when(monitoringUserIdStore.getOrCreate()).thenReturn(Optional.of(UUID.fromString("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))); |
| 90 | + when(telemetryServerAttributesProvider.getGessieConnectionInfo("scopeId")).thenReturn(Optional.empty()); |
| 91 | + |
| 92 | + underTest.supportedLanguageViewed("scopeId"); |
| 93 | + |
| 94 | + var captor = ArgumentCaptor.forClass(IDESupportedLanguageViewedPayload.class); |
| 95 | + verify(gessieService).supportedLanguageViewed(captor.capture()); |
| 96 | + var payload = captor.getValue(); |
| 97 | + assertThat(payload.localUserId()).isEqualTo("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); |
| 98 | + assertThat(payload.connectionType()).isNull(); |
| 99 | + assertThat(payload.userUuid()).isNull(); |
| 100 | + assertThat(payload.organizationUuidV4()).isNull(); |
| 101 | + assertThat(payload.sqsInstallationId()).isNull(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void supportedLanguageViewed_sends_event_with_sqc_connection_info() { |
| 106 | + when(gessieService.isEnabled()).thenReturn(true); |
| 107 | + when(monitoringUserIdStore.getOrCreate()).thenReturn(Optional.of(UUID.fromString("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))); |
| 108 | + var connectionInfo = new GessieConnectionInfo(ConnectionType.SQC, "user-uuid", "org-uuid-v4", null); |
| 109 | + when(telemetryServerAttributesProvider.getGessieConnectionInfo("scopeId")).thenReturn(Optional.of(connectionInfo)); |
| 110 | + |
| 111 | + underTest.supportedLanguageViewed("scopeId"); |
| 112 | + |
| 113 | + var captor = ArgumentCaptor.forClass(IDESupportedLanguageViewedPayload.class); |
| 114 | + verify(gessieService).supportedLanguageViewed(captor.capture()); |
| 115 | + var payload = captor.getValue(); |
| 116 | + assertThat(payload.localUserId()).isEqualTo("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); |
| 117 | + assertThat(payload.connectionType()).isEqualTo(ConnectionType.SQC); |
| 118 | + assertThat(payload.userUuid()).isEqualTo("user-uuid"); |
| 119 | + assertThat(payload.organizationUuidV4()).isEqualTo("org-uuid-v4"); |
| 120 | + assertThat(payload.sqsInstallationId()).isNull(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void supportedLanguageViewed_sends_event_with_sqs_connection_info() { |
| 125 | + when(gessieService.isEnabled()).thenReturn(true); |
| 126 | + when(monitoringUserIdStore.getOrCreate()).thenReturn(Optional.of(UUID.fromString("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))); |
| 127 | + var connectionInfo = new GessieConnectionInfo(ConnectionType.SQS, "sqs-user-uuid", null, "installation-id"); |
| 128 | + when(telemetryServerAttributesProvider.getGessieConnectionInfo("scopeId")).thenReturn(Optional.of(connectionInfo)); |
| 129 | + |
| 130 | + underTest.supportedLanguageViewed("scopeId"); |
| 131 | + |
| 132 | + var captor = ArgumentCaptor.forClass(IDESupportedLanguageViewedPayload.class); |
| 133 | + verify(gessieService).supportedLanguageViewed(captor.capture()); |
| 134 | + var payload = captor.getValue(); |
| 135 | + assertThat(payload.localUserId()).isEqualTo("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); |
| 136 | + assertThat(payload.connectionType()).isEqualTo(ConnectionType.SQS); |
| 137 | + assertThat(payload.userUuid()).isEqualTo("sqs-user-uuid"); |
| 138 | + assertThat(payload.organizationUuidV4()).isNull(); |
| 139 | + assertThat(payload.sqsInstallationId()).isEqualTo("installation-id"); |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments