From 7e2153ef97fe65d41310fdf705f22c4f5d6dd999 Mon Sep 17 00:00:00 2001 From: D070615 Date: Fri, 29 Nov 2024 17:01:05 +0100 Subject: [PATCH 1/2] added tests for GenreHierarchy --- .../java/my/bookshop/GenreHierarchyTest.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 srv/src/test/java/my/bookshop/GenreHierarchyTest.java 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..8f7dd583 --- /dev/null +++ b/srv/src/test/java/my/bookshop/GenreHierarchyTest.java @@ -0,0 +1,118 @@ +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.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +@ActiveProfiles({"hybrid","default"}) +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" + + "&$apply=com.sap.vocabularies.Hierarchy.v1.TopLevels(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)" + + "&$count=true&$skip=218&$top=20")) + .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[0].parnt_ID").isEmpty()) + .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[1].parnt_ID").isEmpty()) + .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)" + + "&$count=true&$skip=0&$top=218")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.value[0].ID").value(21)) + .andExpect(jsonPath("$.value[0].name").value("Biography")) + // DistanceFromRoot: fix me? + .andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0)) + .andExpect(jsonPath("$.value[0].DrillState").value("collapsed")) + .andExpect(jsonPath("$.value[0].parnt_ID").value(20)) + .andExpect(jsonPath("$.value[1].ID").value(23)) + .andExpect(jsonPath("$.value[1].name").value("Essay")) + .andExpect(jsonPath("$.value[1].DistanceFromRoot").value(0)) + .andExpect(jsonPath("$.value[1].DrillState").value("leaf")) + .andExpect(jsonPath("$.value[1].parnt_ID").value(20)) + .andExpect(jsonPath("$.value[2].ID").value(24)) + .andExpect(jsonPath("$.value[2].name").value("Speech")) + .andExpect(jsonPath("$.value[2].DistanceFromRoot").value(0)) + .andExpect(jsonPath("$.value[2].DrillState").value("leaf")) + .andExpect(jsonPath("$.value[2].parnt_ID").value(20)) + .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].name").value("Fiction")) + .andExpect(jsonPath("$.value[0].DrillState").value("expanded")) + .andExpect(jsonPath("$.value[14].name").value("Speech")) + .andExpect(jsonPath("$.value[14].DrillState").value("leaf")) + .andExpect(jsonPath("$.value[15]").doesNotExist()); + } +} From ae813c2e44f7d1b84605459c641de20651d6c21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20G=C3=B6rler?= Date: Mon, 2 Dec 2024 13:50:02 +0100 Subject: [PATCH 2/2] test in hybrid mode --- README.md | 9 +++++++ .../java/my/bookshop/GenreHierarchyTest.java | 24 +++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) 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 index 8f7dd583..c16dd0c6 100644 --- a/srv/src/test/java/my/bookshop/GenreHierarchyTest.java +++ b/srv/src/test/java/my/bookshop/GenreHierarchyTest.java @@ -10,12 +10,11 @@ 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.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; @SpringBootTest @AutoConfigureMockMvc -@ActiveProfiles({"hybrid","default"}) +//@ActiveProfiles({"hybrid"}) public class GenreHierarchyTest { @Autowired @@ -41,20 +40,19 @@ void testCountAll() throws Exception { @WithMockUser(username = "admin") void testStartOneLevel() 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=218&$top=20")) + + "?$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[0].parnt_ID").isEmpty()) .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[1].parnt_ID").isEmpty()) .andExpect(jsonPath("$.value[2]").doesNotExist()); } @@ -65,24 +63,17 @@ void testExpandNonFiction() throws Exception { client.perform(get(genresURI + "?$select=DrillState,ID,name" + "&$apply=descendants($root/GenreHierarchy,GenreHierarchy,ID,filter(ID eq 20),1)" - + "&$count=true&$skip=0&$top=218")) + + "/orderby(ID)")) .andExpect(status().isOk()) .andExpect(jsonPath("$.value[0].ID").value(21)) .andExpect(jsonPath("$.value[0].name").value("Biography")) - // DistanceFromRoot: fix me? - .andExpect(jsonPath("$.value[0].DistanceFromRoot").value(0)) .andExpect(jsonPath("$.value[0].DrillState").value("collapsed")) - .andExpect(jsonPath("$.value[0].parnt_ID").value(20)) .andExpect(jsonPath("$.value[1].ID").value(23)) .andExpect(jsonPath("$.value[1].name").value("Essay")) - .andExpect(jsonPath("$.value[1].DistanceFromRoot").value(0)) .andExpect(jsonPath("$.value[1].DrillState").value("leaf")) - .andExpect(jsonPath("$.value[1].parnt_ID").value(20)) .andExpect(jsonPath("$.value[2].ID").value(24)) .andExpect(jsonPath("$.value[2].name").value("Speech")) - .andExpect(jsonPath("$.value[2].DistanceFromRoot").value(0)) .andExpect(jsonPath("$.value[2].DrillState").value("leaf")) - .andExpect(jsonPath("$.value[2].parnt_ID").value(20)) .andExpect(jsonPath("$.value[3]").doesNotExist()); } @@ -109,8 +100,11 @@ void testExpandAll() throws Exception { + "&$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());