Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhrobbins committed Jan 12, 2014
1 parent d0319ae commit 8611404
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public JResponse<List<T>> list(@QueryParam("page") Integer page,
List<T> entities = null;

try {
entities = getFacade().list(page, size, sort, direction, this.getFieldsAsSet(fields));
entities = getFacade().list(page, size, sort, direction, this.getFieldsAsSet(fields));
} catch (InvalidDataAccessApiUsageException e) {
logger.error(e.getMessage(), e);
throw new GenericWebServiceException(Response.Status.BAD_REQUEST,
Expand All @@ -55,7 +55,7 @@ public JResponse<List<T>> list(@QueryParam("page") Integer page,
}

// did we get any results?
if (entities == null) {
if ((entities == null) || (entities.size() == 0)) {
throw new GenericWebServiceException(Response.Status.NOT_FOUND,
"Entities not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.WebApplicationException;
Expand All @@ -21,6 +21,7 @@
import org.robbins.flashcards.exceptions.ServiceException;
import org.robbins.flashcards.facade.TagFacade;
import org.robbins.flashcards.webservices.TagsResource;
import org.robbins.flashcards.webservices.exceptions.GenericWebServiceException;
import org.robbins.tests.BaseMockingTest;
import org.robbins.tests.UnitTest;
import org.springframework.dao.InvalidDataAccessApiUsageException;
Expand All @@ -32,29 +33,40 @@
public class AbstractGenericResourceUT extends BaseMockingTest {

@Mock
TagFacade mockTagFacade;
private TagFacade mockTagFacade;

@Mock
TagDto mockTagDto;
private TagDto mockTagDto;

TagsResource resource;
private List<TagDto> tagDtoList;

private TagsResource resource;

@Before
public void before() {
resource = new TagsResource();
tagDtoList = Arrays.asList(mockTagDto);

ReflectionTestUtils.setField(resource, "tagFacade", mockTagFacade);
}

@Test
public void list() throws ServiceException {
when(mockTagFacade.list(null, null, null, null)).thenReturn(new ArrayList<TagDto>());
when(mockTagFacade.list(null, null, null, null, null)).thenReturn(tagDtoList);

JResponse<List<TagDto>> results = resource.list(null, null, null, null, null);

verify(mockTagFacade).list(null, null, null, null, null);
assertThat(results.getEntity(), is(List.class));
}

@Test(expected =GenericWebServiceException.class)
public void list_NullResult() throws ServiceException {
when(mockTagFacade.list(null, null, null, null)).thenReturn(null);

resource.list(null, null, null, null, null);
}

@Test(expected = WebApplicationException.class)
public void listWithInvalidSortParameter() throws ServiceException {
when(mockTagFacade.list(null, null, "bad_parameter", "asc", null)).thenThrow(new InvalidDataAccessApiUsageException("error"));
Expand All @@ -64,7 +76,7 @@ public void listWithInvalidSortParameter() throws ServiceException {

@Test
public void listWithSort() throws ServiceException {
when(mockTagFacade.list(null, null, "name", "asc")).thenReturn(new ArrayList<TagDto>());
when(mockTagFacade.list(null, null, "name", "asc", null)).thenReturn(tagDtoList);

JResponse<List<TagDto>> results = resource.list(null, null, "name", "asc", null);

Expand All @@ -74,7 +86,7 @@ public void listWithSort() throws ServiceException {

@Test
public void listWithPagingAndSort() throws ServiceException {
when(mockTagFacade.list(0, 1, "name", "desc")).thenReturn(new ArrayList<TagDto>());
when(mockTagFacade.list(0, 1, "name", "desc", null)).thenReturn(tagDtoList);

JResponse<List<TagDto>> results = resource.list(0, 1, "name", "desc", null);

Expand All @@ -84,7 +96,7 @@ public void listWithPagingAndSort() throws ServiceException {

@Test
public void listWithPagingNoSort() throws ServiceException {
when(mockTagFacade.list(0, 1, null, null)).thenReturn(new ArrayList<TagDto>());
when(mockTagFacade.list(0, 1, null, null, null)).thenReturn(tagDtoList);

JResponse<List<TagDto>> results = resource.list(0, 1, null, null, null);

Expand Down Expand Up @@ -112,6 +124,14 @@ public void findOne() throws ServiceException {
verify(mockTagFacade).findOne(anyLong(), anySet());
assertThat(result, is(TagDto.class));
}

@SuppressWarnings("unchecked")
@Test(expected = GenericWebServiceException.class)
public void findOne_ReturnsNull() throws ServiceException {
when(mockTagFacade.findOne(anyLong(), anySet())).thenReturn(null);

resource.findOne(1L, null);
}

@Test
public void post() throws ServiceException {
Expand Down

0 comments on commit 8611404

Please sign in to comment.