use of com.synopsys.integration.alert.descriptor.api.model.DescriptorKey in project hub-alert by blackducksoftware.
the class JobConfigActionsTest method init.
@BeforeEach
public void init() {
this.descriptorKey = new DescriptorKey(DESCRIPTOR_KEY_STRING, "Universal Key") {
};
this.descriptor = createDescriptor(Optional::empty, Optional::empty);
this.descriptorMap = new DescriptorMap(List.of(descriptorKey), List.of(descriptor));
this.fieldModel = createFieldModel();
this.distributionJobModel = createDistributionJobModel();
this.jobId = distributionJobModel.getJobId();
this.jobFieldModel = new JobFieldModel(UUID.randomUUID().toString(), Set.of(fieldModel), List.of());
this.configurationFieldModel = ConfigurationFieldModel.create(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME);
this.configurationFieldModel.setFieldValue(FIELD_VALUE);
mockedAuthorizationManager = Mockito.mock(AuthorizationManager.class);
mockedDescriptorAccessor = Mockito.mock(DescriptorAccessor.class);
mockedConfigurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
mockedJobAccessor = Mockito.mock(JobAccessor.class);
mockedFieldModelProcessor = Mockito.mock(FieldModelProcessor.class);
mockedDescriptorProcessor = Mockito.mock(DescriptorProcessor.class);
mockedConfigurationFieldModelConverter = Mockito.mock(ConfigurationFieldModelConverter.class);
mockedGlobalConfigExistsValidator = Mockito.mock(GlobalConfigExistsValidator.class);
mockedPkixErrorResponseFactory = Mockito.mock(PKIXErrorResponseFactory.class);
mockedJobModelExtractor = Mockito.mock(DistributionJobModelExtractor.class);
Mockito.when(mockedJobModelExtractor.convertToJobModel(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyList())).thenReturn(distributionJobModel);
defaultJobConfigActions = createJobConfigActions(descriptorMap, List.of());
Mockito.when(mockedAuthorizationManager.hasCreatePermission(Mockito.any(ConfigContextEnum.class), Mockito.eq(descriptorKey))).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.eq(descriptorKey))).thenReturn(true);
Mockito.when(mockedAuthorizationManager.anyReadPermission(Mockito.any(), Mockito.any())).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasWritePermission(Mockito.any(ConfigContextEnum.class), Mockito.eq(descriptorKey))).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasDeletePermission(Mockito.any(ConfigContextEnum.class), Mockito.eq(descriptorKey))).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasExecutePermission(Mockito.any(ConfigContextEnum.class), Mockito.eq(descriptorKey))).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasCreatePermission(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasReadPermission(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasWritePermission(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasDeletePermission(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
Mockito.when(mockedAuthorizationManager.hasExecutePermission(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
}
use of com.synopsys.integration.alert.descriptor.api.model.DescriptorKey in project hub-alert by blackducksoftware.
the class ConfigActions method readAllByContextAndDescriptorWithoutChecks.
@Override
protected ActionResponse<MultiFieldModel> readAllByContextAndDescriptorWithoutChecks(String context, String descriptorName) {
ConfigContextEnum configContext = ConfigContextEnum.valueOf(context);
Optional<DescriptorKey> descriptorKey = descriptorMap.getDescriptorKey(descriptorName);
if (!descriptorKey.isPresent()) {
return new ActionResponse<>(HttpStatus.BAD_REQUEST, String.format("Unknown descriptor: %s", descriptorName));
}
List<ConfigurationModel> configurationModels = configurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(descriptorKey.get(), configContext);
List<FieldModel> fieldModels = convertConfigurationModelList(descriptorName, context, configurationModels);
return new ActionResponse<>(HttpStatus.OK, new MultiFieldModel(fieldModels));
}
use of com.synopsys.integration.alert.descriptor.api.model.DescriptorKey in project hub-alert by blackducksoftware.
the class AlertStartupInitializerTest method testOverwrite.
@Test
public void testOverwrite() throws Exception {
Environment environment = Mockito.mock(Environment.class);
DescriptorAccessor baseDescriptorAccessor = Mockito.mock(DescriptorAccessor.class);
ConfigurationModelConfigurationAccessor baseConfigurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
EncryptionUtility encryptionUtility = Mockito.mock(EncryptionUtility.class);
Mockito.when(encryptionUtility.isInitialized()).thenReturn(Boolean.TRUE);
ChannelDescriptor channelDescriptor = new EmailDescriptor(null, null);
ConfigurationModel settingsModel = Mockito.mock(ConfigurationModel.class);
ConfigurationFieldModel envOverrideField = Mockito.mock(ConfigurationFieldModel.class);
ConfigurationModel slackModel = Mockito.mock(ConfigurationModel.class);
Mockito.when(envOverrideField.getFieldValue()).thenReturn(Optional.of("true"));
Mockito.when(baseConfigurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(SETTINGS_DESCRIPTOR_KEY, ConfigContextEnum.GLOBAL)).thenReturn(List.of(settingsModel));
Mockito.when(baseConfigurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(channelDescriptor.getDescriptorKey(), ConfigContextEnum.GLOBAL)).thenReturn(List.of(slackModel));
List<DescriptorKey> descriptorKeys = List.of(channelDescriptor.getDescriptorKey(), SETTINGS_DESCRIPTOR_KEY);
List<Descriptor> descriptors = List.of(channelDescriptor);
DescriptorMap descriptorMap = new DescriptorMap(descriptorKeys, descriptors);
ConfigurationFieldModelConverter modelConverter = new ConfigurationFieldModelConverter(encryptionUtility, baseDescriptorAccessor, descriptorKeys);
Mockito.when(baseDescriptorAccessor.getFieldsForDescriptor(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class))).thenReturn(List.of());
final String value = "newValue";
Mockito.when(environment.getProperty(Mockito.startsWith("ALERT_CHANNEL_"))).thenReturn(value);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
EnvironmentVariableProcessor environmentVariableProcessor = new EnvironmentVariableProcessor(List.of());
FieldModelProcessor fieldModelProcessor = new FieldModelProcessor(modelConverter, new DescriptorProcessor(descriptorMap, baseConfigurationModelConfigurationAccessor, List.of()));
SettingsUtility settingsUtility = Mockito.mock(SettingsUtility.class);
Mockito.when(settingsUtility.getKey()).thenReturn(new SettingsDescriptorKey());
AlertStartupInitializer initializer = new AlertStartupInitializer(descriptorMap, environmentVariableUtility, baseDescriptorAccessor, baseConfigurationModelConfigurationAccessor, modelConverter, fieldModelProcessor, settingsUtility, environmentVariableProcessor);
initializer.initializeComponent();
Mockito.verify(baseDescriptorAccessor, Mockito.times(2)).getFieldsForDescriptor(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class));
Mockito.verify(baseConfigurationModelConfigurationAccessor, Mockito.times(0)).createConfiguration(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class), Mockito.anyCollection());
Mockito.verify(baseConfigurationModelConfigurationAccessor, Mockito.times(2)).getConfigurationsByDescriptorKeyAndContext(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class));
// Mockito.verify(baseConfigurationAccessor, Mockito.times(2)).updateConfiguration(Mockito.anyLong(), Mockito.anyCollection());
}
use of com.synopsys.integration.alert.descriptor.api.model.DescriptorKey in project hub-alert by blackducksoftware.
the class AlertStartupInitializerTest method testInitializeConfigs.
// TODO these tests need to be re-written
@Test
public void testInitializeConfigs() {
Environment environment = Mockito.mock(Environment.class);
DescriptorAccessor baseDescriptorAccessor = Mockito.mock(DescriptorAccessor.class);
ConfigurationModelConfigurationAccessor baseConfigurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
EncryptionUtility encryptionUtility = Mockito.mock(EncryptionUtility.class);
Mockito.when(encryptionUtility.isInitialized()).thenReturn(Boolean.TRUE);
ChannelDescriptor channelDescriptor = new EmailDescriptor(null, null);
SettingsDescriptorKey settingsDescriptorKey = new SettingsDescriptorKey();
List<DescriptorKey> descriptorKeys = List.of(channelDescriptor.getDescriptorKey(), settingsDescriptorKey);
List<Descriptor> descriptors = List.of(channelDescriptor);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
EnvironmentVariableProcessor environmentVariableProcessor = new EnvironmentVariableProcessor(List.of());
DescriptorMap descriptorMap = new DescriptorMap(descriptorKeys, descriptors);
ConfigurationFieldModelConverter modelConverter = new ConfigurationFieldModelConverter(encryptionUtility, baseDescriptorAccessor, descriptorKeys);
Mockito.when(baseDescriptorAccessor.getFieldsForDescriptor(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class))).thenReturn(List.of());
FieldModelProcessor fieldModelProcessor = new FieldModelProcessor(modelConverter, new DescriptorProcessor(descriptorMap, baseConfigurationModelConfigurationAccessor, List.of()));
SettingsUtility settingsUtility = Mockito.mock(SettingsUtility.class);
Mockito.when(settingsUtility.getKey()).thenReturn(settingsDescriptorKey);
AlertStartupInitializer initializer = new AlertStartupInitializer(descriptorMap, environmentVariableUtility, baseDescriptorAccessor, baseConfigurationModelConfigurationAccessor, modelConverter, fieldModelProcessor, settingsUtility, environmentVariableProcessor);
initializer.initializeComponent();
Mockito.verify(baseDescriptorAccessor, Mockito.times(4)).getFieldsForDescriptor(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class));
Mockito.verify(baseConfigurationModelConfigurationAccessor, Mockito.times(2)).getConfigurationsByDescriptorKeyAndContext(Mockito.any(DescriptorKey.class), Mockito.any(ConfigContextEnum.class));
}
use of com.synopsys.integration.alert.descriptor.api.model.DescriptorKey in project hub-alert by blackducksoftware.
the class DefaultDescriptorAccessorTest method getFieldsForDescriptorTest.
@Test
public void getFieldsForDescriptorTest() {
final String name = "name-test";
final Long typeId = 1L;
final ConfigContextEnum configContextEnum = ConfigContextEnum.GLOBAL;
final ConfigContextEnum invalidConfigContextEnum = ConfigContextEnum.DISTRIBUTION;
final String definedFieldsKey = "defined-field-key-test";
Boolean isSensitive = Boolean.TRUE;
RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(name, typeId);
registeredDescriptorEntity.setId(1L);
DescriptorKey descriptorKey = createDescriptorKey("descriptorKey-test");
ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name());
configContextEntity.setId(3L);
DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(definedFieldsKey, isSensitive);
RegisteredDescriptorRepository registeredDescriptorRepository = Mockito.mock(RegisteredDescriptorRepository.class);
DefinedFieldRepository definedFieldRepository = Mockito.mock(DefinedFieldRepository.class);
ConfigContextRepository configContextRepository = Mockito.mock(ConfigContextRepository.class);
DescriptorTypeRepository descriptorTypeRepository = Mockito.mock(DescriptorTypeRepository.class);
Mockito.when(registeredDescriptorRepository.findFirstByName(descriptorKey.getUniversalKey())).thenReturn(Optional.of(registeredDescriptorEntity));
Mockito.when(configContextRepository.findFirstByContext(configContextEnum.name())).thenReturn(Optional.of(configContextEntity));
// Used to test the optional expression
Mockito.when(configContextRepository.findFirstByContext(invalidConfigContextEnum.name())).thenReturn(Optional.empty());
Mockito.when(configContextRepository.save(Mockito.any())).thenReturn(configContextEntity);
Mockito.when(definedFieldRepository.findByDescriptorIdAndContext(Mockito.any(), Mockito.any())).thenReturn(List.of(definedFieldEntity));
DefaultDescriptorAccessor descriptorAccessor = new DefaultDescriptorAccessor(registeredDescriptorRepository, definedFieldRepository, configContextRepository, descriptorTypeRepository);
List<DefinedFieldModel> definedFieldModelList = descriptorAccessor.getFieldsForDescriptor(descriptorKey, configContextEnum);
List<DefinedFieldModel> emptyConfigContextDefinedFieldModelList = descriptorAccessor.getFieldsForDescriptor(descriptorKey, invalidConfigContextEnum);
assertEquals(1, definedFieldModelList.size());
DefinedFieldModel definedFieldModel = definedFieldModelList.get(0);
assertEquals(definedFieldsKey, definedFieldModel.getKey());
assertEquals(isSensitive, definedFieldModel.getSensitive());
List<ConfigContextEnum> configContextList = new ArrayList<>(definedFieldModel.getContexts());
assertEquals(configContextEnum, configContextList.get(0));
assertEquals(1, emptyConfigContextDefinedFieldModelList.size());
DefinedFieldModel emptyConfigContextDefinedFieldModel = emptyConfigContextDefinedFieldModelList.get(0);
assertEquals(definedFieldsKey, emptyConfigContextDefinedFieldModel.getKey());
assertEquals(isSensitive, emptyConfigContextDefinedFieldModel.getSensitive());
List<ConfigContextEnum> configContextList2 = new ArrayList<>(emptyConfigContextDefinedFieldModel.getContexts());
assertEquals(invalidConfigContextEnum, configContextList2.get(0));
}
Aggregations