From 2451f4771a56d79b54d56fbe38671a94ffe580ba Mon Sep 17 00:00:00 2001 From: Ivan-Shaml <72102779+Ivan-Shaml@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:26:17 +0200 Subject: [PATCH] add unit tests --- .../analysis/LogAnalyzerServiceTest.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/test/java/com/faforever/client/logging/analysis/LogAnalyzerServiceTest.java diff --git a/src/test/java/com/faforever/client/logging/analysis/LogAnalyzerServiceTest.java b/src/test/java/com/faforever/client/logging/analysis/LogAnalyzerServiceTest.java new file mode 100644 index 0000000000..03ad1f3132 --- /dev/null +++ b/src/test/java/com/faforever/client/logging/analysis/LogAnalyzerServiceTest.java @@ -0,0 +1,79 @@ +package com.faforever.client.logging.analysis; + +import com.faforever.client.config.ClientProperties; +import com.faforever.client.i18n.I18n; +import com.faforever.client.test.ServiceTest; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + + +public class LogAnalyzerServiceTest extends ServiceTest { + + private static final String SOUND_EXPECTED_TEXT = "Sound issue detected"; + private static final String MINIMIZED_EXPECTED_TEXT = "Game was minimized"; + + @Mock + private I18n i18n; + + @Mock + private ClientProperties clientProperties; + + @InjectMocks + private LogAnalyzerService logAnalyzerService; + + @Test + public void testAnalyzeLogContentsWhenGameMinimizedTrace() { + final String logContents = "info: Minimized true"; + + when(i18n.get(anyString())).thenReturn(MINIMIZED_EXPECTED_TEXT); + + AnalysisResult result = logAnalyzerService.analyzeLogContents(logContents); + + assertTrue(result.analysisMessages().contains(MINIMIZED_EXPECTED_TEXT)); + } + + @Test + public void testAnalyzeLogContentsWhenXactTrace() { + final String logContents = "warning: SND\nXACT"; + + when(i18n.get(anyString(), any())).thenReturn(SOUND_EXPECTED_TEXT); + when(clientProperties.getLinks()).thenReturn(Map.of()); + + AnalysisResult result = logAnalyzerService.analyzeLogContents(logContents); + + assertTrue(result.analysisMessages().contains(SOUND_EXPECTED_TEXT)); + } + + @Test + public void testAnalyzeLogContentsWhenGameMinimizedAndXactTrace() { + final String logContents = "info: Minimized true\nwarning: SND\nXACT"; + + when(i18n.get(anyString())).thenReturn(MINIMIZED_EXPECTED_TEXT); + when(i18n.get(anyString(), any())).thenReturn(SOUND_EXPECTED_TEXT); + when(clientProperties.getLinks()).thenReturn(Map.of()); + + AnalysisResult result = logAnalyzerService.analyzeLogContents(logContents); + + Set results = result.analysisMessages(); + assertTrue(results.contains(MINIMIZED_EXPECTED_TEXT)); + assertTrue(results.contains(SOUND_EXPECTED_TEXT)); + } + + @Test + public void testAnalyzeLogContentsWhenNoRelevantTraces() { + final String logContents = "Some other log content"; + + AnalysisResult result = logAnalyzerService.analyzeLogContents(logContents); + + assertTrue(result.analysisMessages().isEmpty()); + } +}