3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.container;
22 import com.google.common.base.Function;
23 import com.google.common.base.Predicate;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
28 import javax.annotation.Nullable;
29 import org.junit.Test;
30 import org.sonar.core.platform.ComponentContainer;
31 import org.sonar.server.computation.queue.CeTask;
32 import org.sonar.server.computation.step.ComputationStep;
33 import org.sonar.server.computation.step.PersistComponentsStep;
34 import org.sonar.server.computation.step.PersistDevelopersStep;
35 import org.sonar.server.devcockpit.DevCockpitBridge;
36 import org.sonar.server.devcockpit.PersistDevelopersDelegate;
38 import static com.google.common.base.Predicates.notNull;
39 import static com.google.common.collect.FluentIterable.from;
40 import static com.google.common.collect.Sets.difference;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 public class ReportComputeEngineContainerPopulatorTest {
46 private CeTask task = mock(CeTask.class);
47 private ReportComputeEngineContainerPopulator underTest;
50 public void item_is_added_to_the_container() {
51 underTest = new ReportComputeEngineContainerPopulator(task, null);
52 AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
53 underTest.populateContainer(container);
55 assertThat(container.added).contains(task);
59 public void all_computation_steps_are_added_in_order_to_the_container() {
60 underTest = new ReportComputeEngineContainerPopulator(task, null);
61 AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
62 underTest.populateContainer(container);
64 Set<String> computationStepClassNames = from(container.added)
65 .transform(new Function<Object, Class<?>>() {
68 public Class<?> apply(Object input) {
69 if (input instanceof Class) {
70 return (Class<?>) input;
76 .filter(IsComputationStep.INSTANCE)
77 .transform(StepsExplorer.toCanonicalName())
80 // PersistDevelopersStep is the only step that is not in the report container (it's only added when Dev Cockpit plugin is installed)
81 assertThat(difference(StepsExplorer.retrieveStepPackageStepsCanonicalNames(), computationStepClassNames)).containsOnly(PersistDevelopersStep.class.getCanonicalName());
85 public void at_least_one_core_step_is_added_to_the_container() {
86 underTest = new ReportComputeEngineContainerPopulator(task, null);
87 AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
88 underTest.populateContainer(container);
90 assertThat(container.added).contains(PersistComponentsStep.class);
94 public void PersistDevelopersStep_is_not_added_to_the_container_when_DevCockpitBridge_is_null() {
95 underTest = new ReportComputeEngineContainerPopulator(task, null);
96 AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
97 underTest.populateContainer(container);
99 assertThat(container.added).doesNotContain(PersistDevelopersStep.class);
103 public void PersistDevelopersStep_is_added_to_the_container_when_DevCockpitBridge_exist() {
104 DevCockpitBridge devCockpitBridge = mock(DevCockpitBridge.class);
105 when(devCockpitBridge.getCeComponents()).thenReturn(Arrays.<Object>asList(PersistDevelopersDelegateImpl.class));
107 underTest = new ReportComputeEngineContainerPopulator(task, devCockpitBridge);
108 AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
109 container.add(devCockpitBridge);
110 underTest.populateContainer(container);
112 assertThat(container.added).contains(PersistDevelopersStep.class);
116 public void components_from_DevCockpitBridge_are_added_to_the_container_when_DevCockpitBridge_exist() {
117 DevCockpitBridge devCockpitBridge = mock(DevCockpitBridge.class);
118 when(devCockpitBridge.getCeComponents()).thenReturn(Arrays.<Object>asList(PersistDevelopersDelegateImpl.class));
120 underTest = new ReportComputeEngineContainerPopulator(task, devCockpitBridge);
121 AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
122 container.add(devCockpitBridge);
123 underTest.populateContainer(container);
125 assertThat(container.added).contains(PersistDevelopersDelegateImpl.class);
128 private enum IsComputationStep implements Predicate<Class<?>> {
132 public boolean apply(Class<?> input) {
133 return ComputationStep.class.isAssignableFrom(input);
137 private static class AddedObjectsRecorderComputeEngineContainer implements ComputeEngineContainer {
138 private List<Object> added = new ArrayList<>();
141 public ComponentContainer getParent() {
142 throw new UnsupportedOperationException("getParent is not implemented");
146 public void cleanup() {
147 throw new UnsupportedOperationException("cleanup is not implemented");
151 public ComponentContainer add(Object... objects) {
152 added.addAll(Arrays.asList(objects));
153 return null; // not used anyway
157 public ComponentContainer addSingletons(Iterable<?> components) {
158 for (Object component : components) {
159 added.add(component);
161 return null; // not used anyway
165 public <T> T getComponentByType(Class<T> type) {
166 for (Object add : added) {
167 if (add.getClass().getSimpleName().contains(type.getSimpleName())) {
175 private static class PersistDevelopersDelegateImpl implements PersistDevelopersDelegate {
177 public void execute() {