Unit tests on a CSV file upload

Asked

Viewed 430 times

2

Before explaining my problem I will show my CSV file upload code.

public String uploadArquivo() {

        try {

            removeInSession(LISTA_TIPO_INDICIOS);
            removeInSession(LISTA_INDICIOS);
            removeInSession(LISTA_ERRO);

            final File arquivoLeitura = new File(getArquivo());
            final LineNumberReader linhaLeitura =
                    new LineNumberReader(new FileReader(arquivoLeitura));
            linhaLeitura.skip(arquivoLeitura.length());
            final int qtdLinha = linhaLeitura.getLineNumber() + 1;

            final BufferedReader leitor = new BufferedReader(
                    new InputStreamReader(new FileInputStream(getArquivo())));

            String linha = null;

            leitor.readLine();

            for (int indiceIndicio = 2; indiceIndicio <= qtdLinha; indiceIndicio++) {
                statusMatricula = false;
                linha = leitor.readLine();

                if (linha == null) {
                    break;
                }

                final String[] dadosCSV = linha.split(VIRGULA);
                final int numberPositions = dadosCSV.length;

                setNumberPositions(numberPositions);
                if (!dadosCSV[TIPO_DO_INDICIO].isEmpty()) {
                    tipoIndicio = new TipoIndicioEntity();
                    tipoIndicio.setCodigo(
                            Integer.parseInt(dadosCSV[TIPO_DO_INDICIO]));
                    tipoIndicio = tipoDeIndicioService
                            .getPorId(tipoIndicio.getCodigo());
                }
                if (!dadosCSV[CODIGO_UJ].isEmpty()) {
                    uJ = new PessoaJuridicaPublicaEntity();
                    uJ.setCodigo(Long.parseLong(dadosCSV[CODIGO_UJ]));
                    uJ = pessoaJuridicaPublicaService.pesquisarPorId(uJ);
                }
                final ValidaCPF validadorCPF = new ValidaCPF();

                final Pattern pattern = Pattern.compile("^-?\\d*\\,\\d{2}$");
                final Matcher matcher = pattern.matcher(dadosCSV[VALOR]);

                if (!dadosCSV[NATUREZA].isEmpty()) {

                    naturezaIndicio
                            .setCodigo(Integer.parseInt(dadosCSV[NATUREZA]));
                    naturezaIndicio = naturezaIndicioService
                            .getPorId(naturezaIndicio.getCodigo());
                }
                if (!dadosCSV[CODIGO_DO_VINCULO].isEmpty()) {
                    tipoVinculo = new TipoVinculoEntity();
                    tipoVinculo.setCodigo(
                            Integer.parseInt(dadosCSV[CODIGO_DO_VINCULO]));
                    tipoVinculo = tipoVinculoService
                            .getPorId(tipoVinculo.getCodigo());
                }

                if (getNumberPositions() >= MATRICULA_PROPRIETARIO) {
                    if (!dadosCSV[MATRICULA_PROPRIETARIO].isEmpty()) {
                        usuarioCorporativo = new UsuarioCorporativoEntity();
                        usuarioCorporativo.setCodigoUsuario(Integer
                                .parseInt(dadosCSV[MATRICULA_PROPRIETARIO]));
                        usuarioCorporativo = usuarioCorporativoService.getPorId(
                                usuarioCorporativo.getCodigoUsuario());
                    }
                }

                final boolean expressaoInvalida = matcher.find() == false;
                final boolean cpfInvalido = !validadorCPF.isCPF(dadosCSV[CPF]);
                final boolean descricaoVazia = dadosCSV[DESCRICAO].isEmpty();
                final boolean naturezaInvalida = naturezaIndicio == null;
                boolean dataValidaEncontrada = false;

                if (!dadosCSV[DATA_ADMISSAO].equals("")) {

                    final SimpleDateFormat format =
                            new SimpleDateFormat("dd/MM/yyyy");

                    format.setLenient(false);
                    try {

                        if (dadosCSV[DATA_ADMISSAO] != null
                                && !dadosCSV[DATA_ADMISSAO].isEmpty()) {
                            final Date dataAdmissao =
                                    format.parse(dadosCSV[DATA_ADMISSAO]);
                        }

                    } catch (final Exception e) {
                        dataValidaEncontrada = true;
                    }

                }
                final int qtdData = dadosCSV[DATA_ADMISSAO].length();
                if (qtdData != 10) {
                    dataValidaEncontrada = true;
                }
                qtdMatriculo = dadosCSV[MATRICULA_PROPRIETARIO].length();

                final char letra_anterior =
                        dadosCSV[MATRICULA_PROPRIETARIO].charAt(0);
                final int vezes = 1;
                int qtdCaracterMatriculaProprietario = 0;
                for (qtdCaracterMatriculaProprietario =
                        1; qtdCaracterMatriculaProprietario < qtdMatriculo; qtdCaracterMatriculaProprietario++) {
                    final char letra_atual = dadosCSV[MATRICULA_PROPRIETARIO]
                            .charAt(qtdCaracterMatriculaProprietario);

                }

                if (qtdCaracterMatriculaProprietario != 4) {
                    statusMatricula = true;
                }

                if (getNumberPositions() <= 6) {
                    if (tipoIndicio == null
                            || dadosCSV[TIPO_DO_INDICIO].isEmpty() || uJ == null
                            || dadosCSV[CODIGO_UJ].isEmpty() || cpfInvalido
                            || descricaoVazia || expressaoInvalida
                            || naturezaIndicio == null) {
                        erroCSVList = ErroCSV.verificarErros6Linhas(dadosCSV,
                                tipoIndicio, uJ, naturezaIndicio,
                                expressaoInvalida, validadorCPF, erroCSVList,
                                indiceIndicio);
                        addInSession(LISTA_ERRO, erroCSVList);

                    } else {
                        verificarListaIndicios(listaTotalIndicio,
                                listaIndicioEntity, uJ, tipoIndicio, dadosCSV);
                    }
                } else if (getNumberPositions() > 6
                        && getNumberPositions() < 13) {
                    if (tipoIndicio == null
                            || dadosCSV[TIPO_DO_INDICIO].isEmpty() || uJ == null
                            || dadosCSV[CODIGO_UJ].isEmpty() || cpfInvalido
                            || dadosCSV[DESCRICAO].isEmpty()
                            || dadosCSV[DESCRICAO] == null || expressaoInvalida
                            || descricaoVazia || naturezaIndicio == null
                            || tipoVinculo == null || usuarioCorporativo == null
                            || statusMatricula) {

                        erroCSVList = ErroCSV.verificarErros12Linhas(dadosCSV,
                                tipoIndicio, uJ, naturezaIndicio,
                                expressaoInvalida, validadorCPF, erroCSVList,
                                indiceIndicio, dataValidaEncontrada,
                                tipoVinculo, usuarioCorporativo);
                        addInSession(LISTA_ERRO, erroCSVList);

                    } else {
                        verificarListaIndicios(listaTotalIndicio,
                                listaIndicioEntity, uJ, tipoIndicio, dadosCSV);
                    }
                } else if (getNumberPositions() >= 13) {

                    if (tipoIndicio == null
                            || dadosCSV[TIPO_DO_INDICIO].isEmpty() || uJ == null
                            || dadosCSV[CODIGO_UJ].isEmpty() || cpfInvalido
                            || dadosCSV[DESCRICAO].isEmpty()
                            || dadosCSV[DESCRICAO] == null || descricaoVazia
                            || expressaoInvalida || naturezaIndicio == null
                            || dadosCSV[NATUREZA].isEmpty()
                            || dataValidaEncontrada || tipoVinculo == null
                            || usuarioCorporativo == null || statusMatricula) {

                        erroCSVList = ErroCSV.verificarErros12Linhas(dadosCSV,
                                tipoIndicio, uJ, naturezaIndicio,
                                expressaoInvalida, validadorCPF, erroCSVList,
                                indiceIndicio, dataValidaEncontrada,
                                tipoVinculo, usuarioCorporativo);

                        addInSession(LISTA_ERRO, erroCSVList);

                    } else {
                        verificarListaIndicios(listaTotalIndicio,
                                listaIndicioEntity, uJ, tipoIndicio, dadosCSV);

                    }

                }

            }

            leitor.close();

        } catch (

        final FileNotFoundException e) {
            logger.error(e);
            erroInterno();
            e.printStackTrace();
        } catch (final IOException e) {
            logger.error(e);
            erroInterno();
            e.printStackTrace();
        } catch (final NegocioException e) {
            logger.error(e);
            erroInterno();
            e.printStackTrace();
        } catch (final PrincipalException e) {
            logger.error(e);
            erroInterno();
            e.printStackTrace();
        }

        return PAGE_IMPORTAR;
    }

