Skip to content

Commit

Permalink
- MessageService post method + test
Browse files Browse the repository at this point in the history
TODO: Fix mixed wrong spring and hibernate transitive deps to old versions by maven (for IDEA run, just remove them from project)
  • Loading branch information
Rene Gielen authored and Rene Gielen committed Apr 14, 2009
1 parent b4c7841 commit 71fbde0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/aurifa/demo/strutter/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public class Message {
String text;
User author;

public Message() {
}

public Message(User author, String text) {
this.author = author;
if (text!=null) {
this.text = text.length()>140?text.substring(0, 140):text;
}
this.sent = new Date();
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Message_SEQ")
public Long getId() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/aurifa/demo/strutter/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import java.util.List;
import java.util.ArrayList;

/**
* User.
Expand All @@ -17,7 +18,7 @@ public class User {
String alias;
String realname;
String shortBio;
List<Message> messages;
List<Message> messages = new ArrayList<Message>();

public User() {
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/aurifa/demo/strutter/service/MessageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.aurifa.demo.strutter.service;

import org.aurifa.demo.strutter.model.Message;
import org.aurifa.demo.strutter.model.User;
import org.springframework.util.Assert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageService extends GenericEntityService<Message, Long> {

@Resource
UserService userService;

protected Class<Message> entityClass() {
return Message.class;
}

public Message post ( User author, String text ) {
Assert.notNull(author, "User must be provided");

if (text!=null) {
Message m = new Message(author, text);
save(m);
author.getMessages().add(0, m);
return m;
} else {
return null;
}
}

public List<Message> getTimeline(User user) {
return null;
}

public List<Message> getPosts(User user) {
return null;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/strutter-service-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<property name="hibernateProperties">
<value>
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.show_sql=true
hibernate.connection.autocommit=false
</value>
</property>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.aurifa.demo.strutter.service;

import org.aurifa.demo.strutter.AbstractSpringEnabledTest;
import org.aurifa.demo.strutter.model.User;
import org.aurifa.demo.strutter.model.Message;
import org.junit.runner.RunWith;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static junit.framework.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback=true)
@Transactional
public class MessageServiceTest extends AbstractSpringEnabledTest {

@Resource
MessageService messageService;

@Test
public void testInjection() {
assertNotNull(messageService);
}

@Test
public void testPost() {
User u = new User("palim", null, null);
messageService.userService.save(u);
messageService.getCurrentSession().flush();

try {
messageService.post(null, "huhu");
fail("Exception expected");
} catch (Exception e) {
//expected
}

Message m = messageService.post(u, "huhu");
messageService.getCurrentSession().flush();

assertSame(u, m.getAuthor());
assertTrue(u.getMessages().contains(m));
}

@Test
public void testGetTimeline() {
// Add your code here
}

@Test
public void testGetPosts() {
// Add your code here
}
}

0 comments on commit 71fbde0

Please sign in to comment.