use of org.springframework.core.env.Environment 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 org.springframework.core.env.Environment 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 org.springframework.core.env.Environment in project hub-alert by blackducksoftware.
the class ConfigurationOverridesStartupComponentTest method testInitializeNoChange.
@Test
public void testInitializeNoChange() throws AlertException {
Environment environment = Mockito.mock(Environment.class);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
ConfigurationOverridesStartupComponent configurationOverridesStartupComponent = new ConfigurationOverridesStartupComponent(environmentVariableUtility, userAccessor, descriptorKey, configurationModelConfigurationAccessor, apiAction, configurationFieldModelConverter);
// Update the sysadmin password
Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
UserModel sysadmin = sysadminOptional.get();
assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
UserModel updatedSysadmin = changeUserPassword(sysadmin, UPDATED_PASSWORD);
userAccessor.updateUser(updatedSysadmin, false);
// Run the initialize method
configurationOverridesStartupComponent.initialize();
// Verify the sysadmin password is the updated password
sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
sysadmin = sysadminOptional.get();
assertNotEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
HttpSession session = Mockito.mock(HttpSession.class);
Mockito.when(servletRequest.getSession()).thenReturn(session);
HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
// Try to login with the updated password
LoginConfig updatedLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, UPDATED_PASSWORD);
ActionResponse<Void> actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, updatedLoginConfig);
assertEquals(HttpStatus.NO_CONTENT, actionResponse.getHttpStatus());
// Try to login with the default password
LoginConfig defaultLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, DEFAULT_PASSWORD);
actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, defaultLoginConfig);
assertEquals(HttpStatus.UNAUTHORIZED, actionResponse.getHttpStatus());
}
use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.
the class ConfigurationOverridesStartupComponentTest method testInitializeResetPassword.
@Test
public void testInitializeResetPassword() throws AlertException {
Environment environment = Mockito.mock(Environment.class);
Mockito.when(environment.getProperty(ConfigurationOverridesStartupComponent.ENV_VAR_ADMIN_USER_PASSWORD_RESET)).thenReturn("true");
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
ConfigurationOverridesStartupComponent configurationOverridesStartupComponent = new ConfigurationOverridesStartupComponent(environmentVariableUtility, userAccessor, descriptorKey, configurationModelConfigurationAccessor, apiAction, configurationFieldModelConverter);
// Update the sysadmin password
Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
UserModel sysadmin = sysadminOptional.get();
assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
UserModel updatedSysadmin = changeUserPassword(sysadmin, UPDATED_PASSWORD);
userAccessor.updateUser(updatedSysadmin, false);
// Run the initialize method
configurationOverridesStartupComponent.initialize();
// Verify the sysadmin password is the default password
sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
sysadmin = sysadminOptional.get();
assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
HttpSession session = Mockito.mock(HttpSession.class);
Mockito.when(servletRequest.getSession()).thenReturn(session);
HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
// Try to login with the updated password
LoginConfig updatedLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, UPDATED_PASSWORD);
ActionResponse<Void> actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, updatedLoginConfig);
assertEquals(HttpStatus.UNAUTHORIZED, actionResponse.getHttpStatus());
// Try to login with the default password
LoginConfig defaultLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, DEFAULT_PASSWORD);
actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, defaultLoginConfig);
assertEquals(HttpStatus.NO_CONTENT, actionResponse.getHttpStatus());
}
use of org.springframework.core.env.Environment in project metron by apache.
the class SensorParserConfigServiceImplTest method setUp.
@BeforeEach
public void setUp() {
objectMapper = mock(ObjectMapper.class);
curatorFramework = mock(CuratorFramework.class);
Environment environment = mock(Environment.class);
Authentication authentication = mock(Authentication.class);
when(authentication.getName()).thenReturn(user);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(environment.getProperty(GROK_TEMP_PATH_SPRING_PROPERTY)).thenReturn("./target");
grokService = new GrokServiceImpl(environment, mock(Grok.class), new HdfsServiceImpl(new Configuration()));
cache = mock(ConfigurationsCache.class);
sensorParserConfigService = new SensorParserConfigServiceImpl(objectMapper, curatorFramework, grokService, cache);
}
Aggregations