|
| 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