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

OZG-3539 OZG-6824 add PUT method

parent df86e538
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ import java.util.Objects; ...@@ -28,6 +28,7 @@ import java.util.Objects;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
import jakarta.ws.rs.PATCH; import jakarta.ws.rs.PATCH;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path; import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam; import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces; import jakarta.ws.rs.Produces;
...@@ -75,6 +76,7 @@ public class UserSettingsResource { ...@@ -75,6 +76,7 @@ public class UserSettingsResource {
return resourceAssembler.toResource(userSettingsService.getByUserId(userId), userId, userManagerUrl); return resourceAssembler.toResource(userSettingsService.getByUserId(userId), userId, userManagerUrl);
} }
@Deprecated
@PATCH @PATCH
@ResponseStatus(200) @ResponseStatus(200)
@Path("/{id}/settings") @Path("/{id}/settings")
...@@ -91,6 +93,22 @@ public class UserSettingsResource { ...@@ -91,6 +93,22 @@ public class UserSettingsResource {
.orElseThrow(() -> new FunctionalException(() -> "Invalid user id")); .orElseThrow(() -> new FunctionalException(() -> "Invalid user id"));
} }
@PUT
@ResponseStatus(200)
@Path("/{id}/settings")
@Produces({ MediaType.APPLICATION_JSON, RestMediaType.APPLICATION_HAL_JSON })
public HalEntityWrapper putUserSettings(@PathParam("id") String userId, UserSettings userSettings) {
checkUserAccess(userId);
if (Objects.isNull(userSettings)) {
throw new FunctionalException(() -> "Request Body missing.");
}
return userSettingsService.updateByUserId(userId, userSettings)
.map(updatedSettings -> resourceAssembler.toResource(updatedSettings, userId, userManagerUrl))
.orElseThrow(() -> new FunctionalException(() -> "Invalid user id"));
}
void checkUserAccess(String userId) { void checkUserAccess(String userId) {
var user = userService.findByExternalId(jwt.getSubject()); var user = userService.findByExternalId(jwt.getSubject());
......
...@@ -198,6 +198,90 @@ class UserSettingsResourceTest { ...@@ -198,6 +198,90 @@ class UserSettingsResourceTest {
} }
} }
@DisplayName("Update Usersettings")
@Nested
class TestPutUserSettings {
private final UserSettings userSettings = UserSettingsTestFactory.create();
private final UserSettings updatedUserSettings = UserSettingsTestFactory.create();
@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))
.withMessageStartingWith("Functional error: Request Body missing");
}
}
@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");
}
@Test
void shouldThrowFunctionalExceptionIfEmptyUserId() {
var user = UserSettingsTestFactory.create();
assertThatExceptionOfType(FunctionalException.class)
.isThrownBy(() -> resource.putUserSettings("", user))
.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);
}
@Test
void shouldCallUserSettingsService() {
when(resourceAssembler.toResource(any(), anyString(), anyString())).thenReturn(new HalEntityWrapper(null));
resource.putUserSettings(USER_ID, userSettings);
verify(userSettingsService).updateByUserId(USER_ID, userSettings);
}
@Test
void
shouldCallResourceAssembler() {
when(resourceAssembler.toResource(any(), anyString(), anyString())).thenReturn(new HalEntityWrapper(null));
resource.putUserSettings(USER_ID, userSettings);
verify(resourceAssembler).toResource(updatedUserSettings, USER_ID, USER_MANAGER_URL);
}
}
}
@DisplayName("Check User access") @DisplayName("Check User access")
@Nested @Nested
class TestCheckUserAccess { class TestCheckUserAccess {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment