feat(system): versioned company privacy notice

GET and POST /company/{id}/privacyNotice: the newest row is current and
saving always adds a version, so consent records keep pointing at the
text the visitor saw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-01 12:15:16 +08:00
1 parent 85b40424ce
commit ab1643e36e
6 files changed
+323

No files matched your search

@@ -0,0 +1,99 @@
package com.solvway.services.system.controller;
import com.solvway.services.system.model.CompanyPrivacyNotice;
import com.solvway.services.system.service.CompanyPrivacyNoticeService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
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.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(MockitoExtension.class)
class CompanyPrivacyNoticeControllerTest {
@Mock private CompanyPrivacyNoticeService service;
@InjectMocks private CompanyPrivacyNoticeController controller;
private MockMvc mvc;
@BeforeEach
void setUp() {
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
private static CompanyPrivacyNotice notice() {
CompanyPrivacyNotice n = new CompanyPrivacyNotice();
n.setId(5L);
n.setCompanyId(1L);
n.setTextEn("en");
n.setTextMs("ms");
n.setCreatedBy("admin");
n.setCreatedAt(LocalDateTime.of(2026, 12, 1, 9, 0));
return n;
}
@Test
void getReturnsCurrentNotice() throws Exception {
when(service.current(1L)).thenReturn(notice());
mvc.perform(get("/company/1/privacyNotice")).andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(5)).andExpect(jsonPath("$.companyId").value(1))
.andExpect(jsonPath("$.textEn").value("en")).andExpect(jsonPath("$.textMs").value("ms"))
.andExpect(jsonPath("$.createdBy").value("admin")).andExpect(jsonPath("$.createdAt").exists());
}
@Test
void getWithNoneIs404() throws Exception {
when(service.current(1L)).thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND, "No privacy notice"));
mvc.perform(get("/company/1/privacyNotice")).andExpect(status().isNotFound());
}
@Test
void postAddsVersionAndReturnsIt() throws Exception {
when(service.add(1L, "en", "ms", "admin")).thenReturn(notice());
mvc.perform(post("/company/1/privacyNotice").contentType(MediaType.APPLICATION_JSON)
.content("{\"textEn\":\"en\",\"textMs\":\"ms\",\"doneBy\":\"admin\"}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.id").value(5));
verify(service).add(1L, "en", "ms", "admin");
}
@Test
void invalidTextIs400WithMessage() throws Exception {
when(service.add(1L, "", "ms", null)).thenThrow(new IllegalArgumentException("English text is required"));
mvc.perform(post("/company/1/privacyNotice").contentType(MediaType.APPLICATION_JSON)
.content("{\"textEn\":\"\",\"textMs\":\"ms\"}"))
.andExpect(status().isBadRequest()).andExpect(jsonPath("$.message").value("English text is required"));
}
@Test
void unknownCompanyIs404() throws Exception {
when(service.add(9L, "en", "ms", null)).thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND, "Company not found"));
mvc.perform(post("/company/9/privacyNotice").contentType(MediaType.APPLICATION_JSON)
.content("{\"textEn\":\"en\",\"textMs\":\"ms\"}"))
.andExpect(status().isNotFound());
}
@Test
void missingBodyIs400() throws Exception {
mvc.perform(post("/company/1/privacyNotice").contentType(MediaType.APPLICATION_JSON).content(""))
.andExpect(status().isBadRequest());
}
}