Skip to content

Commit e49cac1

Browse files
committed
Update handling of empty response from latest-package-revision and latest-package-revision-since. Empty map is an acceptable response from latest-package-revision-since. Adresses issue akin to 2641
1 parent b4245e3 commit e49cac1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

plugin-infra/go-plugin-access/src/com/thoughtworks/go/plugin/access/packagematerial/JsonMessageHandler1_0.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ public String requestMessageForLatestRevision(PackageConfiguration packageConfig
159159

160160
@Override
161161
public PackageRevision responseMessageForLatestRevision(String responseBody) {
162-
return toPackageRevision(responseBody);
162+
PackageRevision packageRevision = toPackageRevision(responseBody);
163+
if( packageRevision == null) {
164+
throw new RuntimeException("Empty response body");
165+
} else return packageRevision;
163166
}
164167

165168
@Override
@@ -173,7 +176,6 @@ public String requestMessageForLatestRevisionSince(PackageConfiguration packageC
173176

174177
@Override
175178
public PackageRevision responseMessageForLatestRevisionSince(String responseBody) {
176-
if (isEmpty(responseBody)) return null;
177179
return toPackageRevision(responseBody);
178180
}
179181

@@ -270,9 +272,10 @@ PackageRevision toPackageRevision(String responseBody) {
270272
throw new RuntimeException("Package revision should be returned as a map");
271273
}
272274
if (map == null || map.isEmpty()) {
273-
throw new RuntimeException("Empty response body");
275+
return null;
274276
}
275277

278+
276279
String revision;
277280
try {
278281
revision = (String) map.get("revision");

plugin-infra/go-plugin-access/test/com/thoughtworks/go/plugin/access/packagematerial/JsonMessageHandler1_0Test.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ public void shouldBuildPackageRevisionFromLatestRevisionResponse() throws Except
182182
assertPackageRevision(packageRevision, "abc.rpm", "some-user", "2011-07-14T19:43:37.100Z", "comment", "http:\\localhost:9999");
183183
}
184184

185+
@Test
186+
public void shouldThrowExceptionWhenAttemptingToGetLatestRevisionFromEmptyResponse(){
187+
assertThat(getErrorMessageFromLatestRevision(""), is("Empty response body"));
188+
assertThat(getErrorMessageFromLatestRevision("{}"), is("Empty response body"));
189+
assertThat(getErrorMessageFromLatestRevision(null), is("Empty response body"));
190+
}
191+
185192
@Test
186193
public void shouldBuildRequestBodyForLatestRevisionSinceRequest() throws Exception {
187194
Date timestamp = new SimpleDateFormat(DATE_FORMAT).parse("2011-07-13T19:43:37.100Z");
@@ -208,6 +215,7 @@ public void shouldBuildPackageRevisionFromLatestRevisionSinceResponse() throws E
208215
public void shouldBuildNullPackageRevisionFromLatestRevisionSinceWhenEmptyResponse() throws Exception {
209216
assertThat(messageHandler.responseMessageForLatestRevisionSince(""), nullValue());
210217
assertThat(messageHandler.responseMessageForLatestRevisionSince(null), nullValue());
218+
assertThat(messageHandler.responseMessageForLatestRevisionSince("{}"), nullValue());
211219
}
212220

213221
@Test
@@ -269,7 +277,6 @@ public void shouldValidateIncorrectJsonResponseForPackageConfiguration() {
269277

270278
@Test
271279
public void shouldValidateIncorrectJsonForPackageRevision() {
272-
assertThat(errorMessageForPackageRevision(""), is("Unable to de-serialize json response. Empty response body"));
273280
assertThat(errorMessageForPackageRevision("[{\"revision\":\"abc.rpm\"}]"), is("Unable to de-serialize json response. Package revision should be returned as a map"));
274281
assertThat(errorMessageForPackageRevision("{\"revision\":{}}"), is("Unable to de-serialize json response. Package revision should be of type string"));
275282
assertThat(errorMessageForPackageRevision("{\"revisionComment\":{}}"), is("Unable to de-serialize json response. Package revision comment should be of type string"));
@@ -343,4 +350,13 @@ private String errorMessageForPackageRevision(String message) {
343350
}
344351
return null;
345352
}
353+
354+
private String getErrorMessageFromLatestRevision(String responseBody) {
355+
try{
356+
messageHandler.responseMessageForLatestRevision(responseBody);
357+
fail("Should throw exception");
358+
} catch( RuntimeException e){
359+
return e.getMessage();
360+
} return null;
361+
}
346362
}

0 commit comments

Comments
 (0)