use of io.gravitee.cockpit.api.command.user.UserReply 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();
}
}
use of io.gravitee.cockpit.api.command.user.UserReply in project gravitee-management-rest-api by gravitee-io.
the class UserCommandHandlerTest method handleWithUpdateException.
@Test
public void handleWithUpdateException() {
UserPayload userPayload = new UserPayload();
UserCommand command = new UserCommand(userPayload);
final String sourceId = "user#1";
userPayload.setId(sourceId);
userPayload.setOrganizationId("orga#1");
when(userService.findBySource("cockpit", sourceId, false)).thenReturn(new UserEntity());
when(userService.update(any(), any(UpdateUserEntity.class))).thenThrow(new RuntimeException("fake error"));
TestObserver<UserReply> obs = cut.handle(command).test();
obs.awaitTerminalEvent();
obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.ERROR));
}
use of io.gravitee.cockpit.api.command.user.UserReply in project gravitee-management-rest-api by gravitee-io.
the class UserCommandHandlerTest method handleWithCreateException.
@Test
public void handleWithCreateException() {
UserPayload userPayload = new UserPayload();
UserCommand command = new UserCommand(userPayload);
final String sourceId = "user#1";
userPayload.setId(sourceId);
userPayload.setOrganizationId("orga#1");
when(userService.findBySource("cockpit", sourceId, false)).thenThrow(new UserNotFoundException(sourceId));
when(userService.create(any(NewExternalUserEntity.class), eq(false))).thenThrow(new RuntimeException("fake error"));
TestObserver<UserReply> obs = cut.handle(command).test();
obs.awaitTerminalEvent();
obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.ERROR));
}
use of io.gravitee.cockpit.api.command.user.UserReply in project gravitee-management-rest-api by gravitee-io.
the class UserCommandHandlerTest method handleUpdate.
@Test
public void handleUpdate() {
UserPayload userPayload = new UserPayload();
UserCommand command = new UserCommand(userPayload);
final String sourceId = "user#1";
userPayload.setId(sourceId);
userPayload.setOrganizationId("orga#1");
userPayload.setUsername("New Username");
userPayload.setFirstName("New Firstname");
userPayload.setLastName("New Lastname");
userPayload.setPicture("https://gravitee.io/my-new-picture");
userPayload.setEmail("my-new-email@gravitee.io");
HashMap<String, Object> additionalInformation = new HashMap<>();
additionalInformation.put("info1", "new_value1");
additionalInformation.put("new_info3", "new_value3");
userPayload.setAdditionalInformation(additionalInformation);
UserEntity existingCockpitUser = new UserEntity();
existingCockpitUser.setId("apim_user#1");
existingCockpitUser.setSourceId(sourceId);
existingCockpitUser.setSource("cockpit");
existingCockpitUser.setFirstname("Firstname");
existingCockpitUser.setLastname("Lastname");
existingCockpitUser.setEmail("email@gravitee.io");
existingCockpitUser.setPicture("https://gravitee.io/picture");
HashMap<String, Object> customFields = new HashMap<>();
customFields.put("info1", "value1");
customFields.put("info2", "value2");
existingCockpitUser.setCustomFields(customFields);
when(userService.findBySource("cockpit", sourceId, false)).thenReturn(existingCockpitUser);
when(userService.update(eq("apim_user#1"), argThat(updatedUser -> updatedUser.getFirstname().equals(userPayload.getFirstName()) && updatedUser.getLastname().equals(userPayload.getLastName()) && updatedUser.getEmail().equals(userPayload.getEmail()) && updatedUser.getPicture().equals(userPayload.getPicture()) && updatedUser.getCustomFields().get("info1").equals(additionalInformation.get("info1")) && updatedUser.getCustomFields().get("new_info3").equals(additionalInformation.get("new_info3")) && updatedUser.getCustomFields().get(PICTURE).equals(userPayload.getPicture()) && updatedUser.getCustomFields().get(SUB).equals(userPayload.getUsername())))).thenReturn(new UserEntity());
TestObserver<UserReply> obs = cut.handle(command).test();
obs.awaitTerminalEvent();
obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
use of io.gravitee.cockpit.api.command.user.UserReply in project gravitee-management-rest-api by gravitee-io.
the class UserCommandHandlerTest method handleCreation.
@Test
public void handleCreation() {
UserPayload userPayload = new UserPayload();
UserCommand command = new UserCommand(userPayload);
final String sourceId = "user#1";
userPayload.setId(sourceId);
userPayload.setOrganizationId("orga#1");
userPayload.setUsername("Username");
userPayload.setFirstName("Firstname");
userPayload.setLastName("Lastname");
userPayload.setPicture("https://gravitee.io/my-picture");
userPayload.setEmail("email@gravitee.io");
HashMap<String, Object> additionalInformation = new HashMap<>();
additionalInformation.put("info1", "value1");
additionalInformation.put("info2", "value2");
userPayload.setAdditionalInformation(additionalInformation);
when(userService.findBySource("cockpit", sourceId, false)).thenThrow(new UserNotFoundException(sourceId));
when(userService.create(argThat(newUser -> newUser.getSourceId().equals(userPayload.getId()) && newUser.getSource().equals("cockpit") && newUser.getFirstname().equals(userPayload.getFirstName()) && newUser.getLastname().equals(userPayload.getLastName()) && newUser.getEmail().equals(userPayload.getEmail()) && newUser.getPicture().equals(userPayload.getPicture()) && newUser.getCustomFields().get("info1").equals(additionalInformation.get("info1")) && newUser.getCustomFields().get("info2").equals(additionalInformation.get("info2")) && newUser.getCustomFields().get(PICTURE).equals(userPayload.getPicture()) && newUser.getCustomFields().get(SUB).equals(userPayload.getUsername())), eq(false))).thenReturn(new UserEntity());
TestObserver<UserReply> obs = cut.handle(command).test();
obs.awaitTerminalEvent();
obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
Aggregations