]> source.dussan.org Git - sonarqube.git/blob
14a43069951445fafdc8bef0a386219bc379a37a
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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.queue.report;
21
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;
38
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;
45
46 public class ReportSubmitterTest {
47
48   @Rule
49   public UserSessionRule userSession = UserSessionRule.standalone();
50
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);
56
57   @Test
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"));
62
63     underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
64
65     verifyZeroInteractions(permissionService);
66     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
67       @Override
68       protected boolean matchesSafely(CeTaskSubmit submit) {
69         return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
70           submit.getUuid().equals("TASK_1");
71       }
72
73       @Override
74       public void describeTo(Description description) {
75
76       }
77     }));
78   }
79
80   @Test
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"));
86
87     underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
88
89     verify(permissionService).applyDefaultPermissionTemplate("MY_PROJECT");
90     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
91       @Override
92       protected boolean matchesSafely(CeTaskSubmit submit) {
93         return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals("P1") &&
94           submit.getUuid().equals("TASK_1");
95       }
96
97       @Override
98       public void describeTo(Description description) {
99
100       }
101     }));
102
103   }
104 }