Hello,

I have written a data service to send an HttpPost command to post JSON to
Solr. The code is working, but now I want to switch to using an embedded
Solr server for just  the unit tests. The problem is that the embedded Solr
server doesn't seem to be starting an embedded server with a port. So I'm
at a loss on how to test this. I guess I have two questions. (1) How do I
unit test my post command with an embedded Solr server? (2) If it isn't
possible to use the embedded Solr server, I believe I read somewhere that
Solr uses a Jetty server. Is it possible to convert an embedded jetty
server (with a port I can access) to a Solr server?

Here is the class I am trying to test:

public class SolrDataServiceClient {

private String urlString;
private HttpClient httpClient;
private final Logger LOGGER = LoggerFactory.getLogger
(SolrDataServiceClient.class);

        /**
         * Constructor for connecting to the Solr Server
         * @param solrCore
         * @param serverName
         * @param portNumber
         */
        public SolrDataServiceClient(String solrCore, String serverName,
String portNumber){
                LOGGER.info("Initializing new Http Client to Connect To Solr");
                urlString = serverName + ":" + portNumber + "/solr/" + solrCore
;

                if(httpClient == null){
                        httpClient = new HttpClient();
                }
        }

/**
* Post the provided JSON to Solr
*/
        public CloseableHttpResponse postJSON(String jsonToAdd) {
                CloseableHttpResponse response = null;
                try {
                        CloseableHttpClient client = 
HttpClients.createDefault();
                        HttpPost httpPost = new HttpPost(urlString +
"/update/json/docs");
                        HttpEntity entity = new ByteArrayEntity(jsonToAdd
.getBytes("UTF-8"));
                        httpPost.setEntity(entity);
                        httpPost.setHeader("Content-type", "application/json");
                        LOGGER.debug("httpPost = " + httpPost.toString());
                        response = client.execute(httpPost);
                        String result = EntityUtils.toString(response.getEntity
());
                        LOGGER.debug("result = " + result);
                        client.close();
                } catch (IOException e) {
                        LOGGER.error("IOException", e);
                }

                return response;
}


Here is my JUnit test:

public class SolrDataServiceClientTest {

private static EmbeddedSolrServer embeddedServer;
private static SolrDataServiceClient solrDataServiceClient;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
        System.setProperty("solr.solr.home", "solr/conf");
        System.setProperty("solr.data.dir", new File(
"target/solr-embedded-data").getAbsolutePath());
        CoreContainer coreContainer = new CoreContainer("solr/conf");
        coreContainer.load();

        CoreDescriptor cd = new CoreDescriptor(coreContainer, "myCoreName",
new File("solr").getAbsolutePath());
        coreContainer.create(cd);

        embeddedServer = new EmbeddedSolrServer(coreContainer, "myCoreName");

        solrDataServiceClient = new SolrDataServiceClient("myCoreName",
"http://localhost";, "8983"); //I'm not sure what should go here
}

@Test
public void testPostJson() {
         String testJson = " { " +
         "\"observationId\": \"12345c\"," +
         "\"observationType\": \"image\"," +
        "\"locationLat\": 38.9215," +
         "\"locationLon\": -77.235" +
        "}";
         CloseableHttpResponse response = solrDataServiceClient.postJSON(
testJson);
         assertEquals(response.getStatusLine().getStatusCode(), 200);
         }

Thank you!

Jennifer

Reply via email to