1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* Copyright (C) 2009-2014 SonarSource SA
* All rights reserved
* mailto:contact AT sonarsource DOT com
*/
package batch.suite;
import static org.assertj.core.api.Assertions.assertThat;
import util.ItUtils;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SonarRunner;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.sonar.wsclient.SonarClient;
import org.sonar.wsclient.project.NewProject;
import org.sonar.wsclient.services.PropertyUpdateQuery;
public class ProjectProvisioningTest {
@ClassRule
public static Orchestrator orchestrator = BatchTestSuite.ORCHESTRATOR;
@BeforeClass
public static void init() {
orchestrator.resetData();
orchestrator.executeBuild(
SonarRunner.create(ItUtils.projectDir("shared/xoo-sample"))
);
}
@AfterClass
public static void resetAutoProjectCreation() {
setProperty("sonar.preventAutoProjectCreation", "false");
}
private SonarClient client;
@Before
public void initClient() {
client = orchestrator.getServer().adminWsClient();
}
/**
* SONAR-3871
* SONAR-4713
*/
@Test
public void should_allow_existing_project_scan() {
setProperty("sonar.preventAutoProjectCreation", "true");
// xoo-sample already exists => pass
checkBuildSuccess("shared/xoo-sample");
}
/**
* SONAR-3871
* SONAR-4713
*/
@Test
@Ignore("This test should be moved to a Medium test of the Compute Engine")
public void should_prevent_project_creation() {
setProperty("sonar.preventAutoProjectCreation", "true");
// xoo-sample-with-tests does not exist => fail
checkBuildFailed("shared/xoo-sample-with-tests");
// provision xoo-sample-with-tests and retry
client.projectClient().create(
NewProject.create()
.key("sample-with-tests")
.name("Sample With Tests"));
checkBuildSuccess("shared/xoo-sample-with-tests");
}
/**
* SONAR-3871
* SONAR-4713
*/
@Test
public void should_allow_provisioned_project() {
setProperty("sonar.preventAutoProjectCreation", "true");
// provision xoo-multi-modules-sample before 1st scan and check build OK
client.projectClient().create(
NewProject.create()
.key("com.sonarsource.it.samples:multi-modules-sample")
.name("Xoo Multi Modules Sample"));
checkBuildSuccess("shared/xoo-multi-modules-sample");
}
/**
* SONAR-5547
*/
@Test
public void should_allow_provisioned_project_with_branch() {
setProperty("sonar.preventAutoProjectCreation", "true");
// provision xoo-multi-modules-sample before 1st scan and check build OK
client.projectClient().create(
NewProject.create()
.key("com.sonarsource.it.samples:multi-modules-sample:branch")
.name("Xoo Multi Modules Sample - Branch"));
checkBuildSuccess("shared/xoo-multi-modules-sample", "sonar.branch", "branch");
}
/**
* SONAR-3871
* SONAR-4713
*/
@Test
public void should_allow_provisioned_project_even_when_provisioning_not_enforced() {
setProperty("sonar.preventAutoProjectCreation", "false");
client.projectClient().create(
NewProject.create()
.key("xo")
.name("xo"));
checkBuildSuccess("shared/xoo-two-letters-named");
}
private static BuildResult checkBuildSuccess(String projectPath, String... propertiesKeyValues) {
BuildResult result = scan(projectPath, propertiesKeyValues);
assertThat(result.getStatus()).isZero();
return result;
}
private static BuildResult checkBuildFailed(String projectPath) {
BuildResult result = scan(projectPath);
assertThat(result.getStatus()).isNotEqualTo(0);
return result;
}
private static BuildResult scan(String projectPath, String... propertiesKeyValues) {
return orchestrator.executeBuildQuietly(
SonarRunner.create(ItUtils.projectDir(projectPath)).setProperties(propertiesKeyValues));
}
private static void setProperty(String key, String value) {
orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery(key, value));
}
}
|