]> source.dussan.org Git - sonarqube.git/blob
5007d2d87c9cfd3afdba0ea689a36e6a2b83c94f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info 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.task.projectanalysis.step;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.mockito.ArgumentCaptor;
26 import org.sonar.api.utils.MessageException;
27 import org.sonar.db.organization.OrganizationDto;
28 import org.sonar.server.computation.task.projectanalysis.analysis.MutableAnalysisMetadataHolderRule;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Organization;
30 import org.sonar.server.organization.BillingValidations;
31 import org.sonar.server.organization.BillingValidations.BillingValidationsException;
32 import org.sonar.server.organization.BillingValidationsProxy;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.Matchers.any;
36 import static org.mockito.Mockito.doThrow;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
40
41 public class VerifyBillingStepTest {
42
43   @Rule
44   public ExpectedException expectedException = ExpectedException.none();
45
46   private OrganizationDto organization = newOrganizationDto();
47   @Rule
48   public MutableAnalysisMetadataHolderRule analysisMetadata = new MutableAnalysisMetadataHolderRule()
49     .setOrganization(Organization.from(organization));
50
51   private BillingValidationsProxy validations = mock(BillingValidationsProxy.class);
52
53   @Test
54   public void execute_fails_with_MessageException_when_organization_is_not_allowed_to_execute_analysis() {
55     doThrow(new BillingValidationsException("This organization cannot execute project analysis"))
56       .when(validations)
57       .checkOnProjectAnalysis(any(BillingValidations.Organization.class));
58
59     VerifyBillingStep underTest = new VerifyBillingStep(analysisMetadata, validations);
60
61     expectedException.expect(MessageException.class);
62     expectedException.expectMessage("This organization cannot execute project analysis");
63
64     underTest.execute();
65
66   }
67
68   @Test
69   public void execute_does_no_fail_when_organization_is_allowed_to_execute_analysis() {
70     ArgumentCaptor<BillingValidations.Organization> orgCaptor = ArgumentCaptor.forClass(BillingValidations.Organization.class);
71
72     VerifyBillingStep underTest = new VerifyBillingStep(analysisMetadata, validations);
73     underTest.execute();
74
75     verify(validations).checkOnProjectAnalysis(orgCaptor.capture());
76     BillingValidations.Organization calledOrg = orgCaptor.getValue();
77     assertThat(calledOrg.getKey()).isEqualTo(organization.getKey());
78   }
79
80 }