Skip to content

Instantly share code, notes, and snippets.

@oleg
Created March 1, 2012 19:08
Show Gist options
  • Save oleg/1952374 to your computer and use it in GitHub Desktop.
Save oleg/1952374 to your computer and use it in GitHub Desktop.
package logic;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import play.libs.XML;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
public class RssToPostTest extends Assert {
public static final String URL =
"http://shpilenok.livejournal.com/data/rss";
@Test
public void testName() throws Exception {
RssToPost poster = spy(new RssToPost(URL, "1"));
doReturn(xml()).when(poster).getXml();
doReturn(false).when(poster).isPostExist(any(String.class), any(String.class));
doNothing().when(poster).newPost(any(String.class));
int result = poster.make(2);
assertEquals(2, result);
}
private Document xml() {
return XML.getDocument(twoPosts);
}
String twoPosts = "<?xml version='1.0' encoding='utf-8' ?>\n" +
"<rss version='2.0'>\n" +
"<channel>\n" +
"<item>\n" +
" <pubDate>Tue, 07 Feb 2012 10:14:00 GMT</pubDate>\n" +
" <title>Интересная новость</title>\n" +
" <link>http://demo.example.com/297526.html</link>\n" +
" <description></description>\n" +
"</item>\n" +
"<item>\n" +
" <pubDate>Tue, 01 Jan 2012 10:14:00 GMT</pubDate>\n" +
" <title>Новый год прошел</title>\n" +
" <link>http://demo.example.com/297526.html</link>\n" +
" <description></description>\n" +
"</item>\n" +
"</channel>\n" +
"</rss>";
}
package logic;
import models.Twit;
import models.User;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import play.libs.WS;
import play.libs.XPath;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
public class RssToPost {
private final String url;
private String userId;
public RssToPost(String url, String userId) {
this.url = url;
this.userId = userId;
}
public int make(int limit) throws IOException, ParserConfigurationException, SAXException {
List<Node> nodes = XPath.selectNodes("//item", getXml());
int i;
for (i = 0; i < limit && i < nodes.size(); i++) {
Node node = nodes.get(i);
String title = XPath.selectNode("title", node).getTextContent();
if (!isPostExist(title, userId)) {
String message;
if (StringUtils.isNotBlank(title)) {
message = "Новая запись в журнале: " + title;
} else {
String description = XPath.selectNode("description", node).getTextContent();
message = "Новая запись в журнале: " + description;
}
newPost(message);
}
}
return i;
}
Document getXml() {
return WS.url(url).get().getXml();
}
boolean isPostExist(String title, String userId) {
return Twit.find("message like ? and user.id = ?",
title + "%", userId).<Twit>first() != null;
}
void newPost(String message) {
Twit twit = new Twit();
twit.message = message;
twit.user = User.findById(userId);
twit.save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment