BAEL-2520 code changes done (#6446)
This commit is contained in:
parent
725a6e04d5
commit
b577e94b5c
|
@ -1,5 +1,7 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
|
@ -12,4 +14,8 @@ public interface CustomItemRepository {
|
|||
Item findItemById(Long id);
|
||||
|
||||
void findThenDelete(Long id);
|
||||
|
||||
List<Item> findItemsByColorAndGrade();
|
||||
|
||||
List<Item> findItemByColorOrGrade();
|
||||
}
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@Repository
|
||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
||||
|
@ -29,4 +35,54 @@ public class CustomItemRepositoryImpl implements CustomItemRepository {
|
|||
final Item item = entityManager.find(Item.class, id);
|
||||
entityManager.remove(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemsByColorAndGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForColor = criteriaBuilder.or(predicateForBlueColor, predicateForRedColor);
|
||||
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "A");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForGrade = criteriaBuilder.or(predicateForGradeA, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.and(predicateForColor, predicateForGrade);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemByColorOrGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "D");
|
||||
Predicate predicateForBlueColorAndGradeA = criteriaBuilder.and(predicateForBlueColor, predicateForGradeA);
|
||||
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForRedColorAndGradeB = criteriaBuilder.and(predicateForRedColor, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.or(predicateForBlueColorAndGradeA, predicateForRedColorAndGradeB);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
||||
public class CustomItemRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
CustomItemRepository customItemRepositoryImpl;
|
||||
|
||||
@Autowired
|
||||
EntityManager entityManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
Item firstItem = new Item();
|
||||
firstItem.setColor("blue");
|
||||
firstItem.setGrade("C");
|
||||
firstItem.setId(10l);
|
||||
|
||||
entityManager.persist(firstItem);
|
||||
|
||||
Item secondItem = new Item();
|
||||
secondItem.setColor("red");
|
||||
secondItem.setGrade("C");
|
||||
secondItem.setId(11l);
|
||||
|
||||
entityManager.persist(secondItem);
|
||||
|
||||
Item thirdItem = new Item();
|
||||
thirdItem.setColor("blue");
|
||||
thirdItem.setGrade("A");
|
||||
thirdItem.setId(12l);
|
||||
|
||||
entityManager.persist(thirdItem);
|
||||
|
||||
Item fourthItem = new Item();
|
||||
fourthItem.setColor("red");
|
||||
fourthItem.setGrade("D");
|
||||
fourthItem.setId(13l);
|
||||
|
||||
entityManager.persist(fourthItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemsByColorAndGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepositoryImpl.findItemsByColorAndGrade();
|
||||
|
||||
assertFalse("No items found", CollectionUtils.isEmpty(items));
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have blue color", "blue", item.getColor());
|
||||
assertEquals("this item does not belong to A grade", "A", item.getGrade());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemByColorOrGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepositoryImpl.findItemByColorOrGrade();
|
||||
|
||||
assertFalse("No items found", CollectionUtils.isEmpty(items));
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have red color", "red", item.getColor());
|
||||
assertEquals("this item does not belong to D grade", "D", item.getGrade());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue