Java Code Examples for org.apache.commons.collections4.IterableUtils#get()

The following examples show how to use org.apache.commons.collections4.IterableUtils#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttributeDaoHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateBusinessObjectDataAttributesAttributeAdded()
{
    // Create a list of attributes.
    List<Attribute> attributes = Arrays.asList(new Attribute(ATTRIBUTE_NAME, ATTRIBUTE_VALUE));

    // Create a business object data entity without attributes.
    BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity();
    businessObjectDataEntity.setAttributes(new ArrayList<>());

    // Call the method under test.
    attributeDaoHelper.updateBusinessObjectDataAttributes(businessObjectDataEntity, attributes);

    // Verify the external calls.
    verifyNoMoreInteractionsHelper();

    // Validate the results.
    assertEquals(1, CollectionUtils.size(businessObjectDataEntity.getAttributes()));
    BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity = IterableUtils.get(businessObjectDataEntity.getAttributes(), 0);
    assertEquals(businessObjectDataEntity, businessObjectDataAttributeEntity.getBusinessObjectData());
    assertEquals(ATTRIBUTE_NAME, businessObjectDataAttributeEntity.getName());
    assertEquals(ATTRIBUTE_VALUE, businessObjectDataAttributeEntity.getValue());
}
 
Example 2
Source File: AttributeDaoHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateBusinessObjectDataAttributesAttributeValueNotUpdated()
{
    // Create a business object data attribute entity.
    BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity = new BusinessObjectDataAttributeEntity();
    businessObjectDataAttributeEntity.setName(ATTRIBUTE_NAME);
    businessObjectDataAttributeEntity.setValue(ATTRIBUTE_VALUE);

    // Create a business object data entity that contains one attribute entity.
    BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity();
    List<BusinessObjectDataAttributeEntity> businessObjectDataAttributeEntities = new ArrayList<>();
    businessObjectDataEntity.setAttributes(businessObjectDataAttributeEntities);
    businessObjectDataAttributeEntities.add(businessObjectDataAttributeEntity);

    // Call the method under test.
    attributeDaoHelper.updateBusinessObjectDataAttributes(businessObjectDataEntity, Arrays.asList(new Attribute(ATTRIBUTE_NAME, ATTRIBUTE_VALUE)));

    // Verify the external calls.
    verifyNoMoreInteractionsHelper();

    // Validate the results.
    assertEquals(1, CollectionUtils.size(businessObjectDataEntity.getAttributes()));
    BusinessObjectDataAttributeEntity result = IterableUtils.get(businessObjectDataEntity.getAttributes(), 0);
    assertEquals(ATTRIBUTE_NAME, result.getName());
    assertEquals(ATTRIBUTE_VALUE, result.getValue());
}
 
Example 3
Source File: AttributeDaoHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateBusinessObjectDataAttributesAttributeValueUpdated()
{
    // Create a business object data attribute entity.
    BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity = new BusinessObjectDataAttributeEntity();
    businessObjectDataAttributeEntity.setName(ATTRIBUTE_NAME);
    businessObjectDataAttributeEntity.setValue(ATTRIBUTE_VALUE);

    // Create a business object data entity that contains one attribute entity.
    BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity();
    List<BusinessObjectDataAttributeEntity> businessObjectDataAttributeEntities = new ArrayList<>();
    businessObjectDataEntity.setAttributes(businessObjectDataAttributeEntities);
    businessObjectDataAttributeEntities.add(businessObjectDataAttributeEntity);

    // Call the method under test.
    attributeDaoHelper.updateBusinessObjectDataAttributes(businessObjectDataEntity, Arrays.asList(new Attribute(ATTRIBUTE_NAME, ATTRIBUTE_VALUE_2)));

    // Verify the external calls.
    verifyNoMoreInteractionsHelper();

    // Validate the results.
    assertEquals(1, CollectionUtils.size(businessObjectDataEntity.getAttributes()));
    BusinessObjectDataAttributeEntity result = IterableUtils.get(businessObjectDataEntity.getAttributes(), 0);
    assertEquals(ATTRIBUTE_NAME, result.getName());
    assertEquals(ATTRIBUTE_VALUE_2, result.getValue());
}
 
Example 4
Source File: PropertiesResolver.java    From multiapps with Apache License 2.0 5 votes vote down vote up
private Object resolveKeyInIterable(String key, Collection<?> listOfProperties, String longKey) {
    if (StringUtils.isNumeric(key)) {
        try {
            return IterableUtils.get(listOfProperties, Integer.parseInt(key));
        } catch (IndexOutOfBoundsException e) { // Intentionally ignored
        }
    }

    throw new ContentException(Messages.UNABLE_TO_RESOLVE, getPrefixedName(prefix, longKey));
}
 
Example 5
Source File: UploadDownloadServiceTest.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the target business object data that is created is using the target storage name that is specified in the request.
 */
@Test
public void testInitiateUploadSingleAssertUseTargetStorageInRequest()
{
    // Create database entities required for testing.
    uploadDownloadServiceTestHelper.createDatabaseEntitiesForUploadDownloadTesting();
    StorageEntity storageEntity = storageDaoTestHelper.createStorageEntity(STORAGE_NAME_3);
    storageEntity.getAttributes().add(storageDaoTestHelper
        .createStorageAttributeEntity(storageEntity, configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_BUCKET_NAME), "testBucketName"));
    storageEntity.getAttributes().add(storageDaoTestHelper
        .createStorageAttributeEntity(storageEntity, configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE),
            "$environment/$namespace/$businessObjectDataPartitionValue"));
    storageEntity.getAttributes().add(storageDaoTestHelper
        .createStorageAttributeEntity(storageEntity, configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KMS_KEY_ID),
            "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012"));

    // Initiate a file upload.
    UploadSingleInitiationRequest uploadSingleInitiationRequest = uploadDownloadServiceTestHelper.createUploadSingleInitiationRequest();
    uploadSingleInitiationRequest.setTargetStorageName(STORAGE_NAME_3);

    UploadSingleInitiationResponse resultUploadSingleInitiationResponse = uploadDownloadService.initiateUploadSingle(uploadSingleInitiationRequest);

    // Validate the returned object.
    uploadDownloadServiceTestHelper
        .validateUploadSingleInitiationResponse(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, NAMESPACE, BDEF_NAME_2,
            FORMAT_USAGE_CODE_2, FORMAT_FILE_TYPE_CODE_2, FORMAT_VERSION_2, businessObjectDefinitionServiceTestHelper.getNewAttributes(), FILE_NAME,
            FILE_SIZE_1_KB, STORAGE_NAME_3, resultUploadSingleInitiationResponse);

    BusinessObjectDataEntity targetBusinessObjectDataEntity = businessObjectDataDao.getBusinessObjectDataByAltKey(
        new BusinessObjectDataKey(NAMESPACE, BDEF_NAME_2, FORMAT_USAGE_CODE_2, FORMAT_FILE_TYPE_CODE_2, FORMAT_VERSION_2,
            resultUploadSingleInitiationResponse.getTargetBusinessObjectData().getPartitionValue(), null, 0));

    assertNotNull(targetBusinessObjectDataEntity);
    assertNotNull(targetBusinessObjectDataEntity.getStorageUnits());
    assertEquals(1, targetBusinessObjectDataEntity.getStorageUnits().size());
    StorageUnitEntity storageUnit = IterableUtils.get(targetBusinessObjectDataEntity.getStorageUnits(), 0);
    assertNotNull(storageUnit);
    assertNotNull(storageUnit.getStorage());
    assertEquals(STORAGE_NAME_3, storageUnit.getStorage().getName());
}
 
