Skip to content

Commit

Permalink
Add structure and test for issue #6, adding multiple tags (existing).
Browse files Browse the repository at this point in the history
  • Loading branch information
johanmynhardt committed Aug 7, 2017
1 parent 01d2329 commit a527169
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/afrozaar/wordpress/wpapi/v2/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"type",
"slug",
"date",
"date_gmt"
"date_gmt",
"categories",
"tags"
})
public class Post {

Expand Down Expand Up @@ -88,6 +90,9 @@ public class Post {
@JsonProperty("categories")
private List<Long> categoryIds = new ArrayList<>();

@JsonProperty("tags")
private List<Long> tagIds = new ArrayList<>();

public List<Long> getCategoryIds() {
return categoryIds;
}
Expand Down Expand Up @@ -316,4 +321,13 @@ public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

@JsonProperty("tags")
public List<Long> getTagIds() {
return tagIds;
}

@JsonProperty("tags")
public void setTagIds(List<Long> tagIds) {
this.tagIds = tagIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author johan
Expand All @@ -36,6 +34,7 @@ public class PostBuilder {
private String type;
private String pingStatus;
private List<Long> categoryIds;
private List<Long> tagIds;

private PostBuilder() {
}
Expand Down Expand Up @@ -134,10 +133,19 @@ public PostBuilder withCategories(List<Long> categoryIds) {
return this;
}

public PostBuilder withTags(List<Long> tagIds) {
this.tagIds = tagIds;
return this;
}

public PostBuilder withCategories(Term... terms) {
return withCategories(Arrays.stream(terms).map(Term::getId).collect(toList()));
}

public PostBuilder withTags(Term... tags) {
return withTags(Arrays.stream(tags).map(Term::getId).collect(toList()));
}

public PostBuilder but() {
return aPost()
.withAuthor(author)
Expand All @@ -157,7 +165,8 @@ public PostBuilder but() {
.withModifiedGmt(modifiedGmt)
.withType(type)
.withPingStatus(pingStatus)
.withCategories(categoryIds);
.withCategories(categoryIds)
.withTags(tagIds);
}

public Post build() {
Expand All @@ -180,6 +189,7 @@ public Post build() {
post.setType(type);
post.setPingStatus(pingStatus);
post.setCategoryIds(categoryIds);
post.setTagIds(tagIds);
return post;
}

Expand Down
33 changes: 30 additions & 3 deletions src/test/java/com/afrozaar/wordpress/wpapi/v2/ClientLiveIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.afrozaar.wordpress.wpapi.v2.model.Taxonomy;
import com.afrozaar.wordpress.wpapi.v2.model.Term;
import com.afrozaar.wordpress.wpapi.v2.model.User;
import com.afrozaar.wordpress.wpapi.v2.model.builder.PostBuilder;
import com.afrozaar.wordpress.wpapi.v2.model.builder.UserBuilder;
import com.afrozaar.wordpress.wpapi.v2.request.Request;
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
Expand Down Expand Up @@ -626,6 +627,27 @@ public void testDeletePostTagTerm() throws WpApiParsedException {
client.getPostTag(post, postTerm);
}

@Test
public void testCreatePostWithTags() throws WpApiParsedException {

final Post aPost = newTestPostBuilderWithRandomData()
.withTags(
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build()),
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build()),
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build()),
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build())
)
.build();

final Post post = client.createPost(aPost, PostStatus.publish);

final List<Long> tagIds = post.getTagIds();

LOG.debug("Tags: {}", tagIds);

assertThat(tagIds).isNotEmpty();
}

@Test
public void testGetPostTagsPaged() throws WpApiParsedException {
final Post post = client.createPost(newTestPostWithRandomData(), PostStatus.publish);
Expand Down Expand Up @@ -843,12 +865,17 @@ private Page newPageWithRandomData() {
}

private Post newTestPostWithRandomData() {
return aPost().withContent(aContent().withRendered(randomAlphabetic(20)).build())
.withTitle(aTitle().withRendered(randomAlphabetic(5)).build())
.withExcerpt(anExcerpt().withRendered(randomAlphabetic(5)).build())
return newTestPostBuilderWithRandomData()
.build();
}

private PostBuilder newTestPostBuilderWithRandomData() {
return aPost()
.withContent(aContent().withRendered(randomAlphabetic(20)).build())
.withTitle(aTitle().withRendered(randomAlphabetic(5)).build())
.withExcerpt(anExcerpt().withRendered(randomAlphabetic(5)).build());
}

private Tuple2<Post, PostMeta> newTestPostWithRandomDataWithMeta() throws PostCreateException {
final Post post = client.createPost(newTestPostWithRandomData(), PostStatus.publish);
final PostMeta meta = client.createMeta(post.getId(), randomAlphabetic(5), randomAlphabetic(10));
Expand Down

0 comments on commit a527169

Please sign in to comment.