Search in sources :

Example 1 with UserDto

use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.

the class SupervisingUsersReferenceDataServiceTest method testFindAll.

@Test
public void testFindAll() {
    mockArrayResponse(new UserDto[] { userDto });
    Collection<UserDto> userDtos = service.findAll(supervisoryNode, right, program);
    assertThat(userDtos, hasSize(1));
    assertThat(userDtos, hasItem(userDto));
    verify(restTemplate).exchange(uriCaptor.capture(), eq(HttpMethod.GET), entityCaptor.capture(), eq(UserDto[].class));
    URI uri = uriCaptor.getValue();
    String url = serviceUrl + "/api/supervisoryNodes/" + supervisoryNode + "/supervisingUsers";
    assertThat(uri.toString(), startsWith(url));
    assertThat(uri.toString(), containsString("rightId=" + right));
    assertThat(uri.toString(), containsString("programId=" + program));
    assertAuthHeader(entityCaptor.getValue());
    assertNull(entityCaptor.getValue().getBody());
}
Also used : UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) BaseReferenceDataServiceTest(org.openlmis.stockmanagement.service.referencedata.BaseReferenceDataServiceTest) Test(org.junit.Test)

Example 2 with UserDto

use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.

the class NotificationServiceTest method shouldNotifyUser.

@Test
public void shouldNotifyUser() throws Exception {
    UserDto user = mock(UserDto.class);
    when(user.getEmail()).thenReturn(USER_EMAIL);
    notificationService.notify(user, MAIL_SUBJECT, MAIL_CONTENT);
    ArgumentCaptor<HttpEntity> captor = ArgumentCaptor.forClass(HttpEntity.class);
    verify(restTemplate).postForObject(eq(new URI(NOTIFICATION_URL)), captor.capture(), eq(Object.class));
    assertEquals(singletonList("Bearer " + ACCESS_TOKEN), captor.getValue().getHeaders().get(HttpHeaders.AUTHORIZATION));
    assertTrue(EqualsBuilder.reflectionEquals(getNotificationRequest(user), captor.getValue().getBody()));
}
Also used : HttpEntity(org.springframework.http.HttpEntity) UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) URI(java.net.URI) Test(org.junit.Test)

Example 3 with UserDto

use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.

the class AuthenticationHelper method getCurrentUser.

/**
 * Method returns current user based on Spring context
 * and fetches his data from reference-data service.
 *
 * @return UserDto entity of current user.
 * @throws AuthenticationException if user cannot be found.
 */
public UserDto getCurrentUser() {
    UUID userId = (UUID) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserDto user = userReferenceDataService.findOne(userId);
    if (user == null) {
        throw new AuthenticationException(new Message(ERROR_USER_NOT_FOUND, userId));
    }
    return user;
}
Also used : AuthenticationException(org.openlmis.stockmanagement.exception.AuthenticationException) UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) UUID(java.util.UUID)

Example 4 with UserDto

use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.

the class StockCardService method search.

/**
 * Find stock card page by parameters. Allowed multiple id parameters.
 *
 * @param ids      collection of ids for batch fetch
 * @param pageable pagination and sorting parameters
 * @return page of filtered stock cards.
 */
