PublisherSearchRepositorySolrjTest.java 2.61 KB
package org.legrog.entities;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.BindingException;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.test.MockitoExtension;
import org.mockito.Mock;

import java.io.IOException;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import static org.assertj.core.api.Assertions.assertThat;

/*
    Classe testant PublisherSearchRepositorySolrj
 */
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Indexes and searches with SearchRepository")

public class PublisherSearchRepositorySolrjTest {

    private PublisherSearchRepository publisherSearchRepository;
    private SolrClient solrClient;

    @BeforeEach
    public void setup(@Mock SolrClient solrClient) {
        publisherSearchRepository = new PublisherSearchRepositorySolrj(solrClient);
        this.solrClient = solrClient;
    }

    @Nested
    @DisplayName("save method")
    class saveTests {

        @Test
        @DisplayName("when called with right parameters, should addBean IndexedPublisher with commitWithinMs of 1 to repository")
        public void addBeanTest(@Mock IndexedPublisher indexedPublisher) throws IndexingException, SolrServerException, IOException {
            publisherSearchRepository.save(indexedPublisher);
            verify(solrClient).addBean(indexedPublisher, 1);
        }

        @Test
        @DisplayName("When repository in IO error, should throw an IndexingException")
        public void addBeanIOETest(@Mock IndexedPublisher indexedPublisher) throws SolrServerException, IOException {
            when(solrClient.addBean(indexedPublisher, 1)).thenThrow(new IOException());
            Assertions.assertThrows(IndexingException.class, () -> publisherSearchRepository.save(indexedPublisher));
        }

        @Test
        @DisplayName("When repository in SolrServerException, should throw an IndexingException with its root cause")
        public void addBeanSSETest(@Mock IndexedPublisher indexedPublisher) throws SolrServerException, IOException{
            when(solrClient.addBean(indexedPublisher, 1)).thenThrow(new SolrServerException(new BindingException("BE test")));
            try {
                publisherSearchRepository.save(indexedPublisher);
            } catch (IndexingException ie) {
                assertThat(ie.getRootCause().getClass()).isEqualTo(BindingException.class);
            }
        }
    }

}