Java tutorial
/* * 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; import java.util.Date; import java.util.Set; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.craftercms.commerce.api.BaseType; import org.craftercms.commerce.api.Product; import org.craftercms.commerce.api.facet.Facet; import org.craftercms.commerce.api.service.ProductService; import org.craftercms.commerce.api.service.ServiceResponse; import org.craftercms.commerce.client.CrafterCommerceClient; import org.craftercms.commerce.client.itest.data.SolrTestDataService; import org.craftercms.commerce.client.itest.data.TestProduct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { TestConfig.class }) public class ProductServiceTest extends AbstractBaseTest implements BasicCrudTest { @Autowired private CrafterCommerceClient crafterCommerceClient; @Autowired private SolrServer solrServer; // ---------- Tests ---------- @Test public void testFindById() throws Exception { TestProduct testProduct = solrTestDataService.randomTestProduct(); ServiceResponse<Product> response = productService().findById(testProduct.getId()); Product foundProduct = response.getEntity(); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(1, response.getCount()); assertProduct(testProduct, foundProduct); } @Test public void testFindByQuery() throws Exception { String query; ServiceResponse<Product> response; TestProduct testProduct; Set<Product> foundProducts; testProduct = solrTestDataService.randomTestProduct(); query = " id = '" + testProduct.getId() + "' "; response = productService().findByQuery(query, 0, 10); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(1, response.getEntities().size()); Product foundProduct = response.getEntities().iterator().next(); assertProduct(testProduct, foundProduct); query = "size < 2"; response = productService().findByQuery(query, 0, 10); foundProducts = response.getEntities(); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(20, response.getCount()); Assert.assertEquals(10, foundProducts.size()); query = "size > 2"; response = productService().findByQuery(query, 0, 20); foundProducts = response.getEntities(); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(50, response.getCount()); Assert.assertEquals(20, foundProducts.size()); query = "tags = 'bcd'"; response = productService().findByQuery(query, 0, 20); foundProducts = response.getEntities(); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(10, response.getCount()); Assert.assertEquals(10, foundProducts.size()); // TODO <> query } @Test public void testFindByQueryWithFacets() throws Exception { String query, facetField1, facetField2; ServiceResponse<Product> response; Set<Facet> facets; Facet facet; query = " sizes = 18"; facetField1 = "sizes_i_mv"; facetField2 = "tags_s_mv"; response = productService().findByQuery(query, 0, 10, facetField1, facetField2); facets = response.getFacets(); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(2, facets.size()); // TODO Assert the facets themselves // Facet[] facetsArray = facets.toArray(new Facet[0]); // facet = facetsArray[0]; // Assert.assertEquals(facetField1, facet.getName()); } @Test public void testExists() throws Exception { // Check that a random test product exists TestProduct testProduct = solrTestDataService.randomTestProduct(); ServiceResponse<Product> response = productService().exists(testProduct.getId()); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(true, response.isExists()); // Check that a product with a not existing id does NOT exist response = productService().exists("non-existent-id"); Assert.assertEquals(true, response.isSuccess()); Assert.assertEquals(false, response.isExists()); } @Test public void testFindAll() throws Exception { ServiceResponse<Product> response = productService().findAll(); Assert.assertEquals(SolrTestDataService.TEST_DOCUMENT_COUNT, response.getEntities().size()); } @Test public void testCount() throws Exception { ServiceResponse<Product> response = productService().count(); Assert.assertEquals(SolrTestDataService.TEST_DOCUMENT_COUNT, response.getCount()); } @Test public void testSave() throws Exception { String id = "product-2003"; Date now = new Date(); String creator = "Spiderman"; String modifier = "The Hulk"; long numFound; SolrQuery solrQuery = new SolrQuery(BaseType.SOLR_ID_FIELD + ":" + id); solrQuery.setRows(1); numFound = solrServer.query(solrQuery).getResults().getNumFound(); Assert.assertEquals(0, numFound); Product product = new Product(); product.setId(id); product.setCreationDate(now); product.setCreatedBy(creator); product.setLastModifiedBy(modifier); ServiceResponse<Product> response = productService().save(product); Assert.assertEquals(true, response.isSuccess()); SolrDocumentList solrDocList = solrServer.query(solrQuery).getResults(); Assert.assertEquals(1, solrDocList.getNumFound()); Assert.assertEquals(1, solrDocList.size()); SolrDocument solrDoc = solrDocList.get(0); Assert.assertEquals(id, solrDoc.getFieldValue(BaseType.SOLR_ID_FIELD)); Assert.assertEquals(now, solrDoc.getFieldValue(BaseType.SOLR_CREATION_DATE_FIELD)); Assert.assertEquals(creator, solrDoc.getFieldValue(BaseType.SOLR_CREATED_BY_ID_FIELD)); Assert.assertEquals(modifier, solrDoc.getFieldValue(BaseType.SOLR_LAST_MODIFIED_BY_ID_FIELD)); } @Test public void testSaveAll() throws Exception { String id1 = "product-2003"; String id2 = "product-2004"; Date now = new Date(); String creator = "Spiderman"; String modifier = "The Hulk"; long numFound; SolrQuery solrQuery = new SolrQuery( BaseType.SOLR_ID_FIELD + ":" + id1 + " OR " + BaseType.SOLR_ID_FIELD + ":" + id2); solrQuery.setRows(2); numFound = solrServer.query(solrQuery).getResults().getNumFound(); Assert.assertEquals(0, numFound); Product p1 = new Product(); p1.setId(id1); p1.setCreationDate(now); p1.setCreatedBy(creator); p1.setLastModifiedBy(modifier); Product p2 = new Product(); p2.setId(id2); p2.setCreationDate(now); p2.setCreatedBy(creator); p2.setLastModifiedBy(modifier); Product[] newProducts = { p1, p2 }; ServiceResponse<Product> response = productService().saveAll(newProducts); Assert.assertEquals(true, response.isSuccess()); numFound = solrServer.query(solrQuery).getResults().getNumFound(); Assert.assertEquals(2, numFound); } @Test public void testDelete() throws Exception { String randomProductId = "product-" + solrTestDataService.randomIndex(); SolrQuery solrQuery = new SolrQuery(BaseType.SOLR_ID_FIELD + ":" + randomProductId); solrQuery.setRows(1); long numFound = solrServer.query(solrQuery).getResults().getNumFound(); Assert.assertEquals(1, numFound); Product product = new Product(); product.setId(randomProductId); ServiceResponse<Product> response = productService().delete(product); Assert.assertEquals(true, response.isSuccess()); numFound = solrServer.query(solrQuery).getResults().getNumFound(); Assert.assertEquals(0, numFound); } @Test public void testDeleteAll() throws Exception { SolrQuery solrQuery = new SolrQuery(BaseType.SOLR_TYPE_FIELD + ":" + Product.class.getName()); solrQuery.setRows(0); long numFound = solrServer.query(solrQuery).getResults().getNumFound(); Assert.assertEquals(SolrTestDataService.TEST_DOCUMENT_COUNT, numFound); ServiceResponse<Product> response = productService().deleteAll(); Assert.assertEquals(true, response.isSuccess()); numFound = solrServer.query(solrQuery).getResults().getNumFound(); Assert.assertEquals(0, numFound); } // ---------- Utitity methods ---------- private ProductService productService() { return crafterCommerceClient.getProductService(); } /** * @param testProduct The product to assert against * @param foundProduct The retrieved product to assert */ private void assertProduct(TestProduct testProduct, Product foundProduct) { Assert.assertNotNull(foundProduct); // assert basic properties Assert.assertEquals(testProduct.getId(), foundProduct.getId()); Assert.assertEquals(testProduct.getName(), foundProduct.getName()); Assert.assertEquals(testProduct.getDescription(), foundProduct.getDescription()); Assert.assertEquals(testProduct.getCreatedBy(), foundProduct.getCreatedBy()); Assert.assertEquals(testProduct.getLastModifiedBy(), foundProduct.getLastModifiedBy()); Assert.assertEquals(testProduct.getCreationDate(), foundProduct.getCreationDate()); Assert.assertEquals(testProduct.getLastModifiedDate(), foundProduct.getLastModifiedDate()); // assert dynamic properties Assert.assertEquals(testProduct.category_s, foundProduct.getProperty("category")); Assert.assertEquals(testProduct.size_i, foundProduct.getProperty("size")); Assert.assertEquals(testProduct.listPrice_f, foundProduct.getProperty("listPrice")); Assert.assertEquals(testProduct.expirationDate_d, foundProduct.getProperty("expirationDate")); Assert.assertEquals(testProduct.inStock_b, foundProduct.getProperty("inStock")); assertMultiValueProperty((Object[]) testProduct.tags_s_mv, (Object[]) foundProduct.getProperty("tags")); assertMultiValueProperty((Object[]) testProduct.sizes_i_mv, (Object[]) foundProduct.getProperty("sizes")); assertMultiValueProperty((Object[]) testProduct.listPrices_f_mv, (Object[]) foundProduct.getProperty("listPrices")); assertMultiValueProperty((Object[]) testProduct.launchDate_d_mv, (Object[]) foundProduct.getProperty("launchDate")); // TODO: Fix this, not sure why this only returns an array of 2 elements while 3 is expected // assertMultiValueProperty((Object[])testProduct.booleanMulti_b_mv, // (Object[])foundProduct.getProperty("booleanMulti")); } private void assertMultiValueProperty(Object[] testProp, Object[] foundProp) { Assert.assertEquals(testProp.length, foundProp.length); for (int i = 0; i < testProp.length; i++) { Assert.assertEquals(testProp[i], foundProp[i]); } } }