|
| 1 | +package org.baeldung.persistence.repository; |
| 2 | + |
| 3 | +import org.baeldung.config.StudentJpaConfig; |
| 4 | +import org.baeldung.inmemory.persistence.dao.ManyStudentRepository; |
| 5 | +import org.baeldung.inmemory.persistence.dao.ManyTagRepository; |
| 6 | +import org.baeldung.inmemory.persistence.dao.StudentRepository; |
| 7 | +import org.baeldung.inmemory.persistence.model.KVTag; |
| 8 | +import org.baeldung.inmemory.persistence.model.ManyStudent; |
| 9 | +import org.baeldung.inmemory.persistence.model.ManyTag; |
| 10 | +import org.baeldung.inmemory.persistence.model.SkillTag; |
| 11 | +import org.baeldung.inmemory.persistence.model.Student; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.springframework.test.context.ContextConfiguration; |
| 15 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 16 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 17 | +import org.springframework.transaction.annotation.Transactional; |
| 18 | + |
| 19 | +import javax.annotation.Resource; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | + |
| 26 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 27 | +@ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class) |
| 28 | +@Transactional |
| 29 | +public class AdvancedTaggingIntegrationTest { |
| 30 | + @Resource |
| 31 | + private StudentRepository studentRepository; |
| 32 | + |
| 33 | + @Resource |
| 34 | + private ManyStudentRepository manyStudentRepository; |
| 35 | + |
| 36 | + @Resource |
| 37 | + private ManyTagRepository manyTagRepository; |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenStudentWithSkillTags_whenSave_thenGetByNameAndSkillTag() { |
| 41 | + Student student = new Student(1, "Will"); |
| 42 | + SkillTag skill1 = new SkillTag("java", 5); |
| 43 | + student.setSkillTags(Arrays.asList(skill1)); |
| 44 | + studentRepository.save(student); |
| 45 | + |
| 46 | + Student student2 = new Student(2, "Joe"); |
| 47 | + SkillTag skill2 = new SkillTag("java", 1); |
| 48 | + student2.setSkillTags(Arrays.asList(skill2)); |
| 49 | + studentRepository.save(student2); |
| 50 | + |
| 51 | + List<Student> students = studentRepository.retrieveByNameFilterByMinimumSkillTag("java", 3); |
| 52 | + assertEquals("size incorrect", 1, students.size()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenStudentWithKVTags_whenSave_thenGetByTagOk(){ |
| 57 | + Student student = new Student(0, "John"); |
| 58 | + student.setKVTags(Arrays.asList(new KVTag("department", "computer science"))); |
| 59 | + studentRepository.save(student); |
| 60 | + |
| 61 | + Student student2 = new Student(1, "James"); |
| 62 | + student2.setKVTags(Arrays.asList(new KVTag("department", "humanities"))); |
| 63 | + studentRepository.save(student2); |
| 64 | + |
| 65 | + List<Student> students = studentRepository.retrieveByKeyTag("department"); |
| 66 | + assertEquals("size incorrect", 2, students.size()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenStudentWithManyTags_whenSave_theyGetByTagOk() { |
| 71 | + ManyTag tag = new ManyTag("full time"); |
| 72 | + manyTagRepository.save(tag); |
| 73 | + |
| 74 | + ManyStudent student = new ManyStudent("John"); |
| 75 | + student.setManyTags(Collections.singleton(tag)); |
| 76 | + manyStudentRepository.save(student); |
| 77 | + |
| 78 | + List<ManyStudent> students = manyStudentRepository.findByManyTags_Name("full time"); |
| 79 | + assertEquals("size incorrect", 1, students.size()); |
| 80 | + } |
| 81 | +} |
0 commit comments