Example 6
Source File: UploadDownloadServiceTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitiateDownloadSingleMultipleStorageFilesExist()
{
    // Create the upload data.
    UploadSingleInitiationResponse uploadSingleInitiationResponse =
        uploadDownloadServiceTestHelper.createUploadedFileData(BusinessObjectDataStatusEntity.VALID);

    // Get the target business object data entity.
    BusinessObjectDataEntity targetBusinessObjectDataEntity = businessObjectDataDao
        .getBusinessObjectDataByAltKey(businessObjectDataHelper.getBusinessObjectDataKey(uploadSingleInitiationResponse.getTargetBusinessObjectData()));

    // Get the target bushiness object data storage unit.
    StorageUnitEntity targetStorageUnitEntity = IterableUtils.get(targetBusinessObjectDataEntity.getStorageUnits(), 0);

    // Add a second storage file to the target business object data storage unit.
    storageFileDaoTestHelper.createStorageFileEntity(targetStorageUnitEntity, FILE_NAME_2, FILE_SIZE_1_KB, ROW_COUNT_1000);

    // Try to initiate a single file download when business object data has more than one storage file.
    try
    {
        // Initiate the download against the uploaded data (i.e. the target business object data).
        initiateDownload(uploadSingleInitiationResponse.getTargetBusinessObjectData());
        fail("Suppose to throw an IllegalArgumentException when business object has more than one storage file.");
    }
    catch (IllegalArgumentException e)
    {
        BusinessObjectData businessObjectData = uploadSingleInitiationResponse.getTargetBusinessObjectData();
        assertEquals(String.format("Found 2 registered storage files when expecting one in \"%s\" storage for the business object data {%s}.",
            targetStorageUnitEntity.getStorage().getName(), businessObjectDataServiceTestHelper
                .getExpectedBusinessObjectDataKeyAsString(businessObjectDataHelper.getBusinessObjectDataKey(businessObjectData))), e.getMessage());
    }
}
 
Example 7
Source File: AttributeDaoHelperTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateBusinessObjectDataAttributesAttributeValueUpdatedAttributeNamesEqualIgnoreCase()
{
    // Create a business object data attribute entity.
    BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity = new BusinessObjectDataAttributeEntity();
    businessObjectDataAttributeEntity.setName(ATTRIBUTE_NAME.toUpperCase());
    businessObjectDataAttributeEntity.setValue(ATTRIBUTE_VALUE);

    // Create a business object data entity that contains one attribute entity.
    BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity();
    List<BusinessObjectDataAttributeEntity> businessObjectDataAttributeEntities = new ArrayList<>();
    businessObjectDataEntity.setAttributes(businessObjectDataAttributeEntities);
    businessObjectDataAttributeEntities.add(businessObjectDataAttributeEntity);

    // Call the method under test.
    attributeDaoHelper
        .updateBusinessObjectDataAttributes(businessObjectDataEntity, Arrays.asList(new Attribute(ATTRIBUTE_NAME.toLowerCase(), ATTRIBUTE_VALUE_2)));

    // Verify the external calls.
    verifyNoMoreInteractionsHelper();

    // Validate the results.
    assertEquals(1, CollectionUtils.size(businessObjectDataEntity.getAttributes()));
    BusinessObjectDataAttributeEntity result = IterableUtils.get(businessObjectDataEntity.getAttributes(), 0);
    assertEquals(ATTRIBUTE_NAME.toUpperCase(), result.getName());
    assertEquals(ATTRIBUTE_VALUE_2, result.getValue());
}
 
Example 8
Source File: UserNamespaceAuthorizationHelperTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildNamespaceAuthorizationsAssertAuthLookupByUserId()
{
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    String userId = "userId";
    applicationUser.setUserId(userId);

    when(configurationHelper.getBooleanProperty(any())).thenReturn(true);

    List<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntities = new ArrayList<>();
    UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = new UserNamespaceAuthorizationEntity();
    userNamespaceAuthorizationEntity.setUserId("userNamespaceAuthorizationEntityUserId");
    NamespaceEntity namespaceEntity = new NamespaceEntity();
    namespaceEntity.setCode("namespace");
    userNamespaceAuthorizationEntity.setNamespace(namespaceEntity);
    userNamespaceAuthorizationEntities.add(userNamespaceAuthorizationEntity);
    when(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserId(any())).thenReturn(userNamespaceAuthorizationEntities);

    userNamespaceAuthorizationHelper.buildNamespaceAuthorizations(applicationUser);

    assertEquals(1, applicationUser.getNamespaceAuthorizations().size());
    NamespaceAuthorization namespaceAuthorization = IterableUtils.get(applicationUser.getNamespaceAuthorizations(), 0);
    assertEquals(namespaceEntity.getCode(), namespaceAuthorization.getNamespace());

    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserId(eq(userId));
    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserIdStartsWith(eq(WildcardHelper.WILDCARD_TOKEN));
    verifyNoMoreInteractions(userNamespaceAuthorizationDao, wildcardHelper);
}
 
