Example usage for org.apache.commons.collections4 CollectionUtils isNotEmpty

List of usage examples for org.apache.commons.collections4 CollectionUtils isNotEmpty

Introduction

In this page you can find the example usage for org.apache.commons.collections4 CollectionUtils isNotEmpty.

Prototype

public static boolean isNotEmpty(final Collection<?> coll) 

Source Link

Document

Null-safe check if the specified collection is not empty.

Usage

From source file:org.finra.herd.dao.IndexSearchDaoTest.java

private void testIndexSearch(String searchTerm, Set<String> fields, Set<String> match,
        List<IndexSearchFilter> searchFilters, List<String> facetList, boolean isHitHighlightingEnabled,
        boolean testExceptions, boolean setInvalidSearchResultIndexName, boolean isColumnFields)
        throws IOException {
    // Build the mocks
    SearchRequestBuilder searchRequestBuilder = mock(SearchRequestBuilder.class);
    SearchRequestBuilder searchRequestBuilderWithSource = mock(SearchRequestBuilder.class);
    SearchRequestBuilder searchRequestBuilderWithSize = mock(SearchRequestBuilder.class);
    SearchRequestBuilder searchRequestBuilderWithSorting = mock(SearchRequestBuilder.class);
    SearchRequestBuilder searchRequestBuilderWithHighlighting = mock(SearchRequestBuilder.class);

    SearchResponse searchResponse = mock(SearchResponse.class);
    SearchHits searchHits = mock(SearchHits.class);
    SearchHit searchHit1 = mock(SearchHit.class);
    SearchHit searchHit2 = mock(SearchHit.class);
    SearchShardTarget searchShardTarget1 = mock(SearchShardTarget.class);
    SearchShardTarget searchShardTarget2 = mock(SearchShardTarget.class);
    SearchHit[] searchHitArray = new SearchHit[2];
    searchHitArray[0] = searchHit1;//from   w w  w  . j av a  2 s. c o m
    searchHitArray[1] = searchHit2;

    BoolQueryBuilder boolQueryBuilder = mock(BoolQueryBuilder.class);

    HighlightField highlightField = mock(HighlightField.class);
    when(highlightField.getName()).thenReturn("displayName");

    Text[] value = { new Text("match <hlt>fragment</hlt class=\"highlight\">"),
            new Text("<hlt class=\"highlight\">match</hlt>") };
    when(highlightField.getFragments()).thenReturn(value);

    @SuppressWarnings("unchecked")
    ListenableActionFuture<SearchResponse> listenableActionFuture = mock(ListenableActionFuture.class);

    final String highlightFieldsConfigValue = "{\"highlightFields\":[{\"fieldName\":\"displayName\",\"fragmentSize\":100,\"matchedFields\":[\"displayName\",\"displayName.stemmed\",\"displayName.ngrams\"],\"numOfFragments\":5}]}";

    final String highlightFieldsColumnMatchConfigValue = "{\"highlightFields\":[{\"fieldName\":\"columnName\",\"fragmentSize\":100,\"matchedFields\":[\"columnName\",\"columnName.stemmed\",\"columnName.ngrams\"],\"numOfFragments\":5}]}";

    // Mock the call to external methods
    when(configurationHelper.getProperty(ConfigurationValue.TAG_SHORT_DESCRIPTION_LENGTH, Integer.class))
            .thenReturn(300);
    when(configurationHelper.getProperty(ConfigurationValue.BUSINESS_OBJECT_DEFINITION_SHORT_DESCRIPTION_LENGTH,
            Integer.class)).thenReturn(300);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_NGRAMS))
            .thenReturn("{\"displayName\":\"1.0\"}");
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_STEMMED))
            .thenReturn("{\"displayName\":\"1.0\"}");
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_SHINGLES))
            .thenReturn("{\"displayName\":\"1.0\"}");
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_PRETAGS))
            .thenReturn("<hlt class=\"highlight\">");
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_POSTTAGS))
            .thenReturn("</hlt>");
    if (match != null && match.contains(MATCH_COLUMN)) {
        when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_COLUMN_MATCH_HIGHLIGHT_FIELDS))
                .thenReturn(highlightFieldsColumnMatchConfigValue);
    } else {
        when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_FIELDS))
                .thenReturn(highlightFieldsConfigValue);
    }
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_BEST_FIELDS_QUERY_BOOST, Float.class))
            .thenReturn(1f);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_PHRASE_PREFIX_QUERY_BOOST,
            Float.class)).thenReturn(1f);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_PHRASE_QUERY_BOOST, Float.class))
            .thenReturn(1f);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_PHRASE_QUERY_SLOP, Integer.class))
            .thenReturn(5);

    Map<String, String> fieldsBoostMap = new HashMap<>();
    fieldsBoostMap.put("displayName", "1.0");

    if (isColumnFields) {
        fieldsBoostMap.put(COLUMNS_NAME_FIELD, "1.0");
        fieldsBoostMap.put(SCHEMA_COLUMNS_NAME_FIELD, "1.0");
    }

    if (testExceptions) {
        when(jsonHelper.unmarshallJsonToObject(Map.class, "{\"displayName\":\"1.0\"}"))
                .thenThrow(new IOException());
    } else {
        when(jsonHelper.unmarshallJsonToObject(Map.class, "{\"displayName\":\"1.0\"}"))
                .thenReturn(fieldsBoostMap);
    }

    IndexSearchHighlightField indexSearchHighlightField = new IndexSearchHighlightField("displayName", 100,
            Arrays.asList("displayName", "displayName.stemmed", "displayName.ngrams"), 5);

    IndexSearchHighlightFields highlightFields = new IndexSearchHighlightFields(
            Collections.singletonList(indexSearchHighlightField));

    IndexSearchHighlightField indexSearchHighlightFieldColumnOnly = new IndexSearchHighlightField("columnName",
            100, Arrays.asList("columnName", "columnName.stemmed", "columnName.ngrams"), 5);

    IndexSearchHighlightFields highlightFieldsColumnOnly = new IndexSearchHighlightFields(
            Collections.singletonList(indexSearchHighlightFieldColumnOnly));

    if (testExceptions) {
        when(jsonHelper.unmarshallJsonToObject(IndexSearchHighlightFields.class, highlightFieldsConfigValue))
                .thenThrow(new IOException());
    } else {
        if (match != null && match.contains(MATCH_COLUMN)) {
            when(jsonHelper.unmarshallJsonToObject(IndexSearchHighlightFields.class,
                    highlightFieldsColumnMatchConfigValue)).thenReturn(highlightFieldsColumnOnly);
        } else {
            when(jsonHelper.unmarshallJsonToObject(IndexSearchHighlightFields.class,
                    highlightFieldsConfigValue)).thenReturn(highlightFields);
        }
    }

    when(searchRequestBuilder.setSource(any(SearchSourceBuilder.class)))
            .thenReturn(searchRequestBuilderWithSource);
    when(searchRequestBuilderWithSource.setSize(SEARCH_RESULT_SIZE)).thenReturn(searchRequestBuilderWithSize);
    when(searchRequestBuilderWithSize.addSort(any(SortBuilder.class)))
            .thenReturn(searchRequestBuilderWithSorting);
    when(searchRequestBuilderWithSorting.highlighter(any(HighlightBuilder.class)))
            .thenReturn(searchRequestBuilderWithHighlighting);

    when(searchRequestBuilder.execute()).thenReturn(listenableActionFuture);
    when(listenableActionFuture.actionGet()).thenReturn(searchResponse);
    when(searchResponse.getHits()).thenReturn(searchHits);
    when(searchHits.hits()).thenReturn(searchHitArray);
    Map<String, Object> sourceMap1 = new HashMap<>();
    Map<String, Object> tagTypeMap = new HashMap<>();
    tagTypeMap.put(CODE, TAG_TYPE_CODE);
    sourceMap1.put(TAG_TYPE, tagTypeMap);
    when(searchHit1.sourceAsMap()).thenReturn(sourceMap1);
    Map<String, Object> sourceMap2 = new HashMap<>();
    Map<String, Object> businessObjectDefinitionMap = new HashMap<>();
    businessObjectDefinitionMap.put(CODE, NAMESPACE_CODE);
    sourceMap2.put(NAMESPACE, businessObjectDefinitionMap);
    when(searchHit2.sourceAsMap()).thenReturn(sourceMap2);
    when(searchHit1.getShard()).thenReturn(searchShardTarget1);
    when(searchHit2.getShard()).thenReturn(searchShardTarget2);
    when(searchShardTarget1.getIndex()).thenReturn(TAG_SEARCH_INDEX_NAME);
    when(searchShardTarget2.getIndex()).thenReturn(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME);
    when(searchHits.getTotalHits()).thenReturn(200L);

    Map<String, HighlightField> highlightFieldMap = new HashMap<>();
    highlightFieldMap.put("displayName", highlightField);

    when(searchHit1.getHighlightFields()).thenReturn(highlightFieldMap);
    when(searchHit2.getHighlightFields()).thenReturn(highlightFieldMap);

    // Create index search request
    final IndexSearchRequest indexSearchRequest = new IndexSearchRequest(searchTerm, searchFilters, facetList,
            isHitHighlightingEnabled);

    List<TagTypeIndexSearchResponseDto> tagTypeIndexSearchResponseDtos = Collections
            .singletonList(new TagTypeIndexSearchResponseDto("code",
                    Collections.singletonList(new TagIndexSearchResponseDto("tag1", 1, null)), null));
    List<ResultTypeIndexSearchResponseDto> resultTypeIndexSearchResponseDto = Collections
            .singletonList(new ResultTypeIndexSearchResponseDto("type", 1, null));

    when(elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResponse))
            .thenReturn(tagTypeIndexSearchResponseDtos);
    when(elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResponse))
            .thenReturn(resultTypeIndexSearchResponseDto);
    when(elasticsearchHelper.getFacetsResponse(any(ElasticsearchResponseDto.class),
            eq(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), eq(TAG_SEARCH_INDEX_NAME))).thenCallRealMethod();
    when(elasticsearchHelper.addIndexSearchFilterBooleanClause(any(), any(), any()))
            .thenReturn(boolQueryBuilder);
    when(elasticsearchHelper.addFacetFieldAggregations(any(), any(SearchRequestBuilder.class)))
            .thenReturn(searchRequestBuilder);

    when(herdSearchQueryHelper.determineNegationTermsPresent(any(IndexSearchRequest.class)))
            .thenCallRealMethod();
    when(herdSearchQueryHelper.extractNegationTerms(any(IndexSearchRequest.class))).thenCallRealMethod();
    when(herdSearchQueryHelper.extractSearchPhrase(any(IndexSearchRequest.class))).thenCallRealMethod();

    SearchResult searchResult = mock(SearchResult.class);
    when(jestClientHelper.execute(any())).thenReturn(searchResult);

    List<SearchResult.Hit<Map, Void>> searchHitList = new ArrayList<>();
    Gson gson = new Gson();
    Map<String, Object> map = new HashMap<>();
    map.put(DISPLAY_NAME_SOURCE, "Display Name");
    Map<String, Object> codeMap = new HashMap<>();
    codeMap.put(CODE, "Code");
    map.put(TAG_TYPE, codeMap);
    map.put(NAMESPACE, codeMap);
    JsonElement element = gson.toJsonTree(map);
    List<String> highlightList = new ArrayList<>();
    highlightList.add("Highlight 1");
    Map<String, List<String>> highlightMap = new HashMap<>();
    highlightMap.put("field", highlightList);

    SearchResult.Hit<Map, Void> hit1 = new SearchResult(gson).new Hit(HashMap.class, element, HashMap.class,
            null, highlightMap, null, setInvalidSearchResultIndexName ? INVALID_VALUE : TAG_SEARCH_INDEX_NAME,
            TAG_SEARCH_INDEX_NAME, "type", 1.0);
    SearchResult.Hit<Map, Void> hit2 = new SearchResult(gson).new Hit(HashMap.class, element, HashMap.class,
            null, highlightMap, null, BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME,
            BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, "type", 2.0);
    searchHitList.add(hit1);
    searchHitList.add(hit2);

    when(searchResult.getHits(Map.class)).thenReturn(searchHitList);
    when(searchResult.getTotal()).thenReturn(200L);

    MetricAggregation metricAggregation = mock(MetricAggregation.class);
    TermsAggregation termsAggregation = mock(TermsAggregation.class);
    when(searchResult.getAggregations()).thenReturn(metricAggregation);
    when(metricAggregation.getTermsAggregation(TAG_TYPE_FACET_AGGS)).thenReturn(termsAggregation);
    List<TermsAggregation.Entry> buckets = new ArrayList<>();
    TermsAggregation.Entry entry1 = mock(TermsAggregation.Entry.class);
    TermsAggregation.Entry entry2 = mock(TermsAggregation.Entry.class);
    buckets.add(entry1);
    buckets.add(entry2);
    when(termsAggregation.getBuckets()).thenReturn(buckets);

    // Call the method under test
    IndexSearchResponse indexSearchResponse = indexSearchDao.indexSearch(indexSearchRequest, fields, match,
            BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME);
    List<IndexSearchResult> indexSearchResults = indexSearchResponse.getIndexSearchResults();

    assertThat("Index search results list is null.", indexSearchResults, not(nullValue()));
    assertThat(indexSearchResponse.getTotalIndexSearchResults(), is(200L));

    // Verify external method calls.
    if (StringUtils.isNotEmpty(searchTerm)) {
        verify(herdSearchQueryHelper).determineNegationTermsPresent(indexSearchRequest);
        verify(herdSearchQueryHelper).extractNegationTerms(indexSearchRequest);
        verify(herdSearchQueryHelper).extractSearchPhrase(indexSearchRequest);
        verify(configurationHelper, times(3))
                .getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_STEMMED);
        verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_NGRAMS);
        verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_SHINGLES);
        verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_PHRASE_PREFIX_QUERY_BOOST,
                Float.class);
        verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_BEST_FIELDS_QUERY_BOOST,
                Float.class);
        verify(configurationHelper, times(2)).getProperty(ConfigurationValue.ELASTICSEARCH_PHRASE_QUERY_BOOST,
                Float.class);
        verify(configurationHelper, times(5)).getProperty(ConfigurationValue.ELASTICSEARCH_PHRASE_QUERY_SLOP,
                Integer.class);
        verify(jsonHelper, times(5)).unmarshallJsonToObject(Map.class, "{\"displayName\":\"1.0\"}");
    }

    verify(configurationHelper).getProperty(ConfigurationValue.TAG_SHORT_DESCRIPTION_LENGTH, Integer.class);
    verify(configurationHelper)
            .getProperty(ConfigurationValue.BUSINESS_OBJECT_DEFINITION_SHORT_DESCRIPTION_LENGTH, Integer.class);

    if (CollectionUtils.isNotEmpty(indexSearchRequest.getIndexSearchFilters())) {
        verify(elasticsearchHelper).addIndexSearchFilterBooleanClause(
                indexSearchRequest.getIndexSearchFilters(), BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME,
                TAG_SEARCH_INDEX_NAME);
    }

    if (indexSearchRequest.isEnableHitHighlighting() != null) {
        verifyHitHighlightingInteractions(searchRequestBuilder, indexSearchRequest.isEnableHitHighlighting(),
                match);
    }

    if (CollectionUtils.isNotEmpty(indexSearchRequest.getFacetFields())) {
        verify(elasticsearchHelper).addFacetFieldAggregations(any(), any(SearchRequestBuilder.class));

        if (indexSearchRequest.getFacetFields().contains(ElasticsearchHelper.TAG_FACET)) {
            verify(elasticsearchHelper).getNestedTagTagIndexSearchResponseDto(searchResult);
            verify(elasticsearchHelper).getTagTagIndexSearchResponseDto(searchResult);
        }

        if (indexSearchRequest.getFacetFields().contains(ElasticsearchHelper.RESULT_TYPE_FACET)) {
            verify(elasticsearchHelper).getResultTypeIndexSearchResponseDto(searchResult);
        }

        verify(elasticsearchHelper).getFacetsResponse(any(ElasticsearchResponseDto.class),
                eq(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), eq(TAG_SEARCH_INDEX_NAME));
    }

    verify(jestClientHelper).execute(any());
    verify(searchResult).getTotal();
    verify(searchResult).getHits(Map.class);
    verifyNoMoreInteractions(searchRequestBuilder, searchRequestBuilderWithSource, searchRequestBuilderWithSize,
            searchRequestBuilderWithSorting, searchRequestBuilderWithHighlighting, searchResponse, searchHits,
            searchHit1, searchHit2, searchShardTarget1, searchShardTarget2, highlightField,
            listenableActionFuture, searchResult, metricAggregation, termsAggregation, entry1, entry2);
    verifyNoMoreInteractionsHelper();
}

From source file:org.finra.herd.service.helper.BusinessObjectDataDaoHelper.java

/**
 * Creates a new business object data entity from the request information.
 *
 * @param request the request.//from   w ww .  jav a 2  s .c  o  m
 * @param businessObjectFormatEntity the business object format entity.
 * @param businessObjectDataVersion the business object data version.
 *
 * @return the newly created business object data entity.
 */
private BusinessObjectDataEntity createBusinessObjectDataEntity(BusinessObjectDataCreateRequest request,
        BusinessObjectFormatEntity businessObjectFormatEntity, Integer businessObjectDataVersion,
        BusinessObjectDataStatusEntity businessObjectDataStatusEntity) {
    // Create a new entity.
    BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity();
    businessObjectDataEntity.setBusinessObjectFormat(businessObjectFormatEntity);
    businessObjectDataEntity.setPartitionValue(request.getPartitionValue());
    int subPartitionValuesCount = CollectionUtils.size(request.getSubPartitionValues());
    businessObjectDataEntity
            .setPartitionValue2(subPartitionValuesCount > 0 ? request.getSubPartitionValues().get(0) : null);
    businessObjectDataEntity
            .setPartitionValue3(subPartitionValuesCount > 1 ? request.getSubPartitionValues().get(1) : null);
    businessObjectDataEntity
            .setPartitionValue4(subPartitionValuesCount > 2 ? request.getSubPartitionValues().get(2) : null);
    businessObjectDataEntity
            .setPartitionValue5(subPartitionValuesCount > 3 ? request.getSubPartitionValues().get(3) : null);
    businessObjectDataEntity.setVersion(businessObjectDataVersion);
    businessObjectDataEntity.setLatestVersion(true);
    businessObjectDataEntity.setStatus(businessObjectDataStatusEntity);

    // Create the storage unit entities.
    businessObjectDataEntity.setStorageUnits(
            createStorageUnitEntitiesFromStorageUnits(request.getStorageUnits(), businessObjectDataEntity));

    // Create the attributes.
    List<BusinessObjectDataAttributeEntity> attributeEntities = new ArrayList<>();
    businessObjectDataEntity.setAttributes(attributeEntities);

    if (CollectionUtils.isNotEmpty(request.getAttributes())) {
        for (Attribute attribute : request.getAttributes()) {
            BusinessObjectDataAttributeEntity attributeEntity = new BusinessObjectDataAttributeEntity();
            attributeEntities.add(attributeEntity);
            attributeEntity.setBusinessObjectData(businessObjectDataEntity);
            attributeEntity.setName(attribute.getName());
            attributeEntity.setValue(attribute.getValue());
        }
    }

    // Create the parents.
    List<BusinessObjectDataEntity> businessObjectDataParents = new ArrayList<>();
    businessObjectDataEntity.setBusinessObjectDataParents(businessObjectDataParents);

    // Loop through all the business object data parents.
    if (request.getBusinessObjectDataParents() != null) {
        for (BusinessObjectDataKey businessObjectDataKey : request.getBusinessObjectDataParents()) {
            // Look up the business object data for each parent.
            BusinessObjectDataEntity businessObjectDataParent = getBusinessObjectDataEntity(
                    businessObjectDataKey);

            // Add our newly created entity as a dependent (i.e. child) of the looked up parent.
            businessObjectDataParent.getBusinessObjectDataChildren().add(businessObjectDataEntity);

            // Add the looked up parent as a parent of our newly created entity.
            businessObjectDataParents.add(businessObjectDataParent);
        }
    }

    // Return the newly created entity.
    return businessObjectDataEntity;
}

From source file:org.finra.herd.service.helper.BusinessObjectDataDaoHelper.java

/**
 * Creates a list of storage file entities from a list of storage files.
 *
 * @param storageFiles the list of storage files
 * @param storageEntity the storage entity
 * @param storageFilesDiscovered specifies whether the storage files were actually discovered in the storage
 * @param expectedS3KeyPrefix the expected S3 key prefix
 * @param storageUnitEntity the storage unit entity that storage file entities will belong to
 * @param directoryPath the storage directory path
 * @param validatePathPrefix specifies whether the storage has S3 key prefix validation enabled
 * @param validateFileExistence specifies whether the storage has file existence enabled
 * @param validateFileSize specifies whether the storage has file validation enabled
 * @param isS3StoragePlatform specifies whether the storage platform type is S3
 *
 * @return the list of storage file entities
 *//* ww w.  j  a  va2  s  .com*/
private List<StorageFileEntity> createStorageFileEntitiesFromStorageFiles(List<StorageFile> storageFiles,
        StorageEntity storageEntity, boolean storageFilesDiscovered, String expectedS3KeyPrefix,
        StorageUnitEntity storageUnitEntity, String directoryPath, boolean validatePathPrefix,
        boolean validateFileExistence, boolean validateFileSize, boolean isS3StoragePlatform) {
    List<StorageFileEntity> storageFileEntities = null;

    // Process storage files if they are specified.
    if (CollectionUtils.isNotEmpty(storageFiles)) {
        storageFileEntities = new ArrayList<>();
        storageUnitEntity.setStorageFiles(storageFileEntities);

        // If the validate file existence flag is configured for this storage and storage files were not discovered, prepare for S3 file validation.
        S3FileTransferRequestParamsDto params = null;
        Map<String, StorageFile> actualS3Keys = null;
        if (validateFileExistence && isS3StoragePlatform && !storageFilesDiscovered) {
            // Get the validate file parameters.
            params = getFileValidationParams(storageEntity, expectedS3KeyPrefix, storageUnitEntity,
                    validatePathPrefix);

            // When listing S3 files, we ignore 0 byte objects that represent S3 directories.
            actualS3Keys = storageFileHelper
                    .getStorageFilesMapFromS3ObjectSummaries(s3Service.listDirectory(params, true));
        }

        // If the validate path prefix flag is configured, ensure that there are no storage files already registered in this
        // storage by some other business object data that start with the expected S3 key prefix.
        if (validatePathPrefix && isS3StoragePlatform) {
            // Since the S3 key prefix represents a directory, we add a trailing '/' character to it.
            String expectedS3KeyPrefixWithTrailingSlash = expectedS3KeyPrefix + "/";
            Long registeredStorageFileCount = storageFileDao.getStorageFileCount(storageEntity.getName(),
                    expectedS3KeyPrefixWithTrailingSlash);
            if (registeredStorageFileCount > 0) {
                throw new AlreadyExistsException(String.format(
                        "Found %d storage file(s) matching \"%s\" S3 key prefix in \"%s\" "
                                + "storage that is registered with another business object data.",
                        registeredStorageFileCount, expectedS3KeyPrefix, storageEntity.getName()));
            }
        }

        for (StorageFile storageFile : storageFiles) {
            StorageFileEntity storageFileEntity = new StorageFileEntity();
            storageFileEntities.add(storageFileEntity);
            storageFileEntity.setStorageUnit(storageUnitEntity);
            storageFileEntity.setPath(storageFile.getFilePath());
            storageFileEntity.setFileSizeBytes(storageFile.getFileSizeBytes());
            storageFileEntity.setRowCount(storageFile.getRowCount());

            // Skip storage file validation if storage files were discovered.
            if (!storageFilesDiscovered) {
                // Validate that the storage file path matches the key prefix if the validate path prefix flag is configured for this storage.
                // Otherwise, if a directory path is specified, ensure it is consistent with the file path.
                if (validatePathPrefix && isS3StoragePlatform) {
                    // Ensure the S3 file key prefix adheres to the S3 naming convention.
                    Assert.isTrue(storageFileEntity.getPath().startsWith(expectedS3KeyPrefix), String.format(
                            "Specified storage file path \"%s\" does not match the expected S3 key prefix \"%s\".",
                            storageFileEntity.getPath(), expectedS3KeyPrefix));
                } else if (directoryPath != null) {
                    // When storage directory path is specified, ensure that storage file path starts with it.
                    Assert.isTrue(storageFileEntity.getPath().startsWith(directoryPath), String.format(
                            "Storage file path \"%s\" does not match the storage directory path \"%s\".",
                            storageFileEntity.getPath(), directoryPath));
                }

                // Ensure the file exists in S3 if the validate file existence flag is configured for this storage.
                if (validateFileExistence && isS3StoragePlatform) {
                    // Validate storage file.
                    storageFileHelper.validateStorageFile(storageFile, params.getS3BucketName(), actualS3Keys,
                            validateFileSize);
                }
            }
        }
    }

    return storageFileEntities;
}

From source file:org.finra.herd.service.helper.BusinessObjectDataDaoHelper.java

/**
 * Validates the business object data create request. This method also trims appropriate request parameters.
 *
 * @param request the request// w w  w  .  j  a va  2s  .c  o  m
 * @param fileSizeRequired specifies if fileSizeBytes value is required or not
 * @param businessObjectDataStatusEntity The status entity in the request
 *
 * @throws IllegalArgumentException if any validation errors were found.
 */
private void validateBusinessObjectDataCreateRequest(BusinessObjectDataCreateRequest request,
        boolean fileSizeRequired, BusinessObjectDataStatusEntity businessObjectDataStatusEntity) {
    // Validate and trim the request parameters.
    request.setNamespace(alternateKeyHelper.validateStringParameter("namespace", request.getNamespace()));
    request.setBusinessObjectDefinitionName(alternateKeyHelper.validateStringParameter(
            "business object definition name", request.getBusinessObjectDefinitionName()));
    request.setBusinessObjectFormatUsage(alternateKeyHelper
            .validateStringParameter("business object format usage", request.getBusinessObjectFormatUsage()));
    request.setBusinessObjectFormatFileType(alternateKeyHelper.validateStringParameter(
            "business object format file type", request.getBusinessObjectFormatFileType()));
    Assert.notNull(request.getBusinessObjectFormatVersion(),
            "A business object format version must be specified.");
    request.setPartitionKey(
            alternateKeyHelper.validateStringParameter("partition key", request.getPartitionKey()));
    request.setPartitionValue(
            alternateKeyHelper.validateStringParameter("partition value", request.getPartitionValue()));
    businessObjectDataHelper.validateSubPartitionValues(request.getSubPartitionValues());

    Assert.isTrue(CollectionUtils.isNotEmpty(request.getStorageUnits()),
            "At least one storage unit must be specified.");
    for (StorageUnitCreateRequest storageUnit : request.getStorageUnits()) {
        Assert.notNull(storageUnit, "A storage unit can't be null.");

        // Validate and trim the storage name.
        Assert.hasText(storageUnit.getStorageName(), "A storage name is required for each storage unit.");
        storageUnit.setStorageName(storageUnit.getStorageName().trim());

        if (BooleanUtils.isTrue(storageUnit.isDiscoverStorageFiles())) {
            // The auto-discovery of storage files is enabled, thus a storage directory is required and storage files cannot be specified.
            Assert.isTrue(storageUnit.getStorageDirectory() != null,
                    "A storage directory must be specified when discovery of storage files is enabled.");
            Assert.isTrue(CollectionUtils.isEmpty(storageUnit.getStorageFiles()),
                    "Storage files cannot be specified when discovery of storage files is enabled.");
        } else if (!Boolean.TRUE.equals(businessObjectDataStatusEntity.getPreRegistrationStatus())) {
            // Since auto-discovery is disabled, a storage directory or at least one storage file are required for each storage unit.
            Assert.isTrue(
                    storageUnit.getStorageDirectory() != null
                            || CollectionUtils.isNotEmpty(storageUnit.getStorageFiles()),
                    "A storage directory or at least one storage file must be specified for each storage unit.");
        }

        // If storageDirectory element is present in the request, we require it to contain a non-empty directoryPath element.
        if (storageUnit.getStorageDirectory() != null) {
            Assert.hasText(storageUnit.getStorageDirectory().getDirectoryPath(),
                    "A storage directory path must be specified.");
            storageUnit.getStorageDirectory()
                    .setDirectoryPath(storageUnit.getStorageDirectory().getDirectoryPath().trim());
        }

        if (CollectionUtils.isNotEmpty(storageUnit.getStorageFiles())) {
            for (StorageFile storageFile : storageUnit.getStorageFiles()) {
                Assert.hasText(storageFile.getFilePath(), "A file path must be specified.");
                storageFile.setFilePath(storageFile.getFilePath().trim());

                if (fileSizeRequired) {
                    Assert.notNull(storageFile.getFileSizeBytes(), "A file size must be specified.");
                }

                // Ensure row count is not negative.
                if (storageFile.getRowCount() != null) {
                    Assert.isTrue(storageFile.getRowCount() >= 0,
                            "File \"" + storageFile.getFilePath() + "\" has a row count which is < 0.");
                }
            }
        }
    }

    // Validate and trim the parents' keys.
    validateBusinessObjectDataKeys(request.getBusinessObjectDataParents());

    // Validate attributes.
    attributeHelper.validateAttributes(request.getAttributes());
}

