.
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package com.example.service.unit_testing;
|
||||
|
||||
import com.example.dto.dialogflow.base.DetectIntentRequestDTO;
|
||||
import com.example.dto.dialogflow.base.DetectIntentResponseDTO;
|
||||
import com.example.exception.DialogflowClientException;
|
||||
import com.example.mapper.conversation.DialogflowRequestMapper;
|
||||
import com.example.mapper.conversation.DialogflowResponseMapper;
|
||||
import com.example.service.base.DialogflowClientService;
|
||||
import com.google.api.gax.rpc.ApiException;
|
||||
import com.google.api.gax.rpc.StatusCode;
|
||||
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
|
||||
import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse;
|
||||
import com.google.cloud.dialogflow.cx.v3.SessionsClient;
|
||||
import com.google.cloud.dialogflow.cx.v3.SessionsSettings;
|
||||
|
||||
import io.grpc.Status;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class DialogflowClientServiceTest {
|
||||
|
||||
private static final String PROJECT_ID = "test-project";
|
||||
private static final String LOCATION = "us-central1";
|
||||
private static final String AGENT_ID = "test-agent";
|
||||
private static final String SESSION_ID = "test-session-123";
|
||||
|
||||
@Mock
|
||||
private DialogflowRequestMapper mockRequestMapper;
|
||||
@Mock
|
||||
private DialogflowResponseMapper mockResponseMapper;
|
||||
@Mock
|
||||
private SessionsClient mockSessionsClient;
|
||||
|
||||
private MockedStatic<SessionsClient> mockedStaticSessionsClient;
|
||||
|
||||
private DialogflowClientService dialogflowClientService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
mockedStaticSessionsClient = Mockito.mockStatic(SessionsClient.class);
|
||||
mockedStaticSessionsClient.when(() -> SessionsClient.create(any(SessionsSettings.class)))
|
||||
.thenReturn(mockSessionsClient);
|
||||
|
||||
dialogflowClientService = new DialogflowClientService(
|
||||
PROJECT_ID,
|
||||
LOCATION,
|
||||
AGENT_ID,
|
||||
mockRequestMapper,
|
||||
mockResponseMapper
|
||||
);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
mockedStaticSessionsClient.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_shouldInitializeClientSuccessfully() {
|
||||
assertNotNull(dialogflowClientService);
|
||||
mockedStaticSessionsClient.verify(() -> SessionsClient.create(any(SessionsSettings.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void closeSessionsClient_shouldCloseClient() {
|
||||
dialogflowClientService.closeSessionsClient();
|
||||
verify(mockSessionsClient, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectIntent_whenSuccess_shouldReturnMappedResponse() {
|
||||
// Arrange
|
||||
DetectIntentRequestDTO requestDTO = mock(DetectIntentRequestDTO.class);
|
||||
DetectIntentRequest.Builder requestBuilder = DetectIntentRequest.newBuilder();
|
||||
DetectIntentRequest finalRequest = DetectIntentRequest.newBuilder()
|
||||
.setSession(String.format("projects/%s/locations/%s/agents/%s/sessions/%s", PROJECT_ID, LOCATION, AGENT_ID, SESSION_ID))
|
||||
.build();
|
||||
DetectIntentResponse dfResponse = DetectIntentResponse.newBuilder().build();
|
||||
DetectIntentResponseDTO expectedResponseDTO = mock(DetectIntentResponseDTO.class);
|
||||
|
||||
when(mockRequestMapper.mapToDetectIntentRequestBuilder(requestDTO)).thenReturn(requestBuilder);
|
||||
when(mockSessionsClient.detectIntent(any(DetectIntentRequest.class))).thenReturn(dfResponse);
|
||||
when(mockResponseMapper.mapFromDialogflowResponse(dfResponse, SESSION_ID)).thenReturn(expectedResponseDTO);
|
||||
|
||||
// Act & Assert
|
||||
StepVerifier.create(dialogflowClientService.detectIntent(SESSION_ID, requestDTO))
|
||||
.expectNext(expectedResponseDTO)
|
||||
.verifyComplete();
|
||||
|
||||
verify(mockSessionsClient).detectIntent(finalRequest);
|
||||
verify(mockResponseMapper).mapFromDialogflowResponse(dfResponse, SESSION_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectIntent_whenRequestMapperFails_shouldReturnError() {
|
||||
DetectIntentRequestDTO requestDTO = mock(DetectIntentRequestDTO.class);
|
||||
when(mockRequestMapper.mapToDetectIntentRequestBuilder(requestDTO))
|
||||
.thenThrow(new IllegalArgumentException("Invalid mapping"));
|
||||
StepVerifier.create(dialogflowClientService.detectIntent(SESSION_ID, requestDTO))
|
||||
.expectError(IllegalArgumentException.class)
|
||||
.verify();
|
||||
|
||||
verify(mockSessionsClient, never()).detectIntent(any(DetectIntentRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectIntent_whenDialogflowApiThrowsApiException_shouldReturnDialogflowClientException() {
|
||||
DetectIntentRequestDTO requestDTO = mock(DetectIntentRequestDTO.class);
|
||||
DetectIntentRequest.Builder requestBuilder = DetectIntentRequest.newBuilder();
|
||||
|
||||
ApiException apiException = new ApiException(
|
||||
"API Error",
|
||||
null,
|
||||
new StatusCode() {
|
||||
@Override
|
||||
public Code getCode() {
|
||||
return Code.UNAVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransportCode() {
|
||||
return Status.Code.UNAVAILABLE;
|
||||
}
|
||||
},
|
||||
false
|
||||
);
|
||||
|
||||
when(mockRequestMapper.mapToDetectIntentRequestBuilder(requestDTO)).thenReturn(requestBuilder);
|
||||
when(mockSessionsClient.detectIntent(any(DetectIntentRequest.class))).thenThrow(apiException);
|
||||
|
||||
StepVerifier.create(dialogflowClientService.detectIntent(SESSION_ID, requestDTO))
|
||||
.expectError(DialogflowClientException.class)
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectIntent_withNullSessionId_shouldThrowNullPointerException() {
|
||||
DetectIntentRequestDTO requestDTO = mock(DetectIntentRequestDTO.class);
|
||||
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
dialogflowClientService.detectIntent(null, requestDTO);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectIntent_withNullRequest_shouldThrowNullPointerException() {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
dialogflowClientService.detectIntent(SESSION_ID, null);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.example.service;
|
||||
|
||||
import com.example.exception.GeminiClientException;
|
||||
import com.example.service.base.GeminiClientService;
|
||||
import com.google.genai.Client;
|
||||
import com.google.genai.errors.GenAiIOException;
|
||||
import com.google.genai.types.Content;
|
||||
import com.google.genai.types.GenerateContentConfig;
|
||||
import com.google.genai.types.GenerateContentResponse;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
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.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GeminiClientServiceTest {
|
||||
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Client geminiClient;
|
||||
|
||||
@InjectMocks
|
||||
private GeminiClientService geminiClientService;
|
||||
|
||||
private String prompt;
|
||||
private Float temperature;
|
||||
private Integer maxOutputTokens;
|
||||
private String modelName;
|
||||
private Float top_P;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
prompt = "Test prompt";
|
||||
temperature = 0.5f;
|
||||
maxOutputTokens = 100;
|
||||
modelName = "gemini-test-model";
|
||||
top_P=0.85f;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateContent_whenApiSucceeds_returnsGeneratedText() throws GeminiClientException {
|
||||
// Arrange
|
||||
String expectedText = "This is the generated content.";
|
||||
GenerateContentResponse mockResponse = mock(GenerateContentResponse.class);
|
||||
when(mockResponse.text()).thenReturn(expectedText);
|
||||
when(geminiClient.models.generateContent(anyString(), any(Content.class), any(GenerateContentConfig.class)))
|
||||
.thenReturn(mockResponse);
|
||||
|
||||
String actualText = geminiClientService.generateContent(prompt, temperature, maxOutputTokens, modelName,top_P);
|
||||
assertEquals(expectedText, actualText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateContent_whenApiResponseIsNull_throwsGeminiClientException() {
|
||||
// Arrange
|
||||
when(geminiClient.models.generateContent(anyString(), any(Content.class), any(GenerateContentConfig.class)))
|
||||
.thenReturn(null);
|
||||
|
||||
GeminiClientException exception = assertThrows(GeminiClientException.class, () ->
|
||||
geminiClientService.generateContent(prompt, temperature, maxOutputTokens, modelName,top_P)
|
||||
);
|
||||
|
||||
assertEquals("No content generated or unexpected response structure.", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateContent_whenResponseTextIsNull_throwsGeminiClientException() {
|
||||
GenerateContentResponse mockResponse = mock(GenerateContentResponse.class);
|
||||
when(mockResponse.text()).thenReturn(null);
|
||||
when(geminiClient.models.generateContent(anyString(), any(Content.class), any(GenerateContentConfig.class)))
|
||||
.thenReturn(mockResponse);
|
||||
|
||||
GeminiClientException exception = assertThrows(GeminiClientException.class, () ->
|
||||
geminiClientService.generateContent(prompt, temperature, maxOutputTokens, modelName,top_P)
|
||||
);
|
||||
|
||||
assertEquals("No content generated or unexpected response structure.", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateContent_whenGenAiIOExceptionOccurs_throwsGeminiClientException() {
|
||||
// Arrange
|
||||
String errorMessage = "Network issue";
|
||||
when(geminiClient.models.generateContent(anyString(), any(Content.class), any(GenerateContentConfig.class)))
|
||||
.thenThrow(new GenAiIOException(errorMessage, new IOException()));
|
||||
|
||||
GeminiClientException exception = assertThrows(GeminiClientException.class, () ->
|
||||
geminiClientService.generateContent(prompt, temperature, maxOutputTokens, modelName,top_P)
|
||||
);
|
||||
|
||||
assertTrue(exception.getMessage().startsWith("An API communication issue occurred:"));
|
||||
assertTrue(exception.getMessage().contains(errorMessage));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateContent_whenUnexpectedExceptionOccurs_throwsGeminiClientException() {
|
||||
when(geminiClient.models.generateContent(anyString(), any(Content.class), any(GenerateContentConfig.class)))
|
||||
.thenThrow(new RuntimeException("Something went wrong"));
|
||||
|
||||
GeminiClientException exception = assertThrows(GeminiClientException.class, () ->
|
||||
geminiClientService.generateContent(prompt, temperature, maxOutputTokens, modelName,top_P)
|
||||
);
|
||||
|
||||
assertEquals("An unexpected issue occurred during content generation.", exception.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
|
||||
* Your use of it is subject to your agreement with Google.
|
||||
*/
|
||||
package com.example.service.unit_testing;
|
||||
|
||||
import com.example.service.base.GeminiClientService;
|
||||
import com.example.service.base.MessageEntryFilter;
|
||||
import com.example.util.PerformanceTimer;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyFloat;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@DisplayName("MessageEntryFilter Unit Tests")
|
||||
public class MessageEntryFilterTest {
|
||||
|
||||
@Mock
|
||||
private GeminiClientService geminiService;
|
||||
|
||||
@InjectMocks
|
||||
private MessageEntryFilter messageEntryFilter;
|
||||
|
||||
private ListAppender<ILoggingEvent> listAppender;
|
||||
private static final String NOTIFICATION_JSON_EXAMPLE =
|
||||
"{\"idNotificacion\":\"4c2992d3-539d-4b28-8d52-cdea02cd1c75\"," +
|
||||
"\"timestampCreacion\":\"2025-08-01T16:14:02.301671204Z\"," +
|
||||
"\"texto\":\"Tu estado de cuenta de Agosto esta listo\"," +
|
||||
"\"nombreEventoDialogflow\":\"notificacion\"," +
|
||||
"\"codigoIdiomaDialogflow\":\"es\"," +
|
||||
"\"parametros\":{\"notificacion_texto\":\"Tu estado de cuenta de Agosto esta listo\",\"telefono\":\"555555555\"}}";
|
||||
|
||||
private static final String CONVERSATION_JSON_EXAMPLE =
|
||||
"{\"sessionId\":\"ec9f3731-59ac-4bd0-849e-f45fcc18436d\"," +
|
||||
"\"userId\":\"user_by_phone_0102030405060708\"," +
|
||||
"\"telefono\":\"0102030405060708\"," +
|
||||
"\"createdAt\":\"2025-08-06T20:35:05.123699404Z\"," +
|
||||
"\"lastModified\":\"2025-08-06T20:35:05.984574281Z\"," +
|
||||
"\"entries\":[{" +
|
||||
"\"type\":\"USUARIO\"," +
|
||||
"\"timestamp\":\"2025-08-06T20:35:05.123516916Z\"," +
|
||||
"\"text\":\"Hola que tal\"" +
|
||||
"},{" +
|
||||
"\"type\":\"SISTEMA\"," +
|
||||
"\"timestamp\":\"2025-08-06T20:35:05.967828173Z\"," +
|
||||
"\"text\":\"\\u00a1Hola! Bienvenido a Banorte, te saluda Beto. \\u00bfEn qu\\u00e9 te puedo ayudar? \\uD83D\\uDE0A\"," +
|
||||
"\"parameters\":{" +
|
||||
"\"canal\":\"banortec\"," +
|
||||
"\"telefono\":\"0102030405060708\"," +
|
||||
"\"pantalla_contexto\":\"transferencias\"," +
|
||||
"\"usuario_id\":\"user_by_phone_0102030405060708\"," +
|
||||
"\"nickname\":\"John Doe\"" +
|
||||
"}" +
|
||||
"}]" +
|
||||
"}";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(MessageEntryFilter.class);
|
||||
listAppender = new ListAppender<>();
|
||||
listAppender.start();
|
||||
logger.addAppender(listAppender);
|
||||
}
|
||||
|
||||
private List<String> getLogMessages() {
|
||||
return listAppender.list.stream()
|
||||
.map(ILoggingEvent::getFormattedMessage)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should classify as CONVERSATION when Gemini responds with 'CONVERSATION'")
|
||||
void classifyMessage_shouldReturnConversation_whenGeminiRespondsConversation() throws Exception {
|
||||
String query = "Hola,como estas?";
|
||||
when(geminiService.generateContent(any(String.class), anyFloat(), anyInt(), any(String.class), anyFloat()))
|
||||
.thenReturn("CONVERSATION");
|
||||
|
||||
String result = PerformanceTimer.timeExecution("ClassifyConversationTest",
|
||||
() -> messageEntryFilter.classifyMessage(query, null,null));
|
||||
|
||||
assertEquals(MessageEntryFilter.CATEGORY_CONVERSATION, result);
|
||||
|
||||
verify(geminiService, times(1)).generateContent(any(String.class), anyFloat(), anyInt(), any(String.class), anyFloat());
|
||||
|
||||
List<String> logMessages = getLogMessages();
|
||||
assertNotNull(logMessages.stream()
|
||||
.filter(m -> m.contains("Classified as CONVERSATION. Input: 'Hola,como estas?'"))
|
||||
.findFirst()
|
||||
.orElse(null), "Log message for successful classification not found.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Should classify as NOTIFICATION when Gemini responds with 'NOTIFICATION' (with context)")
|
||||
void classifyMessage_shouldReturnNotification_whenGeminiRespondsNotificationWithContext() throws Exception {
|
||||
String query = "Donde puedo descargar mi estado de cuenta";
|
||||
when(geminiService.generateContent(any(String.class), anyFloat(), anyInt(), any(String.class), anyFloat()))
|
||||
.thenReturn("NOTIFICATION");
|
||||
|
||||
String result = PerformanceTimer.timeExecution("ClassifyNotificationTest",
|
||||
() -> messageEntryFilter.classifyMessage(query, NOTIFICATION_JSON_EXAMPLE,CONVERSATION_JSON_EXAMPLE));
|
||||
|
||||
assertEquals(MessageEntryFilter.CATEGORY_NOTIFICATION, result);
|
||||
|
||||
verify(geminiService, times(1)).generateContent(any(String.class), anyFloat(), anyInt(), any(String.class), anyFloat());
|
||||
|
||||
List<String> logMessages = getLogMessages();
|
||||
assertNotNull(logMessages.stream()
|
||||
.filter(m -> m.contains("Classified as NOTIFICATION") && m.contains(query))
|
||||
.findFirst()
|
||||
.orElse(null), "Log message for successful classification not found.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return UNKNOWN if queryInputText is null")
|
||||
void classifyMessage_shouldReturnUnknown_whenQueryInputTextIsNull() throws Exception {
|
||||
String result = messageEntryFilter.classifyMessage(null, null,null);
|
||||
assertEquals(MessageEntryFilter.CATEGORY_UNKNOWN, result);
|
||||
verify(geminiService, times(0)).generateContent(any(), any(), any(), any(), any());
|
||||
assertNotNull(getLogMessages().stream().filter(m -> m.contains("Query input text for classification is null or blank")).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return UNKNOWN if queryInputText is blank")
|
||||
void classifyMessage_shouldReturnUnknown_whenQueryInputTextIsBlank() throws Exception {
|
||||
String result = messageEntryFilter.classifyMessage(" ", null,null);
|
||||
assertEquals(MessageEntryFilter.CATEGORY_UNKNOWN, result);
|
||||
verify(geminiService, times(0)).generateContent(any(), any(), any(), any(), any());
|
||||
assertNotNull(getLogMessages().stream().filter(m -> m.contains("Query input text for classification is null or blank")).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return UNKNOWN if Gemini returns null")
|
||||
void classifyMessage_shouldReturnUnknown_whenGeminiReturnsNull() throws Exception {
|
||||
String query = "Any valid query";
|
||||
when(geminiService.generateContent(any(), any(), any(), any(), any())).thenReturn(null);
|
||||
|
||||
String result = messageEntryFilter.classifyMessage(query, null,null);
|
||||
assertEquals(MessageEntryFilter.CATEGORY_UNKNOWN, result);
|
||||
assertNotNull(getLogMessages().stream().filter(m -> m.contains("Gemini returned an unrecognised classification or was null/blank")).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return UNKNOWN if Gemini returns blank")
|
||||
void classifyMessage_shouldReturnUnknown_whenGeminiReturnsBlank() throws Exception {
|
||||
String query = "Any valid query";
|
||||
when(geminiService.generateContent(any(), any(), any(), any(), any())).thenReturn(" ");
|
||||
|
||||
String result = messageEntryFilter.classifyMessage(query, null, null);
|
||||
assertEquals(MessageEntryFilter.CATEGORY_UNKNOWN, result);
|
||||
assertNotNull(getLogMessages().stream().filter(m -> m.contains("Gemini returned an unrecognised classification or was null/blank")).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return UNKNOWN if Gemini returns an unexpected string")
|
||||
void classifyMessage_shouldReturnUnknown_whenGeminiReturnsUnexpectedString() throws Exception {
|
||||
String query = "Any valid query";
|
||||
when(geminiService.generateContent(any(), any(), any(), any(), any())).thenReturn("INVALID_RESPONSE");
|
||||
|
||||
String result = messageEntryFilter.classifyMessage(query, null,null);
|
||||
assertEquals(MessageEntryFilter.CATEGORY_UNKNOWN, result);
|
||||
assertNotNull(getLogMessages().stream().filter(m -> m.contains("Gemini returned an unrecognised classification")).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return ERROR if Gemini service throws an exception")
|
||||
void classifyMessage_shouldReturnError_whenGeminiServiceThrowsException() throws Exception {
|
||||
String query = "Query causing error";
|
||||
when(geminiService.generateContent(any(), any(), any(), any(), any()))
|
||||
.thenThrow(new RuntimeException("Gemini API error"));
|
||||
|
||||
String result = messageEntryFilter.classifyMessage(query, null,null);
|
||||
assertEquals(MessageEntryFilter.CATEGORY_ERROR, result);
|
||||
assertNotNull(getLogMessages().stream().filter(m -> m.contains("Error during Gemini classification")).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should include notification context in prompt when provided and not blank")
|
||||
void classifyMessage_shouldIncludeNotificationContextInPrompt() throws Exception {
|
||||
String query = "What's up?";
|
||||
|
||||
when(geminiService.generateContent(any(String.class), anyFloat(), anyInt(), any(String.class), anyFloat()))
|
||||
.thenReturn("CONVERSATION");
|
||||
|
||||
messageEntryFilter.classifyMessage(query, NOTIFICATION_JSON_EXAMPLE,CONVERSATION_JSON_EXAMPLE);
|
||||
|
||||
verify(geminiService, times(1)).generateContent(
|
||||
org.mockito.ArgumentMatchers.argThat(prompt ->
|
||||
prompt.contains("Recent Notifications Context:") &&
|
||||
prompt.contains(NOTIFICATION_JSON_EXAMPLE) &&
|
||||
prompt.contains("User Input: 'What's up?'")
|
||||
),
|
||||
anyFloat(), anyInt(), any(String.class), anyFloat()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should NOT include notification context in prompt when provided but blank")
|
||||
void classifyMessage_shouldNotIncludeNotificationContextInPromptWhenBlank() throws Exception {
|
||||
String query = "What's up?";
|
||||
String notifications = " ";
|
||||
String conversations =" ";
|
||||
|
||||
when(geminiService.generateContent(any(String.class), anyFloat(), anyInt(), any(String.class), anyFloat()))
|
||||
.thenReturn("CONVERSATION");
|
||||
|
||||
messageEntryFilter.classifyMessage(query, notifications,conversations);
|
||||
|
||||
verify(geminiService, times(1)).generateContent(
|
||||
org.mockito.ArgumentMatchers.argThat(prompt ->
|
||||
!prompt.contains("Recent Notifications Context:") &&
|
||||
prompt.contains("User Input: 'What's up?'")
|
||||
),
|
||||
anyFloat(), anyInt(), any(String.class), anyFloat()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should NOT include notification context in prompt when null")
|
||||
void classifyMessage_shouldNotIncludeNotificationContextInPromptWhenNull() throws Exception {
|
||||
String query = "What's up?";
|
||||
String notifications = null;
|
||||
String conversations = null;
|
||||
|
||||
when(geminiService.generateContent(any(String.class), anyFloat(), anyInt(), any(String.class), anyFloat()))
|
||||
.thenReturn("CONVERSATION");
|
||||
|
||||
messageEntryFilter.classifyMessage(query, notifications, conversations);
|
||||
|
||||
verify(geminiService, times(1)).generateContent(
|
||||
org.mockito.ArgumentMatchers.argThat(prompt ->
|
||||
!prompt.contains("Recent Notifications Context:") &&
|
||||
prompt.contains("User Input: 'What's up?'")
|
||||
),
|
||||
anyFloat(), anyInt(), any(String.class), anyFloat()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package com.example.service.unit_testing;
|
||||
|
||||
import com.example.dto.dialogflow.base.DetectIntentResponseDTO;
|
||||
import com.example.dto.dialogflow.conversation.*;
|
||||
import com.example.dto.quickreplies.QuestionDTO;
|
||||
import com.example.dto.quickreplies.QuickReplyDTO;
|
||||
import com.example.mapper.conversation.ConversationEntryMapper;
|
||||
import com.example.service.conversation.ConversationManagerService;
|
||||
import com.example.service.conversation.FirestoreConversationService;
|
||||
import com.example.service.conversation.MemoryStoreConversationService;
|
||||
import com.example.service.quickreplies.QuickRepliesManagerService;
|
||||
import com.example.service.quickreplies.QuickReplyContentService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class QuickRepliesManagerServiceTest {
|
||||
|
||||
@Mock private MemoryStoreConversationService memoryStoreConversationService;
|
||||
@Mock private FirestoreConversationService firestoreConversationService;
|
||||
@Mock private QuickReplyContentService quickReplyContentService;
|
||||
@Mock private ConversationManagerService conversationManagerService;
|
||||
@Mock private ConversationEntryMapper conversationEntryMapper;
|
||||
|
||||
private QuickRepliesManagerService quickRepliesManagerService;
|
||||
|
||||
// Test Data
|
||||
private final String PHONE = "5555555555";
|
||||
private final String SESSION_ID = "session-123";
|
||||
private final String USER_ID = "user_by_phone_5555555555";
|
||||
private final String CONTEXTO = "pagos";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
quickRepliesManagerService = new QuickRepliesManagerService(
|
||||
conversationManagerService,
|
||||
memoryStoreConversationService,
|
||||
firestoreConversationService,
|
||||
quickReplyContentService,
|
||||
conversationEntryMapper
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("manageConversation - Count 0 - NO MATCH: Should clear context and delegate to Dialogflow")
|
||||
void manageConversation_Count0_NoMatch_ShouldDelegate() {
|
||||
// 1. SETUP: User typed "Hola", but context "pagos" is active.
|
||||
ExternalConvRequestDTO request = new ExternalConvRequestDTO("Hola", new UsuarioDTO(PHONE, "Nick"), "whatsapp", ConversationEntryType.CONVERSACION, null);
|
||||
ConversationSessionDTO session = new ConversationSessionDTO(SESSION_ID, USER_ID, PHONE, Instant.now(), Instant.now(), "last", CONTEXTO);
|
||||
|
||||
// Mock Session Retrieval
|
||||
when(memoryStoreConversationService.getSessionByTelefono(PHONE)).thenReturn(Mono.just(session));
|
||||
|
||||
// Mock History: Only the SYSTEM message (The Menu) exists. Count = 0.
|
||||
ConversationMessageDTO sysMsg = new ConversationMessageDTO(MessageType.SYSTEM, Instant.now(), "Menu...", null, "whatsapp");
|
||||
when(memoryStoreConversationService.getMessages(SESSION_ID)).thenReturn(Flux.just(sysMsg));
|
||||
|
||||
// Mock QR Content: The menu has options, but "Hola" is NOT one of them.
|
||||
QuestionDTO q1 = new QuestionDTO("Ver Saldo", "desc", "Tu saldo is 10");
|
||||
QuickReplyDTO qrDto = new QuickReplyDTO("Header", "Body", "Btn", "Section", List.of(q1));
|
||||
when(quickReplyContentService.getQuickReplies(CONTEXTO)).thenReturn(Mono.just(qrDto));
|
||||
|
||||
// Mock Orchestrator Delegation (The expected outcome)
|
||||
DetectIntentResponseDTO delegatedResponse = new DetectIntentResponseDTO("df-response", new QueryResultDTO("Hola soy Beto", null));
|
||||
when(memoryStoreConversationService.updateSession(any())).thenReturn(Mono.empty()); // Clearing context
|
||||
when(conversationManagerService.manageConversation(request)).thenReturn(Mono.just(delegatedResponse));
|
||||
|
||||
// 2. EXECUTE
|
||||
StepVerifier.create(quickRepliesManagerService.manageConversation(request))
|
||||
.expectNext(delegatedResponse)
|
||||
.verifyComplete();
|
||||
|
||||
// 3. VERIFY
|
||||
// Ensure we cleared the context
|
||||
verify(memoryStoreConversationService).updateSession(argThat(s -> s.pantallaContexto() == null));
|
||||
// Ensure we called the normal conversation manager
|
||||
verify(conversationManagerService).manageConversation(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("manageConversation - Count 0 - MATCH: Should return QR Answer")
|
||||
void manageConversation_Count0_Match_ShouldAnswer() {
|
||||
// 1. SETUP: User typed "Ver Saldo" (Valid Option)
|
||||
ExternalConvRequestDTO request = new ExternalConvRequestDTO("Ver Saldo", new UsuarioDTO(PHONE, "Nick"), "whatsapp", ConversationEntryType.CONVERSACION, null);
|
||||
ConversationSessionDTO session = new ConversationSessionDTO(SESSION_ID, USER_ID, PHONE, Instant.now(), Instant.now(), "last", CONTEXTO);
|
||||
|
||||
when(memoryStoreConversationService.getSessionByTelefono(PHONE)).thenReturn(Mono.just(session));
|
||||
|
||||
// Count 0 (Last message was System)
|
||||
ConversationMessageDTO sysMsg = new ConversationMessageDTO(MessageType.SYSTEM, Instant.now(), "Menu...", null, "whatsapp");
|
||||
when(memoryStoreConversationService.getMessages(SESSION_ID)).thenReturn(Flux.just(sysMsg));
|
||||
|
||||
// Valid Option exists
|
||||
QuestionDTO q1 = new QuestionDTO("Ver Saldo", "desc", "Tu saldo es 100 pesos");
|
||||
QuickReplyDTO qrDto = new QuickReplyDTO("Header", "Body", "Btn", "Section", List.of(q1));
|
||||
when(quickReplyContentService.getQuickReplies(CONTEXTO)).thenReturn(Mono.just(qrDto));
|
||||
|
||||
// Mocks for saving the conversation turn
|
||||
mockPersistence();
|
||||
when(memoryStoreConversationService.updateSession(any())).thenReturn(Mono.empty());
|
||||
|
||||
// 2. EXECUTE
|
||||
StepVerifier.create(quickRepliesManagerService.manageConversation(request))
|
||||
.assertNext(response -> {
|
||||
// Expect the pre-defined answer from the JSON
|
||||
assert response.queryResult().responseText().equals("Tu saldo es 100 pesos");
|
||||
})
|
||||
.verifyComplete();
|
||||
|
||||
// Verify we did NOT delegate
|
||||
verify(conversationManagerService, never()).manageConversation(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("manageConversation - Count 1 - NO MATCH: Should save User Msg, Clear, and Delegate")
|
||||
void manageConversation_Count1_NoMatch_ShouldDelegate() {
|
||||
// 1. SETUP: User typed "Gracias" (Not an option)
|
||||
ExternalConvRequestDTO request = new ExternalConvRequestDTO("Gracias", new UsuarioDTO(PHONE, "Nick"), "whatsapp", ConversationEntryType.CONVERSACION, null);
|
||||
ConversationSessionDTO session = new ConversationSessionDTO(SESSION_ID, USER_ID, PHONE, Instant.now(), Instant.now(), "last", CONTEXTO);
|
||||
|
||||
when(memoryStoreConversationService.getSessionByTelefono(PHONE)).thenReturn(Mono.just(session));
|
||||
|
||||
// Mock History: System msg -> User msg (Invalid) -> Now this is the 2nd user msg (Count 1 logic)
|
||||
// Wait, logic says count is messages AFTER last init.
|
||||
// Sys (Init) -> User("Hola" - ignored previously) -> Current Request("Gracias")
|
||||
ConversationMessageDTO sysMsg = new ConversationMessageDTO(MessageType.SYSTEM, Instant.now(), "Menu...", null, "whatsapp");
|
||||
ConversationMessageDTO userMsg1 = new ConversationMessageDTO(MessageType.USER, Instant.now(), "Hola", null, "whatsapp");
|
||||
when(memoryStoreConversationService.getMessages(SESSION_ID)).thenReturn(Flux.just(sysMsg, userMsg1));
|
||||
|
||||
QuestionDTO q1 = new QuestionDTO("Ver Saldo", "desc", "Tu saldo es 10");
|
||||
QuickReplyDTO qrDto = new QuickReplyDTO("Header", "Body", "Btn", "Section", List.of(q1));
|
||||
when(quickReplyContentService.getQuickReplies(CONTEXTO)).thenReturn(Mono.just(qrDto));
|
||||
|
||||
// Mock persistence for the user message (Manual save in the else block)
|
||||
mockPersistence();
|
||||
|
||||
// Mock Delegation
|
||||
DetectIntentResponseDTO delegatedResponse = new DetectIntentResponseDTO("df-response", new QueryResultDTO("De nada", null));
|
||||
when(memoryStoreConversationService.updateSession(any())).thenReturn(Mono.empty());
|
||||
when(conversationManagerService.manageConversation(request)).thenReturn(Mono.just(delegatedResponse));
|
||||
|
||||
// 2. EXECUTE
|
||||
StepVerifier.create(quickRepliesManagerService.manageConversation(request))
|
||||
.expectNext(delegatedResponse)
|
||||
.verifyComplete();
|
||||
|
||||
// 3. VERIFY
|
||||
// Ensure manual save was called (verifying flow of `persistConversationTurn`)
|
||||
verify(memoryStoreConversationService, times(1)).saveMessage(eq(SESSION_ID), any());
|
||||
verify(memoryStoreConversationService).updateSession(argThat(s -> s.pantallaContexto() == null));
|
||||
verify(conversationManagerService).manageConversation(request);
|
||||
}
|
||||
|
||||
// Helper to mock the complex save chain
|
||||
private void mockPersistence() {
|
||||
when(conversationEntryMapper.toConversationMessageDTO(any())).thenReturn(new ConversationMessageDTO(MessageType.USER, Instant.now(), "text", null, "wa"));
|
||||
when(memoryStoreConversationService.saveSession(any())).thenReturn(Mono.empty());
|
||||
when(memoryStoreConversationService.saveMessage(anyString(), any())).thenReturn(Mono.empty());
|
||||
when(firestoreConversationService.saveSession(any())).thenReturn(Mono.empty());
|
||||
when(firestoreConversationService.saveMessage(anyString(), any())).thenReturn(Mono.empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
|
||||
package com.example.service.unit_testing;
|
||||
|
||||
import com.example.dto.quickreplies.QuestionDTO;
|
||||
import com.example.dto.quickreplies.QuickReplyDTO;
|
||||
import com.example.service.quickreplies.QuickReplyContentService;
|
||||
import com.google.api.core.ApiFuture;
|
||||
import com.google.cloud.firestore.CollectionReference;
|
||||
import com.google.cloud.firestore.DocumentReference;
|
||||
import com.google.cloud.firestore.DocumentSnapshot;
|
||||
import com.google.cloud.firestore.Firestore;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class QuickReplyContentServiceTest {
|
||||
|
||||
@Mock
|
||||
private Firestore firestore;
|
||||
|
||||
@Mock
|
||||
private CollectionReference collectionReference;
|
||||
|
||||
@Mock
|
||||
private DocumentReference documentReference;
|
||||
|
||||
@Mock
|
||||
private ApiFuture<DocumentSnapshot> apiFuture;
|
||||
|
||||
@Mock
|
||||
private DocumentSnapshot documentSnapshot;
|
||||
|
||||
@InjectMocks
|
||||
private QuickReplyContentService quickReplyContentService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getQuickReplies_success() throws ExecutionException, InterruptedException {
|
||||
// Given
|
||||
String collectionId = "home";
|
||||
String header = "home_header";
|
||||
String body = "home_body";
|
||||
String button = "home_button";
|
||||
String headerSection = "home_header_section";
|
||||
List<Map<String, Object>> preguntas = Collections.singletonList(
|
||||
Map.of("titulo", "title", "descripcion", "description", "respuesta", "response")
|
||||
);
|
||||
List<QuestionDTO> questionDTOs = Collections.singletonList(
|
||||
new QuestionDTO("title", "description", "response")
|
||||
);
|
||||
QuickReplyDTO expected = new QuickReplyDTO(header, body, button, headerSection, questionDTOs);
|
||||
|
||||
when(firestore.collection("artifacts")).thenReturn(collectionReference);
|
||||
when(collectionReference.document("default-app-id")).thenReturn(documentReference);
|
||||
when(documentReference.collection("quick-replies")).thenReturn(collectionReference);
|
||||
when(collectionReference.document(collectionId)).thenReturn(documentReference);
|
||||
when(documentReference.get()).thenReturn(apiFuture);
|
||||
when(apiFuture.get()).thenReturn(documentSnapshot);
|
||||
when(documentSnapshot.exists()).thenReturn(true);
|
||||
when(documentSnapshot.getString("header")).thenReturn(header);
|
||||
when(documentSnapshot.getString("body")).thenReturn(body);
|
||||
when(documentSnapshot.getString("button")).thenReturn(button);
|
||||
when(documentSnapshot.getString("header_section")).thenReturn(headerSection);
|
||||
when(documentSnapshot.get("preguntas")).thenReturn(preguntas);
|
||||
|
||||
// When
|
||||
Mono<QuickReplyDTO> result = quickReplyContentService.getQuickReplies(collectionId);
|
||||
|
||||
// Then
|
||||
StepVerifier.create(result)
|
||||
.expectNext(expected)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getQuickReplies_emptyWhenNotFound() throws ExecutionException, InterruptedException {
|
||||
// Given
|
||||
String collectionId = "non-existent-collection";
|
||||
|
||||
when(firestore.collection("artifacts")).thenReturn(collectionReference);
|
||||
when(collectionReference.document("default-app-id")).thenReturn(documentReference);
|
||||
when(documentReference.collection("quick-replies")).thenReturn(collectionReference);
|
||||
when(collectionReference.document(collectionId)).thenReturn(documentReference);
|
||||
when(documentReference.get()).thenReturn(apiFuture);
|
||||
when(apiFuture.get()).thenReturn(documentSnapshot);
|
||||
when(documentSnapshot.exists()).thenReturn(false);
|
||||
|
||||
// When
|
||||
Mono<QuickReplyDTO> result = quickReplyContentService.getQuickReplies(collectionId);
|
||||
|
||||
// Then
|
||||
StepVerifier.create(result)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getQuickReplies_emptyWhenCollectionIdIsBlank() {
|
||||
// Given
|
||||
String collectionId = "";
|
||||
|
||||
// When
|
||||
Mono<QuickReplyDTO> result = quickReplyContentService.getQuickReplies(collectionId);
|
||||
|
||||
// Then
|
||||
StepVerifier.create(result)
|
||||
.expectNext(new QuickReplyDTO("empty", null, null, null, Collections.emptyList()))
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user