Skip to content

Commit a527169

Browse files
committed
Add structure and test for issue #6, adding multiple tags (existing).
1 parent 01d2329 commit a527169

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

src/main/java/com/afrozaar/wordpress/wpapi/v2/model/Post.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
"type",
3838
"slug",
3939
"date",
40-
"date_gmt"
40+
"date_gmt",
41+
"categories",
42+
"tags"
4143
})
4244
public class Post {
4345

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

93+
@JsonProperty("tags")
94+
private List<Long> tagIds = new ArrayList<>();
95+
9196
public List<Long> getCategoryIds() {
9297
return categoryIds;
9398
}
@@ -316,4 +321,13 @@ public void setAdditionalProperty(String name, Object value) {
316321
this.additionalProperties.put(name, value);
317322
}
318323

324+
@JsonProperty("tags")
325+
public List<Long> getTagIds() {
326+
return tagIds;
327+
}
328+
329+
@JsonProperty("tags")
330+
public void setTagIds(List<Long> tagIds) {
331+
this.tagIds = tagIds;
332+
}
319333
}

src/main/java/com/afrozaar/wordpress/wpapi/v2/model/builder/PostBuilder.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
import java.util.Arrays;
1313
import java.util.List;
14-
import java.util.stream.Collectors;
15-
import java.util.stream.Stream;
1614

1715
/**
1816
* @author johan
@@ -36,6 +34,7 @@ public class PostBuilder {
3634
private String type;
3735
private String pingStatus;
3836
private List<Long> categoryIds;
37+
private List<Long> tagIds;
3938

4039
private PostBuilder() {
4140
}
@@ -134,10 +133,19 @@ public PostBuilder withCategories(List<Long> categoryIds) {
134133
return this;
135134
}
136135

136+
public PostBuilder withTags(List<Long> tagIds) {
137+
this.tagIds = tagIds;
138+
return this;
139+
}
140+
137141
public PostBuilder withCategories(Term... terms) {
138142
return withCategories(Arrays.stream(terms).map(Term::getId).collect(toList()));
139143
}
140144

145+
public PostBuilder withTags(Term... tags) {
146+
return withTags(Arrays.stream(tags).map(Term::getId).collect(toList()));
147+
}
148+
141149
public PostBuilder but() {
142150
return aPost()
143151
.withAuthor(author)
@@ -157,7 +165,8 @@ public PostBuilder but() {
157165
.withModifiedGmt(modifiedGmt)
158166
.withType(type)
159167
.withPingStatus(pingStatus)
160-
.withCategories(categoryIds);
168+
.withCategories(categoryIds)
169+
.withTags(tagIds);
161170
}
162171

163172
public Post build() {
@@ -180,6 +189,7 @@ public Post build() {
180189
post.setType(type);
181190
post.setPingStatus(pingStatus);
182191
post.setCategoryIds(categoryIds);
192+
post.setTagIds(tagIds);
183193
return post;
184194
}
185195

src/test/java/com/afrozaar/wordpress/wpapi/v2/ClientLiveIT.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.afrozaar.wordpress.wpapi.v2.model.Taxonomy;
3636
import com.afrozaar.wordpress.wpapi.v2.model.Term;
3737
import com.afrozaar.wordpress.wpapi.v2.model.User;
38+
import com.afrozaar.wordpress.wpapi.v2.model.builder.PostBuilder;
3839
import com.afrozaar.wordpress.wpapi.v2.model.builder.UserBuilder;
3940
import com.afrozaar.wordpress.wpapi.v2.request.Request;
4041
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
@@ -626,6 +627,27 @@ public void testDeletePostTagTerm() throws WpApiParsedException {
626627
client.getPostTag(post, postTerm);
627628
}
628629

630+
@Test
631+
public void testCreatePostWithTags() throws WpApiParsedException {
632+
633+
final Post aPost = newTestPostBuilderWithRandomData()
634+
.withTags(
635+
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build()),
636+
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build()),
637+
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build()),
638+
client.createTag(aTerm().withName("tag-" + randomAlphabetic(5)).build())
639+
)
640+
.build();
641+
642+
final Post post = client.createPost(aPost, PostStatus.publish);
643+
644+
final List<Long> tagIds = post.getTagIds();
645+
646+
LOG.debug("Tags: {}", tagIds);
647+
648+
assertThat(tagIds).isNotEmpty();
649+
}
650+
629651
@Test
630652
public void testGetPostTagsPaged() throws WpApiParsedException {
631653
final Post post = client.createPost(newTestPostWithRandomData(), PostStatus.publish);
@@ -843,12 +865,17 @@ private Page newPageWithRandomData() {
843865
}
844866

845867
private Post newTestPostWithRandomData() {
846-
return aPost().withContent(aContent().withRendered(randomAlphabetic(20)).build())
847-
.withTitle(aTitle().withRendered(randomAlphabetic(5)).build())
848-
.withExcerpt(anExcerpt().withRendered(randomAlphabetic(5)).build())
868+
return newTestPostBuilderWithRandomData()
849869
.build();
850870
}
851871

872+
private PostBuilder newTestPostBuilderWithRandomData() {
873+
return aPost()
874+
.withContent(aContent().withRendered(randomAlphabetic(20)).build())
875+
.withTitle(aTitle().withRendered(randomAlphabetic(5)).build())
876+
.withExcerpt(anExcerpt().withRendered(randomAlphabetic(5)).build());
877+
}
878+
852879
private Tuple2<Post, PostMeta> newTestPostWithRandomDataWithMeta() throws PostCreateException {
853880
final Post post = client.createPost(newTestPostWithRandomData(), PostStatus.publish);
854881
final PostMeta meta = client.createMeta(post.getId(), randomAlphabetic(5), randomAlphabetic(10));

0 commit comments

Comments
 (0)