Search in sources :

Example 1 with UserPayload

use of io.gravitee.cockpit.api.command.user.UserPayload 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();
    }
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) Logger(org.slf4j.Logger) PICTURE(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE) SUB(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) LoggerFactory(org.slf4j.LoggerFactory) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) HashMap(java.util.HashMap) Single(io.reactivex.Single) Command(io.gravitee.cockpit.api.command.Command) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Component(org.springframework.stereotype.Component) UserService(io.gravitee.rest.api.service.UserService) CommandHandler(io.gravitee.cockpit.api.command.CommandHandler) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) UserEntity(io.gravitee.rest.api.model.UserEntity) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) HashMap(java.util.HashMap) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply)

Example 2 with UserPayload

use of io.gravitee.cockpit.api.command.user.UserPayload 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));
}
Also used : UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Test(org.junit.Test)

Example 3 with UserPayload

use of io.gravitee.cockpit.api.command.user.UserPayload 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));
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Test(org.junit.Test)

Example 4 with UserPayload

use of io.gravitee.cockpit.api.command.user.UserPayload 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));
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) PICTURE(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE) SUB(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB) Mock(org.mockito.Mock) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Command(io.gravitee.cockpit.api.command.Command) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply) UserService(io.gravitee.rest.api.service.UserService) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) UserEntity(io.gravitee.rest.api.model.UserEntity) Before(org.junit.Before) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) HashMap(java.util.HashMap) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Test(org.junit.Test)

Example 5 with UserPayload

use of io.gravitee.cockpit.api.command.user.UserPayload 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));
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) ArgumentMatchers(org.mockito.ArgumentMatchers) PICTURE(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE) SUB(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB) Mock(org.mockito.Mock) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Command(io.gravitee.cockpit.api.command.Command) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply) UserService(io.gravitee.rest.api.service.UserService) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) UserEntity(io.gravitee.rest.api.model.UserEntity) Before(org.junit.Before) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) HashMap(java.util.HashMap) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Test(org.junit.Test)

Aggregations

UserCommand (io.gravitee.cockpit.api.command.user.UserCommand)5 UserPayload (io.gravitee.cockpit.api.command.user.UserPayload)5 UserReply (io.gravitee.cockpit.api.command.user.UserReply)5 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)5 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)4 UserEntity (io.gravitee.rest.api.model.UserEntity)4 UserNotFoundException (io.gravitee.rest.api.service.exceptions.UserNotFoundException)4 Test (org.junit.Test)4 Command (io.gravitee.cockpit.api.command.Command)3 CommandStatus (io.gravitee.cockpit.api.command.CommandStatus)3 PICTURE (io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE)3 SUB (io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB)3 UserService (io.gravitee.rest.api.service.UserService)3 HashMap (java.util.HashMap)3 TestObserver (io.reactivex.observers.TestObserver)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Before (org.junit.Before)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers (org.mockito.ArgumentMatchers)2 Mock (org.mockito.Mock)2