Skip to content
Snippets Groups Projects
Commit 7891bb86 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-3539 OZG-6824 improve code tests

parent b4f40a2c
Branches
Tags
No related merge requests found
......@@ -205,15 +205,25 @@ class UserSettingsResourceTest {
private final UserSettings userSettings = UserSettingsTestFactory.create();
private final UserSettings updatedUserSettings = UserSettingsTestFactory.create();
@BeforeEach
void mockAccess() {
doNothing().when(resource).checkUserAccess(anyString());
}
@Test
void shouldCheckAccess() {
when(userSettingsService.updateByUserId(USER_ID, userSettings)).thenReturn(Optional.of(updatedUserSettings));
when(resourceAssembler.toResource(any(), anyString(), anyString())).thenReturn(new HalEntityWrapper(null));
resource.putUserSettings(USER_ID, userSettings);
verify(resource).checkUserAccess(USER_ID);
}
@DisplayName("with empty body")
@Nested
class TestOnEmptyBody {
@BeforeEach
void mockAccess() {
doNothing().when(resource).checkUserAccess(anyString());
}
@Test
void shouldThrowFunctionalExceptionOnMissingBody() {
assertThatExceptionOfType(FunctionalException.class).isThrownBy(() -> resource.putUserSettings(USER_ID, null))
......@@ -223,50 +233,28 @@ class UserSettingsResourceTest {
@Nested
class TestUserIdNotExist {
@BeforeEach
void mockAccess() {
doNothing().when(resource).checkUserAccess(anyString());
}
@Test
void shouldThrowFunctionalExceptionIfInvalidUserId() {
var user = UserSettingsTestFactory.create();
assertThatExceptionOfType(FunctionalException.class)
.isThrownBy(() -> resource.putUserSettings("wrong_id", user))
.withMessageStartingWith("Functional error: Invalid user id");
}
void shouldThrowFunctionalException() {
when(userSettingsService.updateByUserId(USER_ID, userSettings)).thenReturn(Optional.empty());
@Test
void shouldThrowFunctionalExceptionIfEmptyUserId() {
var user = UserSettingsTestFactory.create();
assertThatExceptionOfType(FunctionalException.class)
.isThrownBy(() -> resource.putUserSettings("", user))
.isThrownBy(() -> resource.putUserSettings(USER_ID, userSettings))
.withMessageStartingWith("Functional error: Invalid user id");
}
}
@DisplayName("with filled body")
@Nested
class TestFilledBody {
@BeforeEach
void mockAccess() {
doNothing().when(resource).checkUserAccess(anyString());
when(userSettingsService.updateByUserId(anyString(), any(UserSettings.class))).thenReturn(Optional.of(updatedUserSettings));
}
@Test
void shouldCheckAccess() {
when(resourceAssembler.toResource(any(), anyString(), anyString())).thenReturn(new HalEntityWrapper(null));
resource.putUserSettings(USER_ID, userSettings);
verify(resource).checkUserAccess(USER_ID);
void setUp() {
when(userSettingsService.updateByUserId(USER_ID, userSettings)).thenReturn(Optional.of(updatedUserSettings));
}
@Test
void shouldCallUserSettingsService() {
when(resourceAssembler.toResource(any(), anyString(), anyString())).thenReturn(new HalEntityWrapper(null));
when(resourceAssembler.toResource(updatedUserSettings, USER_ID, USER_MANAGER_URL)).thenReturn(new HalEntityWrapper(null));
resource.putUserSettings(USER_ID, userSettings);
......@@ -276,7 +264,7 @@ class UserSettingsResourceTest {
@Test
void
shouldCallResourceAssembler() {
when(resourceAssembler.toResource(any(), anyString(), anyString())).thenReturn(new HalEntityWrapper(null));
when(resourceAssembler.toResource(updatedUserSettings, USER_ID, USER_MANAGER_URL)).thenReturn(new HalEntityWrapper(null));
resource.putUserSettings(USER_ID, userSettings);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment