@Test public void getProviderConfigurationByNameTest() throws Exception { final String providerConfigName = "provider-config-name-test"; final String emptyProviderConfigName = "bad-config-name"; final Long fieldId = 1L; final Long descriptorId = 4L; final Long configurationId = 6L; DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(fieldKey, false); definedFieldEntity.setId(fieldId); FieldValueEntity fieldValueEntity = new FieldValueEntity(2L, 3L, fieldValue); DescriptorConfigEntity descriptorConfigEntity = new DescriptorConfigEntity(descriptorId, 5L, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp()); descriptorConfigEntity.setId(configurationId); ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name()); Mockito.when(definedFieldRepository.findFirstByKey(Mockito.any())).thenReturn(Optional.of(definedFieldEntity)); Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName)).thenReturn(List.of(fieldValueEntity)); Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, emptyProviderConfigName)).thenReturn(List.of()); setupGetJobMocks(descriptorConfigEntity, configContextEntity, fieldValueEntity, definedFieldEntity); DefaultConfigurationAccessor configurationAccessor = new DefaultConfigurationAccessor(null, null, definedFieldRepository, descriptorConfigRepository, null, configContextRepository, fieldValueRepository, encryptionUtility); Optional<ConfigurationModel> configurationModelOptional = configurationAccessor.getProviderConfigurationByName(providerConfigName); Optional<ConfigurationModel> configurationModelProviderConfigsEmpty = configurationAccessor.getProviderConfigurationByName(emptyProviderConfigName); assertTrue(configurationModelOptional.isPresent()); assertFalse(configurationModelProviderConfigsEmpty.isPresent()); ConfigurationModel configurationModel = configurationModelOptional.get(); testConfigurationModel(configurationId, descriptorId, configurationModel); }
@Override public Optional<ConfigurationModel> getProviderConfigurationByName(String providerConfigName) throws AlertDatabaseConstraintException { if (StringUtils.isBlank(providerConfigName)) { throw new AlertDatabaseConstraintException("The provider configuration name cannot be null"); } Long fieldId = definedFieldRepository.findFirstByKey(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME) .map(DefinedFieldEntity::getId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("The key '%s' is not registered in the database", ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME))); List<Long> providerConfigIds = fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName) .stream() .map(FieldValueEntity::getConfigId) .collect(Collectors.toList()); if (!providerConfigIds.isEmpty()) { for (Long configId : providerConfigIds) { Optional<ConfigurationModel> globalModel = getConfigurationById(configId) .filter(model -> model.getDescriptorContext() == ConfigContextEnum.GLOBAL); if (globalModel.isPresent()) { return globalModel; } } } return Optional.empty(); }
DefaultConfigurationAccessor implements ConfigurationAccessor { @Override public Optional<ConfigurationModel> getProviderConfigurationByName(String providerConfigName) throws AlertDatabaseConstraintException { if (StringUtils.isBlank(providerConfigName)) { throw new AlertDatabaseConstraintException("The provider configuration name cannot be null"); } Long fieldId = definedFieldRepository.findFirstByKey(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME) .map(DefinedFieldEntity::getId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("The key '%s' is not registered in the database", ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME))); List<Long> providerConfigIds = fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName) .stream() .map(FieldValueEntity::getConfigId) .collect(Collectors.toList()); if (!providerConfigIds.isEmpty()) { for (Long configId : providerConfigIds) { Optional<ConfigurationModel> globalModel = getConfigurationById(configId) .filter(model -> model.getDescriptorContext() == ConfigContextEnum.GLOBAL); if (globalModel.isPresent()) { return globalModel; } } } return Optional.empty(); } }
DefaultConfigurationAccessor implements ConfigurationAccessor { @Override public Optional<ConfigurationModel> getProviderConfigurationByName(String providerConfigName) throws AlertDatabaseConstraintException { if (StringUtils.isBlank(providerConfigName)) { throw new AlertDatabaseConstraintException("The provider configuration name cannot be null"); } Long fieldId = definedFieldRepository.findFirstByKey(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME) .map(DefinedFieldEntity::getId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("The key '%s' is not registered in the database", ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME))); List<Long> providerConfigIds = fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName) .stream() .map(FieldValueEntity::getConfigId) .collect(Collectors.toList()); if (!providerConfigIds.isEmpty()) { for (Long configId : providerConfigIds) { Optional<ConfigurationModel> globalModel = getConfigurationById(configId) .filter(model -> model.getDescriptorContext() == ConfigContextEnum.GLOBAL); if (globalModel.isPresent()) { return globalModel; } } } return Optional.empty(); } @Autowired  DefaultConfigurationAccessor(RegisteredDescriptorRepository registeredDescriptorRepository, DescriptorTypeRepository descriptorTypeRepository, DefinedFieldRepository definedFieldRepository,
        DescriptorConfigRepository descriptorConfigsRepository, ConfigGroupRepository configGroupRepository, ConfigContextRepository configContextRepository, FieldValueRepository fieldValueRepository,
        EncryptionUtility encryptionUtility); }
