BusinessObjectDataInitiateDestroyHelperServiceImpl implements BusinessObjectDataInitiateDestroyHelperService { void validateBusinessObjectData(BusinessObjectDataEntity businessObjectDataEntity, BusinessObjectDataKey businessObjectDataKey) { BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectDataEntity.getBusinessObjectFormat(); BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(businessObjectDataKey.getNamespace(), businessObjectDataKey.getBusinessObjectDefinitionName(), businessObjectDataKey.getBusinessObjectFormatUsage(), businessObjectDataKey.getBusinessObjectFormatFileType(), null); BusinessObjectFormatEntity latestVersionBusinessObjectFormatEntity = businessObjectFormatEntity.getLatestVersion() ? businessObjectFormatEntity : businessObjectFormatDao.getBusinessObjectFormatByAltKey(businessObjectFormatKey); String retentionType = latestVersionBusinessObjectFormatEntity.getRetentionType() != null ? latestVersionBusinessObjectFormatEntity.getRetentionType().getCode() : null; Integer retentionPeriodInDays = latestVersionBusinessObjectFormatEntity.getRetentionPeriodInDays(); Timestamp currentTimestamp = herdDao.getCurrentTimestamp(); if (retentionType != null) { switch (retentionType) { case RetentionTypeEntity.PARTITION_VALUE: Assert.notNull(retentionPeriodInDays, String.format("Retention period in days must be specified for %s retention type.", RetentionTypeEntity.PARTITION_VALUE)); Assert.isTrue(retentionPeriodInDays > 0, String.format("A positive retention period in days must be specified for %s retention type.", RetentionTypeEntity.PARTITION_VALUE)); Date primaryPartitionValue = businessObjectDataHelper.getDateFromString(businessObjectDataEntity.getPartitionValue()); if (primaryPartitionValue == null) { throw new IllegalArgumentException(String .format("Primary partition value \"%s\" cannot get converted to a valid date. Business object data: {%s}", businessObjectDataEntity.getPartitionValue(), businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey))); } Date primaryPartitionValueThreshold = new Date(HerdDateUtils.addDays(currentTimestamp, -retentionPeriodInDays).getTime()); if (primaryPartitionValue.compareTo(primaryPartitionValueThreshold) > 0) { throw new IllegalArgumentException(String.format( "Business object data fails retention threshold check for retention type \"%s\" with retention period of %d days. " + "Business object data: {%s}", retentionType, retentionPeriodInDays, businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey))); } break; case RetentionTypeEntity.BDATA_RETENTION_DATE: Assert.isNull(retentionPeriodInDays, String.format("A retention period in days cannot be specified for %s retention type.", retentionType)); Assert.notNull(businessObjectDataEntity.getRetentionExpiration(), String .format("Retention information with retention type %s must be specified for the Business Object Data: {%s}", retentionType, businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey))); if (!(businessObjectDataEntity.getRetentionExpiration().before(currentTimestamp))) { throw new IllegalArgumentException(String.format( "Business object data fails retention threshold check for retention type \"%s\" with retention expiration date %s. " + "Business object data: {%s}", retentionType, businessObjectDataEntity.getRetentionExpiration(), businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey))); } break; default: throw new IllegalArgumentException(String .format("Retention type \"%s\" is not supported by the business object data destroy feature. Business object format: {%s}", retentionType, businessObjectFormatHelper.businessObjectFormatKeyToString(businessObjectFormatKey))); } } else { throw new IllegalArgumentException(String .format("Retention information is not configured for the business object format. Business object format: {%s}", businessObjectFormatHelper.businessObjectFormatKeyToString(businessObjectFormatKey))); } }  @PublishNotificationMessages @Override @Transactional(propagation = Propagation.REQUIRES_NEW) BusinessObjectData executeInitiateDestroyAfterStep(BusinessObjectDataDestroyDto businessObjectDataDestroyDto); @Override @Transactional(propagation = Propagation.NOT_SUPPORTED) void executeS3SpecificSteps(BusinessObjectDataDestroyDto businessObjectDataDestroyDto); @PublishNotificationMessages @Override @Transactional(propagation = Propagation.REQUIRES_NEW) void prepareToInitiateDestroy(BusinessObjectDataDestroyDto businessObjectDataDestroyDto, BusinessObjectDataKey businessObjectDataKey);  }
@Test public void testValidateBusinessObjectDataInvalidPrimaryPartitionValue() { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, NO_FORMAT_VERSION); BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, NO_SUBPARTITION_VALUES, DATA_VERSION); RetentionTypeEntity retentionTypeEntity = new RetentionTypeEntity(); retentionTypeEntity.setCode(RetentionTypeEntity.PARTITION_VALUE); BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity(); businessObjectFormatEntity.setLatestVersion(true); businessObjectFormatEntity.setRetentionType(retentionTypeEntity); businessObjectFormatEntity.setRetentionPeriodInDays(RETENTION_PERIOD_DAYS); BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); businessObjectDataEntity.setBusinessObjectFormat(businessObjectFormatEntity); businessObjectDataEntity.setPartitionValue(PARTITION_VALUE); Timestamp currentTimestamp = new Timestamp(new Date().getTime()); when(businessObjectDataHelper.getDateFromString(PARTITION_VALUE)).thenReturn(null); when(businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); when(herdDao.getCurrentTimestamp()).thenReturn(currentTimestamp); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Primary partition value \"%s\" cannot get converted to a valid date. Business object data: {%s}", PARTITION_VALUE, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } verify(businessObjectDataHelper).getDateFromString(PARTITION_VALUE); verify(businessObjectDataHelper).businessObjectDataKeyToString(businessObjectDataKey); verify(herdDao).getCurrentTimestamp(); verifyNoMoreInteractionsHelper(); }
@Test public void testValidateBusinessObjectDataInvalidRetentionType() { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, NO_FORMAT_VERSION); BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, NO_SUBPARTITION_VALUES, DATA_VERSION); RetentionTypeEntity retentionTypeEntity = new RetentionTypeEntity(); retentionTypeEntity.setCode(INVALID_VALUE); BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity(); businessObjectFormatEntity.setLatestVersion(true); businessObjectFormatEntity.setRetentionType(retentionTypeEntity); businessObjectFormatEntity.setRetentionPeriodInDays(RETENTION_PERIOD_DAYS); BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); businessObjectDataEntity.setBusinessObjectFormat(businessObjectFormatEntity); businessObjectDataEntity.setPartitionValue(PARTITION_VALUE); Timestamp currentTimestamp = new Timestamp(new Date().getTime()); when(businessObjectFormatHelper.businessObjectFormatKeyToString(businessObjectFormatKey)).thenReturn(BUSINESS_OBJECT_FORMAT_KEY_AS_STRING); when(herdDao.getCurrentTimestamp()).thenReturn(currentTimestamp); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String .format("Retention type \"%s\" is not supported by the business object data destroy feature. Business object format: {%s}", INVALID_VALUE, BUSINESS_OBJECT_FORMAT_KEY_AS_STRING), e.getMessage()); } verify(businessObjectFormatHelper).businessObjectFormatKeyToString(businessObjectFormatKey); verify(herdDao).getCurrentTimestamp(); verifyNoMoreInteractionsHelper(); }
@Test public void testValidateBusinessObjectDataNoRetentionInformation() { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, NO_FORMAT_VERSION); BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, INITIAL_FORMAT_VERSION, PARTITION_VALUE, NO_SUBPARTITION_VALUES, DATA_VERSION); RetentionTypeEntity retentionTypeEntity = new RetentionTypeEntity(); BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity(); businessObjectFormatEntity.setBusinessObjectFormatVersion(INITIAL_FORMAT_VERSION); businessObjectFormatEntity.setLatestVersion(false); BusinessObjectFormatEntity latestVersionBusinessObjectFormatEntity = new BusinessObjectFormatEntity(); latestVersionBusinessObjectFormatEntity.setBusinessObjectFormatVersion(INITIAL_FORMAT_VERSION); latestVersionBusinessObjectFormatEntity.setLatestVersion(true); latestVersionBusinessObjectFormatEntity.setRetentionType(null); latestVersionBusinessObjectFormatEntity.setRetentionPeriodInDays(RETENTION_PERIOD_DAYS); BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); businessObjectDataEntity.setBusinessObjectFormat(businessObjectFormatEntity); businessObjectDataEntity.setPartitionValue(PARTITION_VALUE); Timestamp currentTimestamp = new Timestamp(new Date().getTime()); when(businessObjectFormatDao.getBusinessObjectFormatByAltKey(businessObjectFormatKey)).thenReturn(latestVersionBusinessObjectFormatEntity); when(businessObjectFormatHelper.businessObjectFormatKeyToString(businessObjectFormatKey)).thenReturn(BUSINESS_OBJECT_FORMAT_KEY_AS_STRING); when(herdDao.getCurrentTimestamp()).thenReturn(currentTimestamp); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Retention information is not configured for the business object format. Business object format: {%s}", BUSINESS_OBJECT_FORMAT_KEY_AS_STRING), e.getMessage()); } retentionTypeEntity.setCode(RetentionTypeEntity.PARTITION_VALUE); latestVersionBusinessObjectFormatEntity.setRetentionType(retentionTypeEntity); latestVersionBusinessObjectFormatEntity.setRetentionPeriodInDays(null); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Retention period in days must be specified for %s retention type.", RetentionTypeEntity.PARTITION_VALUE), e.getMessage()); } retentionTypeEntity.setCode(RetentionTypeEntity.BDATA_RETENTION_DATE); latestVersionBusinessObjectFormatEntity.setRetentionType(retentionTypeEntity); latestVersionBusinessObjectFormatEntity.setRetentionPeriodInDays(RETENTION_PERIOD_DAYS); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("A retention period in days cannot be specified for %s retention type.", RetentionTypeEntity.BDATA_RETENTION_DATE), e.getMessage()); } verify(businessObjectFormatDao, times(3)).getBusinessObjectFormatByAltKey(businessObjectFormatKey); verify(businessObjectFormatHelper, times(1)).businessObjectFormatKeyToString(businessObjectFormatKey); verify(herdDao, times(3)).getCurrentTimestamp(); verifyNoMoreInteractionsHelper(); }
@Test public void testValidateBusinessObjectDataRetentionInformationCheckFails() { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, NO_FORMAT_VERSION); BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, INITIAL_FORMAT_VERSION, PARTITION_VALUE, NO_SUBPARTITION_VALUES, DATA_VERSION); RetentionTypeEntity retentionTypeEntity = new RetentionTypeEntity(); retentionTypeEntity.setCode(RetentionTypeEntity.BDATA_RETENTION_DATE); BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity(); businessObjectFormatEntity.setBusinessObjectFormatVersion(INITIAL_FORMAT_VERSION); businessObjectFormatEntity.setLatestVersion(false); BusinessObjectFormatEntity latestVersionBusinessObjectFormatEntity = new BusinessObjectFormatEntity(); latestVersionBusinessObjectFormatEntity.setBusinessObjectFormatVersion(INITIAL_FORMAT_VERSION); latestVersionBusinessObjectFormatEntity.setLatestVersion(true); latestVersionBusinessObjectFormatEntity.setRetentionType(retentionTypeEntity); BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); businessObjectDataEntity.setBusinessObjectFormat(businessObjectFormatEntity); businessObjectDataEntity.setPartitionValue(PARTITION_VALUE); businessObjectDataEntity.setRetentionExpiration(new Timestamp(RETENTION_EXPIRATION_DATE_IN_FUTURE.toGregorianCalendar().getTimeInMillis())); Timestamp currentTimestamp = new Timestamp(new Date().getTime()); when(businessObjectFormatDao.getBusinessObjectFormatByAltKey(businessObjectFormatKey)).thenReturn(latestVersionBusinessObjectFormatEntity); when(businessObjectFormatHelper.businessObjectFormatKeyToString(businessObjectFormatKey)).thenReturn(BUSINESS_OBJECT_FORMAT_KEY_AS_STRING); when(herdDao.getCurrentTimestamp()).thenReturn(currentTimestamp); when(businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Business object data fails retention threshold check for retention type \"%s\" with retention expiration date %s. " + "Business object data: {%s}", retentionTypeEntity.getCode(), businessObjectDataEntity.getRetentionExpiration(), BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } }
@Test public void testValidateBusinessObjectDataWithNoRetentionInformationCheckFails() { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, NO_FORMAT_VERSION); BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, INITIAL_FORMAT_VERSION, PARTITION_VALUE, NO_SUBPARTITION_VALUES, DATA_VERSION); RetentionTypeEntity retentionTypeEntity = new RetentionTypeEntity(); retentionTypeEntity.setCode(RetentionTypeEntity.BDATA_RETENTION_DATE); BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity(); businessObjectFormatEntity.setBusinessObjectFormatVersion(INITIAL_FORMAT_VERSION); businessObjectFormatEntity.setLatestVersion(false); BusinessObjectFormatEntity latestVersionBusinessObjectFormatEntity = new BusinessObjectFormatEntity(); latestVersionBusinessObjectFormatEntity.setBusinessObjectFormatVersion(INITIAL_FORMAT_VERSION); latestVersionBusinessObjectFormatEntity.setLatestVersion(true); latestVersionBusinessObjectFormatEntity.setRetentionType(retentionTypeEntity); BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); businessObjectDataEntity.setBusinessObjectFormat(businessObjectFormatEntity); businessObjectDataEntity.setPartitionValue(PARTITION_VALUE); Timestamp currentTimestamp = new Timestamp(new Date().getTime()); when(businessObjectFormatDao.getBusinessObjectFormatByAltKey(businessObjectFormatKey)).thenReturn(latestVersionBusinessObjectFormatEntity); when(businessObjectFormatHelper.businessObjectFormatKeyToString(businessObjectFormatKey)).thenReturn(BUSINESS_OBJECT_FORMAT_KEY_AS_STRING); when(herdDao.getCurrentTimestamp()).thenReturn(currentTimestamp); when(businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String .format("Retention information with retention type %s must be specified for the Business Object Data: {%s}", retentionTypeEntity.getCode(), BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } }
@Test public void testValidateBusinessObjectDataRetentionThresholdCheckFails() { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, NO_FORMAT_VERSION); BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, NO_SUBPARTITION_VALUES, DATA_VERSION); RetentionTypeEntity retentionTypeEntity = new RetentionTypeEntity(); retentionTypeEntity.setCode(RetentionTypeEntity.PARTITION_VALUE); BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity(); businessObjectFormatEntity.setLatestVersion(true); businessObjectFormatEntity.setRetentionType(retentionTypeEntity); businessObjectFormatEntity.setRetentionPeriodInDays(RETENTION_PERIOD_DAYS); BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); businessObjectDataEntity.setBusinessObjectFormat(businessObjectFormatEntity); businessObjectDataEntity.setPartitionValue(PARTITION_VALUE); Timestamp currentTimestamp = new Timestamp(new Date().getTime()); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -(RETENTION_PERIOD_DAYS - 1)); Date primaryPartitionValueDate = calendar.getTime(); when(businessObjectDataHelper.getDateFromString(PARTITION_VALUE)).thenReturn(primaryPartitionValueDate); when(herdDao.getCurrentTimestamp()).thenReturn(currentTimestamp); when(businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateDestroyHelperServiceImpl.validateBusinessObjectData(businessObjectDataEntity, businessObjectDataKey); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format( "Business object data fails retention threshold check for retention type \"%s\" with retention period of %d days. Business object data: {%s}", RetentionTypeEntity.PARTITION_VALUE, RETENTION_PERIOD_DAYS, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } verify(businessObjectDataHelper).getDateFromString(PARTITION_VALUE); verify(businessObjectDataHelper).businessObjectDataKeyToString(businessObjectDataKey); verify(herdDao).getCurrentTimestamp(); verifyNoMoreInteractionsHelper(); }