<commit_before>/*************************GO-LICENSE-START*********************************
 * Copyright 2014 ThoughtWorks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *************************GO-LICENSE-END***********************************/

package com.thoughtworks.go.server.service.tasks;

import com.thoughtworks.go.config.AntTask;
import com.thoughtworks.go.config.ExecTask;
import com.thoughtworks.go.config.FetchTask;
import com.thoughtworks.go.config.pluggabletask.PluggableTask;
import com.thoughtworks.go.config.registry.ConfigElementImplementationRegistry;
import com.thoughtworks.go.domain.Task;
import com.thoughtworks.go.domain.config.Configuration;
import com.thoughtworks.go.domain.config.PluginConfiguration;
import com.thoughtworks.go.plugin.access.pluggabletask.PluggableTaskConfigStore;
import com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference;
import com.thoughtworks.go.plugin.api.config.Property;
import com.thoughtworks.go.plugin.api.response.validation.ValidationResult;
import com.thoughtworks.go.plugin.api.task.TaskConfig;
import com.thoughtworks.go.plugin.api.task.TaskConfigProperty;
import com.thoughtworks.go.plugin.api.task.TaskExecutor;
import com.thoughtworks.go.plugin.api.task.TaskView;
import com.thoughtworks.go.plugin.infra.PluginManager;
import com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor;
import com.thoughtworks.go.plugins.presentation.PluggableViewModel;
import com.thoughtworks.go.plugins.presentation.Renderer;
import com.thoughtworks.go.presentation.TaskViewModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static com.thoughtworks.go.domain.packagerepository.ConfigurationPropertyMother.create;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertNull;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

public class TaskViewServiceTest {
    private ConfigElementImplementationRegistry registry;
    private TaskViewService taskViewService;
    private PluginManager pluginManager;

    @Before
    public void setUp() {
        cleanupTaskPreferences();
        registry = mock(ConfigElementImplementationRegistry.class);
        pluginManager = mock(PluginManager.class);
        taskViewService = new TaskViewService(registry, pluginManager);
    }

    @After
    public void tearDown() throws Exception {
        cleanupTaskPreferences();
    }

    private void cleanupTaskPreferences() {
        Set<String> plugins = PluggableTaskConfigStore.store().pluginsWithPreference();
        for (String pluginId : plugins) {
            PluggableTaskConfigStore.store().removePreferenceFor(pluginId);
        }
    }

    @Test
    public void shouldGetViewModelsForBuiltinTasks() {
        List<Class<? extends Task>> taskClasses = taskImplementations();

        when(registry.implementersOf(Task.class)).thenReturn(taskClasses);
        when(registry.getViewModelFor(new AntTask(), "new")).thenReturn(viewModel(new AntTask()));
        when(registry.getViewModelFor(new ExecTask(), "new")).thenReturn(new TaskViewModel(new ExecTask(), "", Renderer.ERB));

        List<PluggableViewModel> taskViewModels = taskViewService.getTaskViewModels();

        assertThat(taskViewModels.size(), is(3));
        assertThat(taskViewModels, hasItem((PluggableViewModel) viewModel(new AntTask())));
        assertThat(taskViewModels, hasItem((PluggableViewModel) new TaskViewModel(new ExecTask(), "", Renderer.ERB)));
    }

    private void storeTaskPreferences(String pluginId, String... properties){
        FakeTask task = new FakeTask(properties);
        PluggableTaskConfigStore.store().setPreferenceFor(pluginId, new TaskPreference(task));
    }

    private void storeTaskPreferences(String pluginId, Property... properties){
        FakeTask task = new FakeTask(properties);
        PluggableTaskConfigStore.store().setPreferenceFor(pluginId, new TaskPreference(task));
    }