From source file:org.finra.herd.service.helper.BusinessObjectDataDaoHelper.java

/**
 * Validates the business object data keys. This will validate, trim, and make lowercase appropriate fields.
 *
 * @param keys the business object data keys to validate
 *///w  ww  .  ja v  a  2  s.  c om
public void validateBusinessObjectDataKeys(List<BusinessObjectDataKey> keys) {
    // Create a cloned business object data keys list where all keys are lowercase. This will be used for ensuring no duplicates are present in a
    // case-insensitive way.
    List<BusinessObjectDataKey> businessObjectDataLowercaseKeys = new ArrayList<>();

    if (CollectionUtils.isNotEmpty(keys)) {
        for (BusinessObjectDataKey key : keys) {
            Assert.notNull(key, "A business object data key must be specified.");

            // Validate and trim the alternate key parameter values.
            key.setNamespace(alternateKeyHelper.validateStringParameter("namespace", key.getNamespace()));
            key.setBusinessObjectDefinitionName(alternateKeyHelper.validateStringParameter(
                    "business object definition name", key.getBusinessObjectDefinitionName()));
            key.setBusinessObjectFormatUsage(alternateKeyHelper.validateStringParameter(
                    "business object format usage", key.getBusinessObjectFormatUsage()));
            key.setBusinessObjectFormatFileType(alternateKeyHelper.validateStringParameter(
                    "business object format file type", key.getBusinessObjectFormatFileType()));
            Assert.notNull(key.getBusinessObjectFormatVersion(),
                    "A business object format version must be specified.");
            key.setPartitionValue(
                    alternateKeyHelper.validateStringParameter("partition value", key.getPartitionValue()));
            businessObjectDataHelper.validateSubPartitionValues(key.getSubPartitionValues());
            Assert.notNull(key.getBusinessObjectDataVersion(),
                    "A business object data version must be specified.");

            // Add a lowercase clone to the lowercase key list.
            businessObjectDataLowercaseKeys.add(cloneToLowerCase(key));
        }
    }

    // Check for duplicates by ensuring the lowercase list size and the hash set (removes duplicates) size are the same.
    if (businessObjectDataLowercaseKeys.size() != new HashSet<>(businessObjectDataLowercaseKeys).size()) {
        throw new IllegalArgumentException("Business object data keys can not contain duplicates.");
    }
}