Example 9
Source File: UserNamespaceAuthorizationHelperTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildNamespaceAuthorizationsAssertWildcardQueryExecuted()
{
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    String userId = "userId";
    applicationUser.setUserId(userId);

    when(configurationHelper.getBooleanProperty(any())).thenReturn(true);

    List<UserNamespaceAuthorizationEntity> wildcardEntities = new ArrayList<>();
    UserNamespaceAuthorizationEntity wildcardEntity = new UserNamespaceAuthorizationEntity();
    wildcardEntity.setUserId("wildcardEntityUserId");
    NamespaceEntity namespaceEntity = new NamespaceEntity();
    namespaceEntity.setCode("namespace");
    wildcardEntity.setNamespace(namespaceEntity);
    wildcardEntities.add(wildcardEntity);
    when(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserIdStartsWith(any())).thenReturn(wildcardEntities);

    when(wildcardHelper.matches(any(), any())).thenReturn(true);

    userNamespaceAuthorizationHelper.buildNamespaceAuthorizations(applicationUser);

    assertEquals(1, applicationUser.getNamespaceAuthorizations().size());
    NamespaceAuthorization namespaceAuthorization = IterableUtils.get(applicationUser.getNamespaceAuthorizations(), 0);
    assertEquals(namespaceEntity.getCode(), namespaceAuthorization.getNamespace());

    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserId(eq(userId));
    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserIdStartsWith(eq(WildcardHelper.WILDCARD_TOKEN));
    verify(wildcardHelper).matches(eq(userId.toUpperCase()), eq(wildcardEntity.getUserId().toUpperCase()));
    verifyNoMoreInteractions(userNamespaceAuthorizationDao, wildcardHelper);
}
 
Example 10
Source File: EventManager.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
private void validatePaymentProxies(List<PaymentProxy> paymentProxies, int organizationId) {
    var conflicts = paymentManager.validateSelection(paymentProxies, organizationId);
    if(!conflicts.isEmpty()) {
        var firstConflict = IterableUtils.get(conflicts, 0);
        throw new IllegalStateException("Conflicting providers found: "+firstConflict.getValue());
    }
}
 
Example 11
Source File: UploadDownloadServiceImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@NamespacePermission(fields = "#namespace", permissions = NamespacePermissionEnum.READ)
@Override
public DownloadSingleInitiationResponse initiateDownloadSingle(String namespace, String businessObjectDefinitionName, String businessObjectFormatUsage,
    String businessObjectFormatFileType, Integer businessObjectFormatVersion, String partitionValue, Integer businessObjectDataVersion)
{
    // Create the business object data key.
    BusinessObjectDataKey businessObjectDataKey =
        new BusinessObjectDataKey(namespace, businessObjectDefinitionName, businessObjectFormatUsage, businessObjectFormatFileType,
            businessObjectFormatVersion, partitionValue, null, businessObjectDataVersion);

    // Validate the parameters
    businessObjectDataHelper.validateBusinessObjectDataKey(businessObjectDataKey, true, true);

    // Retrieve the persisted business object data
    BusinessObjectDataEntity businessObjectDataEntity = businessObjectDataDaoHelper.getBusinessObjectDataEntity(businessObjectDataKey);

    // Make sure the status of the business object data is VALID
    businessObjectDataHelper.assertBusinessObjectDataStatusEquals(BusinessObjectDataStatusEntity.VALID, businessObjectDataEntity);

    // Get the external storage registered against this data
    // Validate that the storage unit exists
    StorageUnitEntity storageUnitEntity = IterableUtils.get(businessObjectDataEntity.getStorageUnits(), 0);

    // Validate that the storage unit contains only 1 file
    assertHasOneStorageFile(storageUnitEntity);

    String s3BucketName = storageHelper.getStorageBucketName(storageUnitEntity.getStorage());
    String s3ObjectKey = IterableUtils.get(storageUnitEntity.getStorageFiles(), 0).getPath();

    // Get the temporary credentials
    Credentials downloaderCredentials =
        getExternalDownloaderCredentials(storageUnitEntity.getStorage(), String.valueOf(businessObjectDataEntity.getId()), s3ObjectKey);

    // Generate a pre-signed URL
    Date expiration = downloaderCredentials.getExpiration();
    S3FileTransferRequestParamsDto s3BucketAccessParams = storageHelper.getS3BucketAccessParams(storageUnitEntity.getStorage());
    String presignedUrl = s3Dao.generateGetObjectPresignedUrl(s3BucketName, s3ObjectKey, expiration, s3BucketAccessParams);

    // Construct and return the response
    DownloadSingleInitiationResponse response = new DownloadSingleInitiationResponse();
    response.setBusinessObjectData(businessObjectDataHelper.createBusinessObjectDataFromEntity(businessObjectDataEntity));
    response.setAwsAccessKey(downloaderCredentials.getAccessKeyId());
    response.setAwsSecretKey(downloaderCredentials.getSecretAccessKey());
    response.setAwsSessionToken(downloaderCredentials.getSessionToken());
    response.setAwsSessionExpirationTime(HerdDateUtils.getXMLGregorianCalendarValue(expiration));
    response.setPreSignedUrl(presignedUrl);
    return response;
}
 
