Skip to content

Commit 81bd62e

Browse files
etimreugene-andreevagoerler
authored
Test: added additional tests for GenreHierarchy (#413)
Co-authored-by: D070615 <[email protected]> Co-authored-by: Evgeny Andreev <[email protected]> Co-authored-by: Adrian Görler <[email protected]> Co-authored-by: Evgeny Andreev <[email protected]>
1 parent ee8b025 commit 81bd62e

File tree

3 files changed

+209
-11
lines changed

3 files changed

+209
-11
lines changed

srv/src/main/java/my/bookshop/handlers/HierarchyHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private static long distanceFromRoot(GenreHierarchy gh) {
267267
return dfr;
268268
}
269269

270-
private class Sorter implements Comparator<GenreHierarchy> {
270+
static class Sorter implements Comparator<GenreHierarchy> {
271271

272272
@Override
273273
public int compare(GenreHierarchy gh1, GenreHierarchy gh2) {

srv/src/test/java/my/bookshop/GenreHierarchyTest.java

Lines changed: 138 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package my.bookshop;
22

3+
import static org.assertj.core.api.Assumptions.assumeThat;
34
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
45
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
56
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
67

8+
import java.net.URI;
79

810
import org.junit.jupiter.api.Test;
911
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,6 +16,7 @@
1416
import org.springframework.security.test.context.support.WithMockUser;
1517
import org.springframework.test.web.servlet.MockMvc;
1618
import org.springframework.test.web.servlet.ResultActions;
19+
import org.springframework.web.util.UriComponentsBuilder;
1720

1821
@SpringBootTest
1922
@AutoConfigureMockMvc
@@ -24,7 +27,7 @@ public class GenreHierarchyTest {
2427

2528
@Autowired
2629
Environment env;
27-
30+
2831
private static final String genresURI = "/api/admin/GenreHierarchy";
2932

3033
@Test
@@ -45,7 +48,7 @@ void testCountAll() throws Exception {
4548
@WithMockUser(username = "admin")
4649
void testStartOneLevel() throws Exception {
4750
client.perform(get(genresURI
48-
+ "?$select=DrillState,ID,name,DistanceFromRoot"
51+
+ "?$select=DrillState,ID,name,DistanceFromRoot"
4952
+ "&$apply=orderby(name)/"
5053
+ "com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)"
5154
+ "&$count=true"))
@@ -59,7 +62,30 @@ void testStartOneLevel() throws Exception {
5962
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(0))
6063
.andExpect(jsonPath("$.value[1].DrillState").value("collapsed"))
6164
.andExpect(jsonPath("$.value[2]").doesNotExist());
65+
}
6266

67+
@Test
68+
@WithMockUser(username = "admin")
69+
void testStartTwoLevels() throws Exception {
70+
client.perform(get(genresURI
71+
+ "?$select=DrillState,ID,name,DistanceFromRoot"
72+
+ "&$apply=orderby(name)/"
73+
+ "com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=2)"
74+
+ "&$count=true"))
75+
.andExpect(status().isOk())
76+
.andExpect(jsonPath("$.value[0].ID").value(10))
77+
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
78+
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
79+
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
80+
.andExpect(jsonPath("$.value[1].ID").value(11))
81+
.andExpect(jsonPath("$.value[1].name").value("Drama"))
82+
.andExpect(jsonPath("$.value[1].DrillState").value("leaf"))
83+
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(1))
84+
.andExpect(jsonPath("$.value[11].ID").value(21))
85+
.andExpect(jsonPath("$.value[11].name").value("Biography"))
86+
.andExpect(jsonPath("$.value[11].DrillState").value("collapsed"))
87+
.andExpect(jsonPath("$.value[11].DistanceFromRoot").value(1))
88+
.andExpect(jsonPath("$.value[14]").doesNotExist());
6389
}
6490

6591
@Test
@@ -70,7 +96,7 @@ void testExpandNonFiction() throws Exception {
7096
+ "&$apply=descendants($root/GenreHierarchy,GenreHierarchy,ID,filter(ID eq 20),1)"
7197
+ "/orderby(ID)"))
7298
.andExpect(status().isOk())
73-
.andExpect(jsonPath("$.value[0].ID").value(21))
99+
.andExpect(jsonPath("$.value[0].ID").value(21))
74100
.andExpect(jsonPath("$.value[0].name").value("Biography"))
75101
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
76102
.andExpect(jsonPath("$.value[1].ID").value(23))
@@ -100,11 +126,12 @@ void testCollapseAll() throws Exception {
100126
@Test
101127
@WithMockUser(username = "admin")
102128
void testExpandAll() throws Exception {
103-
ResultActions expectactions =
104-
client.perform(get(genresURI
129+
String url = genresURI
105130
+ "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name"
106131
+ "&$apply=com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID')"
107-
+ "&$count=true&$skip=0&$top=238"))
132+
+ "&$count=true&$skip=0&$top=238";
133+
134+
ResultActions expectations = client.perform(get(url))
108135
.andExpect(status().isOk())
109136
.andExpect(jsonPath("$.value[0].ID").value(10))
110137
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
@@ -113,8 +140,109 @@ void testExpandAll() throws Exception {
113140
.andExpect(jsonPath("$.value[14].name").value("Speech"))
114141
.andExpect(jsonPath("$.value[14].DrillState").value("leaf"))
115142
.andExpect(jsonPath("$.value[15]").doesNotExist());
116-
if (env.acceptsProfiles(Profiles.of("hybrid"))) {
117-
expectactions.andExpect(jsonPath("$.value[0].LimitedDescendantCount").value(9));
118-
}
143+
if (isOnHana()) {
144+
expectations.andExpect(jsonPath("$.value[0].LimitedDescendantCount").value(9));
145+
}
146+
}
147+
148+
@Test
149+
@WithMockUser(username = "admin")
150+
void testSearch() throws Exception {
151+
ResultActions expectations = client.perform(get(genresURI
152+
+ "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name"
153+
+ "&$apply=ancestors($root/GenreHierarchy,GenreHierarchy,ID,search(\"ry\"),keep start)"
154+
+ "/orderby(name)"
155+
+ "/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID')"
156+
+ "&$count=true"))
157+
.andExpect(status().isOk())
158+
.andExpect(jsonPath("$.value[0].ID").value(10))
159+
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
160+
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
161+
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
162+
.andExpect(jsonPath("$.value[1].ID").value(19))
163+
.andExpect(jsonPath("$.value[1].name").value("Fairy Tale"))
164+
.andExpect(jsonPath("$.value[1].DrillState").value("leaf"))
165+
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(1))
166+
.andExpect(jsonPath("$.value[2].ID").value(16))
167+
.andExpect(jsonPath("$.value[2].name").value("Mystery"))
168+
.andExpect(jsonPath("$.value[2].DrillState").value("leaf"))
169+
.andExpect(jsonPath("$.value[2].DistanceFromRoot").value(1))
170+
.andExpect(jsonPath("$.value[3].ID").value(12))
171+
.andExpect(jsonPath("$.value[3].name").value("Poetry"))
172+
.andExpect(jsonPath("$.value[3].DrillState").value("leaf"))
173+
.andExpect(jsonPath("$.value[3].DistanceFromRoot").value(1))
174+
.andExpect(jsonPath("$.value[4]").doesNotExist());
175+
if (isOnHana()) {
176+
expectations.andExpect(jsonPath("$.value[0].LimitedDescendantCount").value(3))
177+
.andExpect(jsonPath("$.value[1].LimitedDescendantCount").value(0))
178+
.andExpect(jsonPath("$.value[2].LimitedDescendantCount").value(0))
179+
.andExpect(jsonPath("$.value[3].LimitedDescendantCount").value(0));
180+
}
181+
}
182+
183+
@Test
184+
@WithMockUser(username = "admin")
185+
void testFilterNotExpanded() throws Exception {
186+
client.perform(get(genresURI
187+
+ "?$select=DrillState,ID,name,DistanceFromRoot"
188+
+ "&$apply=ancestors($root/GenreHierarchy,GenreHierarchy,ID,filter(name eq 'Autobiography'),keep start)/orderby(name)"
189+
+ "/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)"))
190+
.andExpect(status().isOk())
191+
.andExpect(jsonPath("$.value[0].ID").value(20))
192+
.andExpect(jsonPath("$.value[0].name").value("Non-Fiction"))
193+
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
194+
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
195+
.andExpect(jsonPath("$.value[1]").doesNotExist());
196+
}
197+
198+
@Ignore
199+
@Test
200+
@WithMockUser(username = "admin")
201+
void testFilterExpandLevels() throws Exception {
202+
String expandLevelsJson = """
203+
[{"NodeID":10,"Levels":1},{"NodeID":20,"Levels":1}]\
204+
""";
205+
String unencoded = genresURI + "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name"
206+
+ "&$apply=ancestors($root/GenreHierarchy,GenreHierarchy,ID,filter(name eq 'Autobiography'),keep start)/orderby(name)"
207+
+ "/com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1,ExpandLevels="
208+
+ expandLevelsJson + ")&$count=true";
209+
String uriString = UriComponentsBuilder.fromUriString(unencoded).toUriString();
210+
URI uri = URI.create(uriString);
211+
client.perform(get(uri))
212+
.andExpect(status().isOk())
213+
.andExpect(jsonPath("$.value[0].ID").value(20))
214+
.andExpect(jsonPath("$.value[0].name").value("Non-Fiction"))
215+
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
216+
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
217+
.andExpect(jsonPath("$.value[2]").doesNotExist());
218+
}
219+
220+
@Test
221+
@WithMockUser(username = "admin")
222+
void testStartTwoLevelsOrderByDescHANA() throws Exception {
223+
assumeThat(env.getActiveProfiles()).contains("hybrid");
224+
client.perform(get(genresURI
225+
+ "?$select=DrillState,ID,name,DistanceFromRoot"
226+
+ "&$apply=orderby(name desc)/"
227+
+ "com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=2)"
228+
+ "&$count=true"))
229+
.andExpect(status().isOk())
230+
.andExpect(jsonPath("$.value[0].ID").value(20))
231+
.andExpect(jsonPath("$.value[0].name").value("Non-Fiction"))
232+
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
233+
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
234+
.andExpect(jsonPath("$.value[1].ID").value(24))
235+
.andExpect(jsonPath("$.value[1].name").value("Speech"))
236+
.andExpect(jsonPath("$.value[1].DrillState").value("leaf"))
237+
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(1))
238+
.andExpect(jsonPath("$.value[3].ID").value(21))
239+
.andExpect(jsonPath("$.value[3].name").value("Biography"))
240+
.andExpect(jsonPath("$.value[3].DrillState").value("collapsed"))
241+
.andExpect(jsonPath("$.value[3].DistanceFromRoot").value(1))
242+
.andExpect(jsonPath("$.value[14]").doesNotExist());
243+
}
244+
245+
private boolean isOnHana() {
246+
return env.acceptsProfiles(Profiles.of("hybrid"));
119247
}
120-
}
248+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package my.bookshop.handlers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import java.util.List;
5+
import org.junit.jupiter.api.Test;
6+
7+
import cds.gen.adminservice.GenreHierarchy;
8+
import my.bookshop.handlers.HierarchyHandler.Sorter;
9+
10+
public class HierarchyHandlerSorterTest {
11+
12+
@Test
13+
public void testSortRoots() {
14+
GenreHierarchy r1 = genre("Philosophical fiction");
15+
GenreHierarchy r2 = genre("Epic");
16+
List<GenreHierarchy> sorted = sorted(r1, r2);
17+
18+
assertEquals("Epic", sorted.get(0).getName());
19+
assertEquals("Philosophical fiction", sorted.get(1).getName());
20+
}
21+
22+
@Test
23+
public void testSortSiblings() {
24+
GenreHierarchy root = genre("Folklore");
25+
GenreHierarchy g1 = genre("Urban legend", root);
26+
GenreHierarchy g2 = genre("Fairy tale", root);
27+
List<GenreHierarchy> sorted = sorted(g1, g2);
28+
29+
assertEquals("Fairy tale", sorted.get(0).getName());
30+
assertEquals("Urban legend", sorted.get(1).getName());
31+
}
32+
33+
@Test
34+
public void testSortChildrenWithDifRoot() {
35+
GenreHierarchy r1 = genre("Thriller");
36+
GenreHierarchy r2 = genre("Folklore");
37+
GenreHierarchy g2 = genre("Urban legend", r2);
38+
List<GenreHierarchy> sorted = sorted(r1, g2);
39+
40+
assertEquals("Urban legend", sorted.get(0).getName());
41+
assertEquals("Thriller", sorted.get(1).getName());
42+
}
43+
44+
@Test
45+
public void testSortChildrenSameRoot() {
46+
GenreHierarchy r1 = genre("Folklore");
47+
GenreHierarchy g1 = genre("Urban legend", r1);
48+
List<GenreHierarchy> sorted = sorted(g1, r1);
49+
50+
assertEquals("Folklore", sorted.get(0).getName());
51+
assertEquals("Urban legend", sorted.get(1).getName());
52+
}
53+
54+
private static GenreHierarchy genre(String name, GenreHierarchy parent) {
55+
GenreHierarchy genre = GenreHierarchy.create();
56+
genre.setName(name);
57+
if (parent != null) {
58+
genre.setParent(parent);
59+
}
60+
return genre;
61+
}
62+
63+
private static GenreHierarchy genre(String name) {
64+
return genre(name, null);
65+
}
66+
67+
private static List<GenreHierarchy> sorted(GenreHierarchy... h) {
68+
return List.of(h).stream().sorted(new Sorter()).toList();
69+
}
70+
}

0 commit comments

Comments
 (0)