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());
}
}
@@ -0,0 +1,97 @@
package com.solvway.services.system.service;
import com.solvway.services.system.model.CompanyPrivacyNotice;
import com.solvway.services.system.repo.CompanyPrivacyNoticeRepository;
import com.solvway.services.system.repo.CompanyRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.web.server.ResponseStatusException;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class CompanyPrivacyNoticeServiceTest {
@Mock private CompanyPrivacyNoticeRepository notices;
@Mock private CompanyRepository companies;
private CompanyPrivacyNoticeService service;
@BeforeEach
void setUp() {
service = new CompanyPrivacyNoticeService(notices, companies);
when(companies.existsById(1L)).thenReturn(true);
when(notices.save(any(CompanyPrivacyNotice.class))).thenAnswer(i -> i.getArgument(0));
}
private static CompanyPrivacyNotice notice(Long id) {
CompanyPrivacyNotice n = new CompanyPrivacyNotice();
n.setId(id);
n.setCompanyId(1L);
return n;
}
private static void assertStatus(Throwable e, int code) {
assertThat(e).isInstanceOfSatisfying(ResponseStatusException.class, r -> assertThat(r.getStatusCode().value()).isEqualTo(code));
}
@Test
void currentIsTheNewestVersion() {
CompanyPrivacyNotice newest = notice(7L);
when(notices.findFirstByCompanyIdOrderByIdDesc(1L)).thenReturn(Optional.of(newest));
assertThat(service.current(1L)).isSameAs(newest);
}
@Test
void currentWithNoNoticeIs404() {
when(notices.findFirstByCompanyIdOrderByIdDesc(1L)).thenReturn(Optional.empty());
assertThatThrownBy(() -> service.current(1L)).satisfies(e -> assertStatus(e, 404));
}
@Test
void addInsertsANewRowTrimmedAndNeverTouchesOldOnes() {
CompanyPrivacyNotice saved = service.add(1L, " English ", " Bahasa ", "admin");
assertThat(saved.getId()).isNull(); // always a fresh insert, never an update of an existing row
assertThat(saved.getCompanyId()).isEqualTo(1L);
assertThat(saved.getTextEn()).isEqualTo("English");
assertThat(saved.getTextMs()).isEqualTo("Bahasa");
assertThat(saved.getCreatedBy()).isEqualTo("admin");
assertThat(saved.getCreatedAt()).isNotNull();
verify(notices).save(any(CompanyPrivacyNotice.class));
}
@Test
void bothTextsAreRequired() {
assertThatThrownBy(() -> service.add(1L, " ", "ms", "a")).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> service.add(1L, "en", null, "a")).isInstanceOf(IllegalArgumentException.class);
verify(notices, never()).save(any());
}
@Test
void textsAreLimitedTo5000Characters() {
assertThat(service.add(1L, "e".repeat(5000), "m".repeat(5000), "a")).isNotNull();
assertThatThrownBy(() -> service.add(1L, "e".repeat(5001), "ms", "a")).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> service.add(1L, "en", "m".repeat(5001), "a")).isInstanceOf(IllegalArgumentException.class);
}
@Test
void addForUnknownCompanyIs404() {
assertThatThrownBy(() -> service.add(9L, "en", "ms", "a")).satisfies(e -> assertStatus(e, 404));
verify(notices, never()).save(any());
}
}