feat(system): language registry with guarded updates and a catalog version
Ported from babyboo locale-registry: a duplicate code, a code change, switching English off and switching off the last enabled language are refused as 409s with a message code; every accepted change bumps the catalog version. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
075a3a34d1
commit
4622a5219d
9 files changed
+359
-1
No files matched your search
+139
@@ -0,0 +1,139 @@
|
||||
package com.solvway.services.system.service;
|
||||
|
||||
import com.solvway.services.system.model.SupportedLocale;
|
||||
import com.solvway.services.system.repo.SupportedLocaleRepository;
|
||||
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.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
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;
|
||||
|
||||
/** Language registry rules ported from babyboo locale-registry: duplicates, locked code, English and last enabled, version bump. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SupportedLocaleServiceTest {
|
||||
|
||||
@Mock
|
||||
private SupportedLocaleRepository repository;
|
||||
@Mock
|
||||
private CatalogVersionService catalogVersion;
|
||||
@InjectMocks
|
||||
private SupportedLocaleService service;
|
||||
|
||||
@Test
|
||||
void enabledListComesInOrder() {
|
||||
List<SupportedLocale> enabled = List.of(locale(1L, "en_US", true), locale(2L, "zh_CN", true));
|
||||
when(repository.findByEnabledTrueOrderBySortOrderAsc()).thenReturn(enabled);
|
||||
|
||||
assertThat(service.findAllEnabled()).isEqualTo(enabled);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addingANewCodeSavesAndBumpsTheVersion() {
|
||||
SupportedLocale ms = locale(null, "ms_MY", false);
|
||||
when(repository.findByCode("ms_MY")).thenReturn(Optional.empty());
|
||||
when(repository.save(ms)).thenReturn(ms);
|
||||
|
||||
assertThat(service.save(ms)).isSameAs(ms);
|
||||
verify(catalogVersion).bump();
|
||||
}
|
||||
|
||||
@Test
|
||||
void addingAnExistingCodeIsRefusedWithoutABump() {
|
||||
when(repository.findByCode("zh_CN")).thenReturn(Optional.of(locale(2L, "zh_CN", true)));
|
||||
|
||||
assertRefused(() -> service.save(locale(null, "zh_CN", true)), "LocalesDuplicateCode");
|
||||
verify(repository, never()).save(any());
|
||||
verify(catalogVersion, never()).bump();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRaceOnTheUniqueKeyIsStillADuplicate() {
|
||||
SupportedLocale ms = locale(null, "ms_MY", true);
|
||||
when(repository.findByCode("ms_MY")).thenReturn(Optional.empty());
|
||||
when(repository.save(ms)).thenThrow(new DataIntegrityViolationException("uk_supported_locale_code"));
|
||||
|
||||
assertRefused(() -> service.save(ms), "LocalesDuplicateCode");
|
||||
verify(catalogVersion, never()).bump();
|
||||
}
|
||||
|
||||
@Test
|
||||
void renamingACodeIsRefused() {
|
||||
when(repository.findById(2L)).thenReturn(Optional.of(locale(2L, "zh_CN", true)));
|
||||
|
||||
assertRefused(() -> service.update(locale(2L, "zh_TW", true)), "LocalesCodeLocked");
|
||||
verify(repository, never()).save(any());
|
||||
verify(catalogVersion, never()).bump();
|
||||
}
|
||||
|
||||
@Test
|
||||
void switchingEnglishOffIsRefused() {
|
||||
when(repository.findById(1L)).thenReturn(Optional.of(locale(1L, "en_US", true)));
|
||||
|
||||
assertRefused(() -> service.update(locale(1L, "en_US", false)), "LocalesFallbackLocked");
|
||||
verify(catalogVersion, never()).bump();
|
||||
}
|
||||
|
||||
@Test
|
||||
void switchingOffTheLastEnabledLanguageIsRefused() {
|
||||
when(repository.findById(2L)).thenReturn(Optional.of(locale(2L, "zh_CN", true)));
|
||||
when(repository.findByEnabledTrueOrderBySortOrderAsc()).thenReturn(List.of(locale(2L, "zh_CN", true)));
|
||||
|
||||
assertRefused(() -> service.update(locale(2L, "zh_CN", false)), "LocalesLastEnabledLocked");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateKeepsFieldsTheBodyLeavesOutAndBumps() {
|
||||
SupportedLocale stored = locale(3L, "ms_MY", false);
|
||||
stored.setLabel("Bahasa Melayu");
|
||||
stored.setSortOrder(3);
|
||||
when(repository.findById(3L)).thenReturn(Optional.of(stored));
|
||||
when(repository.save(stored)).thenReturn(stored);
|
||||
SupportedLocale body = new SupportedLocale();
|
||||
body.setId(3L);
|
||||
body.setEnabled(true);
|
||||
|
||||
SupportedLocale saved = service.update(body);
|
||||
|
||||
assertThat(saved.getCode()).isEqualTo("ms_MY");
|
||||
assertThat(saved.getLabel()).isEqualTo("Bahasa Melayu");
|
||||
assertThat(saved.getSortOrder()).isEqualTo(3);
|
||||
assertThat(saved.getEnabled()).isTrue();
|
||||
verify(catalogVersion).bump();
|
||||
}
|
||||
|
||||
@Test
|
||||
void updatingAnUnknownIdIsNotFound() {
|
||||
when(repository.findById(9L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.update(locale(9L, "xx", true)))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
private static void assertRefused(Runnable call, String reason) {
|
||||
assertThatThrownBy(call::run).isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||
assertThat(e.getReason()).isEqualTo(reason);
|
||||
});
|
||||
}
|
||||
|
||||
private static SupportedLocale locale(Long id, String code, boolean enabled) {
|
||||
SupportedLocale l = new SupportedLocale();
|
||||
l.setId(id);
|
||||
l.setCode(code);
|
||||
l.setEnabled(enabled);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user