125 lines
4.5 KiB
Java
125 lines
4.5 KiB
Java
|
|
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();
|
|
}
|
|
}
|