/
JpaMetadataSpec.groovy
78 lines (61 loc) · 2.38 KB
/
JpaMetadataSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.springframework.data.rest.repository.spec
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.data.rest.repository.RepositoryExporter
import org.springframework.data.rest.repository.RepositoryMetadata
import org.springframework.data.rest.repository.test.ApplicationConfig
import org.springframework.data.rest.repository.test.Family
import org.springframework.data.rest.repository.test.FamilyRepository
import org.springframework.data.rest.repository.test.Person
import org.springframework.data.rest.repository.test.PersonRepository
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
import javax.persistence.EntityManager
import javax.persistence.PersistenceContext
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@ContextConfiguration(classes = [ApplicationConfig])
class JpaMetadataSpec extends Specification {
@Autowired
ApplicationContext applicationContext
@PersistenceContext
EntityManager entityManager
@Autowired
List<RepositoryExporter> exporters
RepositoryMetadata metadata(name) {
exporters.find { null != it.repositoryMetadataFor(name) }?.repositoryMetadataFor(name)
}
def "finds repositories in ApplicationContext"() {
when: "find repo by String identifier"
def repo = metadata("person")?.repository()
then:
null != repo
repo instanceof PersonRepository
when: "find repo by domain Class<?>"
repo = metadata(Family)?.repository()
then:
null != repo
repo instanceof FamilyRepository
}
def "provides entity metadata"() {
given:
def personRepo = metadata(Person)?.repository()
def familyRepo = metadata(Family)?.repository()
def johnDoe = personRepo?.save(new Person("John Doe"))
def janeDoe = personRepo?.save(new Person("Jane Doe"))
def doeFamily = familyRepo?.save(new Family(
surname: "Doe",
members: [johnDoe, janeDoe]
))
when:
def personMeta = metadata(Person)?.entityMetadata()
def familyMeta = metadata(Family)?.entityMetadata()
then:
personMeta?.attribute("name")?.get(johnDoe) == "John Doe"
familyMeta?.attribute("surname")?.get(doeFamily) == "Doe"
familyMeta?.attribute("members")?.get(doeFamily)?.size() == 2
personMeta?.embeddedAttributes()?.size() == 1
familyMeta?.linkedAttributes()?.size() == 1
}
}