Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit 843a1d6

Browse files
committed
Adding missing consumes annotation on REST resources #117
* Adding integration test for REST API.
1 parent b8600f3 commit 843a1d6

File tree

10 files changed

+264
-6
lines changed

10 files changed

+264
-6
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ install:
1818
script:
1919
- atlas-package -q -Dstash.version=4.0.0
2020
- atlas-package -q
21+
# - ./integration-test.sh
2122
notifications:
2223
email: false

CHANGELOG.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
Changelog of Pull Request Notifier for Bitbucket.
44

55
## Unreleased
6+
### GitHub [#117](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/117) PULL_REQUEST_TO_HASH
7+
Adding missing consumes annotation on REST resources
8+
9+
* Adding integration test for REST API.
10+
11+
[e8f7cc71c9a97d3](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/e8f7cc71c9a97d3) Tomas Bjerre *2016-05-14 22:04:19*
12+
613
### No issue
7-
Doc
14+
Avoiding looking for legacy settings if no such keys
15+
16+
* Also adding Curl examples to README.
17+
* Documenting REST API.
18+
19+
Logging legacy settings
820

9-
[bf28a8fad66abda](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/bf28a8fad66abda) Tomas Bjerre *2016-05-12 17:22:42*
21+
[b8600f3a7d972c4](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/b8600f3a7d972c4) Tomas Bjerre *2016-05-14 15:35:56*
1022

1123
Reusing Podam factory, to use caching
1224

integration-test-local.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
#
3+
# Assuming you have the plugin running locally. You can start it with atlas-run.
4+
#
5+
atlas-mvn test -P integration-test

integration-test.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
#
4+
# Cleanup
5+
#
6+
7+
function build_clean {
8+
for KILLPID in `ps ax | grep 'Dbitbucket' | awk ' { print $1;}'`; do
9+
echo "Bitbucket Server is running at $KILLPID, killing it"
10+
kill $KILLPID || echo;
11+
done
12+
atlas-mvn clean
13+
}
14+
15+
function on_exit {
16+
build_clean
17+
}
18+
trap on_exit EXI
19+
build_clean
20+
21+
#
22+
# Start Bitbucket Server
23+
#
24+
atlas-run -q || exit 1 &
25+
26+
BITBUCKET_URL=http://localhost:7990/bitbucket
27+
until $(curl --output /dev/null --silent --head --fail $BITBUCKET_URL); do
28+
printf '.'
29+
sleep 1
30+
done
31+
echo
32+
echo
33+
echo Bitbucket Server started at $BITBUCKET_URL
34+
echo
35+
echo
36+
37+
#
38+
# Run tests
39+
#
40+
./integration-test-local.sh

pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@
130130
<version>6.0.2.RELEASE</version>
131131
<scope>test</scope>
132132
</dependency>
133+
<dependency>
134+
<groupId>com.jayway.restassured</groupId>
135+
<artifactId>rest-assured</artifactId>
136+
<scope>test</scope>
137+
</dependency>
138+
<dependency>
139+
<groupId>org.codehaus.groovy</groupId>
140+
<artifactId>groovy-all</artifactId>
141+
<scope>test</scope>
142+
</dependency>
133143
</dependencies>
134144
<build>
135145
<plugins>
@@ -213,6 +223,11 @@ Changelog of Pull Request Notifier for Bitbucket.
213223
<groupId>org.apache.maven.plugins</groupId>
214224
<artifactId>maven-surefire-plugin</artifactId>
215225
<version>2.17</version>
226+
<configuration>
227+
<includes>
228+
<include>**/*Test.java</include>
229+
</includes>
230+
</configuration>
216231
</plugin>
217232
</plugins>
218233
<resources>
@@ -222,6 +237,26 @@ Changelog of Pull Request Notifier for Bitbucket.
222237
</resource>
223238
</resources>
224239
</build>
240+
241+
<profiles>
242+
<profile>
243+
<id>integration-test</id>
244+
<build>
245+
<plugins>
246+
<plugin>
247+
<groupId>org.apache.maven.plugins</groupId>
248+
<artifactId>maven-surefire-plugin</artifactId>
249+
<configuration>
250+
<includes>
251+
<include>**/*TestIntegration.java</include>
252+
</includes>
253+
</configuration>
254+
</plugin>
255+
</plugins>
256+
</build>
257+
</profile>
258+
</profiles>
259+
225260
<properties>
226261
<bitbucket.version>4.4.0</bitbucket.version>
227262
<bitbucket.data.version>${bitbucket.version}</bitbucket.data.version>

src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.UUID;
1414

