<commit_before>package net.nemerosa.ontrack.service.support.configuration;

import net.nemerosa.ontrack.extension.support.ConfigurationNotFoundException;
import net.nemerosa.ontrack.model.events.Event;
import net.nemerosa.ontrack.model.events.EventFactory;
import net.nemerosa.ontrack.model.events.EventPostService;
import net.nemerosa.ontrack.model.security.EncryptionService;
import net.nemerosa.ontrack.model.security.GlobalSettings;
import net.nemerosa.ontrack.model.security.SecurityService;
import net.nemerosa.ontrack.model.support.ConfigurationRepository;
import org.junit.Before;
import org.junit.Test;

import java.util.Optional;
import java.util.function.Function;

import static net.nemerosa.ontrack.service.support.configuration.TestConfiguration.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.*;

public class ConfigurationServiceTest {

    private TestConfigurationService configurationService;
    private SecurityService securityService;
    private EventPostService eventPostService;
    private EventFactory eventFactory;
    private ConfigurationRepository configurationRepository;

    @Before
    public void before() {
        configurationRepository = mock(ConfigurationRepository.class);
        securityService = mock(SecurityService.class);
        EncryptionService encryptionService = mock(EncryptionService.class);
        eventPostService = mock(EventPostService.class);
        eventFactory = mock(EventFactory.class);
        configurationService = new TestConfigurationServiceImpl(
                configurationRepository,
                securityService,
                encryptionService,
                eventPostService,
                eventFactory
        );
    }

    @Test
    public void replace_configuration_same() {
        TestConfiguration config = config("test");
        TestConfiguration replacedConfig = configurationService.replaceConfiguration(
                config,
                Function.identity()
        );
        assertSame("Same config is returned", config, replacedConfig);
    }

    @Test(expected = ConfigurationNotFoundException.class)
    public void replace_configuration_different_not_authorised() {
        TestConfiguration config = config("test");
        when(securityService.isGlobalFunctionGranted(GlobalSettings.class)).thenReturn(false);
        configurationService.replaceConfiguration(
                config,
                s -> s.replaceAll("test", "new")
        );
    }

    @Test
    public void replace_configuration_different_authorised() {
        TestConfiguration config = config("test");
        when(securityService.isGlobalFunctionGranted(GlobalSettings.class)).thenReturn(true);
        TestConfiguration replacedConfig = configurationService.replaceConfiguration(
                config,
                s -> s.replaceAll("test", "new")
        );
        assertEquals(config("new"), replacedConfig);
    }

    @Test
    public void event_on_new_configuration() {
        TestConfiguration config = config("test");
        Event event = Event.of(EventFactory.NEW_CONFIGURATION).with("configuration", "test").get();
        when(eventFactory.newConfiguration(config)).thenReturn(event);
        configurationService.newConfiguration(config);
        verify(eventPostService).post(event);
    }

    @Test
    public void event_on_update_configuration() {
        TestConfiguration config = config("test");
        Event event = Event.of(EventFactory.UPDATE_CONFIGURATION).with("configuration", "test").get();
        when(eventFactory.updateConfiguration(config)).thenReturn(event);
        configurationService.updateConfiguration("test", config);
        verify(eventPostService).post(event);
    }

    @Test
    public void event_on_delete_configuration() {
        TestConfiguration config = config("test");
        Event event = Event.of(EventFactory.DELETE_CONFIGURATION).with("configuration", "test").get();
        when(configurationRepository.find(TestConfiguration.class, "test")).thenReturn(Optional.of(config));
        when(eventFactory.deleteConfiguration(config)).thenReturn(event);
        configurationService.deleteConfiguration("test");
        verify(eventPostService).post(event);
    }

}<commit_msg>#344 Correction of tests - password is obfuscated on creation<commit_after>package net.nemerosa.ontrack.service.support.configuration;

import net.nemerosa.ontrack.extension.support.ConfigurationNotFoundException;
import net.nemerosa.ontrack.model.events.Event;
import net.nemerosa.ontrack.model.events.EventFactory;
import net.nemerosa.ontrack.model.events.EventPostService;
import net.nemerosa.ontrack.model.security.EncryptionService;
import net.nemerosa.ontrack.model.security.GlobalSettings;
import net.nemerosa.ontrack.model.security.SecurityService;
import net.nemerosa.ontrack.model.support.ConfigurationRepository;
import org.junit.Before;
import org.junit.Test;

import java.util.Optional;
import java.util.function.Function;

import static net.nemerosa.ontrack.service.support.configuration.TestConfiguration.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.*;

public class ConfigurationServiceTest {

    private TestConfigurationService configurationService;
    private SecurityService securityService;
    private EventPostService eventPostService;
    private EventFactory eventFactory;
    private ConfigurationRepository configurationRepository;

    @Before
    public void before() {
        configurationRepository = mock(ConfigurationRepository.class);
        securityService = mock(SecurityService.class);
        EncryptionService encryptionService = mock(EncryptionService.class);
        eventPostService = mock(EventPostService.class);
        eventFactory = mock(EventFactory.class);
        configurationService = new TestConfigurationServiceImpl(
                configurationRepository,
                securityService,
                encryptionService,
                eventPostService,
                eventFactory
        );
    }

    @Test
    public void replace_configuration_same() {
        TestConfiguration config = config("test");
        TestConfiguration replacedConfig = configurationService.replaceConfiguration(
                config,
                Function.identity()
        );
        assertSame("Same config is returned", config, replacedConfig);
    }

    @Test(expected = ConfigurationNotFoundException.class)
    public void replace_configuration_different_not_authorised() {
        TestConfiguration config = config("test");
        when(securityService.isGlobalFunctionGranted(GlobalSettings.class)).thenReturn(false);
        configurationService.replaceConfiguration(
                config,
                s -> s.replaceAll("test", "new")
        );
    }

    @Test
    public void replace_configuration_different_authorised() {
        TestConfiguration config = config("test");
        when(securityService.isGlobalFunctionGranted(GlobalSettings.class)).thenReturn(true);
        TestConfiguration replacedConfig = configurationService.replaceConfiguration(
                config,
                s -> s.replaceAll("test", "new")
        );
        assertEquals(config("new").withPassword(""), replacedConfig);
    }

    @Test
    public void event_on_new_configuration() {
        TestConfiguration config = config("test");
        Event event = Event.of(EventFactory.NEW_CONFIGURATION).with("configuration", "test").get();
        when(eventFactory.newConfiguration(config)).thenReturn(event);
        configurationService.newConfiguration(config);
        verify(eventPostService).post(event);
    }

    @Test
    public void event_on_update_configuration() {
        TestConfiguration config = config("test");
        Event event = Event.of(EventFactory.UPDATE_CONFIGURATION).with("configuration", "test").get();
        when(eventFactory.updateConfiguration(config)).thenReturn(event);
        configurationService.updateConfiguration("test", config);
        verify(eventPostService).post(event);
    }

    @Test
    public void event_on_delete_configuration() {
        TestConfiguration config = config("test");
        Event event = Event.of(EventFactory.DELETE_CONFIGURATION).with("configuration", "test").get();
        when(configurationRepository.find(TestConfiguration.class, "test")).thenReturn(Optional.of(config));
        when(eventFactory.deleteConfiguration(config)).thenReturn(event);
        configurationService.deleteConfiguration("test");
        verify(eventPostService).post(event);
    }

}
{'fraction_non_alphanumeric': 0.09522164651698331, 'fraction_numerical': 0.0003454231433506045, 'mean_word_length': 3.7360959651035985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5401856', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2050, 'n_words': 354}