org.craftercms.commerce.client.itest.data.SolrTestDataService.java Source code

Java tutorial

Introduction

Here is the source code for org.craftercms.commerce.client.itest.data.SolrTestDataService.java

Source

/*
 * Copyright (C) 2007-2013 Crafter Software Corporation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.craftercms.commerce.client.itest.data;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.craftercms.commerce.api.Product;
import org.springframework.web.client.RestTemplate;

/**
 * A service to create and destroy test data in a Apache Solr backend.
 * This is useful for integration testing.
 * 
 * @author Michiel Verkaik (mverkaik@rivetlogic.com)
 *
 */
public class SolrTestDataService implements TestDataService<Object> {

    // -------- Constants --------

    private static final Log LOGGER = LogFactory.getLog(SolrTestDataService.class);
    private static final String DELETE_ALL_COMMAND = "<delete><query>*:*</query></delete>";
    private static final String UPDATE_URI = "/update?commit=true";
    private static final Date START_TIME = new Date();
    public static final int TEST_DOCUMENT_COUNT = 100; // How many test documents will be created

    private String solrServerUrl;

    // -------- Constructors --------

    public SolrTestDataService(String solrServerUrl) {
        this.solrServerUrl = solrServerUrl;
    }

    // -------- Implementation --------

    /* 
     * Do a multipart file-upload with Solr XML using SpringRestTemplate
     */
    public void createTestData() throws IOException, SolrServerException {

        SolrServer solrServer = new HttpSolrServer(solrServerUrl);
        for (int i = 0; i < TEST_DOCUMENT_COUNT; i++) {
            solrServer.addBean((Product) testProduct(i));
        }
        solrServer.commit();

        LOGGER.info("Finished creation Solr test data.");
    }

    public void verifyTestData() {
        LOGGER.info("Finished verifying Solr test data ( NOT YET IMPLEMENTED !!! )");
    }

    /*
     * Delete the full Solr index
     */
    public void destroyTestData() {

        String updateUrl = solrServerUrl + UPDATE_URI;
        new RestTemplate().postForLocation(updateUrl, DELETE_ALL_COMMAND);

        LOGGER.info("Finished destroying Solr test data.");
    }

    // ---------- Utility Methods ----------

    public int randomIndex() {
        return (int) Math.round(Math.random() * TEST_DOCUMENT_COUNT);
    }

    public TestProduct randomTestProduct() {
        return testProduct(randomIndex());
    }

    public TestProduct testProduct(int i) {

        Calendar cal = new GregorianCalendar();
        cal.setTime(SolrTestDataService.START_TIME);
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + i);
        cal.set(Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR + i);
        cal.set(Calendar.HOUR_OF_DAY, 1);
        cal.set(Calendar.MINUTE, 1);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 1);

        TestProduct product = new TestProduct();
        product.setId("product-" + i);
        product.setName("product-" + i + "-name");
        product.setCreatedBy("product-" + i + "-created-by");
        product.setCreationDate(cal.getTime());
        product.setLastModifiedBy("product-" + i + "-modified-by");
        product.setLastModifiedDate(cal.getTime());
        product.setDescription("product-" + i + "-description");

        if (i < 20) {
            // 0-19
            product.category_s = "A";
            product.size_i = 1;
            product.listPrice_f = (float) 15.99;
            cal.set(Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR + 10);
            product.expirationDate_d = cal.getTime();
            product.inStock_b = true;
            product.tags_s_mv[0] = "abc";
            product.tags_s_mv[1] = "def";
            product.tags_s_mv[2] = "efg";
            product.sizes_i_mv[0] = 12;
            product.sizes_i_mv[1] = 13;
            product.sizes_i_mv[2] = 14;
            product.sizes_i_mv[3] = 15;
            product.listPrices_f_mv[0] = (float) 2.3;
            product.listPrices_f_mv[1] = (float) 3.2;
            cal.set(Calendar.DAY_OF_YEAR, 1);
            product.launchDate_d_mv[0] = cal.getTime();
            product.booleanMulti_b_mv[0] = true;
            product.booleanMulti_b_mv[1] = true;
            product.booleanMulti_b_mv[2] = false;
        } else if (i < 50) {
            // 20-49
            product.category_s = "B";
            product.size_i = 2;
            product.listPrice_f = (float) 20.99;
            cal.set(Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR + 20);
            product.expirationDate_d = cal.getTime();
            product.inStock_b = false;
            product.tags_s_mv[0] = "hij";
            product.tags_s_mv[1] = "klm";
            product.tags_s_mv[2] = "nop";
            product.sizes_i_mv[0] = 8;
            product.sizes_i_mv[1] = 10;
            product.sizes_i_mv[2] = 12;
            product.sizes_i_mv[3] = 14;
            product.listPrices_f_mv[0] = (float) 18.34;
            product.listPrices_f_mv[1] = (float) 39.21;
            cal.set(Calendar.DAY_OF_YEAR, 2);
            product.launchDate_d_mv[0] = cal.getTime();
            product.booleanMulti_b_mv[0] = false;
            product.booleanMulti_b_mv[1] = true;
            product.booleanMulti_b_mv[2] = false;
        } else if (i < 90) {
            // 50-89
            product.category_s = "C";
            product.size_i = 3;
            product.listPrice_f = (float) 25.99;
            cal.set(Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR + 30);
            product.expirationDate_d = cal.getTime();
            product.inStock_b = true;
            product.tags_s_mv[0] = "qrs";
            product.tags_s_mv[1] = "tuv";
            product.tags_s_mv[2] = "xyz";
            product.sizes_i_mv[0] = 21;
            product.sizes_i_mv[1] = 9;
            product.sizes_i_mv[2] = 3;
            product.sizes_i_mv[3] = 18;
            product.listPrices_f_mv[0] = (float) 12.3;
            product.listPrices_f_mv[1] = (float) 32.2;
            cal.set(Calendar.DAY_OF_YEAR, 3);
            product.launchDate_d_mv[0] = cal.getTime();
            product.booleanMulti_b_mv[0] = true;
            product.booleanMulti_b_mv[1] = true;
            product.booleanMulti_b_mv[2] = true;
        } else {
            // 90-99
            product.category_s = "D";
            product.size_i = 4;
            product.listPrice_f = (float) 30.99;
            cal.set(Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR + 40);
            product.expirationDate_d = cal.getTime();
            product.inStock_b = false;
            product.tags_s_mv[0] = "bcd";
            product.tags_s_mv[1] = "efg";
            product.tags_s_mv[2] = "hij";
            product.sizes_i_mv[0] = 41;
            product.sizes_i_mv[1] = 12;
            product.sizes_i_mv[2] = 1;
            product.sizes_i_mv[3] = 19;
            product.listPrices_f_mv[0] = (float) 32.33;
            product.listPrices_f_mv[1] = (float) 42.21;
            cal.set(Calendar.DAY_OF_YEAR, 4);
            product.launchDate_d_mv[0] = cal.getTime();
            product.booleanMulti_b_mv[0] = false;
            product.booleanMulti_b_mv[1] = false;
            product.booleanMulti_b_mv[2] = false;
        }

        return product;

    }

}