package org.pegadi.server.article;
import no.dusken.common.model.Person;
import org.junit.Before;
import org.junit.Test;
import org.pegadi.articlesearch.JournalistTerm;
import org.pegadi.articlesearch.PhotographerTerm;
import org.pegadi.model.*;
import org.pegadi.server.*;
import org.pegadi.server.user.UserServer;
import org.pegadi.sqlsearch.AndTerm;
import org.pegadi.sqlsearch.OrTerm;
import org.pegadi.sqlsearch.SearchTerm;
import org.w3c.dom.Document;
import java.awt.*;
import java.io.File;
import java.util.*;
import java.util.List;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Marvin B. Lillehaug <lillehau@underdusken.no>
*/
public class ArticleServerImplTest extends AbstractDatabaseTest {
private ArticleServer articleServer;
private UserServer userServer;
private PublicationServer publicationServer;
private ArticleTypeServer articleTypeServer;
private ArticleStatusServer articleStatusServer;
private SectionServer sectionServer;
@Before
public void onSetup() throws Exception {
articleServer = new ArticleServerImpl();
publicationServer = mock(PublicationServer.class);
userServer = mock(UserServer.class);
articleTypeServer = mock(ArticleTypeServer.class);
sectionServer = mock(SectionServer.class);
articleStatusServer = new ArticleStatusServerImpl();
((ArticleStatusServerImpl) articleStatusServer).setDataSource(getDataSource());
((ArticleServerImpl) articleServer).setDataSource(getDataSource());
((ArticleServerImpl) articleServer).setUserServer(userServer);
((ArticleServerImpl) articleServer).setPublicationServer(publicationServer);
((ArticleServerImpl) articleServer).setArticleTypeServer(articleTypeServer);
((ArticleServerImpl) articleServer).setSectionServer(sectionServer);
((ArticleServerImpl) articleServer).setArticleStatusServer(articleStatusServer);
File xmlFolder = getXmlFolder();
((ArticleServerImpl) articleServer).setXmlLocation(xmlFolder);
File location = File.createTempFile("article", "server");
location.mkdirs();
location.deleteOnExit();
((ArticleServerImpl) articleServer).setBackupLocation(location.getParentFile());
when(userServer.getUserByUsername("journalist")).thenReturn(newPerson(1));
when(userServer.getUserByUsername("journalist2")).thenReturn(newPerson(2));
when(userServer.getUserByUsername("journalist3")).thenReturn(newPerson(3));
when(userServer.getUserByUsername("journalist4")).thenReturn(newPerson(4));
when(userServer.getUserByUsername("journalist5")).thenReturn(newPerson(5));
when(userServer.getUserByUsername("photographer6")).thenReturn(newPerson(6));
when(publicationServer.getPublicationByID(1)).thenReturn(new Publication(1));
when(publicationServer.getPublicationByID(2)).thenReturn(new Publication(2));
when(publicationServer.getPublicationByID(3)).thenReturn(new Publication(3));
when(articleTypeServer.getArticleType(1)).thenReturn(new ArticleType(1));
when(articleTypeServer.getArticleType(15)).thenReturn(new ArticleType(15));
when(sectionServer.getDepartment(1)).thenReturn(new Section(1));
when(sectionServer.getDepartment(2)).thenReturn(new Section(2));
}
private File getXmlFolder() {
File v = new File("src/test/resources");
if(!v.exists()){
v = new File("server/src/test/resources");
}
return v;
}
private Person newPerson(long i) {
return new Person(i, "f", "l", new Random().nextGaussian() + "", "dp");
}
@Test
public void testCanEditWhenGod() throws Exception {
Person person = new Person(1L, "name", "lastname", "iser", "df@f.no");
when(userServer.isGod(person)).thenReturn(true);
boolean canEdit = articleServer.canEdit(person, 1);
assertTrue(canEdit);
}
@Test
public void testCanEditWhenJournalist() throws Exception {
Person person = newPerson(1);
when(userServer.isGod(person)).thenReturn(false);
when(userServer.getUserByUsername("journalist")).thenReturn(person);
boolean canEdit = articleServer.canEdit(person, 1);
assertTrue(canEdit);
}
@Test
public void testCanEditWhenPhotographer() throws Exception {
Person person = newPerson(2);
when(userServer.isGod(person)).thenReturn(false);
when(userServer.getUserByUsername("photographer")).thenReturn(person);
boolean canEdit = articleServer.canEdit(person, 1);
assertTrue(canEdit);
}
@Test
public void testCanEditWhenCoJournalist() throws Exception {
Person person = new Person(1L, "name", "lastname", "journalist7", "journalist7@derp.no");
when(userServer.isGod(person)).thenReturn(false);
when(userServer.getUserByUsername("journalist7")).thenReturn(person);
boolean canEdit = articleServer.canEdit(person, 1);
assertTrue(canEdit);
}
@Test
public void testCanEditWhenKorrektur(){
Person person = newPerson(8);
when(userServer.isGod(person)).thenReturn(false);
when(userServer.hasRole(19, 8L)).thenReturn(true);
boolean canEdit = articleServer.canEdit(person, 1);
assertTrue(canEdit);
}
@Test
public void testCanDeleteWhenJournalist() throws Exception {
Person person = newPerson(1);
when(userServer.isGod(person)).thenReturn(false);
when(userServer.getUserByUsername("journalist")).thenReturn(person);
boolean canDelete = articleServer.canDelete(person, 1);
assertTrue(canDelete);
}
@Test
public void testCanDeleteWhenGod() throws Exception {
Person person = newPerson(3);
when(userServer.isGod(person)).thenReturn(true);
when(userServer.getUserByUsername("journalist")).thenReturn(person);
boolean canDelete = articleServer.canDelete(person, 1);
assertTrue(canDelete);
}
@Test
public void testGetArticleStatuses() throws Exception {
List<ArticleStatus> statuses = articleStatusServer.getArticleStatuses();
assertEquals("Wrong number of articlestatuses", 7, statuses.size());
for(ArticleStatus status : statuses){
assertNotNull(status);
}
}
@Test
public void testDeleteArticle() throws Exception {
Person person = newPerson(1);
Article a = articleServer.getArticleByID(person, 1);
assertNotNull(a);
int count = countRowsInTable("DeletedArticle");
assertEquals("DeletedArticles was not empty", 0, count);
articleServer.deleteArticle(1);
a = articleServer.getArticleByID(person, 1);
assertNull(a);
count = countRowsInTable("DeletedArticle");
assertEquals("DeletedArticles was empty", 1, count);
}
@Test
public void testSaveArticle() throws Exception {
Article a = new Article();
a.setArticleStatus(articleStatusServer.getArticleStatus(1));
a.setArticleType(articleTypeServer.getArticleType(1));
a.setSection(sectionServer.getDepartment(1));
a.setCurrentNumberOfCharacters(666);
a.setDescription("A very good article");
a.setJournalist(journalist());
a.setName("A article");
a.setPhotographer(photographer());
a.setLastSaved((new GregorianCalendar()).getTime());
a.setPublication(publicationServer.getPublicationByID(1));
a.setText("Artikkeltekst");
a.setWantedNumberOfCharacters(666);
a.setWantedNumberOfPages(1);
int id = articleServer.saveArticle(a);
assertTrue("Wrong article id", id > 0);
Map<String, Object> map = simpleJdbcTemplate.queryForMap("SELECT * FROM Article where id=?", id);
assertEquals("Wrong articlestatus", new Integer(1), map.get("refStatus"));
assertEquals("Wrong article type", new Integer(1), map.get("refArticleType"));
assertEquals("Wrong department", new Integer(1), map.get("refSection"));
assertEquals("Wrong currentNumberOfCharacters", new Integer(666), map.get("wantedCharacters"));
assertEquals("Wrong description", "A very good article", map.get("description"));
assertEquals("Wrong journalist", "journalist", map.get("refJournalist"));
assertEquals("Wrong photographer", "photographer6", map.get("refPhotographer"));
assertEquals("Wrong name", "A article", map.get("name"));
assertEquals("Wrong publication", new Integer(1), map.get("refPublication"));
assertEquals("Wrong text", "Artikkeltekst", map.get("articlexml"));
assertEquals("Wrong wanted number of characters", new Integer(666), map.get("wantedCharacters"));
assertEquals("Wrong wanted number of pages", 1.0d, map.get("wantedPages"));
}
private Person photographer() {
return new Person(1L, "m", "s", "photographer6", "d@f.vk");
}
private Person journalist() {
return new Person(1L, "m", "s", "journalist", "d@f.vk");
}
@Test
public void testSaveArticleText() throws Exception {
Article a = articleServer.getArticleByID(newPerson(3), 3);
String text = "<?xml version=\"1.0\" encoding=\"utf-8\"?><fullArtikkel ID=\"24\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://dusken15.samfundet.no:8080/redsysweb/xml/schema/ud1.xsd\"> <head> <authorgroup> <person ID=\"26\"> <name>Håvard Wigtil</name> <email>havardw@stud.ntnu.no</email> <initials>hw</initials> </person> </authorgroup> </head> <text> <tittel>Tittel</tittel> <ingress>Ingress</ingress> <brodtekst>Brødtekst</brodtekst> <mellomtittel>Mellomtittel</mellomtittel> <brodtekst>Meir brødtekst. Gjenta til ferdig,,,</brodtekst> </text> <imagegroup> <image type=\"0\"> <imagekeyword>Stikkord</imagekeyword> <imagecaption>Bilde test</imagecaption> </image> </imagegroup> <notes> <note h=\"100\" state=\"maximum\" w=\"300\" x=\"40\" y=\"40\"> <p>Noen dager er fulle av tant og fjas</p> </note> </notes></fullArtikkel>";
a.setText(text);
boolean success = articleServer.saveArticleText(a);
assertTrue("Save was not successfull", success);
Map<String, Object> map = simpleJdbcTemplate.queryForMap("SELECT articlexml, characterCount FROM Article where id=?", a.getId());
assertEquals("Wrong text", text, map.get("articlexml"));
assertEquals("Wrong char could", a.getCurrentNumberOfCharacters(), map.get("characterCount"));
}
@Test
public void testUpdateArticleChangeArticleType(){
Article a = articleServer.getArticleByID(newPerson(3), 3);
a.setCurrentNumberOfCharacters(999);
a.setSection(sectionServer.getDepartment(2));
a.setArticleStatus(articleStatusServer.getArticleStatus(3));
a.setArticleType(articleTypeServer.getArticleType(15));
a.setDescription("NY beskrivelse");
a.setJournalist(journalist());
a.setName("Nyny");
a.setPhotographer(photographer());
a.setPublication(publicationServer.getPublicationByID(2));
String text = "<?xml version=\"1.0\" encoding=\"utf-8\"?><fullArtikkel ID=\"24\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://dusken15.samfundet.no:8080/redsysweb/xml/schema/ud1.xsd\"> <head> <authorgroup> <person ID=\"26\"> <name>Håvard Wigtil</name> <email>havardw@stud.ntnu.no</email> <initials>hw</initials> </person> </authorgroup> </head> <text> <tittel>Tittel</tittel> <ingress>Ingress</ingress> <brodtekst>Brødtekst</brodtekst> <mellomtittel>Mellomtittel</mellomtittel> <brodtekst>Meir brødtekst. Gjenta til ferdig,,,</brodtekst> </text> <imagegroup> <image type=\"0\"> <imagekeyword>Stikkord</imagekeyword> <imagecaption>Bilde test</imagecaption> </image> </imagegroup> <notes> <note h=\"100\" state=\"maximum\" w=\"300\" x=\"40\" y=\"40\"> <p>Noen dager er fulle av tant og fjas</p> </note> </notes></fullArtikkel>";
a.setText(text);
a.setWantedNumberOfCharacters(999);
a.setWantedNumberOfPages(2);
a.parseText();
Document doc = a.getDocument();
assertEquals("Old document type wrong", "fullArtikkel", doc.getDocumentElement().getNodeName());
int status = articleServer.saveArticle(a);
assertEquals("Save failed", 0, status);
Map<String, Object> map = simpleJdbcTemplate.queryForMap("SELECT * FROM Article where id=?", a.getId());
assertEquals("Wrong articlestatus", new Integer(3), map.get("refStatus"));
assertEquals("Wrong article type", new Integer(15), map.get("refArticleType"));
assertEquals("Wrong department", new Integer(2), map.get("refSection"));
assertEquals("Wrong currentNumberOfCharacters", new Integer(999), map.get("wantedCharacters"));
assertEquals("Wrong description", "NY beskrivelse", map.get("description"));
assertEquals("Wrong journalist", "journalist", map.get("refJournalist"));
assertEquals("Wrong photographer", "photographer6", map.get("refPhotographer"));
assertEquals("Wrong name", "Nyny", map.get("name"));
assertEquals("Wrong publication", new Integer(2), map.get("refPublication"));
String newtext = "<?xml version=\"1.0\" encoding=\"utf-8\"?><debattArtikkel xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" ID=\"24\" xsi:noNamespaceSchemaLocation=\"http://dusken15.samfundet.no:8080/redsysweb/xml/schema/ud1.xsd\"> <head> <authorgroup> <person ID=\"26\"> <name>Håvard Wigtil</name> <email>havardw@stud.ntnu.no</email> <initials>hw</initials> </person> </authorgroup> </head> <text> <tittel>Tittel</tittel> <ingress>Ingress</ingress> <brodtekst>Brødtekst</brodtekst> <mellomtittel>Mellomtittel</mellomtittel> <brodtekst>Meir brødtekst. Gjenta til ferdig,,,</brodtekst> </text> <imagegroup> <image type=\"0\"> <imagekeyword>Stikkord</imagekeyword> <imagecaption>Bilde test</imagecaption> </image> </imagegroup> <notes> <note h=\"100\" state=\"maximum\" w=\"300\" x=\"40\" y=\"40\"> <p>Noen dager er fulle av tant og fjas</p> </note> </notes></debattArtikkel>";
assertEquals("Wrong text", newtext, map.get("articlexml"));
assertEquals("Wrong wanted number of characters", new Integer(999), map.get("wantedCharacters"));
assertEquals("Wrong wanted number of pages", 2.0d, map.get("wantedPages"));
a.parseText();
doc = a.getDocument();
assertEquals("New document type wrong", "debattArtikkel", doc.getDocumentElement().getNodeName());
}
@Test
public void testUpdateArticle(){
Article a = articleServer.getArticleByID(newPerson(3), 3);
a.setCurrentNumberOfCharacters(999);
a.setSection(sectionServer.getDepartment(2));
a.setArticleStatus(articleStatusServer.getArticleStatus(3));
a.setArticleType(articleTypeServer.getArticleType(15));
a.setDescription("NY beskrivelse");
a.setJournalist(journalist());
a.setName("Nyny");
a.setPhotographer(photographer());
a.setPublication(publicationServer.getPublicationByID(2));
a.setText("NYTEKST");
a.setWantedNumberOfCharacters(999);
a.setWantedNumberOfPages(2);
int status = articleServer.saveArticle(a);
assertEquals("Save failed", 0, status);
Map<String, Object> map = simpleJdbcTemplate.queryForMap("SELECT * FROM Article where id=?", a.getId());
assertEquals("Wrong articlestatus", new Integer(3), map.get("refStatus"));
assertEquals("Wrong article type", new Integer(15), map.get("refArticleType"));
assertEquals("Wrong department", new Integer(2), map.get("refSection"));
assertEquals("Wrong currentNumberOfCharacters", new Integer(999), map.get("wantedCharacters"));
assertEquals("Wrong description", "NY beskrivelse", map.get("description"));
assertEquals("Wrong journalist", "journalist", map.get("refJournalist"));
assertEquals("Wrong photographer", "photographer6", map.get("refPhotographer"));
assertEquals("Wrong name", "Nyny", map.get("name"));
assertEquals("Wrong publication", new Integer(2), map.get("refPublication"));
assertEquals("Wrong text", "NYTEKST", map.get("articlexml"));
assertEquals("Wrong wanted number of characters", new Integer(999), map.get("wantedCharacters"));
assertEquals("Wrong wanted number of pages", 2.0d, map.get("wantedPages"));
}
@Test
public void testGetArticleByID() throws Exception {
Article a = articleServer.getArticleByID(newPerson(3), 3);
assertNotNull("Should have returned article", a);
assertEquals("Kul redaktør", a.getName());
assertNotNull(a.getPhotographer());
assertNotNull(a.getJournalist());
assertNotNull(a.getText());
assertTrue(a.getText().length() > 10);
assertEquals(3, a.getId());
assertNotNull(a.getArticleStatus());
assertEquals(new Integer(2), a.getArticleStatus().getId());
assertEquals(new Integer(2), a.getPublication().getId());
assertNotNull(a.getArticleType());
assertEquals(new Integer(1), a.getArticleType().getId());
assertNotNull(a.getSection());
assertEquals(1, a.getSection().getId());
assertNotNull(a.getLastSaved());
}
@Test
public void testGetHiddenArticleByID(){
Article a = articleServer.getArticleByID(newPerson(3), 3);
assertNotNull("Should have returned article", a);
}
@Test
public void testGetHiddenArticleByIDWrongUser(){
Article a = articleServer.getArticleByID(newPerson(3), 4);
assertNull("Should not have returned article", a);
}
@Test
public void testGetNonExistingArticle(){
Article a = articleServer.getArticleByID(newPerson(123L), 123);
assertNull(a);
}
@Test
public void testGetArticleStatus() throws Exception {
ArticleStatus articleStatus = articleStatusServer.getArticleStatus(2);
assertEquals("Skrives", articleStatus.getName());
assertEquals("Journalisten jobber med saken.", articleStatus.getDescription());
Color color = articleStatus.getColor();
assertEquals(255, color.getRed());
assertEquals(255, color.getGreen());
assertEquals(255, color.getBlue());
assertEquals(new Integer(2), articleStatus.getId());
}
@Test
public void testGetTemplate() throws Exception {
String template = articleServer.getTemplate(2);
assertNotNull("Template was null", template);
assertTrue("Template was less than 10 chars", template.length() > 10);
}
@Test
public void testGetCoJournalistsForArticle() throws Exception {
Person user = journalist();
when(userServer.getUserByUsername("journalist4")).thenReturn(user);
Person user2 = photographer();
when(userServer.getUserByUsername("journalist7")).thenReturn(user2);
List<Person> coJournalists = articleServer.getCoJournalistsForArticle(1);
assertTrue(coJournalists.size() == 2);
assertEquals(user.getUsername(), coJournalists.get(0).getUsername());
assertEquals(user2.getUsername(), coJournalists.get(1).getUsername());
}
@Test
public void testSetCoJournalistsForArticle() throws Exception {
Person user = newPerson(8);
when(userServer.getUserByUsername("journalist8")).thenReturn(user);
Person user2 = newPerson(9);
when(userServer.getUserByUsername("journalist9")).thenReturn(user2);
List<Person> coJournalists = articleServer.getCoJournalistsForArticle(4);
assertTrue(coJournalists.size() == 0);
articleServer.setCoJournalistsForArticle(4, Arrays.asList("journalist8", "journalist9"));
coJournalists = articleServer.getCoJournalistsForArticle(4);
assertTrue(coJournalists.size() == 2);
assertEquals(user, coJournalists.get(0));
assertEquals(user2, coJournalists.get(1));
}
@Test
public void testDuplicatedWhenManyHasAccessToArticle(){
long userId = 2;
Calendar publicationDate = new GregorianCalendar(2001, 7, 16);
Person person = newPerson(userId);
person.setUsername("journalist3");
SearchTerm searchTerm = new AndTerm(new OrTerm(new JournalistTerm(person), new PhotographerTerm(person)));
List<Article> articles = articleServer.getArticlesBySearchTerm(person, searchTerm);
assertEquals("Wrong number of articles", 1, articles.size());
}
}