15+
import javax.ws.rs.Consumes;
1516
import javax.ws.rs.DELETE;
1617
import javax.ws.rs.GET;
1718
import javax.ws.rs.POST;
@@ -42,6 +43,7 @@ public ButtonServlet(ButtonsService buttonsService, SettingsService settingsServ
4243

4344
@POST
4445
@XsrfProtectionExcluded
46+
@Consumes(APPLICATION_JSON)
4547
@Produces(APPLICATION_JSON)
4648
public Response create(ButtonDTO buttonDto) {
4749
if (!this.userCheckService.isAdminAllowed()) {

src/main/java/se/bjurr/prnfb/presentation/NotificationServlet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414
import java.util.UUID;
1515

16+
import javax.ws.rs.Consumes;
1617
import javax.ws.rs.DELETE;
1718
import javax.ws.rs.GET;
1819
import javax.ws.rs.POST;
@@ -40,6 +41,7 @@ public NotificationServlet(SettingsService settingsService, UserCheckService use
4041

4142
@POST
4243
@XsrfProtectionExcluded
44+
@Consumes(APPLICATION_JSON)
4345
@Produces(APPLICATION_JSON)
4446
public Response create(NotificationDTO notificationDto) {
4547
if (!this.userCheckService.isAdminAllowed()) {

src/main/java/se/bjurr/prnfb/presentation/SettingsDataServlet.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package se.bjurr.prnfb.presentation;
22

33
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
4-
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
54
import static javax.ws.rs.core.Response.noContent;
65
import static javax.ws.rs.core.Response.ok;
76
import static javax.ws.rs.core.Response.status;
@@ -49,7 +48,7 @@ public Response get() {
4948
@POST
5049
@XsrfProtectionExcluded
5150
@Consumes(APPLICATION_JSON)
52-
@Produces(TEXT_PLAIN)
51+
@Produces(APPLICATION_JSON)
5352
public Response post(SettingsDataDTO settingsDataDto) {
5453
if (!this.userCheckService.isAdminAllowed()) {
5554
return status(UNAUTHORIZED).build();

src/main/resources/admin.vm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@
9999
</legend>
100100
<div class="radio">
101101
<label>
102-
<input class="radio" type="radio" name="adminRestriction" value="ADMIN"> System administrators</label>
102+
<input class="radio" type="radio" name="adminRestriction" value="SYSTEM_ADMIN"> System administrators</label>
103103
</div>
104104
<div class="radio">
105105
<label>
106-
<input class="radio" type="radio" name="adminRestriction" value="SYSTEM_ADMIN"> Administrators and system administrators</label>
106+
<input class="radio" type="radio" name="adminRestriction" value="ADMIN"> Administrators and system administrators</label>
107107
</div>
108108
<div class="radio">
109109
<label>
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package se.bjurr.prnfb.test.integration;
2+
3+
import static com.google.common.collect.Lists.newArrayList;
4+
import static com.jayway.restassured.RestAssured.basic;
5+
import static com.jayway.restassured.RestAssured.get;
6+
import static com.jayway.restassured.RestAssured.given;
7+
import static com.jayway.restassured.RestAssured.when;
8+
import static com.jayway.restassured.config.LogConfig.logConfig;
9+
import static com.jayway.restassured.http.ContentType.JSON;
10+
import static org.assertj.core.api.Assertions.fail;
11+
import static org.hamcrest.Matchers.equalTo;
12+
import static se.bjurr.prnfb.listener.PrnfbPullRequestAction.COMMENTED;
13+
import static se.bjurr.prnfb.settings.USER_LEVEL.ADMIN;
14+
import static se.bjurr.prnfb.settings.USER_LEVEL.EVERYONE;
15+
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
19+
import se.bjurr.prnfb.presentation.dto.ButtonDTO;
20+
import se.bjurr.prnfb.presentation.dto.NotificationDTO;
21+
import se.bjurr.prnfb.presentation.dto.SettingsDataDTO;
22+
23+
import com.jayway.restassured.RestAssured;
24+
import com.jayway.restassured.builder.RequestSpecBuilder;
25+
import com.jayway.restassured.builder.ResponseSpecBuilder;
26+
import com.jayway.restassured.filter.log.LogDetail;
27+
import com.jayway.restassured.response.Response;
28+
29+
public class PrnfbTestIntegration {
30+
@Before
31+
public void before() throws InterruptedException {
32+
RestAssured.baseURI = "http://localhost:7990/bitbucket/rest/prnfb-admin/1.0";
33+
RestAssured.authentication = basic("admin", "admin");
34+
RestAssured.requestSpecification = new RequestSpecBuilder()//
35+
.log(LogDetail.ALL).build()//
36+
.auth().preemptive().basic("admin", "admin")//
37+
.accept(JSON)//
38+
.contentType(JSON);
39+
RestAssured.config().logConfig(
40+
logConfig().enableLoggingOfRequestAndResponseIfValidationFails().enablePrettyPrinting(true));
41+
RestAssured.responseSpecification = new ResponseSpecBuilder()//
42+
.build();
43+
44+
boolean startedOk = false;
45+
int waitCount = 0;
46+
while (!startedOk) {
47+
try {
48+
waitCount++;
49+
if (waitCount > 90) {
50+
fail("Giving up waiting for server to start!");
51+
}
52+
Response response = get("http://localhost:7990/bitbucket/").andReturn();
53+
if (response.getStatusCode() == 200) {
54+
startedOk = true;
55+
}
56+
} catch (Exception e) {
57+
System.out.println("Waiting for Bitbucket to start");
58+
Thread.sleep(1000);
59+
}
60+
}
61+
}
62+
63+
@Test
64+
public void testThatButtonsCanBeStored() {
65+
ButtonDTO buttonDto = new ButtonDTO();
66+
buttonDto.setName("name");
67+
buttonDto.setProjectKey("projectKey");
68+
buttonDto.setRepositorySlug("repositorySlug");
69+
buttonDto.setUserLevel(ADMIN);
70+
71+
String uuid = given()//
72+
.body(buttonDto)//
73+
.when()//
74+
.post("/settings/buttons")//
75+
.then()//
76+
.log().all()//
77+
.extract().body().path("uuid");
78+
79+
when()//
80+
.get("/settings/buttons/" + uuid)//
81+
.then()//
82+
.log().all()//
83+
.body("name", equalTo("name"))//
84+
.body("projectKey", equalTo("projectKey"))//
85+
.body("repositorySlug", equalTo("repositorySlug"))//
86+
.body("userLevel", equalTo(ADMIN.name()));
87+
}
88+
89+
@Test
90+
public void testThatGlobalSettingsCanBeStored() {
91+
SettingsDataDTO settingsData = new SettingsDataDTO();
92+
settingsData.setAdminRestriction(ADMIN);
93+
settingsData.setShouldAcceptAnyCertificate(false);
94+
settingsData.setKeyStore("keyStore");
95+
settingsData.setKeyStorePassword("keyStorePassword");
96+
settingsData.setKeyStoreType("keyStoreType");
97+
98+
given()//
99+
.body(settingsData)//
100+
.when()//
101+
.post("/settings")//
102+
.then()//
103+
.log().all();
104+
105+
when()//
106+
.get("/settings")//
107+
.then()//
108+
.log().all()//
109+
.body("shouldAcceptAnyCertificate", equalTo(false))//
110+
.body("adminRestriction", equalTo(ADMIN.name()))//
111+
.body("keyStore", equalTo("keyStore"))//
112+
.body("keyStorePassword", equalTo("keyStorePassword"))//
113+
.body("keyStoreType", equalTo("keyStoreType"));
114+
115+
settingsData.setAdminRestriction(EVERYONE);
116+
settingsData.setShouldAcceptAnyCertificate(true);
117+
settingsData.setKeyStore("keyStore2");
118+
settingsData.setKeyStorePassword("keyStorePassword2");
119+
settingsData.setKeyStoreType("keyStoreType2");
120+
121+
given()//
122+
.body(settingsData)//
123+
.when()//
124+
.post("/settings")//
125+
.then()//
126+
.log().all();
127+
128+
when()//
129+
.get("/settings")//
130+
.then()//
131+
.log().all()//
132+
.body("shouldAcceptAnyCertificate", equalTo(true))//
133+
.body("adminRestriction", equalTo(EVERYONE.name()))//
134+
.body("keyStore", equalTo("keyStore2"))//
135+
.body("keyStorePassword", equalTo("keyStorePassword2"))//
136+
.body("keyStoreType", equalTo("keyStoreType2"));
137+
}
138+
139+
@Test
140+
public void testThatNotificationsCanBeStored() {
141+
NotificationDTO notificationDto = new NotificationDTO();
142+
notificationDto.setName("name");
143+
notificationDto.setTriggers(newArrayList(COMMENTED.name()));
144+
notificationDto.setUrl("http://bjurr.se/");
145+
146+
String uuid = given()//
147+
.body(notificationDto)//
148+
.when()//
149+
.post("/settings/notifications")//
150+
.then()//
151+
.log().all()//
152+
.extract().body().path("uuid");
153+
154+
when()//
155+
.get("/settings/notifications/" + uuid)//
156+
.then()//
157+
.log().all()//
158+
.body("name", equalTo("name"))//
159+
.body("triggers[0]", equalTo(COMMENTED.name()))//
160+
.body("url", equalTo("http://bjurr.se/"));
161+
}
162+
}

0 commit comments

Comments
 (0)