DefaultConfigurationAccessor implements ConfigurationAccessor { @Override public Optional<ConfigurationModel> getProviderConfigurationByName(String providerConfigName) throws AlertDatabaseConstraintException { if (StringUtils.isBlank(providerConfigName)) { throw new AlertDatabaseConstraintException("The provider configuration name cannot be null"); } Long fieldId = definedFieldRepository.findFirstByKey(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME) .map(DefinedFieldEntity::getId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("The key '%s' is not registered in the database", ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME))); List<Long> providerConfigIds = fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName) .stream() .map(FieldValueEntity::getConfigId) .collect(Collectors.toList()); if (!providerConfigIds.isEmpty()) { for (Long configId : providerConfigIds) { Optional<ConfigurationModel> globalModel = getConfigurationById(configId) .filter(model -> model.getDescriptorContext() == ConfigContextEnum.GLOBAL); if (globalModel.isPresent()) { return globalModel; } } } return Optional.empty(); } @Autowired  DefaultConfigurationAccessor(RegisteredDescriptorRepository registeredDescriptorRepository, DescriptorTypeRepository descriptorTypeRepository, DefinedFieldRepository definedFieldRepository,
        DescriptorConfigRepository descriptorConfigsRepository, ConfigGroupRepository configGroupRepository, ConfigContextRepository configContextRepository, FieldValueRepository fieldValueRepository,
        EncryptionUtility encryptionUtility); @Override List<ConfigurationJobModel> getAllJobs(); @Override Optional<ConfigurationJobModel> getJobById(UUID jobId); @Override List<ConfigurationJobModel> getJobsByFrequency(FrequencyType frequency); @Override ConfigurationJobModel createJob(Collection<String> descriptorNames, Collection<ConfigurationFieldModel> configuredFields); @Override ConfigurationJobModel updateJob(UUID jobId, Collection<String> descriptorNames, Collection<ConfigurationFieldModel> configuredFields); @Override void deleteJob(UUID jobId); @Override Optional<ConfigurationModel> getProviderConfigurationByName(String providerConfigName); @Override Optional<ConfigurationModel> getConfigurationById(Long id); @Override List<ConfigurationModel> getConfigurationsByDescriptorKey(DescriptorKey descriptorKey); @Override List<ConfigurationModel> getConfigurationsByDescriptorType(DescriptorType descriptorType); @Override List<ConfigurationModel> getChannelConfigurationsByFrequency(FrequencyType frequencyType); @Override List<ConfigurationModel> getConfigurationsByDescriptorKeyAndContext(DescriptorKey descriptorKey, ConfigContextEnum context); @Override ConfigurationModel createConfiguration(DescriptorKey descriptorKey, ConfigContextEnum context, Collection<ConfigurationFieldModel> configuredFields); @Override List<ConfigurationModel> getConfigurationsByDescriptorNameAndContext(String descriptorName, ConfigContextEnum context); @Override ConfigurationModel updateConfiguration(Long descriptorConfigId, Collection<ConfigurationFieldModel> configuredFields); @Override void deleteConfiguration(ConfigurationModel configModel); @Override void deleteConfiguration(Long descriptorConfigId); }
DefaultConfigurationAccessor implements ConfigurationAccessor { @Override public Optional<ConfigurationModel> getProviderConfigurationByName(String providerConfigName) throws AlertDatabaseConstraintException { if (StringUtils.isBlank(providerConfigName)) { throw new AlertDatabaseConstraintException("The provider configuration name cannot be null"); } Long fieldId = definedFieldRepository.findFirstByKey(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME) .map(DefinedFieldEntity::getId) .orElseThrow(() -> new AlertDatabaseConstraintException(String.format("The key '%s' is not registered in the database", ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME))); List<Long> providerConfigIds = fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName) .stream() .map(FieldValueEntity::getConfigId) .collect(Collectors.toList()); if (!providerConfigIds.isEmpty()) { for (Long configId : providerConfigIds) { Optional<ConfigurationModel> globalModel = getConfigurationById(configId) .filter(model -> model.getDescriptorContext() == ConfigContextEnum.GLOBAL); if (globalModel.isPresent()) { return globalModel; } } } return Optional.empty(); } @Autowired  DefaultConfigurationAccessor(RegisteredDescriptorRepository registeredDescriptorRepository, DescriptorTypeRepository descriptorTypeRepository, DefinedFieldRepository definedFieldRepository,
        DescriptorConfigRepository descriptorConfigsRepository, ConfigGroupRepository configGroupRepository, ConfigContextRepository configContextRepository, FieldValueRepository fieldValueRepository,
        EncryptionUtility encryptionUtility); @Override List<ConfigurationJobModel> getAllJobs(); @Override Optional<ConfigurationJobModel> getJobById(UUID jobId); @Override List<ConfigurationJobModel> getJobsByFrequency(FrequencyType frequency); @Override ConfigurationJobModel createJob(Collection<String> descriptorNames, Collection<ConfigurationFieldModel> configuredFields); @Override ConfigurationJobModel updateJob(UUID jobId, Collection<String> descriptorNames, Collection<ConfigurationFieldModel> configuredFields); @Override void deleteJob(UUID jobId); @Override Optional<ConfigurationModel> getProviderConfigurationByName(String providerConfigName); @Override Optional<ConfigurationModel> getConfigurationById(Long id); @Override List<ConfigurationModel> getConfigurationsByDescriptorKey(DescriptorKey descriptorKey); @Override List<ConfigurationModel> getConfigurationsByDescriptorType(DescriptorType descriptorType); @Override List<ConfigurationModel> getChannelConfigurationsByFrequency(FrequencyType frequencyType); @Override List<ConfigurationModel> getConfigurationsByDescriptorKeyAndContext(DescriptorKey descriptorKey, ConfigContextEnum context); @Override ConfigurationModel createConfiguration(DescriptorKey descriptorKey, ConfigContextEnum context, Collection<ConfigurationFieldModel> configuredFields); @Override List<ConfigurationModel> getConfigurationsByDescriptorNameAndContext(String descriptorName, ConfigContextEnum context); @Override ConfigurationModel updateConfiguration(Long descriptorConfigId, Collection<ConfigurationFieldModel> configuredFields); @Override void deleteConfiguration(ConfigurationModel configModel); @Override void deleteConfiguration(Long descriptorConfigId); static final String EXCEPTION_FORMAT_DESCRIPTOR_KEY_IS_NOT_VALID; }