Skip to content

Commit

Permalink
Merge pull request syndesisio#386 from nicolaferraro/368-reject-if-pr…
Browse files Browse the repository at this point in the history
…esent
  • Loading branch information
pure-bot[bot] authored Nov 21, 2017
2 parents 1e712a6 + 2ce70fd commit f96ba53
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,23 @@ public Extension upload(MultipartFormDataInput dataInput, @QueryParam("updatedId
Extension embeddedExtension = extractExtension(fileLocation);

if (updatedId != null) {
// Update
Extension replacedExtension = getDataManager().fetch(Extension.class, updatedId);
if (!replacedExtension.getExtensionId().equals(embeddedExtension.getExtensionId())) {
throw new IllegalArgumentException("The uploaded extensionId (" + embeddedExtension.getExtensionId() +
") does not match the existing extensionId (" + replacedExtension.getExtensionId() + ")");
}
} else {
// New import
Set<String> ids = getDataManager().fetchIdsByPropertyValue(Extension.class,
"extensionId", embeddedExtension.getExtensionId(),
"status", Extension.Status.Installed.name());

if (!ids.isEmpty()) {
throw new IllegalArgumentException("An extension with the same extensionId (" + embeddedExtension.getExtensionId() +
") is already installed. Please update the existing extension instead of importing a new one");
}

}

Extension extension = new Extension.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ public void testUpdateExtensionFailure() throws IOException {
Void.class, tokenRule.validToken(), HttpStatus.BAD_REQUEST, multipartHeaders());
}

@Test
public void testReimportExtensionFailure() throws IOException {
ResponseEntity<Extension> created = post("/api/v1beta1/extensions", multipartBody(extensionData(1)),
Extension.class, tokenRule.validToken(), HttpStatus.OK, multipartHeaders());

assertThat(created.getBody().getId().isPresent());
String id = created.getBody().getId().get();

// Install it
post("/api/v1beta1/extensions/" + id + "/install", null, Void.class,
tokenRule.validToken(), HttpStatus.NO_CONTENT);

// Using same extension without setting extensionId
post("/api/v1beta1/extensions", multipartBody(extensionData(1)),
Void.class, tokenRule.validToken(), HttpStatus.BAD_REQUEST, multipartHeaders());
}

@Test
public void testValidateExtension() throws IOException {
// Create one extension
Expand All @@ -124,7 +141,7 @@ public void testValidateExtension() throws IOException {
tokenRule.validToken(), HttpStatus.NO_CONTENT);

// Create another extension with same extension-id
ResponseEntity<Extension> created2 = post("/api/v1beta1/extensions", multipartBody(extensionData(1)),
ResponseEntity<Extension> created2 = post("/api/v1beta1/extensions?updatedId=" + id1, multipartBody(extensionData(1)),
Extension.class, tokenRule.validToken(), HttpStatus.OK, multipartHeaders());

assertThat(created2.getBody().getId().isPresent());
Expand Down Expand Up @@ -173,7 +190,7 @@ public void testExtensionActivation() throws IOException {
assertThat(got2.getBody().getStatus()).contains(Extension.Status.Installed);

// Create another extension with same extension-id
ResponseEntity<Extension> createdCopy1 = post("/api/v1beta1/extensions", multipartBody(extensionData(1)),
ResponseEntity<Extension> createdCopy1 = post("/api/v1beta1/extensions?updatedId=" + id1, multipartBody(extensionData(1)),
Extension.class, tokenRule.validToken(), HttpStatus.OK, multipartHeaders());

assertThat(createdCopy1.getBody().getId().isPresent());
Expand Down

0 comments on commit f96ba53

Please sign in to comment.