Skip to content

Commit

Permalink
Attachments are not added for root page when REPLACE_ANCESTOR publish…
Browse files Browse the repository at this point in the history
…ing strategy is used
  • Loading branch information
Anastasiia Smirnova committed Feb 27, 2019
1 parent 760e9b0 commit 6eeb5d8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import org.sahli.asciidoc.confluence.publisher.client.metadata.ConfluencePublisherMetadata;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static java.util.Arrays.asList;
import static java.util.UUID.randomUUID;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -66,6 +69,10 @@ public void publish_singlePageAndReplaceAncestorPublishingStrategy_pageIsUpdated
// arrange
String title = uniqueTitle("Single Page");
ConfluencePageMetadata confluencePageMetadata = confluencePageMetadata(title, absolutePathTo("single-page/single-page.xhtml"));
Map<String, String> attachments = new HashMap<>();
attachments.put("attachmentOne.txt", absolutePathTo("attachments/attachmentOne.txt"));
attachments.put("attachmentTwo.txt", absolutePathTo("attachments/attachmentTwo.txt"));
confluencePageMetadata.setAttachments(attachments);
ConfluencePublisherMetadata confluencePublisherMetadata = confluencePublisherMetadata(confluencePageMetadata);

ConfluencePublisher confluencePublisher = confluencePublisher(confluencePublisherMetadata, REPLACE_ANCESTOR);
Expand All @@ -77,6 +84,13 @@ public void publish_singlePageAndReplaceAncestorPublishingStrategy_pageIsUpdated
givenAuthenticatedAsPublisher()
.when().get(rootPage())
.then().body("title", is(title));
givenAuthenticatedAsPublisher()
.when().get(rootPageAttachments())
.then()
.body("results", hasSize(2))
.body("results[0].title", is("attachmentOne.txt"))
.body("results[1].title", is("attachmentTwo.txt"));

}

@Test
Expand Down Expand Up @@ -157,6 +171,10 @@ private static String rootPage() {
return "http://localhost:8090/rest/api/content/" + ANCESTOR_ID;
}

private static String rootPageAttachments() {
return "http://localhost:8090/rest/api/content/" + ANCESTOR_ID + "/child/attachment";
}

private static String pageVersionOf(String contentId) {
return "http://localhost:8090/rest/api/content/" + contentId + "?expand=version";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
attachment1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
attachment2
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ public void publish() {
startPublishingUnderAncestorId(this.metadata.getPages(), this.metadata.getSpaceKey(), this.metadata.getAncestorId());
break;
case REPLACE_ANCESTOR:
ConfluencePageMetadata rootPageMetaData = singleRootPageMetadata(this.metadata);

if (rootPageMetaData != null) {
updatePage(this.metadata.getAncestorId(), null, rootPageMetaData);
startPublishingUnderAncestorId(rootPageMetaData.getChildren(), this.metadata.getSpaceKey(), this.metadata.getAncestorId());
}
startPublishingReplacingAncestorId(this.metadata, this.metadata.getSpaceKey(), this.metadata.getAncestorId());
break;
default:
throw new IllegalArgumentException("Invalid publishing strategy '" + this.publishingStrategy + "'");
Expand All @@ -112,13 +107,27 @@ private static ConfluencePageMetadata singleRootPageMetadata(ConfluencePublisher
return null;
}

private void startPublishingReplacingAncestorId(ConfluencePublisherMetadata metadata, String spaceKey, String ancestorId) {
ConfluencePageMetadata rootPage = singleRootPageMetadata(metadata);

if (rootPage != null) {
updatePage(ancestorId, null, rootPage);

deleteConfluenceAttachmentsNotPresentUnderPage(ancestorId, rootPage.getAttachments());
addAttachments(ancestorId, rootPage.getAttachments());

startPublishingUnderAncestorId(rootPage.getChildren(), spaceKey, ancestorId);
}
}

private void startPublishingUnderAncestorId(List<ConfluencePageMetadata> pages, String spaceKey, String ancestorId) {
deleteConfluencePagesNotPresentUnderAncestor(pages, ancestorId);
pages.forEach(page -> {
String contentId = addOrUpdatePageUnderAncestor(spaceKey, ancestorId, page);

deleteConfluenceAttachmentsNotPresentUnderPage(contentId, page.getAttachments());
addAttachments(contentId, page.getAttachments());

startPublishingUnderAncestorId(page.getChildren(), spaceKey, contentId);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,39 @@ public void publish_metadataWithExistingPageWithSameContentButDifferentTitleAndR
verifyNoMoreInteractions(confluencePublisherListenerMock);
}

@Test
public void publish_metadataWithExistingPageAndNewAttachments_sendsUpdateAndAddAttachmentRequests() {
// arrange
ConfluencePage existingPage = new ConfluencePage("72189173", "Existing Page (Old Title)", "<h1>Some Confluence Content</h1>", 1);

ConfluenceRestClient confluenceRestClientMock = mock(ConfluenceRestClient.class);
when(confluenceRestClientMock.getPageWithContentAndVersionById("72189173")).thenReturn(existingPage);
when(confluenceRestClientMock.getPropertyByKey("72189173", CONTENT_HASH_PROPERTY_KEY)).thenReturn(SOME_CONFLUENCE_CONTENT_SHA256_HASH);
when(confluenceRestClientMock.getAttachmentByFileName("72189173", "attachmentOne.txt"))
.thenThrow(new NotFoundException());
when(confluenceRestClientMock.getAttachmentByFileName("72189173", "attachmentTwo.txt"))
.thenReturn(new ConfluenceAttachment("att2", "attachmentOne.txt", "/download/attachmentOne.txt", 1));
when(confluenceRestClientMock.getAttachmentContent("/download/attachmentOne.txt"))
.thenReturn(new ByteArrayInputStream("Old content".getBytes()));

ConfluencePublisherListener confluencePublisherListenerMock = mock(ConfluencePublisherListener.class);

ConfluencePublisher confluencePublisher = confluencePublisher("root-ancestor-id-page-with-attachments", REPLACE_ANCESTOR, confluenceRestClientMock, confluencePublisherListenerMock, null);

// act
confluencePublisher.publish();

// assert
verify(confluenceRestClientMock, never()).addPageUnderAncestor(any(), any(), any(), any(), any());
verify(confluenceRestClientMock, times(1)).updatePage(eq("72189173"), eq(null), eq("Some Confluence Content"), eq("<h1>Some Confluence Content</h1>"), eq(2), eq(null));
verify(confluenceRestClientMock).addAttachment(eq("72189173"), eq("attachmentOne.txt"), any(InputStream.class));
verify(confluenceRestClientMock).updateAttachmentContent(eq("72189173"), eq("att2"), any(InputStream.class));

verify(confluencePublisherListenerMock, times(1)).pageUpdated(existingPage, new ConfluencePage("72189173", "Some Confluence Content", "<h1>Some Confluence Content</h1>", 2));
verify(confluencePublisherListenerMock, times(1)).publishCompleted();
verifyNoMoreInteractions(confluencePublisherListenerMock);
}

@Test
public void publish_metadataWithExistingPageAndAttachmentWithDifferentAttachmentContentUnderRootSpace_sendsUpdateAttachmentRequest() {
// arrange
Expand Down

0 comments on commit 6eeb5d8

Please sign in to comment.