    @Test
    public void shouldGetViewModelsForPluggedInTasks_ButOnlyForExistingPlugins() throws Exception {
        String plugin1 = "task-plugin-1";
        String plugin2 = "task-plugin-2";
        when(pluginManager.getPluginDescriptorFor(plugin1)).thenReturn(new GoPluginDescriptor(plugin1, "1", null, null, null, false));
        when(pluginManager.getPluginDescriptorFor(plugin2)).thenReturn(new GoPluginDescriptor(plugin2, "1", null, null, null, false));
        storeTaskPreferences(plugin1, "key_1", "key_2");
        storeTaskPreferences(plugin2, "key_3", "key_4");
        when(registry.implementersOf(Task.class)).thenReturn(Arrays.<Class<? extends Task>>asList(ExecTask.class, PluggableTask.class));

        PluggableTask expectedPluggableTaskForPlugin1 = new PluggableTask("", new PluginConfiguration(plugin1, "1"), new Configuration(create("key_1"), create("key_2")));
        PluggableTask expectedPluggableTaskForPlugin2 = new PluggableTask("", new PluginConfiguration(plugin2, "1"), new Configuration(create("key_3"), create("key_4")));

        when(registry.getViewModelFor(new ExecTask(), "new")).thenReturn(viewModel(new ExecTask()));
        when(registry.getViewModelFor(expectedPluggableTaskForPlugin1, "new")).thenReturn(viewModel(expectedPluggableTaskForPlugin1));
        when(registry.getViewModelFor(expectedPluggableTaskForPlugin2, "new")).thenReturn(viewModel(expectedPluggableTaskForPlugin2));

        List<PluggableViewModel> viewModels = taskViewService.getTaskViewModels();

        assertThat(viewModels.size(), is(3));
        assertThat(viewModels.get(0).getModel(), instanceOf(ExecTask.class));
        assertThat(viewModels.get(1).getModel(), instanceOf(PluggableTask.class));
        assertThat(viewModels.get(2).getModel(), instanceOf(PluggableTask.class));

        assertThat((PluggableTask) viewModels.get(1).getModel(), is(expectedPluggableTaskForPlugin1));
        assertThat((PluggableTask) viewModels.get(2).getModel(), is(expectedPluggableTaskForPlugin2));

        verify(registry).getViewModelFor(expectedPluggableTaskForPlugin1, "new");
        verify(registry).getViewModelFor(expectedPluggableTaskForPlugin2, "new");
    }

    @Test
    public void shouldStoreDefaultValuesGivenForPropertiesInAPluginWhenInitializingANewTaskPlugin() throws Exception {
        String plugin = "task-plugin";
        when(pluginManager.getPluginDescriptorFor(plugin)).thenReturn(new GoPluginDescriptor(plugin, "1", null, null, null, false));
        Property taskConfigProperty1 = new Property("key1");
        Property taskConfigProperty2 = new Property("key2");
        Property taskConfigProperty3 = new Property("key3");
        storeTaskPreferences(plugin, taskConfigProperty1.withDefault("default1"), taskConfigProperty2.withDefault("default2"), taskConfigProperty3);
        when(registry.implementersOf(Task.class)).thenReturn(Arrays.<Class<? extends Task>>asList(PluggableTask.class));

        PluggableTask pluggableTask = (PluggableTask) taskViewService.taskInstanceFor(new PluggableTask("", new PluginConfiguration(plugin, "1"), new Configuration()).getTaskType());
        assertThat(pluggableTask.getConfiguration().getProperty("key1").getValue(), is("default1"));
        assertThat(pluggableTask.getConfiguration().getProperty("key2").getValue(), is("default2"));
        assertNull(pluggableTask.getConfiguration().getProperty("key3").getValue());
    }

