use of io.gravitee.rest.api.model.UpdateUserEntity in project gravitee-management-rest-api by gravitee-io.
the class UserResourceTest method shouldUpdateCurrentUser.
@Test
public void shouldUpdateCurrentUser() {
UserInput userInput = new UserInput();
final String newAvatar = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
final String expectedAvatar = "data:image/gif;base64,R0lGODlhyADIAPAAAAAAAP///ywAAAAAyADIAEAC/4SPqcvtD6OctNqL" + "s968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+" + "f0uv2Oz+v3/L7/DxgoOEhYaHiImKi4yNjo+AgZKTlJWWl5iZmpucnZ6fkJGio6SlpqeoqaqrrK2ur6ChsrO0tba3uLm6u7y9vr" + "+wscLDxMXGx8jJysvMzc7PwMHS09TV1tfY2drb3N3e39DR4uPk5ebn6Onq6+zt7u/g4fLz9PX29/j5+vv8/f7/8PMKDAgQQLGj" + "yIMKHChQwbOnwIMaLEiRQrWryIMaPGjQYcO3osUwAAOw==";
userInput.setAvatar(newAvatar);
userInput.setId(USER_NAME);
when(userService.update(eq(USER_NAME), any())).thenReturn(new UserEntity());
final Response response = target().request().put(Entity.json(userInput));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
ArgumentCaptor<UpdateUserEntity> user = ArgumentCaptor.forClass(UpdateUserEntity.class);
Mockito.verify(userService).update(eq(USER_NAME), user.capture());
final UpdateUserEntity updateUserEntity = user.getValue();
assertNotNull(updateUserEntity);
assertEquals(expectedAvatar, updateUserEntity.getPicture());
assertNull(updateUserEntity.getStatus());
User updateUser = response.readEntity(User.class);
assertNotNull(updateUser);
}
use of io.gravitee.rest.api.model.UpdateUserEntity in project gravitee-management-rest-api by gravitee-io.
the class DefaultUserStatusUpgrader method upgrade.
@Override
public boolean upgrade() {
// Initialize default user status
UpdateUserEntity updateUserEntity = new UpdateUserEntity();
updateUserEntity.setStatus(UserStatus.ACTIVE.name());
userService.search(new UserCriteria.Builder().noStatus().build(), new PageableImpl(1, Integer.MAX_VALUE)).getContent().forEach(userEntity -> {
if (userEntity.getStatus() == null) {
userService.update(userEntity.getId(), updateUserEntity);
}
});
return true;
}
use of io.gravitee.rest.api.model.UpdateUserEntity in project gravitee-management-rest-api by gravitee-io.
the class UserCommandHandler method handle.
@Override
public Single<UserReply> handle(UserCommand command) {
UserPayload userPayload = command.getPayload();
GraviteeContext.setCurrentOrganization(userPayload.getOrganizationId());
try {
final UserEntity existingUser = userService.findBySource(COCKPIT_SOURCE, userPayload.getId(), false);
UpdateUserEntity updatedUser = new UpdateUserEntity();
updatedUser.setFirstname(userPayload.getFirstName());
updatedUser.setLastname(userPayload.getLastName());
updatedUser.setEmail(userPayload.getEmail());
updatedUser.setPicture(userPayload.getPicture());
updatedUser.setCustomFields(new HashMap<>());
if (userPayload.getAdditionalInformation() != null) {
updatedUser.getCustomFields().putAll(userPayload.getAdditionalInformation());
}
updatedUser.getCustomFields().computeIfAbsent(PICTURE, k -> userPayload.getPicture());
updatedUser.getCustomFields().computeIfAbsent(SUB, k -> userPayload.getUsername());
UserEntity cockpitUserEntity = userService.update(existingUser.getId(), updatedUser);
logger.info("User [{}] with APIM id [{}] updated.", userPayload.getUsername(), cockpitUserEntity.getId());
return Single.just(new UserReply(command.getId(), CommandStatus.SUCCEEDED));
} catch (UserNotFoundException unfe) {
NewExternalUserEntity newUser = new NewExternalUserEntity();
newUser.setSourceId(userPayload.getId());
newUser.setFirstname(userPayload.getFirstName());
newUser.setLastname(userPayload.getLastName());
newUser.setEmail(userPayload.getEmail());
newUser.setPicture(userPayload.getPicture());
newUser.setSource(COCKPIT_SOURCE);
newUser.setCustomFields(new HashMap<>());
if (userPayload.getAdditionalInformation() != null) {
newUser.getCustomFields().putAll(userPayload.getAdditionalInformation());
}
newUser.getCustomFields().computeIfAbsent(PICTURE, k -> userPayload.getPicture());
newUser.getCustomFields().computeIfAbsent(SUB, k -> userPayload.getUsername());
try {
UserEntity cockpitUserEntity = userService.create(newUser, false);
logger.info("User [{}] created with APIM id [{}].", userPayload.getUsername(), cockpitUserEntity.getId());
return Single.just(new UserReply(command.getId(), CommandStatus.SUCCEEDED));
} catch (Exception e) {
logger.info("Error occurred when creating user [{}] for organization [{}].", userPayload.getUsername(), userPayload.getOrganizationId(), e);
return Single.just(new UserReply(command.getId(), CommandStatus.ERROR));
}
} catch (Exception e) {
logger.info("Error occurred when updating user [{}] for organization [{}].", userPayload.getUsername(), userPayload.getOrganizationId(), e);
return Single.just(new UserReply(command.getId(), CommandStatus.ERROR));
} finally {
GraviteeContext.cleanContext();
}
}
Aggregations