11package my .bookshop ;
22
3+ import static org .assertj .core .api .Assumptions .assumeThat ;
34import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
45import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
56import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
67
8+ import java .net .URI ;
79
810import org .junit .jupiter .api .Test ;
911import org .springframework .beans .factory .annotation .Autowired ;
1416import org .springframework .security .test .context .support .WithMockUser ;
1517import org .springframework .test .web .servlet .MockMvc ;
1618import 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+ }
0 commit comments