Search in sources :

Example 1 with Command

use of io.gravitee.cockpit.api.command.Command 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 Command

use of io.gravitee.cockpit.api.command.Command in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentCommandHandlerTest method handle.

@Test
public void handle() {
    EnvironmentPayload environmentPayload = new EnvironmentPayload();
    EnvironmentCommand command = new EnvironmentCommand(environmentPayload);
    environmentPayload.setId("env#1");
    environmentPayload.setCockpitId("env#cockpit-1");
    environmentPayload.setHrids(Collections.singletonList("env-1"));
    environmentPayload.setOrganizationId("orga#1");
    environmentPayload.setDescription("Environment description");
    environmentPayload.setName("Environment name");
    environmentPayload.setDomainRestrictions(Arrays.asList("domain.restriction1.io", "domain.restriction2.io"));
    when(environmentService.createOrUpdate(eq("orga#1"), eq("env#1"), argThat(newEnvironment -> newEnvironment.getCockpitId().equals(environmentPayload.getCockpitId()) && newEnvironment.getHrids().equals(environmentPayload.getHrids()) && newEnvironment.getDescription().equals(environmentPayload.getDescription()) && newEnvironment.getName().equals(environmentPayload.getName()) && newEnvironment.getDomainRestrictions().equals(environmentPayload.getDomainRestrictions())))).thenReturn(new EnvironmentEntity());
    TestObserver<EnvironmentReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
Also used : EnvironmentCommand(io.gravitee.cockpit.api.command.environment.EnvironmentCommand) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) EnvironmentService(io.gravitee.rest.api.service.EnvironmentService) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) EnvironmentPayload(io.gravitee.cockpit.api.command.environment.EnvironmentPayload) Command(io.gravitee.cockpit.api.command.Command) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) EnvironmentReply(io.gravitee.cockpit.api.command.environment.EnvironmentReply) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) EnvironmentCommand(io.gravitee.cockpit.api.command.environment.EnvironmentCommand) EnvironmentReply(io.gravitee.cockpit.api.command.environment.EnvironmentReply) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) EnvironmentPayload(io.gravitee.cockpit.api.command.environment.EnvironmentPayload) Test(org.junit.Test)

Example 3 with Command

use of io.gravitee.cockpit.api.command.Command in project gravitee-management-rest-api by gravitee-io.

the class OrganizationCommandHandlerTest method handle.

@Test
public void handle() {
    OrganizationPayload organizationPayload = new OrganizationPayload();
    OrganizationCommand command = new OrganizationCommand(organizationPayload);
    organizationPayload.setId("orga#1");
    organizationPayload.setCockpitId("org#cockpit-1");
    organizationPayload.setHrids(Collections.singletonList("orga-1"));
    organizationPayload.setDescription("Organization description");
    organizationPayload.setName("Organization name");
    organizationPayload.setDomainRestrictions(Arrays.asList("domain.restriction1.io", "domain.restriction2.io"));
    when(organizationService.createOrUpdate(eq("orga#1"), argThat(newOrganization -> newOrganization.getCockpitId().equals(organizationPayload.getCockpitId()) && newOrganization.getHrids().equals(organizationPayload.getHrids()) && newOrganization.getDescription().equals(organizationPayload.getDescription()) && newOrganization.getName().equals(organizationPayload.getName()) && newOrganization.getDomainRestrictions().equals(organizationPayload.getDomainRestrictions())))).thenReturn(new OrganizationEntity());
    TestObserver<OrganizationReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
Also used : UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) OrganizationService(io.gravitee.rest.api.service.OrganizationService) Command(io.gravitee.cockpit.api.command.Command) OrganizationPayload(io.gravitee.cockpit.api.command.organization.OrganizationPayload) OrganizationReply(io.gravitee.cockpit.api.command.organization.OrganizationReply) OrganizationCommand(io.gravitee.cockpit.api.command.organization.OrganizationCommand) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) OrganizationPayload(io.gravitee.cockpit.api.command.organization.OrganizationPayload) OrganizationReply(io.gravitee.cockpit.api.command.organization.OrganizationReply) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) OrganizationCommand(io.gravitee.cockpit.api.command.organization.OrganizationCommand) Test(org.junit.Test)

Example 4 with Command

use of io.gravitee.cockpit.api.command.Command 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 Command

use of io.gravitee.cockpit.api.command.Command 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

Command (io.gravitee.cockpit.api.command.Command)5 CommandStatus (io.gravitee.cockpit.api.command.CommandStatus)5 TestObserver (io.reactivex.observers.TestObserver)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Before (org.junit.Before)4 Test (org.junit.Test)4 RunWith (org.junit.runner.RunWith)4 ArgumentMatchers (org.mockito.ArgumentMatchers)4 Mock (org.mockito.Mock)4 Mockito.when (org.mockito.Mockito.when)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 UserCommand (io.gravitee.cockpit.api.command.user.UserCommand)3 UserPayload (io.gravitee.cockpit.api.command.user.UserPayload)3 UserReply (io.gravitee.cockpit.api.command.user.UserReply)3 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)3 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)3 UserEntity (io.gravitee.rest.api.model.UserEntity)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