@RequestMapping(value = "/product/{prod}", method = RequestMethod.DELETE) @Transactional public ResponseEntity<ProductResult> doIt( @PathVariable("prod") String product, Authentication auth) { if (!ApplicationSecurity.isRoot(auth)) { return new ResponseEntity<ProductResult>(HttpStatus.FORBIDDEN); } Product reqProduct = new Product(product, null); List<String> errors = DomainValidator.checkForErrors(reqProduct); if (!errors.isEmpty()) { return new ResponseEntity<ProductResult>( new ProductResult(reqProduct, errors), HttpStatus.BAD_REQUEST); } if (!products.exists(reqProduct.getName())) { return new ResponseEntity<ProductResult>( new ProductResult(reqProduct, Product.NOT_FOUND), HttpStatus.NOT_FOUND); } products.delete(reqProduct.getName()); components.deleteByKeyProduct(reqProduct.getName()); properties.deleteByKeyProduct(reqProduct.getName()); userProducts.deleteByKeyProduct(reqProduct.getName()); return new ResponseEntity<ProductResult>(HttpStatus.OK); }
@Test public void testDefaultAndOperator() { List<ProductBean> found = repo.findByAvailableIn(Arrays.asList(Boolean.TRUE)); Assert.assertEquals(3, found.size()); found = repo.findByAvailableIn(Arrays.asList(Boolean.TRUE, Boolean.FALSE)); Assert.assertTrue(found.isEmpty()); }
@Test public void testDefaultOrOperator() { List<ProductBean> found = repo.findByAvailableInWithOrOperator(Arrays.asList(Boolean.TRUE)); Assert.assertEquals(3, found.size()); found = repo.findByAvailableInWithOrOperator(Arrays.asList(Boolean.TRUE, Boolean.FALSE)); Assert.assertEquals(4, found.size()); }
@Test public void testFindByIsNull() { ProductBean beanWithoutName = createProductBean("5", 3, true, "product"); beanWithoutName.setName(null); repo.save(beanWithoutName); List<ProductBean> found = repo.findByNameIsNull(); Assert.assertEquals(1, found.size()); Assert.assertEquals(beanWithoutName.getId(), found.get(0).getId()); }
@Before public void setUp() { repo.deleteAll(); repo.save( Arrays.asList( POPULAR_AVAILABLE_PRODUCT, UNPOPULAR_AVAILABLE_PRODUCT, UNAVAILABLE_PRODUCT, NAMED_PRODUCT)); }
@Test public void testWithDefTypeLucene() { final ProductBean anotherProductBean = createProductBean("5", 3, true, "an other product"); repo.save(anotherProductBean); List<ProductBean> found = repo.findByNameIn(Arrays.asList(NAMED_PRODUCT.getName(), anotherProductBean.getName())); Assert.assertEquals(2, found.size()); Assert.assertThat(found, Matchers.containsInAnyOrder(anotherProductBean, NAMED_PRODUCT)); }
@Test public void testFindByIsNotNull() { ProductBean beanWithoutName = createProductBean("5", 3, true, "product"); beanWithoutName.setName(null); repo.save(beanWithoutName); List<ProductBean> found = repo.findByNameIsNotNull(); Assert.assertEquals(4, found.size()); for (ProductBean foundBean : found) { Assert.assertFalse(beanWithoutName.getId().equals(foundBean.getId())); } }
@Test public void initProducts() throws Exception { ProductRepository productRepository = new JdbcProductRepository("client1"); List<Product> products = productRepository.find(); List<DBObject> list = new ArrayList<>(); for (Product product : products) { list.add((DBObject) JSON.parse(Json.toJson(product).toString())); } dataBase.getCollection("demoProducts").insert(list); }
@Test public void testPagination() { Pageable pageable = new PageRequest(0, 2); Page<ProductBean> page1 = repo.findByNameStartingWith("name", pageable); Assert.assertEquals(pageable.getPageSize(), page1.getNumberOfElements()); Assert.assertTrue(page1.hasNextPage()); Assert.assertEquals(3, page1.getTotalElements()); pageable = new PageRequest(1, 2); Page<ProductBean> page2 = repo.findByNameStartingWith("name", pageable); Assert.assertEquals(1, page2.getNumberOfElements()); Assert.assertFalse(page2.hasNextPage()); Assert.assertEquals(3, page2.getTotalElements()); }
@Test public void testFindByNear() { ProductBean locatedInBuffalow = createProductBean("100", 5, true); locatedInBuffalow.setLocation("45.17614,-93.87341"); ProductBean locatedInNYC = createProductBean("200", 5, true); locatedInNYC.setLocation("40.7143,-74.006"); repo.save(Arrays.asList(locatedInBuffalow, locatedInNYC)); List<ProductBean> found = repo.findByLocationNear(new GeoLocation(45.15, -93.85), new Distance(5)); Assert.assertEquals(1, found.size()); Assert.assertEquals(locatedInBuffalow.getId(), found.get(0).getId()); }
@Test public void testFindByAfter() { repo.deleteAll(); ProductBean modifiedMid2012 = createProductBean("2012", 5, true); modifiedMid2012.setLastModified(new DateTime(2012, 6, 1, 0, 0, 0, DateTimeZone.UTC).toDate()); ProductBean modifiedMid2011 = createProductBean("2011", 5, true); modifiedMid2011.setLastModified(new DateTime(2011, 6, 1, 0, 0, 0, DateTimeZone.UTC).toDate()); repo.save(Arrays.asList(modifiedMid2012, modifiedMid2011)); List<ProductBean> found = repo.findByLastModifiedAfter(new DateTime(2012, 1, 1, 0, 0, 0, DateTimeZone.UTC).toDate()); Assert.assertEquals(1, found.size()); Assert.assertEquals(modifiedMid2012.getId(), found.get(0).getId()); }
public List<License> findAll() throws SQLException { try (Connection conn = ds.getConnection()) { try (PreparedStatement statement = conn.prepareStatement("SELECT * FROM License")) { try (ResultSet resultSet = statement.executeQuery()) { List<License> licenses = new ArrayList<>(); while (resultSet.next()) { int productId = resultSet.getInt("productId"); Product productById = productRepository.getProductById(productId); int releaseId = resultSet.getInt("releaseId"); Release release = null; if (releaseId != 0) release = releaseRepository.getReleaseById(releaseId); int customerId = resultSet.getInt("customerId"); Customer customerById = customerRepository.getCustomerById(customerId); Integer licenseTypeId = getInteger(resultSet, "licenseTypeId"); LicenseType licenseType = licenseTypeId != null ? getLicenseTypeById(conn, licenseTypeId) : null; License license = getLicense(resultSet, productById, customerById, release, licenseType); licenses.add(license); } return licenses; } } } }
@Test public void testPaginationNoElementsFound() { Pageable pageable = new PageRequest(0, 2); Page<ProductBean> page = repo.findByNameStartingWith("hpotsirhc", pageable); Assert.assertEquals(0, page.getNumberOfElements()); Assert.assertTrue(page.getContent().isEmpty()); }
public List<License> findExpiringLicenses() throws SQLException { try (Connection connection = ds.getConnection()) { try (PreparedStatement stmnt = connection.prepareStatement( "Select *, DATEDIFF('DAY', CURRENT_DATE, validTill),CURRENT_DATE from License where DATEDIFF('DAY', CURRENT_DATE, validTill) < 31 AND DATEDIFF('DAY', CURRENT_DATE, validTill) > 0")) { try (ResultSet resultSet = stmnt.executeQuery()) { List<License> expiringLicenses = new ArrayList<>(); while (resultSet.next()) { int productId = resultSet.getInt("productId"); Product productById = productRepository.getProductById(productId); int releaseId = resultSet.getInt("releaseId"); Release release = null; if (releaseId != 0) release = releaseRepository.getReleaseById(releaseId); int customerId = resultSet.getInt("customerId"); Customer customerById = customerRepository.getCustomerById(customerId); Integer licenseTypeId = getInteger(resultSet, "licenseTypeId"); LicenseType licenseType = licenseTypeId != null ? getLicenseTypeById(connection, licenseTypeId) : null; License license = getLicense(resultSet, productById, customerById, release, licenseType); expiringLicenses.add(license); } return expiringLicenses; } } } }
public License findById(int id) throws SQLException { try (Connection connection = ds.getConnection()) { try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM License WHERE id = ?;")) { statement.setInt(1, id); try (ResultSet resultSet = statement.executeQuery()) { if (!resultSet.next()) { throw new SQLException("Ei leitud ühtegi litsentsi, kus rida on id-ga " + id); } int releaseId = resultSet.getInt("releaseId"); Release release = null; if (releaseId != 0) release = releaseRepository.getReleaseById(releaseId); Integer licenseTypeId = getInteger(resultSet, "licenseTypeId"); LicenseType licenseType = licenseTypeId != null ? getLicenseTypeById(connection, licenseTypeId) : null; return getLicense( resultSet, productRepository.getProductById(resultSet.getInt("productId")), customerRepository.getCustomerById(resultSet.getInt("customerId")), release, licenseType); } } } }
@Test public void testFindConcatedByAnd() { List<ProductBean> found = repo.findByPopularityAndAvailableTrue(POPULAR_AVAILABLE_PRODUCT.getPopularity()); Assert.assertEquals(1, found.size()); Assert.assertEquals(POPULAR_AVAILABLE_PRODUCT.getId(), found.get(0).getId()); }
@Test public void testSingleFilter() { List<ProductBean> found = repo.findAllFilterAvailableTrue(); Assert.assertEquals(3, found.size()); for (ProductBean bean : found) { Assert.assertTrue(bean.isAvailable()); } }
@Test public void testFindByRegex() { List<ProductBean> found = repo.findByNameRegex("na*"); Assert.assertEquals(3, found.size()); for (ProductBean bean : found) { Assert.assertTrue(bean.getName().startsWith("na")); } }
@Test public void testWithBoost() { repo.deleteAll(); ProductBean beanWithName = createProductBean("1", 5, true, "stackoverflow"); beanWithName.setTitle(Arrays.asList("indexoutofbounds")); ProductBean beanWithTitle = createProductBean("2", 5, true, "indexoutofbounds"); beanWithTitle.setTitle(Arrays.asList("stackoverflow")); repo.save(Arrays.asList(beanWithName, beanWithTitle)); List<ProductBean> found = repo.findByNameStartsWithOrTitleStartsWith("indexoutofbounds", "indexoutofbounds"); Assert.assertEquals(2, found.size()); Assert.assertEquals(beanWithTitle.getId(), found.get(0).getId()); Assert.assertEquals(beanWithName.getId(), found.get(1).getId()); }
@Test public void testFacetWithParametrizedQuery() { FacetPage<ProductBean> facetPage = repo.findAllFacetQueryPopularity(3, new PageRequest(0, 10)); Assert.assertEquals(0, facetPage.getFacetFields().size()); Page<FacetQueryEntry> facets = facetPage.getFacetQueryResult(); Assert.assertEquals(1, facets.getContent().size()); Assert.assertEquals("popularity:[* TO 3]", facets.getContent().get(0).getValue()); Assert.assertEquals(3, facets.getContent().get(0).getValueCount()); }
@Test public void testMultipleFilters() { List<ProductBean> found = repo.findAllFilterAvailableTrueAndPopularityLessThanEqual3(); Assert.assertEquals(2, found.size()); for (ProductBean bean : found) { Assert.assertTrue(bean.isAvailable()); Assert.assertTrue(bean.getPopularity() <= 3); } }
@Test public void testFindWithSortDesc() { repo.deleteAll(); List<ProductBean> values = new ArrayList<ProductBean>(); for (int i = 0; i < 10; i++) { values.add(createProductBean(Integer.toString(i), i, true)); } repo.save(values); List<ProductBean> found = repo.findByAvailableTrueOrderByPopularityDesc(); ProductBean prev = found.get(0); for (int i = 1; i < found.size(); i++) { ProductBean cur = found.get(i); Assert.assertTrue(Long.valueOf(cur.getPopularity()) < Long.valueOf(prev.getPopularity())); prev = cur; } }
@Test public void testProjectionOnFieldsForDerivedQuery() { List<ProductBean> found = repo.findByNameStartingWith("name"); for (ProductBean bean : found) { Assert.assertNotNull(bean.getName()); Assert.assertNotNull(bean.getId()); Assert.assertNull(bean.getPopularity()); } }
@Test public void testFacetOnSingleField() { FacetPage<ProductBean> facetPage = repo.findAllFacetOnPopularity(new PageRequest(0, 10)); Assert.assertEquals(1, facetPage.getFacetFields().size()); Page<FacetFieldEntry> page = facetPage.getFacetResultPage(facetPage.getFacetFields().iterator().next()); Assert.assertEquals(3, page.getContent().size()); for (FacetFieldEntry entry : page) { Assert.assertEquals("popularity", entry.getField().getName()); } }
@Test public void testFindWithSortDescInPageableForNamedQuery() { repo.deleteAll(); List<ProductBean> values = new ArrayList<ProductBean>(); for (int i = 0; i < 10; i++) { values.add(createProductBean(Integer.toString(i), i, true)); } repo.save(values); Page<ProductBean> found = repo.findByAvailableWithSort( true, new PageRequest(0, 30, new Sort(Direction.DESC, "popularity"))); ProductBean prev = found.getContent().get(0); for (int i = 1; i < found.getContent().size(); i++) { ProductBean cur = found.getContent().get(i); Assert.assertTrue(Long.valueOf(cur.getPopularity()) < Long.valueOf(prev.getPopularity())); prev = cur; } }
@Test public void testFacetWithDynamicPrefix() { FacetPage<ProductBean> facetPage = repo.findAllFacetOnNameWithDynamicPrefix("pro", new PageRequest(0, 10)); Assert.assertEquals(1, facetPage.getFacetFields().size()); Page<FacetFieldEntry> page = facetPage.getFacetResultPage("name"); Assert.assertEquals(1, page.getContent().size()); Assert.assertEquals("name", page.getContent().get(0).getField().getName()); Assert.assertEquals("product", page.getContent().get(0).getValue()); Assert.assertEquals(1, page.getContent().get(0).getValueCount()); }
@Test public void testFacetOnMulipleQueries() { FacetPage<ProductBean> facetPage = repo.findAllFacetQueryAvailableTrueAndAvailableFalse(new PageRequest(0, 10)); Assert.assertEquals(0, facetPage.getFacetFields().size()); Page<FacetQueryEntry> facets = facetPage.getFacetQueryResult(); Assert.assertEquals(2, facets.getContent().size()); Assert.assertEquals("inStock:true", facets.getContent().get(0).getValue()); Assert.assertEquals(3, facets.getContent().get(0).getValueCount()); Assert.assertEquals("inStock:false", facets.getContent().get(1).getValue()); Assert.assertEquals(1, facets.getContent().get(1).getValueCount()); }
@Before public void setUp() throws Exception { programProductRepository = new ProgramProductRepository( programRepository, programProductMapper, productRepository, programProductPriceMapper); programProduct = make(a(ProgramProductBuilder.defaultProgramProduct)); programProduct.setModifiedDate(new Date()); when(productRepository.getIdByCode("productCode")).thenReturn(1L); when(programProductMapper.getByProgramAndProductId(anyLong(), anyLong())) .thenReturn(programProduct); }
@Test public void shouldUpdateProgramProductIfExist() throws Exception { Long programId = 88L; Long productId = 99L; programProduct.setId(1L); when(programRepository.getIdByCode(anyString())).thenReturn(programId); when(productRepository.getIdByCode(anyString())).thenReturn(productId); programProductRepository.save(programProduct); assertThat(programProduct.getProgram().getId(), is(88L)); assertThat(programProduct.getProduct().getId(), is(99L)); verify(programProductMapper).update(programProduct); }
private void createProductListAndOffersMap() { productList = new ArrayList<>(); offersMap = new HashMap<>(); List<Long> productsId = optimizationParams.getProductsId(); for (Long productId : productsId) { Product product = productRepository.findOne(productId); if (product != null) { List<Offer> offerList = offerRepository.findByIdProduct(productId); if (offerList.size() > 0) { productList.add(product); offersMap.put(product, offerList); } else { missedProductList.add(new MissedProduct(product.getName())); } } else { missedProductList.add(new MissedProduct("Id: " + productId)); } } }