Example 12
Source File: UploadDownloadServiceImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@NamespacePermission(fields = "#namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public UploadSingleCredentialExtensionResponse extendUploadSingleCredentials(String namespace, String businessObjectDefinitionName,
    String businessObjectFormatUsage, String businessObjectFormatFileType, Integer businessObjectFormatVersion, String partitionValue,
    Integer businessObjectDataVersion)
{
    // Create the business object data key.
    BusinessObjectDataKey businessObjectDataKey =
        new BusinessObjectDataKey(namespace, businessObjectDefinitionName, businessObjectFormatUsage, businessObjectFormatFileType,
            businessObjectFormatVersion, partitionValue, null, businessObjectDataVersion);

    // Validate and trim the business object data key.
    businessObjectDataHelper.validateBusinessObjectDataKey(businessObjectDataKey, true, true);

    // Get the business object data for the key.
    BusinessObjectDataEntity businessObjectDataEntity = businessObjectDataDaoHelper.getBusinessObjectDataEntity(businessObjectDataKey);

    // Ensure the status of the business object data is "uploading" in order to extend credentials.
    if (!(businessObjectDataEntity.getStatus().getCode().equals(BusinessObjectDataStatusEntity.UPLOADING)))
    {
        throw new IllegalArgumentException(String.format(String
            .format("Business object data {%s} has a status of \"%s\" and must be \"%s\" to extend " + "credentials.",
                businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey), businessObjectDataEntity.getStatus().getCode(),
                BusinessObjectDataStatusEntity.UPLOADING)));
    }

    // Get the S3 managed "loading dock" storage entity and make sure it exists.
    StorageEntity storageEntity = storageDaoHelper.getStorageEntity(StorageEntity.MANAGED_LOADING_DOCK_STORAGE);

    String s3BucketName = storageHelper.getStorageBucketName(storageEntity);

    // Get the storage unit entity for this business object data in the S3 managed "loading dock" storage and make sure it exists.
    StorageUnitEntity storageUnitEntity = storageUnitDaoHelper.getStorageUnitEntity(StorageEntity.MANAGED_LOADING_DOCK_STORAGE, businessObjectDataEntity);

    // Validate that the storage unit contains exactly one storage file.
    assertHasOneStorageFile(storageUnitEntity);

    // Get the storage file entity.
    StorageFileEntity storageFileEntity = IterableUtils.get(storageUnitEntity.getStorageFiles(), 0);

    // Get the storage file path.
    String storageFilePath = storageFileEntity.getPath();

    String awsRoleArn = getStorageUploadRoleArn(storageEntity);

    Integer awsRoleDurationSeconds = getStorageUploadSessionDuration(storageEntity);

    String awsKmsKeyId = storageHelper.getStorageKmsKeyId(storageEntity);

    // Get the temporary security credentials to access S3_MANAGED_STORAGE.
    Credentials assumedSessionCredentials = stsDao
        .getTemporarySecurityCredentials(awsHelper.getAwsParamsDto(), String.valueOf(businessObjectDataEntity.getId()), awsRoleArn, awsRoleDurationSeconds,
            createUploaderPolicy(s3BucketName, storageFilePath, awsKmsKeyId));

    // Create the response.
    UploadSingleCredentialExtensionResponse response = new UploadSingleCredentialExtensionResponse();
    response.setAwsAccessKey(assumedSessionCredentials.getAccessKeyId());
    response.setAwsSecretKey(assumedSessionCredentials.getSecretAccessKey());
    response.setAwsSessionToken(assumedSessionCredentials.getSessionToken());
    response.setAwsSessionExpirationTime(HerdDateUtils.getXMLGregorianCalendarValue(assumedSessionCredentials.getExpiration()));

    return response;
}