Skip to content
This repository was archived by the owner on Dec 11, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/main/java/com/scmspain/MsFcTechTestApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.scmspain.configuration.TweetConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about that... if I put the @SpringBootApplication annotation, the tests fail, ¿why is that?
Nevertheless, a unused import must be erased.

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/scmspain/services/TweetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
Expand All @@ -29,7 +31,10 @@ public TweetService(EntityManager entityManager, MetricWriter metricWriter) {
Result - recovered Tweet
*/
public void publishTweet(String publisher, String text) {
if (publisher != null && publisher.length() > 0 && text != null && text.length() > 0 && text.length() < 140) {

boolean publisherIsNotNullOrEmpty = publisher != null && publisher.length() > 0;

if (publisherIsNotNullOrEmpty && tweetIsValid(text)) {
Tweet tweet = new Tweet();
tweet.setTweet(text);
tweet.setPublisher(publisher);
Expand All @@ -41,6 +46,23 @@ public void publishTweet(String publisher, String text) {
}
}

private boolean tweetIsValid(String tweet) {
String linkRegex = "(.*)https?://(.*)";
String space = " ";

String tweetWithoutLink = tweet;

if (tweet.matches(linkRegex)) {
tweetWithoutLink = Arrays.stream(tweet.split(space))
.filter(word -> !word.matches(linkRegex))
.collect(Collectors.joining(space));
}

boolean tweetIsNotNullOrEmpty = tweetWithoutLink != null && tweetWithoutLink.length() > 0;

return tweetIsNotNullOrEmpty && tweetWithoutLink.length() < 140;
}

/**
Recover tweet from repository
Parameter - id - id of the Tweet to retrieve
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/scmspain/services/TweetServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public void shouldInsertANewTweet() throws Exception {
public void shouldThrowAnExceptionWhenTweetLengthIsInvalid() throws Exception {
tweetService.publishTweet("Pirate", "LeChuck? He's the guy that went to the Governor's for dinner and never wanted to leave. He fell for her in a big way, but she told him to drop dead. So he did. Then things really got ugly.");
}

@Test
public void shouldIgnoreLinksForCharacterLimit() {
tweetService.publishTweet("LeChuck", "Please visit my personal web page http://makelechuckgreatagain.com, plenty of eighties stuff " +
"like Alf pictures, Madonna music and so on. #nostalgia #backtothe80s");

verify(entityManager).persist(any(Tweet.class));
}
}