Skip to content

Commit

Permalink
Merge pull request #5 from estigma88/handle-multiple-post-types
Browse files Browse the repository at this point in the history
Adding group field to publish posts by group
  • Loading branch information
estigma88 authored Apr 13, 2024
2 parents c665e64 + c0c13fd commit aecd253
Show file tree
Hide file tree
Showing 23 changed files with 154 additions and 172 deletions.
2 changes: 1 addition & 1 deletion aws-publisher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>[1.12,2.0)</version>
<version>1.13.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public PostAWSRepository(PostDynamoRepository postDynamoRepository) {
}

@Override
public Optional<Post> getNextToPublish() {
public Optional<Post> getNextToPublish(String group) {
return StreamSupport.stream(postDynamoRepository.findAll().spliterator(), false)
.filter(postDynamo -> group.equals(postDynamo.getGroup()))
.min(Comparator.comparing(PostDynamo::getPublishedDate))
.map(this::convert);
}
Expand All @@ -35,6 +36,7 @@ private PostDynamo convert(Post post) {
.name(post.getName())
.tags(post.getTags())
.url(post.getUrl())
.group(post.getGroup())
.build();
}

Expand All @@ -45,6 +47,7 @@ private Post convert(PostDynamo postDynamo) {
.publishedDate(postDynamo.getPublishedDate())
.name(postDynamo.getName())
.tags(postDynamo.getTags())
.group(postDynamo.getGroup())
.url(postDynamo.getUrl()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public class PostDynamo {
@DynamoDBAttribute
@DynamoDBTypeConverted(converter = LocalDateTimeConverter.class)
private LocalDateTime publishedDate;
@DynamoDBAttribute
private String group;
}
15 changes: 11 additions & 4 deletions aws-publisher/src/main/resources/application-secure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ social-media-publisher:
principal-names-allowed:
linkedin: xxx

amazon:
dynamodb:
accesskey: xxx
secretkey: xxx

spring:
security:
oauth2:
Expand All @@ -14,12 +19,14 @@ spring:
authorization-grant-type: authorization_code
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
scope:
- r_liteprofile
- r_emailaddress
- openid
- profile
- email
- w_member_social
provider:
linkedin:
authorization-uri: https://www.linkedin.com/oauth/v2/authorization
token-uri: https://www.linkedin.com/oauth/v2/accessToken
user-info-uri: https://api.linkedin.com/v2/me
user-name-attribute: id
user-info-uri: https://api.linkedin.com/v2/userinfo
user-name-attribute: id
jwk-set-uri: https://www.linkedin.com/oauth/openid/jwks
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class MockSocialMediaCredentialsHandlingTests {
void publish_credentialsExpired_unauthorizedException() throws Exception {
mockingTwitter();

mvc.perform(post("/posts/next")
mvc.perform(post("/posts/group1/next")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.LinkedInShare;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.Media;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.Profile;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.ShareContent;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.SpecificContent;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.Text;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.Visibility;
import com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin.*;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -78,7 +72,7 @@ void publish_allSocialMedia_success() throws Exception {
mockingTwitter();
mockingLinkedIn();

mvc.perform(post("/posts/next")
mvc.perform(post("/posts/group1/next")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
Expand All @@ -90,44 +84,35 @@ private void mockingLinkedIn() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.add("X-Restli-Protocol-Version", "2.0.0");
httpHeaders.add("LinkedIn-Version", "202304");
httpHeaders.setBearerAuth("access123");

HttpEntity<Void> requestMe = new HttpEntity<>(httpHeaders);

when(restTemplate.exchange("https://api.linkedin.com/v2/me", HttpMethod.GET, requestMe, Profile.class))
when(restTemplate.exchange("https://api.linkedin.com/v2/userinfo", HttpMethod.GET, requestMe, Profile.class))
.thenReturn(ResponseEntity.ok(Profile.builder()
.id("memberid")
.sub("memberid")
.build()));

LinkedInShare linkedInShare = LinkedInShare.builder()
.author("urn:li:person:memberid")
.lifecycleState("PUBLISHED")
.specificContent(SpecificContent.builder()
.shareContent(ShareContent.builder()
.shareCommentary(Text.builder()
.text("My second post\n\n#tag1 #tag2")
.build())
.shareMediaCategory("ARTICLE")
.media(Media.builder()
.description(Text.builder()
.text("My second post")
.build())
.title(Text.builder()
.text("My Post 2")
.build())
.status("READY")
.originalUrl("https://coderstower.com/2020/01/13/open-close-principle-by-example/")
.build())
.commentary("My second post\n\n#tag1 #tag2")
.distribution(Distribution.builder().feedDistribution("MAIN_FEED").build())
.lifecycleState("PUBLISHED")
.content(Content.builder()
.article(ArticleContent.builder()
.description("My second post")
.title("My Post 2")
.source("https://coderstower.com/2020/01/13/open-close-principle-by-example/")
.build())
.build())
.visibility(Visibility.builder()
.memberNetworkVisibility("PUBLIC")
.build())
.visibility("PUBLIC")
.build();

HttpEntity<LinkedInShare> requestShare = new HttpEntity<>(linkedInShare, httpHeaders);

when(restTemplate.exchange("https://api.linkedin.com/v2/ugcPosts", HttpMethod.POST, requestShare, Void.class))
when(restTemplate.exchange("https://api.linkedin.com/rest/posts", HttpMethod.POST, requestShare, Void.class))
.thenReturn(ResponseEntity.ok()
.header("X-RestLi-Id", "shareid")
.build());
Expand Down Expand Up @@ -183,6 +168,7 @@ public AmazonDynamoDB amazonDynamoDB() {
post1.addItemEntry("tags", new AttributeValue().withL(new AttributeValue("tag1"), new AttributeValue("tag2")));
post1.addItemEntry("url", new AttributeValue("https://coderstower.com/2020/02/18/unit-tests-vs-integration-tests/"));
post1.addItemEntry("publishedDate", new AttributeValue("2013-09-17T18:47:52"));
post1.addItemEntry("group", new AttributeValue("group1"));

ddb.putItem(post1);

Expand All @@ -194,6 +180,7 @@ public AmazonDynamoDB amazonDynamoDB() {
post2.addItemEntry("tags", new AttributeValue().withL(new AttributeValue("tag1"), new AttributeValue("tag2")));
post2.addItemEntry("url", new AttributeValue("https://coderstower.com/2020/01/13/open-close-principle-by-example/"));
post2.addItemEntry("publishedDate", new AttributeValue("2012-09-17T18:47:52"));
post2.addItemEntry("group", new AttributeValue("group1"));

ddb.putItem(post2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ public void getNextToPublish() throws MalformedURLException {
.name("My Post 2")
.description("My second post")
.tags(List.of("tag1", "tag2"))
.group("group1")
.url(URI.create("https://coderstower.com/2020/01/13/open-close-principle-by-example/").toURL())
.publishedDate(LocalDateTime.parse("2012-09-17T18:47:52"))
.build(), PostDynamo.builder()
.id("1")
.name("My Post 1")
.description("My first post")
.tags(List.of("tag1", "tag2"))
.group("group1")
.url(URI.create("https://coderstower.com/2020/02/18/unit-tests-vs-integration-tests/").toURL())
.publishedDate(LocalDateTime.parse("2013-09-17T18:47:52"))
.build()));

Optional<Post> post = postAWSRepository.getNextToPublish();
Optional<Post> post = postAWSRepository.getNextToPublish("group1");

assertThat(post).isEqualTo(Optional.of(Post.builder()
.id("2")
Expand All @@ -50,6 +52,7 @@ public void getNextToPublish() throws MalformedURLException {
.tags(List.of("tag1", "tag2"))
.url(URI.create("https://coderstower.com/2020/01/13/open-close-principle-by-example/").toURL())
.publishedDate(LocalDateTime.parse("2012-09-17T18:47:52"))
.group("group1")
.build()));
}

Expand Down
2 changes: 1 addition & 1 deletion spring-publisher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
<version>4.0.7</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public PostPublisher(List<SocialMediaPublisher> socialMediaPublishers, PostRepos
this.clock = clock;
}

public Post publishNext() {
public Post publishNext(String group) {
ping(socialMediaPublishers);

Post nextPost = postRepository.getNextToPublish()
Post nextPost = postRepository.getNextToPublish(group)
.orElseThrow(() -> new IllegalStateException("There is not next post to publish"));

List<Publication> publishedPosts = publish(socialMediaPublishers, nextPost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Post {
private final URL url;
private final LocalDateTime publishedDate;
private final List<Publication> publications;
private final String group;

public String basicFormat() {
return String.format(BASIC_FORMAT,
Expand Down Expand Up @@ -52,6 +53,7 @@ public Post updateLastDatePublished(LocalDateTime publishedDate) {
.tags(tags)
.url(url)
.publications(publications)
.group(group)
.publishedDate(publishedDate).build();
}

Expand All @@ -63,6 +65,7 @@ public Post updatePublications(List<Publication> publications) {
.tags(tags)
.url(url)
.publications(publications)
.group(group)
.publishedDate(publishedDate).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import java.util.Optional;

public interface PostRepository {
Optional<Post> getNextToPublish();
Optional<Post> getNextToPublish(String group);
Post update(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.coderstower.socialmediapubisher.springpublisher.abstraction.post.PostPublisher;
import com.coderstower.socialmediapubisher.springpublisher.abstraction.post.repository.Post;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -30,11 +31,11 @@ public Map<String, String> ping() {
return pong;
}

@RequestMapping(path = "/posts/next", method = RequestMethod.POST)
public Post postNext() {
Post post = postPublisher.publishNext();
@RequestMapping(path = "/posts/{group}/next", method = RequestMethod.POST)
public Post postNext(@PathVariable String group) {
Post post = postPublisher.publishNext(group);

log.info("Published Post: {}", post);
log.info("Published Post from group {}: {}", group, post);

return post;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

@Data
@Builder
public class ShareContent {
private final Text shareCommentary;
private final String shareMediaCategory;
@Singular("media")
private final List<Media> media;
public class ArticleContent {
private final String title;
private final String description;
private final String source;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class Content {
private final ArticleContent article;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.coderstower.socialmediapubisher.springpublisher.main.socialmedia.linkedin;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class Distribution {
private final String feedDistribution;
}
Loading

0 comments on commit aecd253

Please sign in to comment.