From cc87f11df07ed657e6b5d53ed8694f01339d4574 Mon Sep 17 00:00:00 2001 From: Daniel Pelaez Date: Sat, 13 Apr 2024 15:50:37 -0500 Subject: [PATCH 1/5] Adding multiple linkedin credencials --- .../oauth2/OAuth2CredentialAWSRepository.java | 12 ++ .../oauth2/OAuth2CredentialDynamo.java | 3 + .../abstraction/post/PostPublisher.java | 2 +- .../post/socialmedia/Publication.java | 1 + .../socialmedia/SocialMediaPublisher.java | 4 +- .../repository/OAuth2Credentials.java | 2 + .../OAuth2CredentialsRepository.java | 2 + .../linkedin/LinkedInPublisher.java | 130 ++++++++++-------- .../socialmedia/twitter/TwitterPublisher.java | 14 +- 9 files changed, 103 insertions(+), 67 deletions(-) diff --git a/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialAWSRepository.java b/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialAWSRepository.java index e877cb6..4cc4340 100644 --- a/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialAWSRepository.java +++ b/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialAWSRepository.java @@ -3,7 +3,10 @@ import com.coderstower.socialmediapubisher.springpublisher.abstraction.security.repository.OAuth2Credentials; import com.coderstower.socialmediapubisher.springpublisher.abstraction.security.repository.OAuth2CredentialsRepository; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; public class OAuth2CredentialAWSRepository implements OAuth2CredentialsRepository { private final OAuth2CredentialDynamoRepository oauth2CredentialDynamoRepository; @@ -27,11 +30,19 @@ public Optional getCredentials(String id) { return oauth2CredentialDynamoRepository.findById(id).map(this::convert); } + @Override + public List findAll() { + return StreamSupport.stream(oauth2CredentialDynamoRepository.findAll().spliterator(), false) + .map(this::convert) + .collect(Collectors.toList()); + } + private OAuth2CredentialDynamo convert(OAuth2Credentials oAuth2Credentials) { return OAuth2CredentialDynamo.builder() .accessToken(oAuth2Credentials.getAccessToken()) .id(oAuth2Credentials.getId()) .expirationDate(oAuth2Credentials.getExpirationDate()) + .allowedGroups(oAuth2Credentials.getAllowedGroups()) .build(); } @@ -40,6 +51,7 @@ private OAuth2Credentials convert(OAuth2CredentialDynamo oauth2CredentialDynamo) .accessToken(oauth2CredentialDynamo.getAccessToken()) .id(oauth2CredentialDynamo.getId()) .expirationDate(oauth2CredentialDynamo.getExpirationDate()) + .allowedGroups(oauth2CredentialDynamo.getAllowedGroups()) .build(); } } diff --git a/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamo.java b/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamo.java index 3c44ccf..9932991 100644 --- a/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamo.java +++ b/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamo.java @@ -11,6 +11,7 @@ import lombok.NoArgsConstructor; import java.time.LocalDateTime; +import java.util.List; @Data @DynamoDBTable(tableName = "Oauth2Credentials") @@ -25,4 +26,6 @@ public class OAuth2CredentialDynamo { @DynamoDBAttribute @DynamoDBTypeConverted(converter = LocalDateTimeConverter.class) private LocalDateTime expirationDate; + @DynamoDBAttribute + private List allowedGroups; } diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisher.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisher.java index 484cc18..2fab901 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisher.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisher.java @@ -48,7 +48,7 @@ private boolean publishedWellOK(List publishedPosts) { private List publish(List socialMediaPublishers, Post nextPost) { return socialMediaPublishers.stream() - .map(socialMediaPublisher -> socialMediaPublisher.publish(nextPost)) + .flatMap(socialMediaPublisher -> socialMediaPublisher.publish(nextPost).stream()) .collect(Collectors.toList()); } diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/Publication.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/Publication.java index 837bea8..70495eb 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/Publication.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/Publication.java @@ -12,6 +12,7 @@ public class Publication { private final String id; private final Status status; private final String publisher; + private final String credentialId; private final LocalDateTime publishedDate; public enum Status{ diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/SocialMediaPublisher.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/SocialMediaPublisher.java index 92c5692..a555fc8 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/SocialMediaPublisher.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/socialmedia/SocialMediaPublisher.java @@ -2,8 +2,10 @@ import com.coderstower.socialmediapubisher.springpublisher.abstraction.post.repository.Post; +import java.util.List; + public interface SocialMediaPublisher { String getName(); Acknowledge ping(); - Publication publish(Post post); + List publish(Post post); } diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2Credentials.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2Credentials.java index dff5688..3362ff5 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2Credentials.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2Credentials.java @@ -4,6 +4,7 @@ import lombok.Data; import java.time.LocalDateTime; +import java.util.List; @Data @Builder @@ -11,6 +12,7 @@ public class OAuth2Credentials { private final String id; private final String accessToken; private final LocalDateTime expirationDate; + private final List allowedGroups; public OAuth2Credentials update(String accessToken, LocalDateTime expirationDate){ return OAuth2Credentials.builder() diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2CredentialsRepository.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2CredentialsRepository.java index 1815d4d..be73c19 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2CredentialsRepository.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/security/repository/OAuth2CredentialsRepository.java @@ -1,5 +1,6 @@ package com.coderstower.socialmediapubisher.springpublisher.abstraction.security.repository; +import java.util.List; import java.util.Optional; public interface OAuth2CredentialsRepository { @@ -8,4 +9,5 @@ public interface OAuth2CredentialsRepository { OAuth2Credentials update(OAuth2Credentials oAuth2Credentials); Optional getCredentials(String id); + List findAll(); } diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java index 3b3719d..2eec18b 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java @@ -19,6 +19,8 @@ import java.time.Clock; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; @Slf4j @@ -44,75 +46,87 @@ public String getName() { @Override public Acknowledge ping() { - OAuth2Credentials credentials = oauth2CredentialsRepository.getCredentials(name) - .orElseThrow(() -> new IllegalArgumentException("The credentials for " + name + " doesn't exist")); - - if (areCredentialsExpired(credentials)) { - throw new UnauthorizedException("Unauthorized. Please login again here: " + loginURL.expand(name)); - } else { - return Acknowledge.builder() - .status(Acknowledge.Status.SUCCESS) - .build(); + List credentials = oauth2CredentialsRepository.findAll(); + + if (credentials.isEmpty()) { + throw new IllegalArgumentException("The credentials for " + name + " doesn't exist"); + } + + for (OAuth2Credentials credential : credentials) { + if (areCredentialsExpired(credential)) { + throw new UnauthorizedException("Unauthorized for " + name + " " + credential.getId() + ". Please login again here: " + loginURL.expand(name)); + } } + + return Acknowledge.builder() + .status(Acknowledge.Status.SUCCESS) + .build(); } @Override - public Publication publish(Post post) { - OAuth2Credentials credentials = oauth2CredentialsRepository.getCredentials(name) - .orElseThrow(() -> new IllegalArgumentException("The credentials for " + name + " doesn't exist")); - - try { - Profile profile = getProfile(credentials); - - LinkedInShare linkedInShare = LinkedInShare.builder() - .author("urn:li:person:" + profile.getSub()) - .commentary(post.basicFormatWithoutURL()) - .distribution(Distribution.builder().feedDistribution("MAIN_FEED").build()) - .lifecycleState("PUBLISHED") - .content(Content.builder() - .article(ArticleContent.builder() - .description(post.getDescription()) - .title(post.getName()) - .source(post.getUrl().toString()) - .build()) - .build()) - .visibility("PUBLIC") - .build(); - - String shareId = publish(linkedInShare, credentials); - - if (Objects.nonNull(shareId)) { - return Publication.builder() - .id(shareId) - .status(Publication.Status.SUCCESS) + public List publish(Post post) { + List credentials = oauth2CredentialsRepository.findAll(); + List publications = new ArrayList<>(); + + for (OAuth2Credentials credential : credentials) { + try { + Profile profile = getProfile(credential); + + LinkedInShare linkedInShare = LinkedInShare.builder() + .author("urn:li:person:" + profile.getSub()) + .commentary(post.basicFormatWithoutURL()) + .distribution(Distribution.builder().feedDistribution("MAIN_FEED").build()) + .lifecycleState("PUBLISHED") + .content(Content.builder() + .article(ArticleContent.builder() + .description(post.getDescription()) + .title(post.getName()) + .source(post.getUrl().toString()) + .build()) + .build()) + .visibility("PUBLIC") + .build(); + + String shareId = publish(linkedInShare, credential); + + if (Objects.nonNull(shareId)) { + publications.add(Publication.builder() + .id(shareId) + .status(Publication.Status.SUCCESS) + .publisher(name) + .publishedDate(LocalDateTime.now(clock)) + .credentialId(credential.getId()) + .build()); + } else { + publications.add(Publication.builder() + .status(Publication.Status.FAILURE) + .publisher(name) + .publishedDate(LocalDateTime.now(clock)) + .credentialId(credential.getId()) + .build()); + } + } catch (HttpClientErrorException e) { + log.error("Error publishing to " + name + ", credentials " + credential.getId() + " response body: " + e.getResponseBodyAsString(), e); + + publications.add(Publication.builder() + .status(Publication.Status.FAILURE) .publisher(name) .publishedDate(LocalDateTime.now(clock)) - .build(); - } else { - return Publication.builder() + .credentialId(credential.getId()) + .build()); + + } catch (Exception e) { + log.error("Error publishing to " + name + ", credentials " + credential.getId(), e); + + publications.add(Publication.builder() .status(Publication.Status.FAILURE) .publisher(name) .publishedDate(LocalDateTime.now(clock)) - .build(); + .credentialId(credential.getId()) + .build()); } - } catch (HttpClientErrorException e) { - log.error("Error publishing to " + name + ", response body: " + e.getResponseBodyAsString(), e); - - return Publication.builder() - .status(Publication.Status.FAILURE) - .publisher(name) - .publishedDate(LocalDateTime.now(clock)) - .build(); - - } catch (Exception e) { - log.error("Error publishing to " + name, e); - - return Publication.builder() - .status(Publication.Status.FAILURE) - .publisher(name) - .publishedDate(LocalDateTime.now(clock)) - .build(); } + return publications; } private String publish(LinkedInShare linkedInShare, OAuth2Credentials credentials) { diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisher.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisher.java index 2340fbb..c039bcb 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisher.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisher.java @@ -72,7 +72,7 @@ public Acknowledge ping() { } @Override - public Publication publish(Post post) { + public List publish(Post post) { OAuth1Credentials credentials = oauth1CredentialsRepository.getCredentials(name) .orElseThrow(() -> new IllegalArgumentException("The credentials for " + name + " doesn't exist")); @@ -84,27 +84,27 @@ public Publication publish(Post post) { Status statuses = twitter.updateStatus(post.basicFormat()); if (Objects.nonNull(statuses)) { - return Publication.builder() + return List.of(Publication.builder() .id(String.valueOf(statuses.getId())) .status(Publication.Status.SUCCESS) .publisher(name) .publishedDate(LocalDateTime.now(clock)) - .build(); + .build()); } else { - return Publication.builder() + return List.of(Publication.builder() .status(Publication.Status.FAILURE) .publisher(name) .publishedDate(LocalDateTime.now(clock)) - .build(); + .build()); } } catch (TwitterException e) { log.error("Error publishing to " + name, e); - return Publication.builder() + return List.of(Publication.builder() .status(Publication.Status.FAILURE) .publisher(name) .publishedDate(LocalDateTime.now(clock)) - .build(); + .build()); } } From 9e0b807171f65c58ee56af111128e9cb5776ffc7 Mon Sep 17 00:00:00 2001 From: Daniel Pelaez Date: Sat, 13 Apr 2024 15:55:12 -0500 Subject: [PATCH 2/5] Adding multiple linkedin credentials --- .../main/socialmedia/linkedin/LinkedInPublisher.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java index 2eec18b..abc83c3 100644 --- a/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java +++ b/spring-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisher.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; @Slf4j public class LinkedInPublisher implements SocialMediaPublisher { @@ -65,7 +66,11 @@ public Acknowledge ping() { @Override public List publish(Post post) { - List credentials = oauth2CredentialsRepository.findAll(); + List credentials = oauth2CredentialsRepository.findAll() + .stream() + .filter(oAuth2Credentials -> oAuth2Credentials.getAllowedGroups().contains(post.getGroup())) + .collect(Collectors.toList()); + List publications = new ArrayList<>(); for (OAuth2Credentials credential : credentials) { From 1ecdd986e6dddfb32030a62b7a6bc4b95626e2aa Mon Sep 17 00:00:00 2001 From: Daniel Pelaez Date: Sun, 14 Apr 2024 10:54:18 -0500 Subject: [PATCH 3/5] Adding multiple linkedin credentials --- .../abstraction/post/PostPublisherTest.java | 16 +++--- .../linkedin/LinkedInPublisherTest.java | 50 +++++++++++-------- .../twitter/TwitterPublisherTest.java | 18 +++---- 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisherTest.java b/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisherTest.java index a13faf5..01e1c46 100644 --- a/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisherTest.java +++ b/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/abstraction/post/PostPublisherTest.java @@ -104,19 +104,19 @@ public void publishNext_publishFailed_exception() throws MalformedURLException { when(postRepository.getNextToPublish("group1")).thenReturn(Optional.of(post)); when(socialMediaPublisher1.ping()).thenReturn(Acknowledge.builder() .status(Acknowledge.Status.SUCCESS).build()); - when(socialMediaPublisher1.publish(post)).thenReturn(Publication.builder() + when(socialMediaPublisher1.publish(post)).thenReturn(List.of(Publication.builder() .id("id") .publishedDate(LocalDateTime.of(2020, 3, 3, 5, 6, 8, 1)) .status(Publication.Status.SUCCESS) .publisher("TWITTER") - .build()); + .build())); when(socialMediaPublisher2.ping()).thenReturn(Acknowledge.builder() .status(Acknowledge.Status.SUCCESS).build()); - when(socialMediaPublisher2.publish(post)).thenReturn(Publication.builder() + when(socialMediaPublisher2.publish(post)).thenReturn(List.of(Publication.builder() .publishedDate(LocalDateTime.of(2020, 3, 3, 5, 6, 8, 1)) .status(Publication.Status.FAILURE) .publisher("LINKEDIN") - .build()); + .build())); IllegalStateException exception = assertThrows(IllegalStateException.class, () -> postPublisher.publishNext("group1")); @@ -171,20 +171,20 @@ public void publishNext_publishSuccess_publications() throws MalformedURLExcepti when(postRepository.getNextToPublish("group1")).thenReturn(Optional.of(post)); when(socialMediaPublisher1.ping()).thenReturn(Acknowledge.builder() .status(Acknowledge.Status.SUCCESS).build()); - when(socialMediaPublisher1.publish(post)).thenReturn(Publication.builder() + when(socialMediaPublisher1.publish(post)).thenReturn(List.of(Publication.builder() .id("id") .publishedDate(LocalDateTime.of(2020, 3, 3, 5, 6, 8, 1)) .status(Publication.Status.SUCCESS) .publisher("TWITTER") - .build()); + .build())); when(socialMediaPublisher2.ping()).thenReturn(Acknowledge.builder() .status(Acknowledge.Status.SUCCESS).build()); - when(socialMediaPublisher2.publish(post)).thenReturn(Publication.builder() + when(socialMediaPublisher2.publish(post)).thenReturn(List.of(Publication.builder() .id("id") .publishedDate(LocalDateTime.of(2020, 3, 3, 5, 6, 8, 1)) .status(Publication.Status.SUCCESS) .publisher("LINKEDIN") - .build()); + .build())); when(postRepository.update(postUpdated)).thenReturn(postUpdated); Post publishedPost = postPublisher.publishNext("group1"); diff --git a/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisherTest.java b/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisherTest.java index 70440e4..962d522 100644 --- a/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisherTest.java +++ b/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/linkedin/LinkedInPublisherTest.java @@ -51,7 +51,7 @@ public void before() { @Test public void ping_noCredential_exception() { - when(oauth2CredentialsRepository.getCredentials("linkedin")).thenReturn(Optional.empty()); + when(oauth2CredentialsRepository.findAll()).thenReturn(List.of()); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> linkedInPublisher.ping()); @@ -60,18 +60,19 @@ public void ping_noCredential_exception() { @Test public void ping_expiredCredential_exception() { - when(oauth2CredentialsRepository.getCredentials("linkedin")).thenReturn(Optional.of(OAuth2Credentials.builder() + when(oauth2CredentialsRepository.findAll()).thenReturn(List.of(OAuth2Credentials.builder() .expirationDate(now.toLocalDateTime().minusMonths(1)) + .id("credential1") .build())); UnauthorizedException exception = assertThrows(UnauthorizedException.class, () -> linkedInPublisher.ping()); - assertThat(exception.getMessage()).isEqualTo("Unauthorized. Please login again here: http://localhost:8080/oauth2/linkedin/credentials"); + assertThat(exception.getMessage()).isEqualTo("Unauthorized for linkedin credential1. Please login again here: http://localhost:8080/oauth2/linkedin/credentials"); } @Test public void ping_goodCredential_success() { - when(oauth2CredentialsRepository.getCredentials("linkedin")).thenReturn(Optional.of(OAuth2Credentials.builder() + when(oauth2CredentialsRepository.findAll()).thenReturn(List.of(OAuth2Credentials.builder() .expirationDate(now.toLocalDateTime().plusMonths(1)) .build())); @@ -91,19 +92,22 @@ public void publish_noProfile_publicationError() { HttpEntity requestEntityProfile = new HttpEntity<>(httpHeadersProfile); - when(oauth2CredentialsRepository.getCredentials("linkedin")).thenReturn(Optional.of(OAuth2Credentials.builder() + when(oauth2CredentialsRepository.findAll()).thenReturn(List.of(OAuth2Credentials.builder() .accessToken("accessToken") + .allowedGroups(List.of("group1")) .build())); when(restTemplate.exchange("https://api.linkedin.com/v2/me", HttpMethod.GET, requestEntityProfile, Profile.class)) .thenReturn(ResponseEntity.notFound().build()); - Publication publication = linkedInPublisher.publish(Post.builder().build()); + List publication = linkedInPublisher.publish(Post.builder() + .group("group1") + .build()); - assertThat(publication).isEqualTo(Publication.builder() + assertThat(publication).isEqualTo(List.of(Publication.builder() .status(Publication.Status.FAILURE) .publisher("linkedin") .publishedDate(now.toLocalDateTime()) - .build()); + .build())); } @Test @@ -113,8 +117,9 @@ public void publish_noShare_publicationError() throws MalformedURLException { httpHeaders.add("X-Restli-Protocol-Version", "2.0.0"); httpHeaders.setBearerAuth("accessToken"); - when(oauth2CredentialsRepository.getCredentials("linkedin")).thenReturn(Optional.of(OAuth2Credentials.builder() + when(oauth2CredentialsRepository.findAll()).thenReturn(List.of(OAuth2Credentials.builder() .accessToken("accessToken") + .allowedGroups(List.of("group1")) .build())); HttpEntity requestEntityProfile = new HttpEntity<>(httpHeaders); @@ -145,20 +150,21 @@ public void publish_noShare_publicationError() throws MalformedURLException { when(restTemplate.exchange("https://api.linkedin.com/v2/ugcPosts", HttpMethod.POST, requestShare, Void.class)) .thenReturn(ResponseEntity.badRequest().build()); - Publication publication = linkedInPublisher.publish(Post.builder() + List publication = linkedInPublisher.publish(Post.builder() .id("2") .name("My Post 2") .description("My second post") .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()); - assertThat(publication).isEqualTo(Publication.builder() + assertThat(publication).isEqualTo(List.of(Publication.builder() .status(Publication.Status.FAILURE) .publisher("linkedin") .publishedDate(now.toLocalDateTime()) - .build()); + .build())); } @Test @@ -168,8 +174,9 @@ public void publish_noShareId_publicationError() throws MalformedURLException { httpHeaders.add("X-Restli-Protocol-Version", "2.0.0"); httpHeaders.setBearerAuth("accessToken"); - when(oauth2CredentialsRepository.getCredentials("linkedin")).thenReturn(Optional.of(OAuth2Credentials.builder() + when(oauth2CredentialsRepository.findAll()).thenReturn(List.of(OAuth2Credentials.builder() .accessToken("accessToken") + .allowedGroups(List.of("group1")) .build())); HttpEntity requestEntityProfile = new HttpEntity<>(httpHeaders); @@ -200,20 +207,21 @@ public void publish_noShareId_publicationError() throws MalformedURLException { when(restTemplate.exchange("https://api.linkedin.com/v2/ugcPosts", HttpMethod.POST, requestShare, Void.class)) .thenReturn(ResponseEntity.ok().build()); - Publication publication = linkedInPublisher.publish(Post.builder() + List publication = linkedInPublisher.publish(Post.builder() .id("2") .name("My Post 2") .description("My second post") .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()); - assertThat(publication).isEqualTo(Publication.builder() + assertThat(publication).isEqualTo(List.of(Publication.builder() .status(Publication.Status.FAILURE) .publisher("linkedin") .publishedDate(now.toLocalDateTime()) - .build()); + .build())); } @Test @@ -224,8 +232,9 @@ public void publish_shareId_publicationSucced() throws MalformedURLException { httpHeaders.add("LinkedIn-Version", "202304"); httpHeaders.setBearerAuth("accessToken"); - when(oauth2CredentialsRepository.getCredentials("linkedin")).thenReturn(Optional.of(OAuth2Credentials.builder() + when(oauth2CredentialsRepository.findAll()).thenReturn(List.of(OAuth2Credentials.builder() .accessToken("accessToken") + .allowedGroups(List.of("group1")) .build())); HttpEntity requestEntityProfile = new HttpEntity<>(httpHeaders); @@ -256,20 +265,21 @@ public void publish_shareId_publicationSucced() throws MalformedURLException { when(restTemplate.exchange("https://api.linkedin.com/rest/posts", HttpMethod.POST, requestShare, Void.class)) .thenReturn(ResponseEntity.ok().header("X-RestLi-Id", "shareId").build()); - Publication publication = linkedInPublisher.publish(Post.builder() + List publication = linkedInPublisher.publish(Post.builder() .id("2") .name("My Post 2") .description("My second post") .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()); - assertThat(publication).isEqualTo(Publication.builder() + assertThat(publication).isEqualTo(List.of(Publication.builder() .id("shareId") .status(Publication.Status.SUCCESS) .publisher("linkedin") .publishedDate(now.toLocalDateTime()) - .build()); + .build())); } } \ No newline at end of file diff --git a/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisherTest.java b/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisherTest.java index bc937ad..a968230 100644 --- a/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisherTest.java +++ b/spring-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/socialmedia/twitter/TwitterPublisherTest.java @@ -139,7 +139,7 @@ public void publish_credentialNoSetTwitterError_shareFailure() throws TwitterExc when(twitter.getAuthorization()).thenReturn(NullAuthorization.getInstance()); when(twitter.updateStatus("My second post\n\n#tag1 #tag2\n\nhttps://coderstower.com/2020/01/13/open-close-principle-by-example/")).thenThrow(new TwitterException("error")); - Publication publish = twitterPublisher.publish(Post.builder() + List publish = twitterPublisher.publish(Post.builder() .id("2") .name("My Post 2") .description("My second post") @@ -148,11 +148,11 @@ public void publish_credentialNoSetTwitterError_shareFailure() throws TwitterExc .publishedDate(LocalDateTime.parse("2012-09-17T18:47:52")) .build()); - assertThat(publish).isEqualTo(Publication.builder() + assertThat(publish).isEqualTo(List.of(Publication.builder() .status(Publication.Status.FAILURE) .publisher("twitter") .publishedDate(now.toLocalDateTime()) - .build()); + .build())); verify(twitter).setOAuthConsumer("consumerKey", "consumerSecret"); verify(twitter).setOAuthAccessToken(new AccessToken("accessToken", "tokenSecret")); } @@ -168,7 +168,7 @@ public void publish_credentialSetEmptyStatus_shareFailure() throws TwitterExcept when(twitter.getAuthorization()).thenReturn(mock(Authorization.class)); when(twitter.updateStatus("My second post\n\n#tag1 #tag2\n\nhttps://coderstower.com/2020/01/13/open-close-principle-by-example/")).thenReturn(null); - Publication publish = twitterPublisher.publish(Post.builder() + List publish = twitterPublisher.publish(Post.builder() .id("2") .name("My Post 2") .description("My second post") @@ -177,11 +177,11 @@ public void publish_credentialSetEmptyStatus_shareFailure() throws TwitterExcept .publishedDate(LocalDateTime.parse("2012-09-17T18:47:52")) .build()); - assertThat(publish).isEqualTo(Publication.builder() + assertThat(publish).isEqualTo(List.of(Publication.builder() .status(Publication.Status.FAILURE) .publisher("twitter") .publishedDate(now.toLocalDateTime()) - .build()); + .build())); verify(twitter, times(0)).setOAuthConsumer("consumerKey", "consumerSecret"); verify(twitter, times(0)).setOAuthAccessToken(new AccessToken("accessToken", "tokenSecret")); } @@ -200,7 +200,7 @@ public void publish_credentialSetStatus_ackSucced() throws TwitterException, Mal when(status.getId()).thenReturn(123L); when(twitter.updateStatus("My second post\n\n#tag1 #tag2\n\nhttps://coderstower.com/2020/01/13/open-close-principle-by-example/")).thenReturn(status); - Publication publish = twitterPublisher.publish(Post.builder() + List publish = twitterPublisher.publish(Post.builder() .id("2") .name("My Post 2") .description("My second post") @@ -209,12 +209,12 @@ public void publish_credentialSetStatus_ackSucced() throws TwitterException, Mal .publishedDate(LocalDateTime.parse("2012-09-17T18:47:52")) .build()); - assertThat(publish).isEqualTo(Publication.builder() + assertThat(publish).isEqualTo(List.of(Publication.builder() .id("123") .status(Publication.Status.SUCCESS) .publisher("twitter") .publishedDate(now.toLocalDateTime()) - .build()); + .build())); verify(twitter, times(0)).setOAuthConsumer("consumerKey", "consumerSecret"); verify(twitter, times(0)).setOAuthAccessToken(new AccessToken("accessToken", "tokenSecret")); } From 3cdf9b899b1a5a065c9ee1f80c198e1c2f30c2d4 Mon Sep 17 00:00:00 2001 From: Daniel Pelaez Date: Sun, 14 Apr 2024 11:06:18 -0500 Subject: [PATCH 4/5] Adding multiple linkedin credentials --- .../repository/oauth2/OAuth2CredentialDynamoRepository.java | 3 ++- .../main/aws/MockSocialMediaCredentialsHandlingTests.java | 4 ++-- .../main/aws/MockSocialMediaSuccessfulTests.java | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamoRepository.java b/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamoRepository.java index 7035c45..11d8525 100644 --- a/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamoRepository.java +++ b/aws-publisher/src/main/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/repository/oauth2/OAuth2CredentialDynamoRepository.java @@ -1,8 +1,9 @@ package com.coderstower.socialmediapubisher.springpublisher.main.aws.repository.oauth2; +import org.socialsignin.spring.data.dynamodb.repository.EnableScan; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; -@Repository +@EnableScan public interface OAuth2CredentialDynamoRepository extends CrudRepository { } diff --git a/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaCredentialsHandlingTests.java b/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaCredentialsHandlingTests.java index 7b29606..fe69fe9 100644 --- a/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaCredentialsHandlingTests.java +++ b/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaCredentialsHandlingTests.java @@ -87,7 +87,7 @@ void publish_credentialsExpired_unauthorizedException() throws Exception { .with(csrf()) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isUnauthorized()) - .andExpect(content().string("Unauthorized. Please login again here: http://localhost:8080/oauth2/linkedin/credentials")); + .andExpect(content().string("Unauthorized for linkedin credential1. Please login again here: http://localhost:8080/oauth2/linkedin/credentials")); } @Test @@ -154,7 +154,7 @@ public AmazonDynamoDB amazonDynamoDB() { PutItemRequest oauth2Credentials = new PutItemRequest(); oauth2Credentials.setTableName("Oauth2Credentials"); - oauth2Credentials.addItemEntry("id", new AttributeValue("linkedin")); + oauth2Credentials.addItemEntry("id", new AttributeValue("credential1")); oauth2Credentials.addItemEntry("accessToken", new AttributeValue("access123")); oauth2Credentials.addItemEntry("expirationDate", new AttributeValue("2020-02-01T05:05:05")); diff --git a/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaSuccessfulTests.java b/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaSuccessfulTests.java index 3209f79..07344da 100644 --- a/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaSuccessfulTests.java +++ b/aws-publisher/src/test/java/com/coderstower/socialmediapubisher/springpublisher/main/aws/MockSocialMediaSuccessfulTests.java @@ -146,6 +146,7 @@ public AmazonDynamoDB amazonDynamoDB() { oauth2Credentials.setTableName("Oauth2Credentials"); oauth2Credentials.addItemEntry("id", new AttributeValue("linkedin")); oauth2Credentials.addItemEntry("accessToken", new AttributeValue("access123")); + oauth2Credentials.addItemEntry("allowedGroups", new AttributeValue().withL(List.of(new AttributeValue("group1")))); oauth2Credentials.addItemEntry("expirationDate", new AttributeValue("2020-04-01T05:05:05")); ddb.putItem(oauth2Credentials); From f0a7019b16a39163be4ed15e87e752bb81af6539 Mon Sep 17 00:00:00 2001 From: Daniel Pelaez Date: Sun, 14 Apr 2024 12:34:02 -0500 Subject: [PATCH 5/5] Adding multiple linkedin credentials --- aws-publisher/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-publisher/pom.xml b/aws-publisher/pom.xml index 7f14d54..65eb6a7 100644 --- a/aws-publisher/pom.xml +++ b/aws-publisher/pom.xml @@ -5,7 +5,7 @@ 4.0.0 aws-publisher - 0.3.0-SNAPSHOT + 0.5.0-SNAPSHOT jar aws-publisher