134 lines
5.2 KiB
Java
134 lines
5.2 KiB
Java
|
|
package com.example.mapper.conversation;
|
|
|
|
import com.example.dto.dialogflow.base.DetectIntentRequestDTO;
|
|
import com.example.dto.dialogflow.conversation.QueryInputDTO;
|
|
import com.example.dto.dialogflow.conversation.QueryParamsDTO;
|
|
import com.example.dto.dialogflow.conversation.TextInputDTO;
|
|
import com.example.dto.dialogflow.notification.EventInputDTO;
|
|
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
|
|
import com.google.cloud.dialogflow.cx.v3.QueryInput;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
import org.mockito.InjectMocks;
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
import java.util.Collections;
|
|
import java.util.Map;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
class DialogflowRequestMapperTest {
|
|
|
|
@InjectMocks
|
|
private DialogflowRequestMapper dialogflowRequestMapper;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
ReflectionTestUtils.setField(dialogflowRequestMapper, "defaultLanguageCode", "es");
|
|
}
|
|
|
|
@Test
|
|
void mapToDetectIntentRequestBuilder_withTextInput_shouldMapCorrectly() {
|
|
// Given
|
|
TextInputDTO textInputDTO = new TextInputDTO("Hola");
|
|
QueryInputDTO queryInputDTO = new QueryInputDTO(textInputDTO, null, "es");
|
|
DetectIntentRequestDTO requestDTO = new DetectIntentRequestDTO(queryInputDTO, null);
|
|
|
|
// When
|
|
DetectIntentRequest.Builder builder = dialogflowRequestMapper.mapToDetectIntentRequestBuilder(requestDTO);
|
|
DetectIntentRequest request = builder.build();
|
|
|
|
// Then
|
|
assertNotNull(request);
|
|
assertTrue(request.hasQueryInput());
|
|
QueryInput queryInput = request.getQueryInput();
|
|
assertEquals("es", queryInput.getLanguageCode());
|
|
assertTrue(queryInput.hasText());
|
|
assertEquals("Hola", queryInput.getText().getText());
|
|
assertFalse(queryInput.hasEvent());
|
|
}
|
|
|
|
@Test
|
|
void mapToDetectIntentRequestBuilder_withEventInput_shouldMapCorrectly() {
|
|
// Given
|
|
EventInputDTO eventInputDTO = new EventInputDTO("welcome_event");
|
|
QueryInputDTO queryInputDTO = new QueryInputDTO(null, eventInputDTO, "es");
|
|
DetectIntentRequestDTO requestDTO = new DetectIntentRequestDTO(queryInputDTO, null);
|
|
|
|
// When
|
|
DetectIntentRequest.Builder builder = dialogflowRequestMapper.mapToDetectIntentRequestBuilder(requestDTO);
|
|
DetectIntentRequest request = builder.build();
|
|
|
|
// Then
|
|
assertNotNull(request);
|
|
assertTrue(request.hasQueryInput());
|
|
QueryInput queryInput = request.getQueryInput();
|
|
assertEquals("es", queryInput.getLanguageCode());
|
|
assertTrue(queryInput.hasEvent());
|
|
assertEquals("welcome_event", queryInput.getEvent().getEvent());
|
|
assertFalse(queryInput.hasText());
|
|
}
|
|
|
|
@Test
|
|
void mapToDetectIntentRequestBuilder_withNoInput_shouldThrowException() {
|
|
// Given
|
|
QueryInputDTO queryInputDTO = new QueryInputDTO(null, null, "es");
|
|
DetectIntentRequestDTO requestDTO = new DetectIntentRequestDTO(queryInputDTO, null);
|
|
|
|
// When & Then
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
dialogflowRequestMapper.mapToDetectIntentRequestBuilder(requestDTO);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
void mapToDetectIntentRequestBuilder_withParameters_shouldMapCorrectly() {
|
|
// Given
|
|
TextInputDTO textInputDTO = new TextInputDTO("Hola");
|
|
QueryInputDTO queryInputDTO = new QueryInputDTO(textInputDTO, null, "es");
|
|
Map<String, Object> parameters = Collections.singletonMap("param1", "value1");
|
|
QueryParamsDTO queryParamsDTO = new QueryParamsDTO(parameters);
|
|
DetectIntentRequestDTO requestDTO = new DetectIntentRequestDTO(queryInputDTO, queryParamsDTO);
|
|
|
|
// When
|
|
DetectIntentRequest.Builder builder = dialogflowRequestMapper.mapToDetectIntentRequestBuilder(requestDTO);
|
|
DetectIntentRequest request = builder.build();
|
|
|
|
// Then
|
|
assertNotNull(request);
|
|
assertTrue(request.hasQueryParams());
|
|
assertTrue(request.getQueryParams().hasParameters());
|
|
assertEquals("value1", request.getQueryParams().getParameters().getFieldsMap().get("param1").getStringValue());
|
|
}
|
|
|
|
@Test
|
|
void mapToDetectIntentRequestBuilder_withNullRequestDTO_shouldThrowException() {
|
|
// When & Then
|
|
assertThrows(NullPointerException.class, () -> {
|
|
dialogflowRequestMapper.mapToDetectIntentRequestBuilder(null);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
void mapToDetectIntentRequestBuilder_withDefaultLanguageCode_shouldMapCorrectly() {
|
|
// Given
|
|
TextInputDTO textInputDTO = new TextInputDTO("Hola");
|
|
QueryInputDTO queryInputDTO = new QueryInputDTO(textInputDTO, null, null);
|
|
DetectIntentRequestDTO requestDTO = new DetectIntentRequestDTO(queryInputDTO, null);
|
|
|
|
// When
|
|
DetectIntentRequest.Builder builder = dialogflowRequestMapper.mapToDetectIntentRequestBuilder(requestDTO);
|
|
DetectIntentRequest request = builder.build();
|
|
|
|
// Then
|
|
assertNotNull(request);
|
|
assertTrue(request.hasQueryInput());
|
|
assertEquals("es", request.getQueryInput().getLanguageCode());
|
|
}
|
|
}
|