de.fhg.iais.cortex.search.SolrSearchServerTest.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.cortex.search.SolrSearchServerTest.java

Source

package de.fhg.iais.cortex.search;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import junit.framework.Assert;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import de.fhg.iais.commons.configuration.Home;
import de.fhg.iais.commons.dbc.DbcException;
import de.fhg.iais.cortex.rest.server.types.ServerType;
import de.fhg.iais.cortex.search.factory.SolrSearchServerFactory;
import de.fhg.iais.cortex.search.jetty.Jetty9SolrRunner;

/**
 * @author cweiland
 */
public class SolrSearchServerTest {

    private SolrSearchServerFactory factoryMock;
    private Home homeMock;

    private final String CONTEXT_URI = "http://localhost:8183/search";
    private final String SOLR_SEARCH_DIR = "./search";
    private final String SOLR_NODESTORE_DIR = "./entity";
    private final String SOLR_ENTITY_DIR = "./nodes";

    private File baseDir;

    @Before
    public void setUp() throws URISyntaxException {
        this.factoryMock = Mockito.mock(SolrSearchServerFactory.class);
        this.homeMock = Mockito.mock(Home.class);

        this.baseDir = new File(new File(this.getClass().getResource(".").toURI()), "temp");
        this.baseDir.mkdirs();

        Mockito.when(this.homeMock.getBaseDirectory()).thenReturn(this.baseDir);
    }

    @After
    public void deleteDirectories() throws IOException {
        FileUtils.deleteDirectory(this.baseDir);
        this.factoryMock = null;
        this.homeMock = null;
        this.baseDir = null;
    }

    @Test
    public void testGetterAndSetterAnWhoAmI() {
        SolrSearchServer solrSearchServer = new SolrSearchServer(this.homeMock, this.factoryMock, this.CONTEXT_URI,
                this.SOLR_SEARCH_DIR, this.SOLR_NODESTORE_DIR, this.SOLR_ENTITY_DIR);

        Assert.assertEquals(this.CONTEXT_URI, solrSearchServer.getContextUri());
        Assert.assertEquals(this.SOLR_SEARCH_DIR, solrSearchServer.getSolrSearchDir());
        Assert.assertEquals(this.SOLR_ENTITY_DIR, solrSearchServer.getSolrEntityDir());
        Assert.assertEquals(this.SOLR_NODESTORE_DIR, solrSearchServer.getSolrNodeStoreDir());
        Assert.assertEquals(ServerType.SEARCH, solrSearchServer.getServerType());
        Assert.assertEquals(SolrSearchServer.class.getSimpleName(), solrSearchServer.whoAmI());
    }

    @Test
    public void testStartExceptions() {
        final String malformedContextURI = "ht:/iam.mal\\formed";
        SolrSearchServer solrSearchServer = new SolrSearchServer(this.homeMock, this.factoryMock,
                malformedContextURI, this.SOLR_SEARCH_DIR, this.SOLR_NODESTORE_DIR, this.SOLR_ENTITY_DIR);

        try {
            solrSearchServer.doStart();
            Assert.fail("Should throw a DbcException.");
        } catch (DbcException e) {
            ;
        }

        Mockito.when(this.factoryMock.createJetty9SolrRunner(new File(this.baseDir, "solr").getAbsolutePath(),
                this.CONTEXT_URI, 8183, false, 5000)).thenThrow(new RuntimeException("Just an exception."));

        solrSearchServer = new SolrSearchServer(this.homeMock, this.factoryMock, this.CONTEXT_URI,
                this.SOLR_SEARCH_DIR, this.SOLR_NODESTORE_DIR, this.SOLR_ENTITY_DIR);

        try {
            solrSearchServer.doStart();
            Assert.fail("Should throw a DbcException.");
        } catch (DbcException e) {
            ;
        }
    }

    @Test
    public void testStart() throws Exception {
        SolrSearchServer solrSearchServer = new SolrSearchServer(this.homeMock, this.factoryMock, this.CONTEXT_URI,
                this.SOLR_SEARCH_DIR, this.SOLR_NODESTORE_DIR, this.SOLR_ENTITY_DIR);

        Jetty9SolrRunner runnerMock = Mockito.mock(Jetty9SolrRunner.class);
        Mockito.when(this.factoryMock.createJetty9SolrRunner(new File(this.baseDir, "solr").getAbsolutePath(), "",
                8183, false, 5000)).thenReturn(runnerMock);

        try {
            solrSearchServer.doStart();
        } catch (DbcException e) {
            Assert.fail("Should not throw a DbcException.");
        }

        try {
            solrSearchServer.doStart();
        } catch (DbcException e) {
            Assert.fail("Should not throw a DbcException.");
        }

        Mockito.verify(runnerMock, Mockito.times(1)).start(false);
        Mockito.verify(this.factoryMock, Mockito.times(1))
                .createJetty9SolrRunner(new File(this.baseDir, "solr").getAbsolutePath(), "", 8183, false, 5000);

        Assert.assertEquals(System.getProperty("solr.data.index.dir"), this.SOLR_SEARCH_DIR);
        Assert.assertEquals(System.getProperty("solr.nodestore.index.dir"), this.SOLR_NODESTORE_DIR);
        Assert.assertEquals(System.getProperty("solr.entity.index.dir"), this.SOLR_ENTITY_DIR);
        Assert.assertEquals(System.getProperty("javax.xml.parsers.DocumentBuilderFactory"),
                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

    }

    @Test
    public void testStopException() throws Exception {
        SolrSearchServer solrSearchServer = new SolrSearchServer(this.homeMock, this.factoryMock, this.CONTEXT_URI,
                this.SOLR_SEARCH_DIR, this.SOLR_NODESTORE_DIR, this.SOLR_ENTITY_DIR);

        solrSearchServer.doStop();

        Jetty9SolrRunner runnerMock = Mockito.mock(Jetty9SolrRunner.class);
        Mockito.when(this.factoryMock.createJetty9SolrRunner(new File(this.baseDir, "solr").getAbsolutePath(), "",
                8183, false, 5000)).thenReturn(runnerMock);
        Mockito.doThrow(new Exception("Some exception.")).when(runnerMock).stop();

        try {
            solrSearchServer.doStart();
        } catch (DbcException e) {
            Assert.fail("Should not throw a DbcException.");
        }

        try {
            solrSearchServer.doStop();
            Assert.fail("Should throw a DbcException.");
        } catch (DbcException e) {
            ;
        }

    }

    @Test
    public void testStop() throws Exception {
        SolrSearchServer solrSearchServer = new SolrSearchServer(this.homeMock, this.factoryMock, this.CONTEXT_URI,
                this.SOLR_SEARCH_DIR, this.SOLR_NODESTORE_DIR, this.SOLR_ENTITY_DIR);

        solrSearchServer.doStop();

        Jetty9SolrRunner runnerMock = Mockito.mock(Jetty9SolrRunner.class);
        Mockito.when(this.factoryMock.createJetty9SolrRunner(new File(this.baseDir, "solr").getAbsolutePath(), "",
                8183, false, 5000)).thenReturn(runnerMock);

        try {
            solrSearchServer.doStart();
        } catch (DbcException e) {
            Assert.fail("Should not throw a DbcException.");
        }

        try {
            solrSearchServer.doStop();
        } catch (DbcException e) {
            Assert.fail("Should throw a DbcException.");
        }

        Mockito.verify(runnerMock, Mockito.times(1)).stop();

    }
}