Commit eeef89c5 authored by huriarte's avatar huriarte

Pruebas Unitarias para Subida de Archivos

parent 0631a1b3
This diff is collapsed.
/*
* 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.controller;
import com.bytesw.bytebot.service.AgentService;
import com.bytesw.bytebot.service.FileManagementService;
import com.bytesw.xdf.config.security.annotation.DummyPermissionEvaluator;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Hernán Uriarte Melchor
* @date 14/09/2020.
* <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(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebMvcTest(controllers = AgentController.class)
@ActiveProfiles(value = "test")
@AutoConfigureMockMvc(secure = false)
public class AgentControllerTest {
public static String baseService = "/service/agent";
@TestConfiguration
static class UserServiceTestContextConfiguration {
@Bean
DummyPermissionEvaluator dummyPermissionEvaluator() {
return new DummyPermissionEvaluator();
}
}
@Autowired
private MockMvc mvc;
@MockBean
private AgentService agentService;
@MockBean
private FileManagementService fileManagementService;
@Before
public void setUp() {
}
@Test
@WithMockUser(username="admin",roles={"USER","ADMIN"})
public void uploadFile_whenFileXLS_isOK() throws Exception {
MockMultipartFile xlsFile = new MockMultipartFile("file", "my-xls-file.xls", "application/vnd.ms-excel",
"my-xls-file".getBytes());
mvc.perform(MockMvcRequestBuilders.multipart(baseService + "/file-upload")
.file(xlsFile))
.andExpect(status().isOk());
}
@Test
@WithMockUser(username="admin",roles={"USER","ADMIN"})
public void uploadFile_whenFileXLSNotAppropriate_throwException() throws Exception {
MockMultipartFile xlsFile = new MockMultipartFile("file", "my-xls-file.xls", "application/vnd.ms-excel",
"my-xls-file".getBytes());
Mockito.when(fileManagementService.validateAndSaveFile(anyString(), eq(xlsFile) ))
.thenThrow(IOException.class);
mvc.perform(MockMvcRequestBuilders.multipart(baseService + "/file-upload")
.file(xlsFile))
.andExpect(status().isInternalServerError());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--<import resource="classpath:applicationContext-aop.xml" />-->
<import resource="classpath:applicationContext-scheduler.xml" />
<!-- MyBatis stuff -->
<!--
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml" />
</bean>
-->
</beans>
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
server:
servlet.context-path: /tenant-server
port: 9443
ssl:
key-store-type: JKS
key-store: 'classpath:keystore.jks'
key-store-password: changeit
key-alias: tomcat
security:
require-ssl: true
basic:
enabled: false
application:
services:
security: none
api.info:
title: 'byteXDF4Java Service'
description: 'API para el desarrollo de software'
version: '1.0.0'
terms-of-service-url: 'https://es.wikipedia.org/wiki/byteXDF4Java'
license: 'Open source licensing'
license-url: 'https://help.github.com/articles/open-source-licensing/'
spring:
application:
name: tenant-server
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
username: sa
password: sa
minimum-idle: 10
maximum-pool-size: 10
validationQuery: SELECT 1
testWhileIdle: true
hikari.registerMbeans: true
jpa:
properties.hibernate.dialect: org.hibernate.dialect.H2Dialect
show-sql: true
logging.level.root: info
logging.pattern.console: '%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n'
logging.level.com.bytesw: debug
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="2" />
<property name="maxPoolSize" value="5" />
<property name="queueCapacity" value="5" />
<property name="waitForTasksToCompleteOnShutdown" value="true" />
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>
</property>
</bean>
</beans>
\ No newline at end of file
CREATE TABLE SEQUENCE_TABLE
(SEQ_NAME VARCHAR(50) NOT NULL,
SEQ_COUNT INTEGER DEFAULT 0,
PRIMARY KEY (SEQ_NAME));
INSERT SEQUENCE_TABLE (SEQ_NAME, SEQ_COUNT) VALUES ('USER_SEQ', 0);
\ No newline at end of file
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