diff --git a/README.md b/README.md index 00df451e..587b31d6 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ User Interface related Features: - [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 - [Custom File Upload extension](app/admin/webapp/extension/Upload.js) which provides a button for uploading `CSV` files - A simple Swagger UI for the CatalogService API at +- UI5 Tree Table CDS Maven Plugin Features: @@ -138,6 +139,14 @@ are defined for local development: - User: `user`, password: `user` to browse books - User: `admin`, password: `admin` to manage books and orders +### Testing in hybrid mode + +You can test the `GenreHierarchyTest` on H2 using the profile `default` as well as on HANA using the profile `hybrid` + +``` +cds bind --exec -- mvn clean install -Dspring.profiles.active=hybrid +``` + ## Using VS Code 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). diff --git a/srv/src/test/java/my/bookshop/GenreHierarchyTest.java b/srv/src/test/java/my/bookshop/GenreHierarchyTest.java new file mode 100644 index 00000000..c16dd0c6 --- /dev/null +++ b/srv/src/test/java/my/bookshop/GenreHierarchyTest.java @@ -0,0 +1,112 @@ +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 org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +//@ActiveProfiles({"hybrid"}) +public class GenreHierarchyTest { + + @Autowired + private MockMvc client; + + private static final String genresURI = "/api/admin/GenreHierarchy"; + + @Test + @WithMockUser(username = "admin") + void testGetAll() throws Exception { + client.perform(get(genresURI)).andExpect(status().isOk()); + } + + @Test + @WithMockUser(username = "admin") + void testCountAll() throws Exception { + client.perform(get(genresURI + "/$count")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").value(15)); + } + + @Test + @WithMockUser(username = "admin") + void testStartOneLevel() 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(10)) + .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(20)) + .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 testExpandNonFiction() throws Exception { + client.perform(get(genresURI + + "?$select=DrillState,ID,name" + + "&$apply=descendants($root/GenreHierarchy,GenreHierarchy,ID,filter(ID eq 20),1)" + + "/orderby(ID)")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.value[0].ID").value(21)) + .andExpect(jsonPath("$.value[0].name").value("Biography")) + .andExpect(jsonPath("$.value[0].DrillState").value("collapsed")) + .andExpect(jsonPath("$.value[1].ID").value(23)) + .andExpect(jsonPath("$.value[1].name").value("Essay")) + .andExpect(jsonPath("$.value[1].DrillState").value("leaf")) + .andExpect(jsonPath("$.value[2].ID").value(24)) + .andExpect(jsonPath("$.value[2].name").value("Speech")) + .andExpect(jsonPath("$.value[2].DrillState").value("leaf")) + .andExpect(jsonPath("$.value[3]").doesNotExist()); + } + + @Test + @WithMockUser(username = "admin") + void testCollapseAll() throws Exception { + client.perform(get(genresURI + + "?$select=DrillState,ID,name" + + "&$apply=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 testExpandAll() throws Exception { + client.perform(get(genresURI + + "?$select=DistanceFromRoot,DrillState,ID,LimitedDescendantCount,name" + + "&$apply=com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID')" + + "&$count=true&$skip=0&$top=238")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.value[0].ID").value(10)) + .andExpect(jsonPath("$.value[0].name").value("Fiction")) + .andExpect(jsonPath("$.value[0].DrillState").value("expanded")) + .andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0)) + .andExpect(jsonPath("$.value[0].LimitedDescendantCount").value(9)) + .andExpect(jsonPath("$.value[14].name").value("Speech")) + .andExpect(jsonPath("$.value[14].DrillState").value("leaf")) + .andExpect(jsonPath("$.value[15]").doesNotExist()); + } +}