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();
}
}
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));
}
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));
}
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));
}
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));
}
Aggregations