Skip to content

Commit b8d8285

Browse files
authored
Test: added tests for GenreHierarchy (#404)
1 parent a8ea2f2 commit b8d8285

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ User Interface related Features:
9090
- [Model Localization](https://cap.cloud.sap/docs/guides/i18n) for [English](app/_i18n/i18n.properties) and [German](app/_i18n/i18n_de.properties) language for static texts
9191
- [Custom File Upload extension](app/admin/webapp/extension/Upload.js) which provides a button for uploading `CSV` files
9292
- A simple Swagger UI for the CatalogService API at <http://localhost:8080/swagger/index.html>
93+
- UI5 Tree Table
9394

9495
CDS Maven Plugin Features:
9596

@@ -138,6 +139,14 @@ are defined for local development:
138139
- User: `user`, password: `user` to browse books
139140
- User: `admin`, password: `admin` to manage books and orders
140141

142+
### Testing in hybrid mode
143+
144+
You can test the `GenreHierarchyTest` on H2 using the profile `default` as well as on HANA using the profile `hybrid`
145+
146+
```
147+
cds bind --exec -- mvn clean install -Dspring.profiles.active=hybrid
148+
```
149+
141150
## Using VS Code
142151

143152
VS Code supports the project out-of-the-box, when using the [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack).
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package my.bookshop;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.security.test.context.support.WithMockUser;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
15+
@SpringBootTest
16+
@AutoConfigureMockMvc
17+
//@ActiveProfiles({"hybrid"})
18+
public class GenreHierarchyTest {
19+
20+
@Autowired
21+
private MockMvc client;
22+
23+
private static final String genresURI = "/api/admin/GenreHierarchy";
24+
25+
@Test
26+
@WithMockUser(username = "admin")
27+
void testGetAll() throws Exception {
28+
client.perform(get(genresURI)).andExpect(status().isOk());
29+
}
30+
31+
@Test
32+
@WithMockUser(username = "admin")
33+
void testCountAll() throws Exception {
34+
client.perform(get(genresURI + "/$count"))
35+
.andExpect(status().isOk())
36+
.andExpect(jsonPath("$").value(15));
37+
}
38+
39+
@Test
40+
@WithMockUser(username = "admin")
41+
void testStartOneLevel() throws Exception {
42+
client.perform(get(genresURI
43+
+ "?$select=DrillState,ID,name,DistanceFromRoot"
44+
+ "&$apply=orderby(name)/"
45+
+ "com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)"
46+
+ "&$count=true"))
47+
.andExpect(status().isOk())
48+
.andExpect(jsonPath("$.value[0].ID").value(10))
49+
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
50+
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
51+
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
52+
.andExpect(jsonPath("$.value[1].ID").value(20))
53+
.andExpect(jsonPath("$.value[1].name").value("Non-Fiction"))
54+
.andExpect(jsonPath("$.value[1].DistanceFromRoot").value(0))
55+
.andExpect(jsonPath("$.value[1].DrillState").value("collapsed"))
56+
.andExpect(jsonPath("$.value[2]").doesNotExist());
57+
58+
}
59+
60+
@Test
61+
@WithMockUser(username = "admin")
62+
void testExpandNonFiction() throws Exception {
63+
client.perform(get(genresURI
64+
+ "?$select=DrillState,ID,name"
65+
+ "&$apply=descendants($root/GenreHierarchy,GenreHierarchy,ID,filter(ID eq 20),1)"
66+
+ "/orderby(ID)"))
67+
.andExpect(status().isOk())
68+
.andExpect(jsonPath("$.value[0].ID").value(21))
69+
.andExpect(jsonPath("$.value[0].name").value("Biography"))
70+
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
71+
.andExpect(jsonPath("$.value[1].ID").value(23))
72+
.andExpect(jsonPath("$.value[1].name").value("Essay"))
73+
.andExpect(jsonPath("$.value[1].DrillState").value("leaf"))
74+
.andExpect(jsonPath("$.value[2].ID").value(24))
75+
.andExpect(jsonPath("$.value[2].name").value("Speech"))
76+
.andExpect(jsonPath("$.value[2].DrillState").value("leaf"))
77+
.andExpect(jsonPath("$.value[3]").doesNotExist());
78+
}
79+
80+
@Test
81+
@WithMockUser(username = "admin")
82+
void testCollapseAll() throws Exception {
83+
client.perform(get(genresURI
84+
+ "?$select=DrillState,ID,name"
85+
+ "&$apply=com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)"
86+
+ "&$count=true&$skip=0&$top=238"))
87+
.andExpect(status().isOk())
88+
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
89+
.andExpect(jsonPath("$.value[0].DrillState").value("collapsed"))
90+
.andExpect(jsonPath("$.value[1].name").value("Non-Fiction"))
91+
.andExpect(jsonPath("$.value[1].DrillState").value("collapsed"))
92+
.andExpect(jsonPath("$.value[2]").doesNotExist());
93+
}
94+
95+
@Test
96+
@WithMockUser(username = "admin")
97+
void testExpandAll() throws Exception {
98+
client.perform(get(genresURI
99+
+ "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name"
100+
+ "&$apply=com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID')"
101+
+ "&$count=true&$skip=0&$top=238"))
102+
.andExpect(status().isOk())
103+
.andExpect(jsonPath("$.value[0].ID").value(10))
104+
.andExpect(jsonPath("$.value[0].name").value("Fiction"))
105+
.andExpect(jsonPath("$.value[0].DrillState").value("expanded"))
106+
.andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0))
107+
.andExpect(jsonPath("$.value[0].LimitedDescendantCount").value(9))
108+
.andExpect(jsonPath("$.value[14].name").value("Speech"))
109+
.andExpect(jsonPath("$.value[14].DrillState").value("leaf"))
110+
.andExpect(jsonPath("$.value[15]").doesNotExist());
111+
}
112+
}

0 commit comments

Comments
 (0)