<commit_before>package com.imcode.imcms.persistence.repository;

import com.imcode.imcms.components.datainitializer.VersionDataInitializer;
import com.imcode.imcms.config.TestConfig;
import com.imcode.imcms.persistence.entity.DocumentUrlJPA;
import com.imcode.imcms.persistence.entity.Version;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.Assert.*;

@Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
public class DocumentUrlRepositoryTest {

    private static final int TEST_VERSION_INDEX = 0;
    private static final int DEFAULT_DOC_ID = 1001;

    @Autowired
    private DocumentUrlRepository documentUrlRepository;

    @Autowired
    private VersionDataInitializer versionDataInitializer;

    @Test
    public void saveOneUrlDocument_Expect_Saved() {
        final DocumentUrlJPA savedUrlDocument = getDocumentUrlList(1).get(0);
        assertNotNull(savedUrlDocument);
        assertTrue(documentUrlRepository.findAll().contains(savedUrlDocument));
    }

    @Test
    public void deleteOneUrlDocById_When_OneSpecifiedUrlDocumentIsSaved_Expect_Deleted() {
        final DocumentUrlJPA savedUrlDocument = getDocumentUrlList(1).get(0);
        documentUrlRepository.delete(savedUrlDocument.getId());
        assertFalse(documentUrlRepository.findAll().contains(savedUrlDocument));
    }

    @Test
    public void searchDocumentById_WhenDocumentIsSaved_Expect_Found() {
        final DocumentUrlJPA savedUrlDocument = getDocumentUrlList(1).get(0);
        assertNotNull(savedUrlDocument);
        assertEquals(savedUrlDocument, documentUrlRepository.findOne(savedUrlDocument.getId()));
    }

    @Test
    public void searchDocumentById_WhenDocumentIsNotSaved_Expect_NotFound() {
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(3);
        final int searchingId = savedDocuments.stream()
                .mapToInt(DocumentUrlJPA::getId)
                .max()
                .orElse(0) + 1;

        assertNull(documentUrlRepository.findOne(searchingId));
    }

    @Test
    public void findUrlDocumentByDocIdAndVersionNo_WhenSpecifiedUrlDocIsSaved_Expect_Found() {
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(3);
        final DocumentUrlJPA expectedDocument = savedDocuments.get(1);

        final int docId = expectedDocument.getVersion().getDocId();
        final int versionNo = expectedDocument.getVersion().getNo();

        final DocumentUrlJPA foundDocument = documentUrlRepository.findByDocIdAndVersionNo(docId, versionNo);

        assertNotNull(foundDocument);
        assertEquals(expectedDocument, foundDocument);
    }

    @Test
    public void findUrlDocumentsByDocId_When_AllDocumentsHasSpecifiedDocId_Expect_Found() {
        final int documentListSize = 5;
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(documentListSize);
        final List<DocumentUrlJPA> foundDocuments = documentUrlRepository.findByDocId(DEFAULT_DOC_ID);

        assertEquals(documentListSize, foundDocuments.size());
        assertEquals(savedDocuments, foundDocuments);
    }

    @Test
    public void findUrlDocumentsByDocId_When_DocIdNotExist_Expect_NotFound() {
        final int documentListSize = 3;
        getDocumentUrlList(documentListSize);
        final List<DocumentUrlJPA> foundDocuments = documentUrlRepository.findByDocId(DEFAULT_DOC_ID + 1);

        assertEquals(0, foundDocuments.size());
    }

    @Test
    public void deleteUrlDocumentsByDocId_When_DocIdExist_Expect_Deleted() {
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(3);

        documentUrlRepository.delete(documentUrlRepository.findByDocId(DEFAULT_DOC_ID));

        final List<DocumentUrlJPA> foundDocumentList = documentUrlRepository.findAll();
        savedDocuments.forEach(documentUrlJPA -> assertFalse(foundDocumentList.contains(documentUrlJPA)));
    }

    private List<DocumentUrlJPA> getDocumentUrlList(int number) {
        return IntStream.range(0, number)
                .mapToObj(i -> {
                    final String someData = "test" + i;
                    DocumentUrlJPA documentUrlJPA = new DocumentUrlJPA();
                    documentUrlJPA.setUrlFrameName(someData);
                    documentUrlJPA.setUrl(someData);
                    documentUrlJPA.setUrlLanguagePrefix("t" + i);
                    documentUrlJPA.setUrlTarget(someData);
                    documentUrlJPA.setUrlText(someData);

                    final Version version = versionDataInitializer.createData(TEST_VERSION_INDEX + i, DEFAULT_DOC_ID);
                    documentUrlJPA.setVersion(version);

                    return documentUrlRepository.saveAndFlush(documentUrlJPA);
                })
                .collect(Collectors.toList());
    }
}<commit_msg>IMCMS-233 - Apply new UI to the admin panel and editors: - Added test when url document is found by version.<commit_after>package com.imcode.imcms.persistence.repository;

import com.imcode.imcms.components.datainitializer.DocumentDataInitializer;
import com.imcode.imcms.components.datainitializer.VersionDataInitializer;
import com.imcode.imcms.config.TestConfig;
import com.imcode.imcms.domain.dto.DocumentUrlDTO;
import com.imcode.imcms.persistence.entity.DocumentUrlJPA;
import com.imcode.imcms.persistence.entity.Version;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.Assert.*;

@Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
public class DocumentUrlRepositoryTest {

