Search in sources :

Example 1 with ITUserResponse

use of com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse in project notifications-backend by RedHatInsights.

the class ITUserServiceTest method shouldPickPrimaryEMailAsUsersEmail.

@Test
void shouldPickPrimaryEMailAsUsersEmail() {
    ITUserResponse itUserResponse = new ITUserResponse();
    final PersonalInformation personalInformation = new PersonalInformation();
    personalInformation.firstName = "myFirstname";
    personalInformation.lastNames = "myLastname";
    itUserResponse.personalInformation = personalInformation;
    Authentication authentication = new Authentication();
    authentication.principal = "myPrincipal";
    authentication.providerName = "myProviderName";
    itUserResponse.authentications = new ArrayList<>();
    itUserResponse.authentications.add(authentication);
    final AccountRelationship accountRelationship = new AccountRelationship();
    Email primaryEmail = new Email();
    primaryEmail.isPrimary = true;
    primaryEmail.address = "first_adress@trashmail.org";
    Email nonPrimaryEmail = new Email();
    nonPrimaryEmail.isPrimary = false;
    nonPrimaryEmail.address = "second_adress@trashmail.org";
    accountRelationship.emails = new ArrayList<>();
    accountRelationship.emails.add(nonPrimaryEmail);
    accountRelationship.emails.add(primaryEmail);
    itUserResponse.accountRelationships = new ArrayList<>();
    itUserResponse.accountRelationships.add(accountRelationship);
    itUserResponse.accountRelationships.get(0).permissions = List.of();
    List<ITUserResponse> itUserResponses = List.of(itUserResponse);
    when(itUserService.getUsers(any(ITUserRequest.class))).thenReturn(itUserResponses);
    final List<User> someAccountId = rbacRecipientUsersProvider.getUsers("someAccountId", "someOrgId", true);
    assertTrue(someAccountId.get(0).isActive());
    assertEquals(someAccountId.get(0).getEmail(), "first_adress@trashmail.org");
}
Also used : PersonalInformation(com.redhat.cloud.notifications.recipients.itservice.pojo.response.PersonalInformation) Email(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Email) User(com.redhat.cloud.notifications.recipients.User) AccountRelationship(com.redhat.cloud.notifications.recipients.itservice.pojo.response.AccountRelationship) Authentication(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Authentication) ITUserRequest(com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 2 with ITUserResponse

use of com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse in project notifications-backend by RedHatInsights.

the class RbacRecipientUsersProvider method getUsers.

@CacheResult(cacheName = "rbac-recipient-users-provider-get-users")
public List<User> getUsers(String accountId, String orgId, boolean adminsOnly) {
    Timer.Sample getUsersTotalTimer = Timer.start(meterRegistry);
    List<User> users;
    List<ITUserResponse> usersPaging;
    List<ITUserResponse> usersTotal = new ArrayList<>();
    int firstResult = 0;
    do {
        ITUserRequest request = new ITUserRequest(accountId, orgId, useOrgId, adminsOnly, firstResult, maxResultsPerPage);
        usersPaging = retryOnItError(() -> itUserService.getUsers(request));
        usersTotal.addAll(usersPaging);
        firstResult += maxResultsPerPage;
    } while (usersPaging.size() == maxResultsPerPage);
    LOGGER.info("usersTotal: " + usersTotal);
    users = transformToUser(usersTotal);
    // Micrometer doesn't like when tags are null and throws a NPE.
    String accountIdTag = accountId == null ? "" : accountId;
    getUsersTotalTimer.stop(meterRegistry.timer("rbac.get-users.total", "accountId", accountIdTag, "users", String.valueOf(users.size())));
    rbacUsers.set(users.size());
    return users;
}
Also used : User(com.redhat.cloud.notifications.recipients.User) Timer(io.micrometer.core.instrument.Timer) ArrayList(java.util.ArrayList) ITUserRequest(com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse) CacheResult(io.quarkus.cache.CacheResult)

Example 3 with ITUserResponse

use of com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse in project notifications-backend by RedHatInsights.

the class RbacRecipientUsersProvider method transformToUser.

List<User> transformToUser(List<ITUserResponse> itUserResponses) {
    List<User> users = new ArrayList<>();
    for (ITUserResponse itUserResponse : itUserResponses) {
        User user = new User();
        user.setUsername(itUserResponse.authentications.get(0).principal);
        final List<Email> emails = itUserResponse.accountRelationships.get(0).emails;
        for (Email email : emails) {
            if (email != null && email.isPrimary != null && email.isPrimary) {
                String address = email.address;
                user.setEmail(address);
            }
        }
        user.setAdmin(false);
        if (itUserResponse.accountRelationships != null) {
            for (AccountRelationship accountRelationship : itUserResponse.accountRelationships) {
                if (accountRelationship.permissions != null) {
                    for (Permission permission : accountRelationship.permissions) {
                        if (ORG_ADMIN_PERMISSION.equals(permission.permissionCode)) {
                            user.setAdmin(true);
                        }
                    }
                }
            }
        }
        user.setActive(true);
        user.setFirstName(itUserResponse.personalInformation.firstName);
        user.setLastName(itUserResponse.personalInformation.lastNames);
        users.add(user);
    }
    return users;
}
Also used : User(com.redhat.cloud.notifications.recipients.User) Email(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Email) AccountRelationship(com.redhat.cloud.notifications.recipients.itservice.pojo.response.AccountRelationship) ArrayList(java.util.ArrayList) Permission(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Permission) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse)

