-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(sandside): #200 add test for UserSettings
- Loading branch information
Showing
3 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...side/src/test/java/fr/ght1pc9kc/baywatch/security/domain/UserSettingsServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package fr.ght1pc9kc.baywatch.security.domain; | ||
|
||
import fr.ght1pc9kc.baywatch.common.api.exceptions.UnauthorizedException; | ||
import fr.ght1pc9kc.baywatch.security.api.AuthenticationFacade; | ||
import fr.ght1pc9kc.baywatch.security.api.UserSettingsService; | ||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.baywatch.security.domain.exceptions.UnauthenticatedUser; | ||
import fr.ght1pc9kc.baywatch.security.domain.ports.UserSettingsPersistencePort; | ||
import fr.ght1pc9kc.baywatch.tests.samples.UserSamples; | ||
import fr.ght1pc9kc.entity.api.Entity; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.util.Locale; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class UserSettingsServiceImplTest { | ||
private UserSettingsService tested; | ||
private AuthenticationFacade mockAuthentication; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
UserSettingsPersistencePort mockPersistence = mock(UserSettingsPersistencePort.class); | ||
doReturn(Mono.just(Entity.identify(new UserSettings(Locale.FRENCH)).withId(UserSamples.LUKE.id()))) | ||
.when(mockPersistence).get(anyString()); | ||
|
||
doAnswer(answer -> { | ||
UserSettings settings = answer.getArgument(1, UserSettings.class); | ||
return Mono.just(Entity.identify(settings).withId(UserSamples.LUKE.id())); | ||
}).when(mockPersistence).persist(anyString(), any(UserSettings.class)); | ||
|
||
mockAuthentication = mock(AuthenticationFacade.class); | ||
tested = new UserSettingsServiceImpl(mockPersistence, mockAuthentication); | ||
} | ||
|
||
@Test | ||
void should_get_user_settings() { | ||
doReturn(Mono.just(UserSamples.LUKE)).when(mockAuthentication).getConnectedUser(); | ||
|
||
StepVerifier.create(tested.get(UserSamples.LUKE.id())) | ||
.assertNext(actual -> Assertions.assertThat(actual.id()).isEqualTo(UserSamples.LUKE.id())) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
void should_fail_get_settings_on_unauthorize() { | ||
doReturn(Mono.just(UserSamples.LUKE)).when(mockAuthentication).getConnectedUser(); | ||
|
||
StepVerifier.create(tested.get(UserSamples.OBIWAN.id())) | ||
.verifyError(UnauthorizedException.class); | ||
} | ||
|
||
@Test | ||
void should_fail_get_settings_on_unauthenticated() { | ||
doReturn(Mono.empty()).when(mockAuthentication).getConnectedUser(); | ||
|
||
StepVerifier.create(tested.get(UserSamples.OBIWAN.id())) | ||
.verifyError(UnauthenticatedUser.class); | ||
} | ||
|
||
@Test | ||
void should_update_user_settings() { | ||
doReturn(Mono.just(UserSamples.LUKE)).when(mockAuthentication).getConnectedUser(); | ||
|
||
StepVerifier.create(tested.update(UserSamples.LUKE.id(), new UserSettings(Locale.FRENCH))) | ||
.assertNext(actual -> Assertions.assertThat(actual.id()).isEqualTo(UserSamples.LUKE.id())) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
void should_fail_update_settings_on_unauthorize() { | ||
doReturn(Mono.just(UserSamples.LUKE)).when(mockAuthentication).getConnectedUser(); | ||
|
||
StepVerifier.create(tested.update(UserSamples.OBIWAN.id(), new UserSettings(Locale.FRENCH))) | ||
.verifyError(UnauthorizedException.class); | ||
} | ||
|
||
@Test | ||
void should_fail_update_settings_on_unauthenticated() { | ||
doReturn(Mono.empty()).when(mockAuthentication).getConnectedUser(); | ||
|
||
StepVerifier.create(tested.update(UserSamples.OBIWAN.id(), new UserSettings(Locale.FRENCH))) | ||
.verifyError(UnauthenticatedUser.class); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...est/java/fr/ght1pc9kc/baywatch/security/infra/persistence/UserSettingsRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package fr.ght1pc9kc.baywatch.security.infra.persistence; | ||
|
||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.baywatch.security.infra.mappers.UserSettingsMapper; | ||
import fr.ght1pc9kc.baywatch.tests.samples.infra.UsersSettingsRecordSamples; | ||
import fr.ght1pc9kc.testy.core.extensions.ChainedExtension; | ||
import fr.ght1pc9kc.testy.jooq.WithDslContext; | ||
import fr.ght1pc9kc.testy.jooq.WithInMemoryDatasource; | ||
import fr.ght1pc9kc.testy.jooq.WithSampleDataLoaded; | ||
import org.assertj.core.api.Assertions; | ||
import org.jooq.DSLContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mapstruct.factory.Mappers; | ||
import reactor.core.scheduler.Schedulers; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static fr.ght1pc9kc.baywatch.dsl.tables.UsersSettings.USERS_SETTINGS; | ||
import static fr.ght1pc9kc.baywatch.tests.samples.infra.UsersSettingsRecordSamples.DSIDIOUS_SETTINGS; | ||
import static fr.ght1pc9kc.baywatch.tests.samples.infra.UsersSettingsRecordSamples.OKENOBI_SETTINGS; | ||
|
||
class UserSettingsRepositoryTest { | ||
private static final WithInMemoryDatasource wDs = WithInMemoryDatasource.builder().build(); | ||
private static final WithDslContext wDslContext = WithDslContext.builder() | ||
.setDatasourceExtension(wDs).build(); | ||
private static final WithSampleDataLoaded wSamples = WithSampleDataLoaded.builder(wDslContext) | ||
.createTablesIfNotExists() | ||
.addDataset(UsersSettingsRecordSamples.SAMPLE) | ||
.build(); | ||
|
||
@RegisterExtension | ||
@SuppressWarnings("unused") | ||
static ChainedExtension chain = ChainedExtension.outer(wDs) | ||
.append(wDslContext) | ||
.append(wSamples) | ||
.register(); | ||
|
||
private UserSettingsRepository tested; | ||
|
||
@BeforeEach | ||
void setUp(DSLContext dslContext) { | ||
UserSettingsMapper userSettingsMapper = Mappers.getMapper(UserSettingsMapper.class); | ||
tested = new UserSettingsRepository(dslContext, Schedulers.immediate(), userSettingsMapper); | ||
} | ||
|
||
@Test | ||
void should_get_settings(WithSampleDataLoaded.Tracker tracker) { | ||
tracker.skipNextSampleLoad(); | ||
StepVerifier.create(tested.get(OKENOBI_SETTINGS.getUsseUserId())) | ||
.assertNext(actual -> Assertions.assertThat(actual.self().preferredLocale()) | ||
.extracting(Locale::toLanguageTag) | ||
.isEqualTo(OKENOBI_SETTINGS.getUssePreferredLocale())) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
void should_create_settings(DSLContext dsl) { | ||
{ | ||
int count = dsl.fetchCount(USERS_SETTINGS, | ||
USERS_SETTINGS.USSE_USER_ID.eq(DSIDIOUS_SETTINGS.getUsseUserId())); | ||
Assertions.assertThat(count).isZero(); | ||
} | ||
StepVerifier.create(tested.persist(DSIDIOUS_SETTINGS.getUsseUserId(), new UserSettings(Locale.GERMANY))) | ||
.assertNext(actual -> Assertions.assertThat(actual.self().preferredLocale()) | ||
.isEqualTo(Locale.GERMANY)) | ||
.verifyComplete(); | ||
{ | ||
int count = dsl.fetchCount(USERS_SETTINGS, | ||
USERS_SETTINGS.USSE_USER_ID.eq(DSIDIOUS_SETTINGS.getUsseUserId())); | ||
Assertions.assertThat(count).isOne(); | ||
} | ||
} | ||
|
||
@Test | ||
void should_update_settings(DSLContext dsl) { | ||
{ | ||
int count = dsl.fetchCount(USERS_SETTINGS, | ||
USERS_SETTINGS.USSE_USER_ID.eq(OKENOBI_SETTINGS.getUsseUserId())); | ||
Assertions.assertThat(count).isOne(); | ||
} | ||
StepVerifier.create(tested.persist(OKENOBI_SETTINGS.getUsseUserId(), new UserSettings(Locale.GERMANY))) | ||
.assertNext(actual -> Assertions.assertThat(actual.self().preferredLocale()) | ||
.isEqualTo(Locale.GERMANY)) | ||
.verifyComplete(); | ||
{ | ||
List<String> actuals = dsl.select().from(USERS_SETTINGS) | ||
.where(USERS_SETTINGS.USSE_USER_ID.eq(OKENOBI_SETTINGS.getUsseUserId())) | ||
.fetch(USERS_SETTINGS.USSE_PREFERRED_LOCALE); | ||
Assertions.assertThat(actuals).hasSize(1); | ||
Assertions.assertThat(actuals.getFirst()).isEqualTo(Locale.GERMANY.toLanguageTag()); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...e/src/test/java/fr/ght1pc9kc/baywatch/tests/samples/infra/UsersSettingsRecordSamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package fr.ght1pc9kc.baywatch.tests.samples.infra; | ||
|
||
import fr.ght1pc9kc.baywatch.dsl.tables.UsersSettings; | ||
import fr.ght1pc9kc.baywatch.dsl.tables.records.UsersSettingsRecord; | ||
import fr.ght1pc9kc.testy.jooq.model.RelationalDataSet; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
public class UsersSettingsRecordSamples implements RelationalDataSet<UsersSettingsRecord> { | ||
public static final UsersSettingsRecordSamples SAMPLE = new UsersSettingsRecordSamples(); | ||
|
||
public static final UsersSettingsRecord OKENOBI_SETTINGS = UsersSettings.USERS_SETTINGS.newRecord() | ||
.setUsseUserId(UsersRecordSamples.OKENOBI.getUserId()) | ||
.setUssePreferredLocale(Locale.FRANCE.toLanguageTag()); | ||
|
||
public static final UsersSettingsRecord LSKYWALKER_SETTINGS = UsersSettings.USERS_SETTINGS.newRecord() | ||
.setUsseUserId(UsersRecordSamples.LSKYWALKER.getUserId()) | ||
.setUssePreferredLocale(Locale.JAPAN.toLanguageTag()); | ||
|
||
public static final UsersSettingsRecord DSIDIOUS_SETTINGS = UsersSettings.USERS_SETTINGS.newRecord() | ||
.setUsseUserId(UsersRecordSamples.DSIDIOUS.getUserId()) | ||
.setUssePreferredLocale(Locale.ENGLISH.toLanguageTag()); | ||
|
||
@Override | ||
public List<UsersSettingsRecord> records() { | ||
return List.of(OKENOBI_SETTINGS, LSKYWALKER_SETTINGS); | ||
} | ||
} |