    private static final int TEST_VERSION_INDEX = 0;
    private static final int DEFAULT_DOC_ID = 1001;

    @Autowired
    private DocumentUrlRepository documentUrlRepository;

    @Autowired
    private DocumentDataInitializer documentDataInitializer;

    @Autowired
    private VersionDataInitializer versionDataInitializer;

    @Test
    public void saveOneUrlDocument_Expect_Saved() {
        final DocumentUrlJPA savedUrlDocument = getDocumentUrlList(1).get(0);
        assertNotNull(savedUrlDocument);
        assertTrue(documentUrlRepository.findAll().contains(savedUrlDocument));
    }

    @Test
    public void deleteOneUrlDocById_When_OneSpecifiedUrlDocumentIsSaved_Expect_Deleted() {
        final DocumentUrlJPA savedUrlDocument = getDocumentUrlList(1).get(0);
        documentUrlRepository.delete(savedUrlDocument.getId());
        assertFalse(documentUrlRepository.findAll().contains(savedUrlDocument));
    }

    @Test
    public void searchDocumentById_WhenDocumentIsSaved_Expect_Found() {
        final DocumentUrlJPA savedUrlDocument = getDocumentUrlList(1).get(0);
        assertNotNull(savedUrlDocument);
        assertEquals(savedUrlDocument, documentUrlRepository.findOne(savedUrlDocument.getId()));
    }

    @Test
    public void searchDocumentById_WhenDocumentIsNotSaved_Expect_NotFound() {
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(3);
        final int searchingId = savedDocuments.stream()
                .mapToInt(DocumentUrlJPA::getId)
                .max()
                .orElse(0) + 1;

        assertNull(documentUrlRepository.findOne(searchingId));
    }

    @Test
    public void findUrlDocumentByDocIdAndVersionNo_WhenSpecifiedUrlDocIsSaved_Expect_Found() {
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(3);
        final DocumentUrlJPA expectedDocument = savedDocuments.get(1);

        final int docId = expectedDocument.getVersion().getDocId();
        final int versionNo = expectedDocument.getVersion().getNo();

        final DocumentUrlJPA foundDocument = documentUrlRepository.findByDocIdAndVersionNo(docId, versionNo);

        assertNotNull(foundDocument);
        assertEquals(expectedDocument, foundDocument);
    }

    @Test
    public void findUrlDocumentsByDocId_When_AllDocumentsHasSpecifiedDocId_Expect_Found() {
        final int documentListSize = 5;
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(documentListSize);
        final List<DocumentUrlJPA> foundDocuments = documentUrlRepository.findByDocId(DEFAULT_DOC_ID);

        assertEquals(documentListSize, foundDocuments.size());
        assertEquals(savedDocuments, foundDocuments);
    }

    @Test
    public void findUrlDocumentsByDocId_When_DocIdNotExist_Expect_NotFound() {
        final int documentListSize = 3;
        getDocumentUrlList(documentListSize);
        final List<DocumentUrlJPA> foundDocuments = documentUrlRepository.findByDocId(DEFAULT_DOC_ID + 1);

        assertEquals(0, foundDocuments.size());
    }

    @Test
    public void deleteUrlDocumentsByDocId_When_DocIdExist_Expect_Deleted() {
        final List<DocumentUrlJPA> savedDocuments = getDocumentUrlList(3);

        documentUrlRepository.delete(documentUrlRepository.findByDocId(DEFAULT_DOC_ID));

        final List<DocumentUrlJPA> foundDocumentList = documentUrlRepository.findAll();
        savedDocuments.forEach(documentUrlJPA -> assertFalse(foundDocumentList.contains(documentUrlJPA)));
    }

    @Test
    public void findByVersion() {
        final DocumentUrlDTO expectedDocument = documentDataInitializer.createUrlDocument().getDocumentUrlDTO();
        final Version version = versionDataInitializer.createData(TEST_VERSION_INDEX, DEFAULT_DOC_ID);
        final List<DocumentUrlJPA> byVersion = documentUrlRepository.findByVersion(version);

        assertEquals(1, byVersion.size());

        final DocumentUrlDTO actualDocument = new DocumentUrlDTO(byVersion.get(0));

        assertEquals(expectedDocument, actualDocument);
    }

    private List<DocumentUrlJPA> getDocumentUrlList(int number) {
        return IntStream.range(0, number)
                .mapToObj(i -> {
                    final String someData = "test" + i;
                    DocumentUrlJPA documentUrlJPA = new DocumentUrlJPA();
                    documentUrlJPA.setUrlFrameName(someData);
                    documentUrlJPA.setUrl(someData);
                    documentUrlJPA.setUrlLanguagePrefix("t" + i);
                    documentUrlJPA.setUrlTarget(someData);
                    documentUrlJPA.setUrlText(someData);

                    final Version version = versionDataInitializer.createData(TEST_VERSION_INDEX + i, DEFAULT_DOC_ID);
                    documentUrlJPA.setVersion(version);

                    return documentUrlRepository.saveAndFlush(documentUrlJPA);
                })
                .collect(Collectors.toList());
    }
}
{'fraction_non_alphanumeric': 0.07968162337094375, 'fraction_numerical': 0.004810635878597044, 'mean_word_length': 3.877986348122867, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, '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': '143808', 'n_tokens_mistral': 3124, 'n_tokens_neox': 2998, 'n_words': 494}