2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.queue.report;
22 import org.apache.commons.io.IOUtils;
23 import org.hamcrest.Description;
24 import org.hamcrest.TypeSafeMatcher;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.core.permission.GlobalPermissions;
28 import org.sonar.db.ce.CeTaskTypes;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.server.component.ComponentService;
31 import org.sonar.server.component.NewComponent;
32 import org.sonar.server.computation.ReportFiles;
33 import org.sonar.server.computation.queue.CeQueue;
34 import org.sonar.server.computation.queue.CeQueueImpl;
35 import org.sonar.server.computation.queue.CeTaskSubmit;
36 import org.sonar.server.permission.PermissionService;
37 import org.sonar.server.tester.UserSessionRule;
39 import static org.mockito.Matchers.any;
40 import static org.mockito.Matchers.argThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.verifyZeroInteractions;
44 import static org.mockito.Mockito.when;
46 public class ReportSubmitterTest {
49 public UserSessionRule userSession = UserSessionRule.standalone();
51 CeQueue queue = mock(CeQueueImpl.class);
52 ReportFiles reportFiles = mock(ReportFiles.class);
53 ComponentService componentService = mock(ComponentService.class);
54 PermissionService permissionService = mock(PermissionService.class);
55 ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService, permissionService);
58 public void submit_a_report_on_existing_project() {
59 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder("TASK_1"));
60 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
61 when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(new ComponentDto().setUuid("P1"));
63 underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
65 verifyZeroInteractions(permissionService);
66 verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
68 protected boolean matchesSafely(CeTaskSubmit submit) {
69 return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
70 submit.getUuid().equals("TASK_1");
74 public void describeTo(Description description) {
81 public void provision_project_if_does_not_exist() throws Exception {
82 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder("TASK_1"));
83 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
84 when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(null);
85 when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1").setKey("MY_PROJECT"));
87 underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
89 verify(permissionService).applyDefaultPermissionTemplate("MY_PROJECT");
90 verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
92 protected boolean matchesSafely(CeTaskSubmit submit) {
93 return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
94 submit.getUuid().equals("TASK_1");
98 public void describeTo(Description description) {