When the user selects the CSV file and clicks to send, this method is triggered, this code just above is working perfectly. What I need is to create a unit test for this method. The goal of the unit test is to test these validations below;

if (getNumberPositions() <= 6) {
                    if (tipoIndicio == null
                            || dadosCSV[TIPO_DO_INDICIO].isEmpty() || uJ == null
                            || dadosCSV[CODIGO_UJ].isEmpty() || cpfInvalido
                            || descricaoVazia || expressaoInvalida
                            || naturezaIndicio == null) {
                        erroCSVList = ErroCSV.verificarErros6Linhas(dadosCSV,
                                tipoIndicio, uJ, naturezaIndicio,
                                expressaoInvalida, validadorCPF, erroCSVList,
                                indiceIndicio);
                        addInSession(LISTA_ERRO, erroCSVList);

                    } else {
                        verificarListaIndicios(listaTotalIndicio,
                                listaIndicioEntity, uJ, tipoIndicio, dadosCSV);
                    }

Now I’ll explain my difficulty;

When unit testing is done it is necessary to create false records to perform simulation of the resources of an application.

But instead of creating fake records I would like to use the data from the CSV file itself in the act of running the tests for uploading the CSV file.

The way the system would behave would be that at the time the user was uploading the method uploadfile the test would be triggered at the same time using data from the CSV file itself.

For lack of experience in unit testing I’m having difficulty implementing. The same data being used in the CSV file and being able to get these records in my test class, I just need help to get records like the archiveLeitura!

What comes in my getArch()

inserir a descrição da imagem aqui

  • 1

    What does the "getArchive()" method do? Just to understand what the input point of your code file is like.

  • 1

    Overall unit tests evaluate an expected output, not a block of code, your problem actually is that your file upload is all coupled with your validations, first you need to separate it from there

1 answer

2


First, you need to read the CSV file from the unit test.

A good practice is to leave these test files in the test Resources directory:

/src/test/resources/

Even better to create your own directory for Csvs:

/src/test/resources/csvs/

Now you need to read the files inside this directory. This utility class will break your branch:

public class LerCsvFile {

    public File getFile(String nomeArquivo) {
        ClassLoader classLoader = getClass().getClassLoader();
        URL resource = classLoader.getResource("csvs/" + nomeArquivo);
        return new File(resource.getPath());
    }

}

And to use in your unit test:

File csvFile = new LerCsvFile().getFile("meu_csv.csv");

Now, to use this in your code, it looks like you need to improve his organization a little bit. The first thing is to break this whole code into more classes. At the very least, you could create a separate class that gets the file. Thus, you can test unitarily with different files much easier.

Example:

public String uploadArquivo() {

    try {

        removeInSession(LISTA_TIPO_INDICIOS);
        removeInSession(LISTA_INDICIOS);
        removeInSession(LISTA_ERRO);

        final File arquivoLeitura = new File(getArquivo());

        LeituraCsvResultado leituraCSvResultado = new LeituraCsv(arquivoLeitura).ler();

        // depois preencher a session com os dados de LeituraCsvResultado 
        addInSession(LISTA_ERRO, leituraCSvResultado.getErroCSVList()); //algo assim

The code that begins in final LineNumberReader linhaLeitura... and goes up to leitor.close(); would stay in this new class LeituraCsv. Something like that:

class LeituraCsv {

        private File arquivoLeitura;

        LeituraCsv(File arquivoLeitura) {
            this.arquivoLeitura = arquivoLeitura;
        }

        public LeituraCsvResultado ler() {
            final LineNumberReader linhaLeitura =
                    new LineNumberReader(new FileReader(arquivoLeitura));
            // restante do código

            // preencher LeituraCsvResultado com as listas que vão na sua sessions
            return new LeituraCsvResultado(); 
        }

}

This way, you can test unitarily Leituracsvresultado with different files and analyzing the return of Leituracsvresultado, making the necessary asserts. Example:

class LeituraCsvResultadoTest {

    public void ler() {
        File arquivo = new LerCsvFile().getFile("meu_csv.csv");
        LeituraCsvResultado resultado = new LeituraCsv(arquivo).ler(); 
        // fazer os asserts em LeituraCsvResultado 
    }

}

Tip: you will get even better tests and codes if you break all the logic in more classes yet, as it seems that your code can be improved by following this path.

  • Dude, awesome, just looking at the suggestion already feel like it’s gonna work, I’m gonna need some time to test your management to see if it’s gonna work. working I will already color as solved. thank you very much.

  • @Dherick looking more calmly, I’m having a little trouble understanding if your suggestion is all in the test class or part of it is in the class where you’re implementing the uploadfile method, my other difficulty is to be able to navigate the correct way to find the csv file using in the folder. I can’t find the Milking method implemented in your suggestion, I just ask that you have a little patience with me.

  • 1

    @wladyband, I edited the answer to make it clearer, there were some mistakes. The Leituracsv class is a normal class, where it receives the file in the constructor and reads it, you need to create it. Part of your existing code needs to be moved into it. It will return through the method "read" the result, which will use to fill the Session. The test is basically what I put even more the asserts you can do to check the expected result

  • 1

    Thank you @Dhreck, no exaggeration, but you will participate in my training as a programmer, now it’s become clearer to me, I will strive to work, thanks even.

  • @wladyband I’ll wait to quote myself in the TCC thanks. Joke :). I’m glad I helped, hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.