    @Test
    public void shouldGetViewModelsOnlyForBuiltInTasks_WhenThereAreNoExistingPlugins() throws Exception {
        List<Class<? extends Task>> taskClasses = taskImplementations();
        taskClasses.add(PluggableTask.class);

        when(registry.implementersOf(Task.class)).thenReturn(taskClasses);
        when(registry.getViewModelFor(new AntTask(), "new")).thenReturn(viewModel(new AntTask()));
        when(registry.getViewModelFor(new ExecTask(), "new")).thenReturn(new TaskViewModel(new ExecTask(), "", Renderer.ERB));

        List<PluggableViewModel> taskViewModels = taskViewService.getTaskViewModels();<commit_msg>#718 - fixing failing test<commit_after>/*************************GO-LICENSE-START*********************************
 * Copyright 2014 ThoughtWorks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *************************GO-LICENSE-END***********************************/

package com.thoughtworks.go.server.service.tasks;

import com.thoughtworks.go.config.AntTask;
import com.thoughtworks.go.config.ExecTask;
import com.thoughtworks.go.config.FetchTask;
import com.thoughtworks.go.config.pluggabletask.PluggableTask;
import com.thoughtworks.go.config.registry.ConfigElementImplementationRegistry;
import com.thoughtworks.go.domain.Task;
import com.thoughtworks.go.domain.config.Configuration;
import com.thoughtworks.go.domain.config.PluginConfiguration;
import com.thoughtworks.go.plugin.access.pluggabletask.PluggableTaskConfigStore;
import com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference;
import com.thoughtworks.go.plugin.api.config.Property;
import com.thoughtworks.go.plugin.api.response.validation.ValidationResult;
import com.thoughtworks.go.plugin.api.task.TaskConfig;
import com.thoughtworks.go.plugin.api.task.TaskConfigProperty;
import com.thoughtworks.go.plugin.api.task.TaskExecutor;
import com.thoughtworks.go.plugin.api.task.TaskView;
import com.thoughtworks.go.plugin.infra.PluginManager;
import com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor;
import com.thoughtworks.go.plugins.presentation.PluggableViewModel;
import com.thoughtworks.go.plugins.presentation.Renderer;
import com.thoughtworks.go.presentation.TaskViewModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static com.thoughtworks.go.domain.packagerepository.ConfigurationPropertyMother.create;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertNull;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

public class TaskViewServiceTest {
    private ConfigElementImplementationRegistry registry;
    private TaskViewService taskViewService;
    private PluginManager pluginManager;

    @Before
    public void setUp() {
        cleanupTaskPreferences();
        registry = mock(ConfigElementImplementationRegistry.class);
        pluginManager = mock(PluginManager.class);
        taskViewService = new TaskViewService(registry, pluginManager);
    }

    @After
    public void tearDown() throws Exception {
        cleanupTaskPreferences();
    }

    private void cleanupTaskPreferences() {
        Set<String> plugins = PluggableTaskConfigStore.store().pluginsWithPreference();
        for (String pluginId : plugins) {
            PluggableTaskConfigStore.store().removePreferenceFor(pluginId);
        }
    }

    @Test
    public void shouldGetViewModelsForBuiltinTasks() {
        List<Class<? extends Task>> taskClasses = taskImplementations();

        when(registry.implementersOf(Task.class)).thenReturn(taskClasses);
        when(registry.getViewModelFor(new AntTask(), "new")).thenReturn(viewModel(new AntTask()));
        when(registry.getViewModelFor(new ExecTask(), "new")).thenReturn(new TaskViewModel(new ExecTask(), "", Renderer.ERB));

        List<PluggableViewModel> taskViewModels = taskViewService.getTaskViewModels();

        assertThat(taskViewModels.size(), is(3));
        assertThat(taskViewModels, hasItem((PluggableViewModel) viewModel(new AntTask())));
        assertThat(taskViewModels, hasItem((PluggableViewModel) new TaskViewModel(new ExecTask(), "", Renderer.ERB)));
    }

    private void storeTaskPreferences(String pluginId, String... properties){
        FakeTask task = new FakeTask(properties);
        PluggableTaskConfigStore.store().setPreferenceFor(pluginId, new TaskPreference(task));
    }

    private void storeTaskPreferences(String pluginId, Property... properties){
        FakeTask task = new FakeTask(properties);
        PluggableTaskConfigStore.store().setPreferenceFor(pluginId, new TaskPreference(task));
    }

    @Test
    public void shouldGetViewModelsForPluggedInTasks_ButOnlyForExistingPlugins() throws Exception {
        String plugin1 = "task-plugin-1";
        String plugin2 = "task-plugin-2";
        when(pluginManager.getPluginDescriptorFor(plugin1)).thenReturn(new GoPluginDescriptor(plugin1, "1", null, null, null, false));
        when(pluginManager.getPluginDescriptorFor(plugin2)).thenReturn(new GoPluginDescriptor(plugin2, "1", null, null, null, false));
        storeTaskPreferences(plugin1, "key_1", "key_2");
        storeTaskPreferences(plugin2, "key_3", "key_4");
        when(registry.implementersOf(Task.class)).thenReturn(Arrays.<Class<? extends Task>>asList(ExecTask.class, PluggableTask.class));

        PluggableTask expectedPluggableTaskForPlugin1 = new PluggableTask("", new PluginConfiguration(plugin1, "1"), new Configuration(create("key_1"), create("key_2")));
        PluggableTask expectedPluggableTaskForPlugin2 = new PluggableTask("", new PluginConfiguration(plugin2, "1"), new Configuration(create("key_3"), create("key_4")));

        when(registry.getViewModelFor(new ExecTask(), "new")).thenReturn(viewModel(new ExecTask()));
        when(registry.getViewModelFor(expectedPluggableTaskForPlugin1, "new")).thenReturn(viewModel(expectedPluggableTaskForPlugin1));
        when(registry.getViewModelFor(expectedPluggableTaskForPlugin2, "new")).thenReturn(viewModel(expectedPluggableTaskForPlugin2));

        List<PluggableViewModel> viewModels = taskViewService.getTaskViewModels();

        assertThat(viewModels.size(), is(3));
        assertThat(viewModels.get(0).getModel(), instanceOf(ExecTask.class));
        assertThat(viewModels.get(1).getModel(), instanceOf(PluggableTask.class));
        assertThat(viewModels.get(2).getModel(), instanceOf(PluggableTask.class));

        assertThat((PluggableTask) viewModels.get(1).getModel(), is(expectedPluggableTaskForPlugin1));
        assertThat((PluggableTask) viewModels.get(2).getModel(), is(expectedPluggableTaskForPlugin2));

        verify(registry).getViewModelFor(expectedPluggableTaskForPlugin1, "new");
        verify(registry).getViewModelFor(expectedPluggableTaskForPlugin2, "new");
    }

    @Test
    public void shouldStoreDefaultValuesGivenForPropertiesInAPluginWhenInitializingANewTaskPlugin() throws Exception {
        String plugin = "task-plugin";
        when(pluginManager.getPluginDescriptorFor(plugin)).thenReturn(new GoPluginDescriptor(plugin, "1", null, null, null, false));
        Property taskConfigProperty1 = new TaskConfigProperty("key1", null);
        Property taskConfigProperty2 = new TaskConfigProperty("key2", null);
        Property taskConfigProperty3 = new TaskConfigProperty("key3", null);
        storeTaskPreferences(plugin, taskConfigProperty1.withDefault("default1"), taskConfigProperty2.withDefault("default2"), taskConfigProperty3);
        when(registry.implementersOf(Task.class)).thenReturn(Arrays.<Class<? extends Task>>asList(PluggableTask.class));

        PluggableTask pluggableTask = (PluggableTask) taskViewService.taskInstanceFor(new PluggableTask("", new PluginConfiguration(plugin, "1"), new Configuration()).getTaskType());
        assertThat(pluggableTask.getConfiguration().getProperty("key1").getValue(), is("default1"));
        assertThat(pluggableTask.getConfiguration().getProperty("key2").getValue(), is("default2"));
        assertNull(pluggableTask.getConfiguration().getProperty("key3").getValue());
    }

    @Test
    public void shouldGetViewModelsOnlyForBuiltInTasks_WhenThereAreNoExistingPlugins() throws Exception {
        List<Class<? extends Task>> taskClasses = taskImplementations();
        taskClasses.add(PluggableTask.class);

        when(registry.implementersOf(Task.class)).thenReturn(taskClasses);
        when(registry.getViewModelFor(new AntTask(), "new")).thenReturn(viewModel(new AntTask()));
        when(registry.getViewModelFor(new ExecTask(), "new")).thenReturn(new TaskViewModel(new ExecTask(), "", Renderer.ERB));

        List<PluggableViewModel> taskViewModels = taskViewService.getTaskViewModels();
{'fraction_non_alphanumeric': 0.12468164582036335, 'fraction_numerical': 0.007753692908483785, 'mean_word_length': 5.353829557713053, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6443660', 'n_tokens_mistral': 4894, 'n_tokens_neox': 4751, 'n_words': 873}