Skip to content

Commit

Permalink
Changes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psenger committed Dec 6, 2015
1 parent 1cb6579 commit f5182f3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/test/java/com/sample/dao/BookDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import sun.jvm.hotspot.utilities.Assert;

import java.util.Collection;
import java.util.Date;
Expand Down
22 changes: 20 additions & 2 deletions src/test/java/com/sample/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.sample.dao;

import com.sample.dao.UserDao;
import com.sample.domain.EntityNotFoundException;
import com.sample.domain.User;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
Expand Down Expand Up @@ -95,7 +98,7 @@ public void testAddUser() throws Exception {
*/
@Test(expected = IllegalArgumentException.class)
public void testAddUserTwice() throws Exception {
dao.addUser(buildUser( "B", "[email protected]", new String[]{"B"}, "B"));
dao.addUser(buildUser("B", "[email protected]", new String[]{"B"}, "B"));
}

/**
Expand Down Expand Up @@ -128,8 +131,23 @@ public void testUpdate() throws Exception {
User update = dao.update(user);
System.out.println("update.getVersion() = " + update.getVersion());
// assertEquals( version, update.getVersion() );
assertEquals( "[email protected]", update.getEmail() );
assertEquals("[email protected]", update.getEmail());
}

/**
* Method: User getUser(String username) throws EntityNotFoundException
*/
@Test(expected = EntityNotFoundException.class)
public void testGetUserByNameEntityNotFoundException () throws Exception {
User user = dao.getUser(UUID.randomUUID().toString());
}

/**
* Method: User getUser(Long id) throws EntityNotFoundException
*/
@Test(expected = EntityNotFoundException.class)
public void testGetUserByIdEntityNotFoundException() throws Exception {
User user = dao.getUser(Long.MAX_VALUE);
}

}
32 changes: 25 additions & 7 deletions src/test/java/com/sample/endpoints/BookResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ public class BookResourceTest extends JerseyTest {
private Token AdminToken;
private Token NormalToken;

private static final User buildUser(Long id, String username, String email, String[] roles, String hashedPassword) {
private static final Book book1 = buildBook("The Sharp Sliver","Clementine Green","1234",new Date(),Genre.FANTASY);
private static final Book book2 = buildBook("Edge of Darkness","Francisco Fry","45567",new Date(),Genre.SCIFI);

private String book1Id;
private String book2Id;

private static User buildUser(Long id, String username, String email, String[] roles, String hashedPassword) {
User user = new User();
user.setEmail(email);
user.setId(id);
Expand All @@ -44,15 +50,27 @@ private static final User buildUser(Long id, String username, String email, Stri
return user;
}

private static Book buildBook( String title, String author, String isbn, Date published, Genre genre) {
Book book = new Book();
book.setTitle(title);
book.setAuthor(author);
book.setIsbn(isbn);
book.setPublished(published);
book.setGenre(genre);
return book;
}

protected javax.ws.rs.core.Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
final BookDao dao = new BookDao();
final BookDao bookDao = new BookDao();
final UserDao userDao = new UserDao();
userDao.addUser(AdminUser);
userDao.addUser(NormalUser);
book1Id = bookDao.addBook(book1).getId();
book2Id = bookDao.addBook(book2).getId();
final Key key = MacProvider.generateKey();
return new com.sample.Application(dao, userDao, key);
return new com.sample.Application(bookDao, userDao, key);
}

@Before
Expand Down Expand Up @@ -165,14 +183,14 @@ public void testAddBookWithNonAuthroizedUser() {
@Test
public void testGetBookAsNormalUser() {
getToken();
Book response = target("books").path("1").request().header("Authorization", "Bearer " + NormalToken.getAuthToken()).get(Book.class);
Book response = target("books").path(book1Id).request().header("Authorization", "Bearer " + NormalToken.getAuthToken()).get(Book.class);
assertNotNull(response);
}

@Test
public void testGetBookAsNormalAdmin() {
getToken();
Book response = target("books").path("1").request().header("Authorization", "Bearer " + AdminToken.getAuthToken()).get(Book.class);
Book response = target("books").path(book2Id).request().header("Authorization", "Bearer " + AdminToken.getAuthToken()).get(Book.class);
assertNotNull(response);
}

Expand Down Expand Up @@ -209,11 +227,11 @@ public void testGetMissingBook() {
@Test
public void testGetBookEntityTag() {
getToken();
EntityTag entityTag = target("books").path("1").request().header("Authorization", "Bearer " + NormalToken.getAuthToken())
EntityTag entityTag = target("books").path(book1Id).request().header("Authorization", "Bearer " + NormalToken.getAuthToken())
.get().getEntityTag();
assertNotNull(entityTag);

Response response = target("books").path("1").request().header("Authorization", "Bearer " + NormalToken.getAuthToken())
Response response = target("books").path(book1Id).request().header("Authorization", "Bearer " + NormalToken.getAuthToken())
.header("If-None-Match", entityTag).get();

assertEquals(304, response.getStatus());
Expand Down

0 comments on commit f5182f3

Please sign in to comment.