Commit df1dadd8 authored by huriarte's avatar huriarte

Pruebas Unitarias para Servicio de Validación

parent e962d691
......@@ -12,4 +12,11 @@
<dockerfile.skip>true</dockerfile.skip>
</properties>
</action>
<action>
<actionName>CUSTOM-basic cobertura</actionName>
<displayName>basic cobertura</displayName>
<goals>
<goal>cobertura:cobertura</goal>
</goals>
</action>
</actions>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.bean.AgentBean;
import com.bytesw.bytebot.bean.ChannelBean;
import com.bytesw.bytebot.bean.CountryBean;
import com.bytesw.bytebot.model.Agent;
import com.bytesw.bytebot.model.Channel;
import com.bytesw.bytebot.model.ChannelParam;
import com.bytesw.bytebot.model.Country;
import com.bytesw.bytebot.model.DeploymentChannel;
import com.bytesw.bytebot.model.DeploymentChannelParamValue;
import com.bytesw.bytebot.model.FrequentQuestion;
import com.bytesw.bytebot.model.QuestionFile;
import com.bytesw.bytebot.model.enums.AgentStatusEnum;
import com.bytesw.bytebot.model.enums.AgentTypeEnum;
import com.bytesw.bytebot.model.enums.FrequentQuestionStatusEnum;
import com.bytesw.bytebot.model.enums.LanguageEnum;
import com.bytesw.bytebot.model.enums.StatusEnum;
import com.bytesw.bytebot.repository.AgentRepository;
import com.bytesw.bytebot.repository.ChannelParamRepository;
import com.bytesw.bytebot.repository.ChannelRepository;
import com.bytesw.bytebot.repository.CountryRepository;
import com.bytesw.bytebot.repository.DeploymentChannelParamValueRepository;
import com.bytesw.bytebot.repository.DeploymentChannelRepository;
import com.bytesw.bytebot.repository.FrequentQuestionRepository;
import com.bytesw.bytebot.repository.QuestionFileRepository;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.camel.ProducerTemplate;
import static org.assertj.core.api.Java6Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.any;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Hernán Uriarte Melchor
* @version 14/09/18.
* <p>
* <p>
* Copyright (c) 2018 Byte, S.A. Todos los derechos reservados.
* <p>
* Este software constituye información confidencial y propietaria de Byte, S.A.
* ("Información Confidencial"). Usted no debe develar dicha Información
* Confidencial y debe usarla de acuerdo con los términos de aceptación de
* licencia de uso que firmó con Byte.
*/
@RunWith(SpringRunner.class)
public class AgentServiceTest {
@TestConfiguration
static class AgentServiceTestContextConfiguration {
@MockBean
private AgentRepository agentRepository;
@MockBean
private CountryRepository countryRepository;
@MockBean
private DeploymentChannelRepository deploymentChannelRepository;
@MockBean
private DeploymentChannelParamValueRepository deploymentChannelParamValueRepository;
@MockBean
private ChannelRepository channelRepository;
@MockBean
private ChannelParamRepository channelParamRepository;
@MockBean
private FrequentQuestionRepository frequentQuestionRepository;
@MockBean
private QuestionFileRepository questionFileRepository;
@MockBean
private ProducerTemplate producerTemplate;
@Bean
public AgentService agentService() {
return new AgentService();
}
}
@Autowired
private AgentService agentService;
@Autowired
private ChannelRepository channelRepository;
@Autowired
private CountryRepository countryRepository;
@Autowired
private AgentRepository agentRepository;
@Autowired
private FrequentQuestionRepository frequentQuestionRepository;
@Autowired
private QuestionFileRepository questionFileRepository;
@Autowired
private ProducerTemplate producerTemplate;
@Before
public void setUp() {
}
@Test
public void getAgent_whenAgentNotExists_OK() {
Long idAgente = 1L;
Mockito.when(agentRepository.findById(idAgente))
.thenReturn(Optional.ofNullable(null));
AgentBean agentBean = agentService.getAgent(idAgente);
assertThat(agentBean).isNull();
}
@Test
public void getAgent_whenAgentExists_OK() {
Long idAgente = 1L;
Long idCountry = 1L;
Long idFrequentQuestion = 1L;
Agent agentA = new Agent();
agentA.setId(idAgente);
agentA.setName("BOT-1");
agentA.setDescription("BOT BASICO DE PREGUNTAS");
agentA.setVersion("0.0.1");
agentA.setLanguage(LanguageEnum.SPANISH);
agentA.setTimezone("GMT-5");
agentA.setType(AgentTypeEnum.FREQUENT_QUESTION);
agentA.setStatus(AgentStatusEnum.CREATED);
agentA.setAvatar("avatar-png");
Country countryA = new Country();
countryA.setId(idCountry);
countryA.setName("Perú");
agentA.setCountry(countryA);
List<FrequentQuestion> frequentQuestions = new ArrayList<>();
FrequentQuestion frequentQuestionA = new FrequentQuestion();
frequentQuestionA.setId(idFrequentQuestion);
frequentQuestionA.setDescription("Preguntas de entrenamiento");
frequentQuestionA.setUser("lortiz");
frequentQuestionA.setFilename("preguntas-base-old.xls");
frequentQuestionA.setUploadDate(LocalDateTime.now());
frequentQuestionA.setStatus(FrequentQuestionStatusEnum.PENDING_SYNCHRONIZED);
frequentQuestions.add(frequentQuestionA);
agentA.setFrequentQuestions(frequentQuestions);
List<DeploymentChannel> deploymentChannels = new ArrayList<>();
DeploymentChannel deploymentChannelA = new DeploymentChannel();
deploymentChannelA.setId(1L);
deploymentChannelA.setName("FACEBOOK");
deploymentChannelA.setStatus(StatusEnum.ACTIVE);
Channel channelA = new Channel();
channelA.setId(1L);
channelA.setName("FACEBOOK");
deploymentChannelA.setChannel(channelA);
List<DeploymentChannelParamValue> parameters = new ArrayList<>();
ChannelParam channelParamA = new ChannelParam();
channelParamA.setName("token");
DeploymentChannelParamValue parameterA = new DeploymentChannelParamValue();
parameterA.setId(1L);
parameterA.setValue("facebook-abc");
parameterA.setParameter(channelParamA);
DeploymentChannelParamValue parameterB = new DeploymentChannelParamValue();
parameterB.setId(2L);
parameterB.setValue("https://facebook....");
parameters.add(parameterA);
parameters.add(parameterB);
deploymentChannelA.setParameters(parameters);
DeploymentChannel deploymentChannelB = new DeploymentChannel();
deploymentChannelB.setId(2L);
deploymentChannelB.setName("WHATSAPP");
deploymentChannelB.setStatus(StatusEnum.INACTIVE);
deploymentChannelB.setParameters(new ArrayList<>());
deploymentChannels.add(deploymentChannelA);
deploymentChannels.add(deploymentChannelB);
agentA.setDeploymentChannels(deploymentChannels);
Mockito.when(agentRepository.findById(idAgente)).thenReturn(Optional.ofNullable(agentA));
AgentBean agentBean = agentService.getAgent(idAgente);
assertThat(agentBean.getId()).isEqualTo(idAgente);
}
@Test
public void delete_whenAgentExists_OK() {
Long idAgente = 1L;
Agent agentA = new Agent();
agentA.setId(idAgente);
agentA.setStatus(AgentStatusEnum.PENDING_SYNCHRONIZED);
Mockito.when(agentRepository.findById(idAgente)).thenReturn(Optional.ofNullable(agentA));
Mockito.when(agentRepository.save(any(Agent.class))).thenReturn(agentA);
boolean isValid = agentService.delete(idAgente);
assertThat(isValid).isEqualTo(Boolean.TRUE);
}
@Test
public void delete_whenAgentNotExists_OK() {
Long idAgente = null;
boolean isValid = agentService.delete(idAgente);
assertThat(isValid).isEqualTo(Boolean.TRUE);
}
@Test
public void delete_whenAgentNotExistsById_OK() {
Long idAgente = 1L;
Agent agentA = null;
Mockito.when(agentRepository.findById(idAgente)).thenReturn(Optional.ofNullable(agentA));
boolean isValid = agentService.delete(idAgente);
assertThat(isValid).isEqualTo(Boolean.TRUE);
}
@Test
public void getCountries_whenCountries_areOK() {
List<Country> countries = new ArrayList<>();
Country countryA = new Country();
Country countryB = new Country();
countries.add(countryA);
countries.add(countryB);
Mockito.when(countryRepository.findAll()).thenReturn(countries);
List<CountryBean> countriesBeanList = agentService.getCountries();
assertThat(countriesBeanList.size()).isEqualTo(2);
}
@Test
public void getChannels_whenChannels_areOK() {
List<Channel> channels = new ArrayList<>();
Channel channelA = new Channel();
Channel channelB = new Channel();
channels.add(channelA);
channels.add(channelB);
Mockito.when(channelRepository.findAll()).thenReturn(channels);
List<ChannelBean> channelsBeanList = agentService.getChannels();
assertThat(channelsBeanList.size()).isEqualTo(2);
}
@Test
public void synchronizeFiles_whenQuestionFileIsNotPresent_OK() {
Long idAgente = 1L;
String user = "lortiz";
String uuid = "1f59cb41-778a-46f6-acfc-3625108275bb";
List<FrequentQuestion> frequentQuestions = new ArrayList<>();
FrequentQuestion frequentQuestionA = new FrequentQuestion();
frequentQuestionA.setId(idAgente);
frequentQuestionA.setUuid(uuid);
frequentQuestions.add(frequentQuestionA);
Agent agentA = new Agent();
agentA.setId(idAgente);
agentA.setStatus(AgentStatusEnum.PENDING_SYNCHRONIZED);
Mockito.when(frequentQuestionRepository.findAllByAgentIdAndStatus(idAgente, FrequentQuestionStatusEnum.PENDING_SYNCHRONIZED))
.thenReturn(frequentQuestions);
Mockito.when(questionFileRepository.findByUuid(uuid))
.thenReturn(Optional.ofNullable(null));
Mockito.when(agentRepository.findById(idAgente))
.thenReturn(Optional.ofNullable(agentA));
agentService.synchronizeFiles(idAgente, user);
//@HU como validar un void method
}
@Test
public void synchronizeFiles_whenQuestionFileIsPresentButNotAgent_OK() {
Long idAgente = 1L;
String user = "lortiz";
String uuid = "1f59cb41-778a-46f6-acfc-3625108275bb";
List<FrequentQuestion> frequentQuestions = new ArrayList<>();
FrequentQuestion frequentQuestionA = new FrequentQuestion();
frequentQuestionA.setId(idAgente);
frequentQuestionA.setUuid(uuid);
frequentQuestions.add(frequentQuestionA);
QuestionFile questionFileA = new QuestionFile();
questionFileA.setName("preguntas-basicas-old");
Mockito.when(frequentQuestionRepository.findAllByAgentIdAndStatus(idAgente, FrequentQuestionStatusEnum.PENDING_SYNCHRONIZED))
.thenReturn(frequentQuestions);
Mockito.when(questionFileRepository.findByUuid(uuid))
.thenReturn(Optional.ofNullable(questionFileA));
Mockito.when(agentRepository.findById(idAgente))
.thenReturn(Optional.ofNullable(null));
agentService.synchronizeFiles(idAgente, user);
//@HU como validar un void method
}
}
......@@ -6,17 +6,19 @@
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.http.FileValidationResponse;
import com.bytesw.bytebot.http.FileValidationResult;
import com.bytesw.bytebot.http.enums.ValidationStatusEnum;
import com.bytesw.bytebot.model.QuestionFile;
import com.bytesw.bytebot.repository.QuestionFileRepository;
import java.awt.MediaTracker;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import static org.assertj.core.api.Java6Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.any;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
......@@ -54,13 +56,16 @@ public class FileManagementServiceTest {
}
@Autowired
FileManagementService fileManagementService;
private FileManagementService fileManagementService;
@Autowired
private QuestionFileRepository questionFileRepository;
@Before
public void setUp() {
}
@Test
public void validateAndSaveFile_whenFileHasInvalidExtension_SimpleError() throws IOException {
InputStream inputData = new FileInputStream("src/test/resources/data/preguntas-ejemplo-sin-columna-doble-columna.xls");
......@@ -70,13 +75,12 @@ public class FileManagementServiceTest {
"preguntas-ejemplo-sin-columna-doble-columna.xml",
MediaType.APPLICATION_XML_VALUE,
inputData);
FileValidationResponse response = fileManagementService.validateAndSaveFile(uuid, xlsFileHeadersRepetead);
assertThat(response.getFileValidationResult().getStatus())
.isEqualTo(ValidationStatusEnum.INCOMPATIBLE_EXTENSION);
}
@Test
public void validateAndSaveFile_whenFileHasHeaderRepetead_ErrorWithDetails() throws IOException {
......@@ -87,10 +91,53 @@ public class FileManagementServiceTest {
"preguntas-ejemplo-sin-columna-doble-columna.xls",
"application/vnd.ms-excel",
inputData);
FileValidationResponse response = fileManagementService.validateAndSaveFile(uuid, xlsFileHeadersRepetead);
assertThat(response.getFileValidationResult().getStatus())
.isEqualTo(ValidationStatusEnum.HEADER_ERROR);
}
@Test
public void validateAndSaveFile_whenFileHasBadContent_ErrorWithDetails() throws IOException {
InputStream inputData = new FileInputStream("src/test/resources/data/preguntas-ejemplo-sin-contenido-espacios-blanco.xlsx");
String uuid = "1f59cb41-778a-46f6-acfc-3625108275bb";
MultipartFile xlsFileBadContent = new MockMultipartFile(
"preguntas-ejemplo-sin-contenido-espacios-blanco.xlsx",
"preguntas-ejemplo-sin-contenido-espacios-blanco.xlsx",
"application/vnd.ms-excel",
inputData);
FileValidationResponse response = fileManagementService.validateAndSaveFile(uuid, xlsFileBadContent);
assertThat(response.getFileValidationResult().getStatus())
.isEqualTo(ValidationStatusEnum.CONTENT_ERROR);
}
@Test
public void validateAndSaveFile_whenFileIsCorrect_thenIsSaved() throws IOException {
InputStream inputData = new FileInputStream("src/test/resources/data/preguntas-ejemplo-base.xlsx");
String uuid = "1f59cb41-778a-46f6-acfc-3625108275bb";
MultipartFile xlsFile = new MockMultipartFile(
"preguntas-ejemplo-base.xlsx",
"preguntas-ejemplo-base.xlsx",
"application/vnd.ms-excel",
inputData);
QuestionFile questionFileBD = new QuestionFile();
questionFileBD.setUuid(uuid);
questionFileBD.setName(xlsFile.getOriginalFilename());
questionFileBD.setSize(xlsFile.getSize());
questionFileBD.setUploadDate(LocalDateTime.now());
questionFileBD.setData(xlsFile.getBytes());
Mockito.when(questionFileRepository.save(any(QuestionFile.class)))
.thenReturn(questionFileBD);
FileValidationResponse response = fileManagementService.validateAndSaveFile(uuid, xlsFile);
assertThat(response.getFileValidationResult().getStatus())
.isEqualTo(ValidationStatusEnum.OK);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment