<commit_before>package br.net.mirante.singular.form.document;

import br.net.mirante.singular.form.PackageBuilder;
import br.net.mirante.singular.form.RefService;
import br.net.mirante.singular.form.SIComposite;
import br.net.mirante.singular.form.STypeComposite;
import br.net.mirante.singular.form.TestCaseForm;
import br.net.mirante.singular.form.type.core.attachment.AttachmentTestUtil;
import br.net.mirante.singular.form.type.core.attachment.IAttachmentPersistenceHandler;
import br.net.mirante.singular.form.type.core.attachment.IAttachmentRef;
import br.net.mirante.singular.form.type.core.attachment.SIAttachment;
import br.net.mirante.singular.form.type.core.attachment.STypeAttachment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Matchers;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(Parameterized.class)
public class TestSDocumentPersistentServices extends TestCaseForm {

    private STypeComposite<?> groupingType;
    private SIAttachment fileFieldInstance;
    private SDocument document;
    private IAttachmentPersistenceHandler tempHandler, persistentHandler;

    public TestSDocumentPersistentServices(TestFormConfig testFormConfig) {
        super(testFormConfig);
    }

    @Before
    public void setup() {
        createTypes(createTestDictionary().createNewPackage("teste"));
        createInstances();
        setupServices();

    }

    private void createTypes(PackageBuilder pb) {
        groupingType = pb.createCompositeType("Grouping");
        groupingType.addField("anexo", STypeAttachment.class);
        groupingType.addFieldInteger("justIgnoreThis");
    }

    private void createInstances() {
        SIComposite instance = (SIComposite) groupingType.newInstance();
        fileFieldInstance = (SIAttachment) instance.getAllChildren().iterator().next();
    }

    private void setupServices() {
        document = fileFieldInstance.getDocument();

        tempHandler = mock(IAttachmentPersistenceHandler.class);
        persistentHandler = mock(IAttachmentPersistenceHandler.class);
        document.setAttachmentPersistenceTemporaryHandler(RefService.of(tempHandler));
        document.bindLocalService("filePersistence",
                IAttachmentPersistenceHandler.class, RefService.of(persistentHandler));
    }

    @Test
    public void deveMigrarOsAnexosParaAPersistencia() throws IOException {
        fileFieldInstance.setFileId("abacate");

        byte[] content = new byte[]{0};

        when(tempHandler.getAttachment("abacate"))
                .thenReturn(attachmentRef("abacate", content));
        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(attachmentRef("abacate", content));

        document.persistFiles();
        verify(persistentHandler).addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length);
    }

    @Test
    public void armazenaOValorDoNovoId() throws IOException {
        fileFieldInstance.setFileId("abacate");

        byte[] content = new byte[]{0};

        when(tempHandler.getAttachment("abacate"))
                .thenReturn(attachmentRef("abacate", content));
        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(attachmentRef("avocado", content));

        document.persistFiles();
        assertThat(fileFieldInstance.getFileId()).isEqualTo("avocado");
        assertThat(fileFieldInstance.getOriginalFileId()).isEqualTo("avocado");
    }

    @Test
    public void deveApagarOTemporarioAposInserirNoPersistente() throws IOException {
        fileFieldInstance.setFileId("abacate");

        byte[] content = new byte[]{0};

        when(tempHandler.getAttachment("abacate"))
                .thenReturn(attachmentRef("abacate", content));
        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(attachmentRef("abacate", content));

        document.persistFiles();
        verify(tempHandler).deleteAttachment("abacate");
    }

    @Test
    public void deveApagarOPersistenteSeEsteSeAlterou() throws IOException {
        fileFieldInstance.setFileId("abacate");
        fileFieldInstance.setOriginalFileId("avocado");

        byte[] content = new byte[]{0};

        when(tempHandler.getAttachment("abacate"))
                .thenReturn(attachmentRef("abacate", content));
        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(attachmentRef("abacate", content));

        document.persistFiles();
        verify(persistentHandler).deleteAttachment("avocado");
    }

    @Test
    public void naoApagaNadaSeNenhumArquivoFoiAlterado() {
        fileFieldInstance.setFileId("abacate");
        fileFieldInstance.setOriginalFileId("abacate");

        document.persistFiles();
        verify(persistentHandler, never()).deleteAttachment(Matchers.any());
        verify(tempHandler, never()).deleteAttachment(Matchers.any());<commit_msg>[SINGULAR-FORM] correção dos testes de persistencia<commit_after>package br.net.mirante.singular.form.document;

import br.net.mirante.singular.form.PackageBuilder;
import br.net.mirante.singular.form.RefService;
import br.net.mirante.singular.form.SIComposite;
import br.net.mirante.singular.form.STypeComposite;
import br.net.mirante.singular.form.TestCaseForm;
import br.net.mirante.singular.form.type.core.attachment.AttachmentTestUtil;
import br.net.mirante.singular.form.type.core.attachment.IAttachmentPersistenceHandler;
import br.net.mirante.singular.form.type.core.attachment.IAttachmentRef;
import br.net.mirante.singular.form.type.core.attachment.SIAttachment;
import br.net.mirante.singular.form.type.core.attachment.STypeAttachment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Matchers;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(Parameterized.class)
public class TestSDocumentPersistentServices extends TestCaseForm {

    private STypeComposite<?> groupingType;
    private SIAttachment fileFieldInstance;
    private SDocument document;
    private IAttachmentPersistenceHandler tempHandler, persistentHandler;

    public TestSDocumentPersistentServices(TestFormConfig testFormConfig) {
        super(testFormConfig);
    }

    @Before
    public void setup() {
        createTypes(createTestDictionary().createNewPackage("teste"));
        createInstances();
        setupServices();

    }

    private void createTypes(PackageBuilder pb) {
        groupingType = pb.createCompositeType("Grouping");
        groupingType.addField("anexo", STypeAttachment.class);
        groupingType.addFieldInteger("justIgnoreThis");
    }

    private void createInstances() {
        SIComposite instance = (SIComposite) groupingType.newInstance();
        fileFieldInstance = (SIAttachment) instance.getAllChildren().iterator().next();
    }

    private void setupServices() {
        document = fileFieldInstance.getDocument();

        tempHandler = mock(IAttachmentPersistenceHandler.class);
        persistentHandler = mock(IAttachmentPersistenceHandler.class);
        document.setAttachmentPersistenceTemporaryHandler(RefService.of(tempHandler));
        document.bindLocalService("filePersistence", IAttachmentPersistenceHandler.class, RefService.of(persistentHandler));
    }

    @Test
    public void deveMigrarOsAnexosParaAPersistencia() throws IOException {
        fileFieldInstance.setFileId("abacate");

        byte[] content = new byte[]{0};

        IAttachmentRef tempRef;
        IAttachmentRef persistentRef;

        when(tempHandler.getAttachment("abacate"))
                .thenReturn(tempRef = attachmentRef("abacate", content));

        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(persistentRef = attachmentRef("abacate", content));

        when(persistentHandler.copy(tempRef))
                .thenReturn(persistentRef);

        document.persistFiles();
        verify(persistentHandler).copy(tempRef);
    }

    @Test
    public void armazenaOValorDoNovoId() throws IOException {
        fileFieldInstance.setFileId("abacate");

        byte[] content = new byte[]{0};

        IAttachmentRef tempRef;
        IAttachmentRef persistentRef;

        when(tempHandler.getAttachment("abacate"))
                .thenReturn(tempRef = attachmentRef("abacate", content));

        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(persistentRef = attachmentRef("avocado", content));

        when(persistentHandler.copy(tempRef))
                .thenReturn(persistentRef);

        document.persistFiles();
        assertThat(fileFieldInstance.getFileId()).isEqualTo("avocado");
        assertThat(fileFieldInstance.getOriginalFileId()).isEqualTo("avocado");
    }

    @Test
    public void deveApagarOTemporarioAposInserirNoPersistente() throws IOException {
        fileFieldInstance.setFileId("abacate");

        byte[] content = new byte[]{0};

        IAttachmentRef tempRef;
        IAttachmentRef persistentRef;

        when(tempHandler.getAttachment("abacate"))
                .thenReturn(tempRef = attachmentRef("abacate", content));

        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(persistentRef = attachmentRef("abacate", content));

        when(persistentHandler.copy(tempRef))
                .thenReturn(persistentRef);


        document.persistFiles();
        verify(tempHandler).deleteAttachment("abacate");
    }

    @Test
    public void deveApagarOPersistenteSeEsteSeAlterou() throws IOException {
        fileFieldInstance.setFileId("abacate");
        fileFieldInstance.setOriginalFileId("avocado");

        byte[] content = new byte[]{0};

        IAttachmentRef tempRef;
        IAttachmentRef persistentRef;
        when(tempHandler.getAttachment("abacate"))
                .thenReturn(tempRef = attachmentRef("abacate", content));
        when(persistentHandler.addAttachment(AttachmentTestUtil.writeBytesToTempFile(content), content.length))
            .thenReturn(persistentRef = attachmentRef("abacate", content));
        when(persistentHandler.copy(tempRef))
                .thenReturn(persistentRef);

        document.persistFiles();
        verify(persistentHandler).deleteAttachment("avocado");
    }

    @Test
    public void naoApagaNadaSeNenhumArquivoFoiAlterado() {
        fileFieldInstance.setFileId("abacate");
        fileFieldInstance.setOriginalFileId("abacate");

        document.persistFiles();
        verify(persistentHandler, never()).deleteAttachment(Matchers.any());
        verify(tempHandler, never()).deleteAttachment(Matchers.any());
{'fraction_non_alphanumeric': 0.10587830869714679, 'fraction_numerical': 0.0006875214850464077, 'mean_word_length': 4.072798605056669, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4235596', 'n_tokens_mistral': 3220, 'n_tokens_neox': 3190, 'n_words': 453}