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.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.queue.CeQueue;
33 import org.sonar.server.computation.queue.CeQueueImpl;
34 import org.sonar.server.computation.queue.CeTaskSubmit;
35 import org.sonar.server.permission.PermissionService;
36 import org.sonar.server.tester.UserSessionRule;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Matchers.argThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.verifyZeroInteractions;
43 import static org.mockito.Mockito.when;
45 public class ReportSubmitterTest {
48 public UserSessionRule userSession = UserSessionRule.standalone();
50 CeQueue queue = mock(CeQueueImpl.class);
51 ReportFiles reportFiles = mock(ReportFiles.class);
52 ComponentService componentService = mock(ComponentService.class);
53 PermissionService permissionService = mock(PermissionService.class);
54 ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService, permissionService);
57 public void submit_a_report_on_existing_project() {
58 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder("TASK_1"));
59 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
60 when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(new ComponentDto().setUuid("P1"));
62 underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
64 verifyZeroInteractions(permissionService);
65 verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
67 protected boolean matchesSafely(CeTaskSubmit submit) {
68 return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
69 submit.getUuid().equals("TASK_1");
73 public void describeTo(Description description) {
80 public void provision_project_if_does_not_exist() throws Exception {
81 when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder("TASK_1"));
82 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
83 when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(null);
84 when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1").setKey("MY_PROJECT"));
86 underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
88 verify(permissionService).applyDefaultPermissionTemplate("MY_PROJECT");
89 verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
91 protected boolean matchesSafely(CeTaskSubmit submit) {
92 return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
93 submit.getUuid().equals("TASK_1");
97 public void describeTo(Description description) {