feat(mobile-api): admin events API with salesperson teams
/api/admin/events list, get, create, update and salesperson-options, all behind BO_EVENTS. Whole-day dates, uuid as a string, team ids checked before saving. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
70e0651a75
commit
1fa7c60fee
8 files changed
+814
-2
No files matched your search
+100
@@ -0,0 +1,100 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.dto.admin.AdminEventRequest;
|
||||
import com.solvway.mobile.api.service.AdminEventService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AdminEventControllerTest {
|
||||
|
||||
@Mock private AdminEventService events;
|
||||
@InjectMocks private AdminEventController controller;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
private static Map<String, Object> row() {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("uuid", "7123456789012345678");
|
||||
m.put("name", "Fair");
|
||||
return m;
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyHandlerIsAuthenticatedAndGuardedByBoEvents() {
|
||||
List<Method> handlers = Arrays.stream(AdminEventController.class.getDeclaredMethods())
|
||||
.filter(m -> Modifier.isPublic(m.getModifiers()) && !m.isSynthetic()).toList();
|
||||
assertThat(handlers).hasSize(5);
|
||||
for (Method m : handlers) {
|
||||
assertThat(m.isAnnotationPresent(AppAuth.class)).as(m.getName() + " @AppAuth").isTrue();
|
||||
assertThat(Arrays.asList(m.getAnnotation(AdminModule.class).value())).as(m.getName()).containsExactly("BO_EVENTS");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_uuidIsAJsonString() throws Exception {
|
||||
when(events.get("7123456789012345678")).thenReturn(row());
|
||||
mvc.perform(get("/api/admin/events/7123456789012345678")).andExpect(status().isOk())
|
||||
.andExpect(content().json("{\"uuid\":\"7123456789012345678\",\"name\":\"Fair\"}", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_passesQueryParamsAndDefaults() throws Exception {
|
||||
when(events.list(any(), any(), eq(0), eq(20), any(), any())).thenReturn(Map.of("content", List.of(row()), "totalElements", 1));
|
||||
mvc.perform(get("/api/admin/events").param("q", "food").param("enabled", "Y").param("sortBy", "endDate").param("sortDir", "desc"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json("{\"content\":[{\"uuid\":\"7123456789012345678\"}],\"totalElements\":1}"));
|
||||
verify(events).list("food", "Y", 0, 20, "endDate", "desc");
|
||||
}
|
||||
|
||||
@Test
|
||||
void salespersonOptions_isNotShadowedByTheUuidRoute() throws Exception {
|
||||
when(events.salespersonOptions()).thenReturn(List.of(Map.of("id", 1, "name", "Amy", "username", "amy")));
|
||||
mvc.perform(get("/api/admin/events/salesperson-options")).andExpect(status().isOk())
|
||||
.andExpect(content().json("[{\"id\":1,\"name\":\"Amy\",\"username\":\"amy\"}]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndUpdate_bindTheBody() throws Exception {
|
||||
when(events.create(any())).thenReturn(row());
|
||||
when(events.update(eq("77"), any())).thenReturn(row());
|
||||
String body = "{\"name\":\"Fair\",\"location\":\"KL\",\"startDate\":\"2027-01-15\",\"endDate\":\"2027-01-17\",\"enabled\":false,\"salespersonIds\":[1,2],\"extra\":1}";
|
||||
mvc.perform(post("/api/admin/events").contentType(MediaType.APPLICATION_JSON).content(body)).andExpect(status().isOk());
|
||||
mvc.perform(put("/api/admin/events/77").contentType(MediaType.APPLICATION_JSON).content(body)).andExpect(status().isOk());
|
||||
ArgumentCaptor<AdminEventRequest> c = ArgumentCaptor.forClass(AdminEventRequest.class);
|
||||
verify(events).create(c.capture());
|
||||
assertThat(c.getValue()).isEqualTo(new AdminEventRequest("Fair", "KL", "2027-01-15", "2027-01-17", false, List.of(1L, 2L)));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user