Example 4 with ITUserResponse

use of com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse in project notifications-backend by RedHatInsights.

the class RbacRecipientUsersProviderTest method createTestResponse.

private List<ITUserResponse> createTestResponse(String permissionCode) {
    List<ITUserResponse> itUserResponses = new ArrayList<>();
    ITUserResponse itUserResponse = new ITUserResponse();
    final AccountRelationship accountRelationship = new AccountRelationship();
    accountRelationship.permissions = new ArrayList<>();
    Permission permission = new Permission();
    permission.permissionCode = permissionCode;
    accountRelationship.permissions.add(permission);
    itUserResponse.authentications = new ArrayList<>();
    final Authentication authentication = new Authentication();
    authentication.principal = "somePrincipal";
    authentication.providerName = "someProviderName";
    itUserResponse.authentications.add(authentication);
    itUserResponse.accountRelationships = new ArrayList<>();
    itUserResponse.accountRelationships.add(accountRelationship);
    accountRelationship.emails = new ArrayList<>();
    itUserResponse.personalInformation = new PersonalInformation();
    itUserResponse.personalInformation.firstName = "someFirstName";
    itUserResponse.personalInformation.lastNames = "someLastName";
    itUserResponses.add(itUserResponse);
    return itUserResponses;
}
Also used : PersonalInformation(com.redhat.cloud.notifications.recipients.itservice.pojo.response.PersonalInformation) AccountRelationship(com.redhat.cloud.notifications.recipients.itservice.pojo.response.AccountRelationship) Authentication(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Authentication) ArrayList(java.util.ArrayList) Permission(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Permission) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse)

Aggregations

ITUserResponse (com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse)4 User (com.redhat.cloud.notifications.recipients.User)3 AccountRelationship (com.redhat.cloud.notifications.recipients.itservice.pojo.response.AccountRelationship)3 ArrayList (java.util.ArrayList)3 ITUserRequest (com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest)2 Authentication (com.redhat.cloud.notifications.recipients.itservice.pojo.response.Authentication)2 Email (com.redhat.cloud.notifications.recipients.itservice.pojo.response.Email)2 Permission (com.redhat.cloud.notifications.recipients.itservice.pojo.response.Permission)2 PersonalInformation (com.redhat.cloud.notifications.recipients.itservice.pojo.response.PersonalInformation)2 Timer (io.micrometer.core.instrument.Timer)1 CacheResult (io.quarkus.cache.CacheResult)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 Test (org.junit.jupiter.api.Test)1