-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathGenreHierarchyTest.java
More file actions
230 lines (213 loc) · 10.4 KB
/
GenreHierarchyTest.java
File metadata and controls
230 lines (213 loc) · 10.4 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package my.bookshop;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.net.URI;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.util.UriComponentsBuilder;
@SpringBootTest
@AutoConfigureMockMvc
class GenreHierarchyTest {
@Autowired private MockMvc client;
private static final String genresURI = "/api/browse/GenreHierarchy";
@Test
@WithMockUser(username = "admin")
void getAll() throws Exception {
client.perform(get(genresURI)).andExpect(status().isOk());
}
@Test
@WithMockUser(username = "admin")
void countAll() throws Exception {
client
.perform(get(genresURI + "/$count"))
.andExpect(status().isOk())
.andExpect(jsonPath("$").value(269));
}
@Test
@WithMockUser(username = "admin")
void startOneLevel() throws Exception {
client
.perform(
get(
genresURI
+ "?$select=DrillState,ID,name,DistanceFromRoot"
+ "&$apply=orderby(name)/"
+ "com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)"
+ "&$count=true"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].ID").value("8bbf14c6-b378-4e35-9b4f-05a9c8878001"))
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
.andExpect(jsonPath("$.value[1].ID").value("8bbf14c6-b378-4e35-9b4f-05a9c8878002"))
.andExpect(jsonPath("$.value[1].name").value("Non-Fiction"))
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[1].DrillState").value("collapsed"))
.andExpect(jsonPath("$.value[2]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void startTwoLevels() throws Exception {
client
.perform(
get(
genresURI
+ "?$select=DrillState,ID,name,DistanceFromRoot"
+ "&$apply=orderby(name)/"
+ "com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=2)"
+ "&$count=true"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[1].name").value("Action & Adventure"))
.andExpect(jsonPath("$.value[1].DrillState").value("leaf"))
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(1))
.andExpect(jsonPath("$.value[182].name").value("True Crime"))
.andExpect(jsonPath("$.value[182].DrillState").value("leaf"))
.andExpect(jsonPath("$.value[182].DistanceFromRoot").value(1))
.andExpect(jsonPath("$.value[183]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void expandNonFiction() throws Exception {
client
.perform(
get(
genresURI
+ "?$select=DrillState,ID,name"
+ "&$apply=descendants($root/GenreHierarchy,GenreHierarchy,ID,filter(ID eq 8bbf14c6-b378-4e35-9b4f-05a9c8878021),1)"
+ "/orderby(ID)"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Detective Fiction"))
.andExpect(jsonPath("$.value[0].DrillState").value("leaf"))
.andExpect(jsonPath("$.value[1]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void collapseAll() throws Exception {
client
.perform(
get(
genresURI
+ "?$select=DrillState,ID,name"
+ "&$apply=orderby(name)/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)"
+ "&$count=true&$skip=0&$top=238"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
.andExpect(jsonPath("$.value[1].name").value("Non-Fiction"))
.andExpect(jsonPath("$.value[1].DrillState").value("collapsed"))
.andExpect(jsonPath("$.value[2]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void expandAllTop100() throws Exception {
String url =
genresURI
+ "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name"
+ "&$apply=orderby(name)/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID')"
+ "&$count=true&$skip=0&$top=100";
client
.perform(get(url))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[99].name").value("New Weird"))
.andExpect(jsonPath("$.value[99].DrillState").value("leaf"))
.andExpect(jsonPath("$.value[100]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void search() throws Exception {
client
.perform(
get(
genresURI
+ "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name"
+ "&$apply=ancestors($root/GenreHierarchy,GenreHierarchy,ID,search(\"true\"),keep start)"
+ "/orderby(name)"
+ "/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID')"
+ "&$count=true"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[1].name").value("Adventure"))
.andExpect(jsonPath("$.value[1].DrillState").value("expanded"))
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(1))
.andExpect(jsonPath("$.value[2].name").value("True Adventure"))
.andExpect(jsonPath("$.value[2].DrillState").value("leaf"))
.andExpect(jsonPath("$.value[2].DistanceFromRoot").value(2))
.andExpect(jsonPath("$.value[3].name").value("Non-Fiction"))
.andExpect(jsonPath("$.value[3].DrillState").value("expanded"))
.andExpect(jsonPath("$.value[3].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[4].name").value("True Crime"))
.andExpect(jsonPath("$.value[4].DrillState").value("leaf"))
.andExpect(jsonPath("$.value[4].DistanceFromRoot").value(1))
.andExpect(jsonPath("$.value[5]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void filterNotExpanded() throws Exception {
client
.perform(
get(
genresURI
+ "?$select=DrillState,ID,name,DistanceFromRoot"
+ "&$apply=ancestors($root/GenreHierarchy,GenreHierarchy,ID,filter(name eq 'Autobiography'),keep start)/orderby(name)"
+ "/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Non-Fiction"))
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[1]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void filterExpandLevels() throws Exception {
String expandLevelsJson =
"""
[{"NodeID":"8bbf14c6-b378-4e35-9b4f-05a9c8878002","Levels":1},{"NodeID":"8bbf14c6-b378-4e35-9b4f-05a9c8878031","Levels":1}]\
""";
String unencoded =
genresURI
+ "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name"
+ "&$apply=ancestors($root/GenreHierarchy,GenreHierarchy,ID,filter(name eq 'Autobiography'),keep start)/orderby(name)"
+ "/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1,ExpandLevels="
+ expandLevelsJson
+ ")&$count=true";
String uriString = UriComponentsBuilder.fromUriString(unencoded).toUriString();
URI uri = URI.create(uriString);
client
.perform(get(uri))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Non-Fiction"))
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
.andExpect(jsonPath("$.value[2]").doesNotExist());
}
@Test
@WithMockUser(username = "admin")
void startTwoLevelsOrderByDesc() throws Exception {
client
.perform(
get(
genresURI
+ "?$select=DrillState,ID,name,DistanceFromRoot"
+ "&$apply=orderby(name desc)/"
+ "com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=2)"
+ "&$count=true"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].name").value("Non-Fiction"))
.andExpect(jsonPath("$.value[1].name").value("True Crime"))
.andExpect(jsonPath("$.value[182].name").value("Action & Adventure"))
.andExpect(jsonPath("$.value[183]").doesNotExist());
}
}