From source file:org.finra.herd.service.helper.BusinessObjectDataInvalidateUnregisteredHelper.java

/**
 * Trims any relevant string values in the request.
 *
 * @param businessObjectDataInvalidateUnregisteredRequest request to trim
 *///  w ww  .  java  2  s . co m
private void trimRequest(
        BusinessObjectDataInvalidateUnregisteredRequest businessObjectDataInvalidateUnregisteredRequest) {
    businessObjectDataInvalidateUnregisteredRequest
            .setNamespace(businessObjectDataInvalidateUnregisteredRequest.getNamespace().trim());
    businessObjectDataInvalidateUnregisteredRequest.setBusinessObjectDefinitionName(
            businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectDefinitionName().trim());
    businessObjectDataInvalidateUnregisteredRequest.setBusinessObjectFormatUsage(
            businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectFormatUsage().trim());
    businessObjectDataInvalidateUnregisteredRequest.setBusinessObjectFormatFileType(
            businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectFormatFileType().trim());
    businessObjectDataInvalidateUnregisteredRequest
            .setPartitionValue(businessObjectDataInvalidateUnregisteredRequest.getPartitionValue().trim());
    businessObjectDataInvalidateUnregisteredRequest
            .setStorageName(businessObjectDataInvalidateUnregisteredRequest.getStorageName().trim());
    List<String> subPartitionValues = businessObjectDataInvalidateUnregisteredRequest.getSubPartitionValues();
    if (CollectionUtils.isNotEmpty(subPartitionValues)) {
        for (int i = 0; i < subPartitionValues.size(); i++) {
            String subPartitionValue = subPartitionValues.get(i);
            subPartitionValues.set(i, subPartitionValue.trim());
        }
    }
}