public Page<StockCardDto> search(@NotNull Collection<UUID> ids, Pageable pageable) {
    OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
    Page<StockCard> page;
    if (!authentication.isClientOnly()) {
        UserDto user = authenticationHelper.getCurrentUser();
        LOGGER.info("list of ids:" + ids);
        PermissionStrings.Handler handler = permissionService.getPermissionStrings(user.getId());
        Set<PermissionStringDto> permissionStrings = handler.get();
        LOGGER.info("list of permission strings:" + permissionStrings);
        Set<UUID> facilityIds = new HashSet<>();
        Set<UUID> programIds = new HashSet<>();
        permissionStrings.stream().filter(permissionString -> STOCK_CARDS_VIEW.equalsIgnoreCase(permissionString.getRightName())).forEach(permission -> {
            facilityIds.add(permission.getFacilityId());
            programIds.add(permission.getProgramId());
        });
        LOGGER.info("list of facility ids:" + facilityIds);
        LOGGER.info("list of program ids:" + programIds);
        if (isEmpty(ids)) {
            page = cardRepository.findByFacilityIdInAndProgramIdIn(facilityIds, programIds, pageable);
        } else {
            page = cardRepository.findByFacilityIdInAndProgramIdInAndIdIn(facilityIds, programIds, ids, pageable);
        }
    } else {
        if (isEmpty(ids)) {
            page = cardRepository.findAll(pageable);
        } else {
            page = cardRepository.findByIdIn(ids, pageable);
        }
    }
    return Pagination.getPage(createDtos(page.getContent()), pageable, page.getTotalElements());
}
Also used : FacilityReferenceDataService(org.openlmis.stockmanagement.service.referencedata.FacilityReferenceDataService) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) OrderableReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService) OrganizationRepository(org.openlmis.stockmanagement.repository.OrganizationRepository) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CollectionUtils.isEmpty(org.apache.commons.collections4.CollectionUtils.isEmpty) OrderableLotIdentity.identityOf(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity.identityOf) Collections.singletonList(java.util.Collections.singletonList) StockCardLineItem(org.openlmis.stockmanagement.domain.card.StockCardLineItem) HashSet(java.util.HashSet) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Lists(com.google.common.collect.Lists) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) LotReferenceDataService(org.openlmis.stockmanagement.service.referencedata.LotReferenceDataService) Service(org.springframework.stereotype.Service) PHYSICAL_INVENTORY(org.openlmis.stockmanagement.domain.reason.ReasonCategory.PHYSICAL_INVENTORY) Pageable(org.springframework.data.domain.Pageable) StockCardRepository(org.openlmis.stockmanagement.repository.StockCardRepository) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) STOCK_CARDS_VIEW(org.openlmis.stockmanagement.service.PermissionService.STOCK_CARDS_VIEW) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node) StockEventLineItemDto(org.openlmis.stockmanagement.dto.StockEventLineItemDto) Logger(org.slf4j.Logger) PermissionStrings(org.openlmis.stockmanagement.service.referencedata.PermissionStrings) UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) Collection(java.util.Collection) Set(java.util.Set) Pagination(org.openlmis.stockmanagement.web.Pagination) UUID(java.util.UUID) NotNull(javax.validation.constraints.NotNull) Page(org.springframework.data.domain.Page) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) PermissionStringDto(org.openlmis.stockmanagement.service.referencedata.PermissionStringDto) List(java.util.List) MessageService(org.openlmis.stockmanagement.i18n.MessageService) AuthenticationHelper(org.openlmis.stockmanagement.util.AuthenticationHelper) Message(org.openlmis.stockmanagement.util.Message) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) StockCardLineItem.createLineItemFrom(org.openlmis.stockmanagement.domain.card.StockCardLineItem.createLineItemFrom) StockCard.createStockCardFrom(org.openlmis.stockmanagement.domain.card.StockCard.createStockCardFrom) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) PermissionStringDto(org.openlmis.stockmanagement.service.referencedata.PermissionStringDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) PermissionStrings(org.openlmis.stockmanagement.service.referencedata.PermissionStrings) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 5 with UserDto

use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.

the class AuthenticationHelperTest method shouldReturnUser.

@Test
public void shouldReturnUser() {
    // given
    UserDto userMock = mock(UserDto.class);
    when(userReferenceDataService.findOne(userId)).thenReturn(userMock);
    // when
    UserDto user = authenticationHelper.getCurrentUser();
    // then
    assertNotNull(user);
}
Also used : UserDto(org.openlmis.stockmanagement.dto.referencedata.UserDto) Test(org.junit.Test)

Aggregations

UserDto (org.openlmis.stockmanagement.dto.referencedata.UserDto)8 Test (org.junit.Test)3 URI (java.net.URI)2 UUID (java.util.UUID)2 Message (org.openlmis.stockmanagement.util.Message)2 Lists (com.google.common.collect.Lists)1 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 NotNull (javax.validation.constraints.NotNull)1 CollectionUtils.isEmpty (org.apache.commons.collections4.CollectionUtils.isEmpty)1 StrSubstitutor (org.apache.commons.lang.text.StrSubstitutor)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)1 StockCard.createStockCardFrom (org.openlmis.stockmanagement.domain.card.StockCard.createStockCardFrom)1 StockCardLineItem (org.openlmis.stockmanagement.domain.card.StockCardLineItem)1 StockCardLineItem.createLineItemFrom (org.openlmis.stockmanagement.domain.card.StockCardLineItem.createLineItemFrom)1 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)1