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.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.junit.rules.ExpectedException;
28 import org.sonar.core.permission.GlobalPermissions;
29 import org.sonar.db.ce.CeTaskTypes;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.server.component.ComponentService;
32 import org.sonar.server.component.NewComponent;
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.exceptions.ForbiddenException;
37 import org.sonar.server.permission.PermissionService;
38 import org.sonar.server.tester.UserSessionRule;
40 import static org.mockito.Matchers.any;
41 import static org.mockito.Matchers.argThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.verifyZeroInteractions;
45 import static org.mockito.Mockito.when;
47 public class ReportSubmitterTest {
49 static final String PROJECT_KEY = "MY_PROJECT";
50 static final String PROJECT_UUID = "P1";
51 static final String PROJECT_NAME = "My Project";
52 static final String TASK_UUID = "TASK_1";
55 public ExpectedException thrown = ExpectedException.none();
58 public UserSessionRule userSession = UserSessionRule.standalone();
60 CeQueue queue = mock(CeQueueImpl.class);
61 ReportFiles reportFiles = mock(ReportFiles.class);
62 ComponentService componentService = mock(ComponentService.class);
63 PermissionService permissionService = mock(PermissionService.class);
64 ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService, permissionService);
67 public void submit_a_report_on_existing_project() {
68 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
70 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
71 when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
73 underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
75 verifyZeroInteractions(permissionService);
76 verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
78 protected boolean matchesSafely(CeTaskSubmit submit) {
79 return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(PROJECT_UUID) &&
80 submit.getUuid().equals(TASK_UUID);
84 public void describeTo(Description description) {
91 public void provision_project_if_does_not_exist() throws Exception {
92 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
94 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
95 when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
96 when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
98 underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
100 verify(permissionService).applyDefaultPermissionTemplate(PROJECT_KEY);
101 verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
103 protected boolean matchesSafely(CeTaskSubmit submit) {
104 return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(PROJECT_UUID) &&
105 submit.getUuid().equals(TASK_UUID);
109 public void describeTo(Description description) {
116 public void submit_a_report_on_new_project_with_global_scan_permission() {
117 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
119 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
120 when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
121 when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
123 underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
125 verify(queue).submit(any(CeTaskSubmit.class));
129 public void submit_a_report_on_existing_project_with_global_scan_permission() {
130 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
132 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
133 when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
135 underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
137 verify(queue).submit(any(CeTaskSubmit.class));
141 public void submit_a_report_on_existing_project_with_project_scan_permission() {
142 userSession.addProjectPermissions(GlobalPermissions.SCAN_EXECUTION, PROJECT_KEY);
144 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
145 when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
147 underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
149 verify(queue).submit(any(CeTaskSubmit.class));
153 public void fail_with_forbidden_exception_when_no_scan_permission() {
154 userSession.setGlobalPermissions(GlobalPermissions.DASHBOARD_SHARING);
156 thrown.expect(ForbiddenException.class);
157 underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
161 public void fail_with_forbidden_exception_on_new_project_when_only_project_scan_permission() {
162 userSession.addProjectPermissions(GlobalPermissions.SCAN_EXECUTION, PROJECT_KEY);
164 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
165 when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
166 when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
168 thrown.expect(ForbiddenException.class);
169 underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));