From source file:org.finra.herd.service.helper.BusinessObjectDataInvalidateUnregisteredHelper.java

/**
 * Validates that the required parameters are specified and are within acceptable range.
 *
 * @param businessObjectDataInvalidateUnregisteredRequest request to validate
 *
 * @throws IllegalArgumentException when any of the parameter fails valdiation
 *//*www  . j a  v  a 2  s .c o m*/
private void validateRequest(
        BusinessObjectDataInvalidateUnregisteredRequest businessObjectDataInvalidateUnregisteredRequest) {
    Assert.notNull(businessObjectDataInvalidateUnregisteredRequest, "The request is required");
    Assert.isTrue(StringUtils.isNotBlank(businessObjectDataInvalidateUnregisteredRequest.getNamespace()),
            "The namespace is required");
    Assert.isTrue(
            StringUtils.isNotBlank(
                    businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectDefinitionName()),
            "The business object definition name is required");
    Assert.isTrue(
            StringUtils
                    .isNotBlank(businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectFormatUsage()),
            "The business object format usage is required");
    Assert.isTrue(
            StringUtils.isNotBlank(
                    businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectFormatFileType()),
            "The business object format file type is required");
    Assert.notNull(businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectFormatVersion(),
            "The business object format version is required");
    Assert.isTrue(businessObjectDataInvalidateUnregisteredRequest.getBusinessObjectFormatVersion() >= 0,
            "The business object format version must be greater than or equal to 0");
    Assert.isTrue(StringUtils.isNotBlank(businessObjectDataInvalidateUnregisteredRequest.getPartitionValue()),
            "The partition value is required");
    Assert.isTrue(StringUtils.isNotBlank(businessObjectDataInvalidateUnregisteredRequest.getStorageName()),
            "The storage name is required");
    if (CollectionUtils.isNotEmpty(businessObjectDataInvalidateUnregisteredRequest.getSubPartitionValues())) {
        for (int i = 0; i < businessObjectDataInvalidateUnregisteredRequest.getSubPartitionValues()
                .size(); i++) {
            String subPartitionValue = businessObjectDataInvalidateUnregisteredRequest.getSubPartitionValues()
                    .get(i);
            Assert.isTrue(StringUtils.isNotBlank(subPartitionValue),
                    "The sub-partition value [" + i + "] must not be blank");
        }
    }
}

From source file:org.finra.herd.service.helper.BusinessObjectDataSearchHelper.java

/**
 * Validates a business object data search request.
 *
 * @param businessObjectDataSearchRequest the business object data search request
 *//*  w  ww  . jav a2s  .  com*/
public void validateBusinessObjectDataSearchRequest(
        BusinessObjectDataSearchRequest businessObjectDataSearchRequest) {
    Assert.notNull(businessObjectDataSearchRequest, "A business object data search request must be specified.");

    List<BusinessObjectDataSearchFilter> businessObjectDataSearchFilters = businessObjectDataSearchRequest
            .getBusinessObjectDataSearchFilters();

    Assert.isTrue(CollectionUtils.isNotEmpty(businessObjectDataSearchFilters),
            "A business object data search filter must be specified.");

    Assert.isTrue(businessObjectDataSearchFilters.size() == 1,
            "A list of business object data search filters can only have one element.");

    BusinessObjectDataSearchFilter businessObjectDataSearchFilter = businessObjectDataSearchFilters.get(0);
    List<BusinessObjectDataSearchKey> businessObjectDataSearchKeys = businessObjectDataSearchFilter
            .getBusinessObjectDataSearchKeys();

    Assert.isTrue(CollectionUtils.isNotEmpty(businessObjectDataSearchKeys),
            "A business object data search key must be specified.");

    Assert.isTrue(businessObjectDataSearchKeys.size() == 1,
            "A list of business object data search keys can only have one element.");

    validateBusinessObjectDataSearchKey(businessObjectDataSearchKeys.get(0));
}

From source file:org.finra.herd.service.helper.BusinessObjectDataSearchHelper.java

/**
 * Validates a business object data search key.
 *
 * @param businessObjectDataSearchKey the business object data search key
 *///w ww  .  j a v  a  2 s.  com
