]> source.dussan.org Git - sonarqube.git/blob
caaa3890edc2d0ecff53dc8d826b90afa2a23204
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.container;
21
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;
27 import java.util.Set;
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;
37
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;
44
45 public class ReportComputeEngineContainerPopulatorTest {
46   private CeTask task = mock(CeTask.class);
47   private ReportComputeEngineContainerPopulator underTest;
48
49   @Test
50   public void item_is_added_to_the_container() {
51     underTest = new ReportComputeEngineContainerPopulator(task, null);
52     AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
53     underTest.populateContainer(container);
54
55     assertThat(container.added).contains(task);
56   }
57
58   @Test
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);
63
64     Set<String> computationStepClassNames = from(container.added)
65       .transform(new Function<Object, Class<?>>() {
66         @Nullable
67         @Override
68         public Class<?> apply(Object input) {
69           if (input instanceof Class) {
70             return (Class<?>) input;
71           }
72           return null;
73         }
74       })
75       .filter(notNull())
76       .filter(IsComputationStep.INSTANCE)
77       .transform(StepsExplorer.toCanonicalName())
78       .toSet();
79
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());
82   }
83
84   @Test
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);
89
90     assertThat(container.added).contains(PersistComponentsStep.class);
91   }
92
93   @Test
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);
98
99     assertThat(container.added).doesNotContain(PersistDevelopersStep.class);
100   }
101
102   @Test
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));
106
107     underTest = new ReportComputeEngineContainerPopulator(task, devCockpitBridge);
108     AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
109     container.add(devCockpitBridge);
110     underTest.populateContainer(container);
111
112     assertThat(container.added).contains(PersistDevelopersStep.class);
113   }
114
115   @Test
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));
119
120     underTest = new ReportComputeEngineContainerPopulator(task, devCockpitBridge);
121     AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer();
122     container.add(devCockpitBridge);
123     underTest.populateContainer(container);
124
125     assertThat(container.added).contains(PersistDevelopersDelegateImpl.class);
126   }
127
128   private enum IsComputationStep implements Predicate<Class<?>> {
129     INSTANCE;
130
131     @Override
132     public boolean apply(Class<?> input) {
133       return ComputationStep.class.isAssignableFrom(input);
134     }
135   }
136
137   private static class AddedObjectsRecorderComputeEngineContainer implements ComputeEngineContainer {
138     private List<Object> added = new ArrayList<>();
139
140     @Override
141     public ComponentContainer getParent() {
142       throw new UnsupportedOperationException("getParent is not implemented");
143     }
144
145     @Override
146     public void cleanup() {
147       throw new UnsupportedOperationException("cleanup is not implemented");
148     }
149
150     @Override
151     public ComponentContainer add(Object... objects) {
152       added.addAll(Arrays.asList(objects));
153       return null; // not used anyway
154     }
155
156     @Override
157     public ComponentContainer addSingletons(Iterable<?> components) {
158       for (Object component : components) {
159         added.add(component);
160       }
161       return null; // not used anyway
162     }
163
164     @Override
165     public <T> T getComponentByType(Class<T> type) {
166       for (Object add : added) {
167         if (add.getClass().getSimpleName().contains(type.getSimpleName())) {
168           return (T) add;
169         }
170       }
171       return null;
172     }
173   }
174
175   private static class PersistDevelopersDelegateImpl implements PersistDevelopersDelegate {
176     @Override
177     public void execute() {
178       // nothing to do
179     }
180   }
181 }