void validateBusinessObjectDataSearchKey(BusinessObjectDataSearchKey businessObjectDataSearchKey) {
    Assert.notNull(businessObjectDataSearchKey, "A business object data search key must be specified.");

    businessObjectDataSearchKey.setNamespace(alternateKeyHelper.validateStringParameter("namespace",
            businessObjectDataSearchKey.getNamespace()));
    businessObjectDataSearchKey.setBusinessObjectDefinitionName(alternateKeyHelper.validateStringParameter(
            "business object definition name", businessObjectDataSearchKey.getBusinessObjectDefinitionName()));

    if (businessObjectDataSearchKey.getBusinessObjectFormatUsage() != null) {
        businessObjectDataSearchKey.setBusinessObjectFormatUsage(
                businessObjectDataSearchKey.getBusinessObjectFormatUsage().trim());
    }

    if (businessObjectDataSearchKey.getBusinessObjectFormatFileType() != null) {
        businessObjectDataSearchKey.setBusinessObjectFormatFileType(
                businessObjectDataSearchKey.getBusinessObjectFormatFileType().trim());
    }

    // Validate partition value filters, if specified.
    if (CollectionUtils.isNotEmpty(businessObjectDataSearchKey.getPartitionValueFilters())) {
        businessObjectDataHelper.validatePartitionValueFilters(
                businessObjectDataSearchKey.getPartitionValueFilters(), null, false);

        // TODO: For now, we only support partition values or partition range in the filter.
        for (PartitionValueFilter partitionValueFilter : businessObjectDataSearchKey
                .getPartitionValueFilters()) {
            List<String> partitionValues = partitionValueFilter.getPartitionValues();
            PartitionValueRange partitionValueRange = partitionValueFilter.getPartitionValueRange();

            // The partition values array should not be empty and partition vale range start and end value should not be empty
            // as it is done above at businessObjectDataHelper.validatePartitionValueFilters().
            if (CollectionUtils.isEmpty(partitionValues) && partitionValueRange == null) {
                throw new IllegalArgumentException(
                        "Only partition values or partition range are supported in partition value filter.");
            }
        }
    }

    // Validate registration date range filter, if specified.
    if (businessObjectDataSearchKey.getRegistrationDateRangeFilter() != null) {
        businessObjectDataHelper.validateRegistrationDateRangeFilter(
                businessObjectDataSearchKey.getRegistrationDateRangeFilter());
    }

    // Validate attribute value filters, if specified.
    if (CollectionUtils.isNotEmpty(businessObjectDataSearchKey.getAttributeValueFilters())) {
        for (AttributeValueFilter attributeValueFilter : businessObjectDataSearchKey
                .getAttributeValueFilters()) {
            if (attributeValueFilter.getAttributeName() != null) {
                attributeValueFilter.setAttributeName(attributeValueFilter.getAttributeName().trim());
            }
            if (StringUtils.isBlank(attributeValueFilter.getAttributeName())
                    && StringUtils.isEmpty(attributeValueFilter.getAttributeValue())) {
                throw new IllegalArgumentException(
                        "Either attribute name or attribute value filter must be specified.");
            }
        }
    }
}

From source file:org.finra.herd.service.helper.DefaultNotificationMessageBuilder.java

@Override
public List<NotificationMessage> buildBusinessObjectDataStatusChangeMessages(
        BusinessObjectDataKey businessObjectDataKey, String newBusinessObjectDataStatus,
        String oldBusinessObjectDataStatus) {
    // Create a result list.
    List<NotificationMessage> notificationMessages = new ArrayList<>();

    // Get notification message definitions.
    NotificationMessageDefinitions notificationMessageDefinitions = configurationDaoHelper
            .getXmlClobPropertyAndUnmarshallToObject(NotificationMessageDefinitions.class,
                    ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_DATA_STATUS_CHANGE_MESSAGE_DEFINITIONS
                            .getKey());//  w ww.ja  v a2  s.  co  m

    // Continue processing if notification message definitions are configured.
    if (notificationMessageDefinitions != null
            && CollectionUtils.isNotEmpty(notificationMessageDefinitions.getNotificationMessageDefinitions())) {
        // Create a context map of values that can be used when building the message.
        Map<String, Object> velocityContextMap = getVelocityContextMap(businessObjectDataKey,
                newBusinessObjectDataStatus, oldBusinessObjectDataStatus);

        // Generate notification message for each notification message definition.
        for (NotificationMessageDefinition notificationMessageDefinition : notificationMessageDefinitions
                .getNotificationMessageDefinitions()) {
            // Validate the notification message type.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageType())) {
                throw new IllegalStateException(String.format(
                        "Notification message type must be specified. Please update \"%s\" configuration entry.",
                        ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_DATA_STATUS_CHANGE_MESSAGE_DEFINITIONS
                                .getKey()));
            }

            // Validate the notification message destination.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageDestination())) {
                throw new IllegalStateException(String.format(
                        "Notification message destination must be specified. Please update \"%s\" configuration entry.",
                        ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_DATA_STATUS_CHANGE_MESSAGE_DEFINITIONS
                                .getKey()));
            }

            // Evaluate the template to generate the message text.
            String messageText = evaluateVelocityTemplate(
                    notificationMessageDefinition.getMessageVelocityTemplate(), velocityContextMap,
                    "businessObjectDataStatusChangeEvent");

            // Build a list of optional message headers.
            List<MessageHeader> messageHeaders = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(notificationMessageDefinition.getMessageHeaderDefinitions())) {
                for (MessageHeaderDefinition messageHeaderDefinition : notificationMessageDefinition
                        .getMessageHeaderDefinitions()) {
                    messageHeaders.add(new MessageHeader(messageHeaderDefinition.getKey(),
                            evaluateVelocityTemplate(messageHeaderDefinition.getValueVelocityTemplate(),
                                    velocityContextMap,
                                    String.format("businessObjectDataStatusChangeEvent_messageHeader_%s",
                                            messageHeaderDefinition.getKey()))));
                }
            }

            // Create a notification message and add it to the result list.
            notificationMessages.add(new NotificationMessage(notificationMessageDefinition.getMessageType(),
                    notificationMessageDefinition.getMessageDestination(), messageText, messageHeaders));
        }
    }

    // Return the results.
    return notificationMessages;
}