Skip to content

Commit 866e14a

Browse files
authored
Merge pull request #55 from TAMULib/sprint5-b03526-sort_services
Item B-03526: As a Manager, I wold like to sort services in the servi…
2 parents fcb1306 + ef8ead6 commit 866e14a

9 files changed

Lines changed: 123 additions & 1 deletion

File tree

src/main/java/edu/tamu/app/controller/ServiceController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import edu.tamu.app.model.Service;
1818
import edu.tamu.app.model.repo.IdeaRepo;
1919
import edu.tamu.app.model.repo.ServiceRepo;
20+
import edu.tamu.app.model.request.FilteredPageRequest;
2021
import edu.tamu.app.model.request.IssueRequest;
2122
import edu.tamu.app.model.request.ServiceRequest;
2223
import edu.tamu.app.service.ProjectService;
@@ -52,6 +53,12 @@ public ApiResponse getPublicServices() {
5253
return new ApiResponse(SUCCESS, serviceRepo.findByIsPublicOrderByStatusDescNameAsc(true));
5354
}
5455

56+
@RequestMapping("/page")
57+
@PreAuthorize("hasRole('ANONYMOUS')")
58+
public ApiResponse page(@RequestBody FilteredPageRequest filteredPageRequest) {
59+
return new ApiResponse(SUCCESS, serviceRepo.findAll(filteredPageRequest.getServiceSpecification(), filteredPageRequest.getPageRequest()));
60+
}
61+
5562
@RequestMapping("/{id}")
5663
@PreAuthorize("hasRole('ANONYMOUS')")
5764
public ApiResponse getService(@PathVariable Long id) {

src/main/java/edu/tamu/app/model/repo/ServiceRepo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.util.List;
44

5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
7+
import org.springframework.data.jpa.domain.Specification;
58
import org.springframework.data.jpa.repository.JpaRepository;
69

710
import edu.tamu.app.enums.Status;
@@ -10,6 +13,8 @@
1013

1114
public interface ServiceRepo extends JpaRepository<Service, Long>, ServiceRepoCustom {
1215

16+
public Page<Service> findAll(Specification<Service> specification, Pageable pageable);
17+
1318
public List<Service> findByIsPublicOrderByStatusDescNameAsc(Boolean isPublic);
1419

1520
public List<Service> findByIsAuto(Boolean isAuto);

src/main/java/edu/tamu/app/model/repo/specification/AbstractSpecification.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuild
4949
}
5050
}
5151

52-
query.orderBy(cb.desc(root.get("lastModified")));
52+
toPredicateDefaultQueryOrderBy(root, query, cb);
5353

5454
return builder.build(cb);
5555
}
5656

57+
/**
58+
* Allow implementing classes to control order by in case lastModified is non-existent.
59+
*/
60+
protected void toPredicateDefaultQueryOrderBy(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
61+
query.orderBy(cb.desc(root.get("lastModified")));
62+
}
63+
5764
private class PredicateBuilder {
5865

5966
private final Map<String, List<Predicate>> predicates;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package edu.tamu.app.model.repo.specification;
2+
3+
import java.util.Map;
4+
5+
import javax.persistence.criteria.CriteriaBuilder;
6+
import javax.persistence.criteria.CriteriaQuery;
7+
import javax.persistence.criteria.Root;
8+
9+
public class ServiceSpecification<E> extends AbstractSpecification<E> {
10+
11+
public ServiceSpecification(Map<String, String[]> filters) {
12+
super(filters);
13+
}
14+
15+
@Override
16+
protected void toPredicateDefaultQueryOrderBy(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
17+
query.orderBy(cb.desc(root.get("name")));
18+
}
19+
}

src/main/java/edu/tamu/app/model/request/FilteredPageRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import edu.tamu.app.model.FeatureProposal;
1414
import edu.tamu.app.model.Idea;
1515
import edu.tamu.app.model.Note;
16+
import edu.tamu.app.model.Service;
1617
import edu.tamu.app.model.repo.specification.FeatureProposalSpecification;
1718
import edu.tamu.app.model.repo.specification.IdeaSpecification;
1819
import edu.tamu.app.model.repo.specification.NoteSpecification;
20+
import edu.tamu.app.model.repo.specification.ServiceSpecification;
1921

2022
public class FilteredPageRequest {
2123

@@ -42,6 +44,11 @@ public IdeaSpecification<Idea> getIdeaSpecification() {
4244
return new IdeaSpecification<Idea>(filters);
4345
}
4446

47+
@JsonIgnore
48+
public ServiceSpecification<Service> getServiceSpecification() {
49+
return new ServiceSpecification<Service>(filters);
50+
}
51+
4552
@JsonIgnore
4653
public FeatureProposalSpecification<FeatureProposal> getFeatureProposalSpecification() {
4754
return new FeatureProposalSpecification<FeatureProposal>(filters);

src/test/java/edu/tamu/app/controller/FeatureProposalControllerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import org.mockito.InjectMocks;
1818
import org.mockito.Mock;
1919
import org.mockito.MockitoAnnotations;
20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.domain.PageImpl;
22+
import org.springframework.data.domain.Pageable;
2023
import org.springframework.messaging.simp.SimpMessagingTemplate;
2124
import org.springframework.test.context.junit4.SpringRunner;
2225

@@ -29,6 +32,8 @@
2932
import edu.tamu.app.model.repo.FeatureProposalRepo;
3033
import edu.tamu.app.model.repo.ServiceRepo;
3134
import edu.tamu.app.model.repo.UserRepo;
35+
import edu.tamu.app.model.repo.specification.FeatureProposalSpecification;
36+
import edu.tamu.app.model.request.FilteredPageRequest;
3237
import edu.tamu.weaver.auth.model.Credentials;
3338
import edu.tamu.weaver.response.ApiResponse;
3439

@@ -54,6 +59,7 @@ public class FeatureProposalControllerTest {
5459
private static FeatureProposal TEST_FEATURE_PROPOSAL3 = new FeatureProposal(TEST_FEATURE_PROPOSAL_TITLE3, TEST_FEATURE_PROPOSAL_DESCRIPTION3, TEST_USER1);
5560
private static FeatureProposal TEST_MODIFIED_FEATURE_PROPOSAL = new FeatureProposal(TEST_MODIFIED_FEATURE_PROPOSAL_TITLE, TEST_MODIFIED_FEATURE_PROPOSAL_DESCRIPTION, TEST_USER2, TEST_SERVICE);
5661
private static List<FeatureProposal> mockFeatureProposalList = new ArrayList<FeatureProposal>(Arrays.asList(new FeatureProposal[] { TEST_FEATURE_PROPOSAL1, TEST_FEATURE_PROPOSAL2, TEST_FEATURE_PROPOSAL3 }));
62+
private static Page<FeatureProposal> mockPageableFeatureProposalList = new PageImpl<FeatureProposal>(Arrays.asList(new FeatureProposal[] { TEST_FEATURE_PROPOSAL1, TEST_FEATURE_PROPOSAL2, TEST_FEATURE_PROPOSAL3 }));
5763

5864
private static User user = new User("123456789");
5965

@@ -78,11 +84,13 @@ public class FeatureProposalControllerTest {
7884
private FeatureProposalController featureProposalController;
7985

8086
@Before
87+
@SuppressWarnings("unchecked")
8188
public void setup() throws UserNotFoundException {
8289
MockitoAnnotations.initMocks(this);
8390
when(credentials.getUin()).thenReturn("123456789");
8491
when(userRepo.findByUsername(any(String.class))).thenReturn(Optional.of(user));
8592
when(featureProposalRepo.findAll()).thenReturn(mockFeatureProposalList);
93+
when(featureProposalRepo.findAll(any(FeatureProposalSpecification.class), any(Pageable.class))).thenReturn(mockPageableFeatureProposalList);
8694
when(featureProposalRepo.findOne(any(Long.class))).thenReturn(TEST_FEATURE_PROPOSAL1);
8795
when(featureProposalRepo.create(any(FeatureProposal.class), any(Credentials.class))).thenReturn(TEST_FEATURE_PROPOSAL1);
8896
when(featureProposalRepo.create(any(Idea.class))).thenReturn(TEST_FEATURE_PROPOSAL1);
@@ -92,6 +100,17 @@ public void setup() throws UserNotFoundException {
92100
doNothing().when(featureProposalRepo).delete(any(FeatureProposal.class));
93101
}
94102

103+
@Test
104+
@SuppressWarnings("unchecked")
105+
public void testPage() {
106+
FilteredPageRequest mockFilter = new FilteredPageRequest();
107+
response = featureProposalController.getAllFeatureProposalsByService(mockFilter);
108+
assertEquals("Not successful at getting paged FeatureProposals", SUCCESS, response.getMeta().getStatus());
109+
110+
Page<FeatureProposal> page = (Page<FeatureProposal>) response.getPayload().get("PageImpl");
111+
assertEquals("The paged list of FeatureProposals is the wrong length", mockPageableFeatureProposalList.getSize(), page.getSize());
112+
}
113+
95114
@Test
96115
public void testFeatureProposal() {
97116
response = featureProposalController.getFeatureProposal(TEST_FEATURE_PROPOSAL1.getId());

src/test/java/edu/tamu/app/controller/IdeaControllerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import org.mockito.InjectMocks;
1818
import org.mockito.Mock;
1919
import org.mockito.MockitoAnnotations;
20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.domain.PageImpl;
22+
import org.springframework.data.domain.Pageable;
2023
import org.springframework.messaging.simp.SimpMessagingTemplate;
2124
import org.springframework.test.context.junit4.SpringRunner;
2225

@@ -28,6 +31,8 @@
2831
import edu.tamu.app.model.repo.IdeaRepo;
2932
import edu.tamu.app.model.repo.ServiceRepo;
3033
import edu.tamu.app.model.repo.UserRepo;
34+
import edu.tamu.app.model.repo.specification.IdeaSpecification;
35+
import edu.tamu.app.model.request.FilteredPageRequest;
3136
import edu.tamu.weaver.auth.model.Credentials;
3237
import edu.tamu.weaver.response.ApiResponse;
3338

@@ -53,6 +58,7 @@ public class IdeaControllerTest {
5358
private static Idea TEST_IDEA3 = new Idea(TEST_IDEA_TITLE3, TEST_IDEA_DESCRIPTION3, TEST_USER1);
5459
private static Idea TEST_MODIFIED_IDEA = new Idea(TEST_MODIFIED_IDEA_TITLE, TEST_MODIFIED_IDEA_DESCRIPTION, TEST_USER2, TEST_SERVICE);
5560
private static List<Idea> mockIdeaList = new ArrayList<Idea>(Arrays.asList(new Idea[] { TEST_IDEA1, TEST_IDEA2, TEST_IDEA3 }));
61+
private static Page<Idea> mockPageableIdeaList = new PageImpl<Idea>(Arrays.asList(new Idea[] { TEST_IDEA1, TEST_IDEA2, TEST_IDEA3 }));
5662

5763
private static User user = new User("123456789");
5864

@@ -77,11 +83,13 @@ public class IdeaControllerTest {
7783
private IdeaController ideaController;
7884

7985
@Before
86+
@SuppressWarnings("unchecked")
8087
public void setup() throws UserNotFoundException {
8188
MockitoAnnotations.initMocks(this);
8289
when(credentials.getUin()).thenReturn("123456789");
8390
when(userRepo.findByUsername(any(String.class))).thenReturn(Optional.of(user));
8491
when(ideaRepo.findAll()).thenReturn(mockIdeaList);
92+
when(ideaRepo.findAll(any(IdeaSpecification.class), any(Pageable.class))).thenReturn(mockPageableIdeaList);
8593
when(ideaRepo.findOne(any(Long.class))).thenReturn(TEST_IDEA1);
8694
when(ideaRepo.create(any(Idea.class), any(Credentials.class))).thenReturn(TEST_IDEA1);
8795
when(ideaRepo.update(any(Idea.class))).thenReturn(TEST_MODIFIED_IDEA);
@@ -90,6 +98,17 @@ public void setup() throws UserNotFoundException {
9098
doNothing().when(ideaRepo).delete(any(Idea.class));
9199
}
92100

101+
@Test
102+
@SuppressWarnings("unchecked")
103+
public void testPage() {
104+
FilteredPageRequest mockFilter = new FilteredPageRequest();
105+
response = ideaController.page(mockFilter);
106+
assertEquals("Not successful at getting paged Ideas", SUCCESS, response.getMeta().getStatus());
107+
108+
Page<Idea> page = (Page<Idea>) response.getPayload().get("PageImpl");
109+
assertEquals("The paged list of Ideas is the wrong length", mockPageableIdeaList.getSize(), page.getSize());
110+
}
111+
93112
@Test
94113
public void testIdea() {
95114
response = ideaController.getById(TEST_IDEA1.getId());

src/test/java/edu/tamu/app/controller/NoteControllerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import org.mockito.InjectMocks;
1818
import org.mockito.Mock;
1919
import org.mockito.MockitoAnnotations;
20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.domain.PageImpl;
22+
import org.springframework.data.domain.Pageable;
2023
import org.springframework.messaging.simp.SimpMessagingTemplate;
2124
import org.springframework.test.context.junit4.SpringRunner;
2225

@@ -29,6 +32,8 @@
2932
import edu.tamu.app.model.repo.NoteRepo;
3033
import edu.tamu.app.model.repo.ServiceRepo;
3134
import edu.tamu.app.model.repo.UserRepo;
35+
import edu.tamu.app.model.repo.specification.NoteSpecification;
36+
import edu.tamu.app.model.request.FilteredPageRequest;
3237
import edu.tamu.weaver.auth.model.Credentials;
3338
import edu.tamu.weaver.response.ApiResponse;
3439

@@ -51,6 +56,7 @@ public class NoteControllerTest {
5156
private static Note TEST_NOTE3 = new Note(TEST_NOTE_TITLE3, TEST_USER1);
5257
private static Note TEST_MODIFIED_NOTE = new Note(TEST_MODIFIED_NOTE_TITLE, TEST_USER2, NoteType.ISSUE, "", TEST_SERVICE);
5358
private static List<Note> mockNoteList = new ArrayList<Note>(Arrays.asList(new Note[] { TEST_NOTE1, TEST_NOTE2, TEST_NOTE3 }));
59+
private static Page<Note> mockPageableNoteList = new PageImpl<Note>(Arrays.asList(new Note[] { TEST_NOTE1, TEST_NOTE2, TEST_NOTE3 }));
5460

5561
private static User user = new User("123456789");
5662

@@ -75,11 +81,13 @@ public class NoteControllerTest {
7581
private NoteController noteController;
7682

7783
@Before
84+
@SuppressWarnings("unchecked")
7885
public void setup() throws UserNotFoundException {
7986
MockitoAnnotations.initMocks(this);
8087
when(credentials.getUin()).thenReturn("123456789");
8188
when(userRepo.findByUsername(any(String.class))).thenReturn(Optional.of(user));
8289
when(noteRepo.findAll()).thenReturn(mockNoteList);
90+
when(noteRepo.findAll(any(NoteSpecification.class), any(Pageable.class))).thenReturn(mockPageableNoteList);
8391
when(noteRepo.findOne(any(Long.class))).thenReturn(TEST_NOTE1);
8492
when(noteRepo.create(any(Note.class), any(Credentials.class))).thenReturn(TEST_NOTE1);
8593
when(noteRepo.update(any(Note.class))).thenReturn(TEST_MODIFIED_NOTE);
@@ -88,6 +96,17 @@ public void setup() throws UserNotFoundException {
8896
doNothing().when(noteRepo).delete(any(Note.class));
8997
}
9098

99+
@Test
100+
@SuppressWarnings("unchecked")
101+
public void testPage() {
102+
FilteredPageRequest mockFilter = new FilteredPageRequest();
103+
response = noteController.page(mockFilter);
104+
assertEquals("Not successful at getting paged Notes", SUCCESS, response.getMeta().getStatus());
105+
106+
Page<Note> page = (Page<Note>) response.getPayload().get("PageImpl");
107+
assertEquals("The paged list of Notes is the wrong length", mockPageableNoteList.getSize(), page.getSize());
108+
}
109+
91110
@Test
92111
public void testNote() {
93112
response = noteController.getById(TEST_NOTE1.getId());

src/test/java/edu/tamu/app/controller/ServiceControllerTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.mockito.InjectMocks;
1919
import org.mockito.Mock;
2020
import org.mockito.MockitoAnnotations;
21+
import org.springframework.data.domain.Page;
22+
import org.springframework.data.domain.PageImpl;
23+
import org.springframework.data.domain.Pageable;
2124
import org.springframework.messaging.simp.SimpMessagingTemplate;
2225
import org.springframework.test.context.junit4.SpringRunner;
2326

@@ -30,7 +33,9 @@
3033
import edu.tamu.app.model.repo.IdeaRepo;
3134
import edu.tamu.app.model.repo.ServiceRepo;
3235
import edu.tamu.app.model.repo.UserRepo;
36+
import edu.tamu.app.model.repo.specification.ServiceSpecification;
3337
import edu.tamu.app.model.request.AbstractRequest;
38+
import edu.tamu.app.model.request.FilteredPageRequest;
3439
import edu.tamu.app.model.request.IssueRequest;
3540
import edu.tamu.app.model.request.ServiceRequest;
3641
import edu.tamu.app.service.ProjectService;
@@ -57,6 +62,7 @@ public class ServiceControllerTest {
5762
private static final Service TEST_SERVICE3 = new Service(TEST_SERVICE3_NAME, TEST_SERVICE_STATUS, TEST_IS_AUTO, TEST_IS_PUBLIC, TEST_NOT_ON_SHORT_LIST, "", "");
5863
private static final Service TEST_MODIFIED_SERVICE1 = new Service(TEST_SERVICE1_NAME, TEST_SERVICE_STATUS, TEST_IS_AUTO, TEST_IS_NOT_PUBLIC, TEST_NOT_ON_SHORT_LIST, "", "");
5964
private static final List<Service> mockServiceList = new ArrayList<Service>(Arrays.asList(new Service[] { TEST_SERVICE1, TEST_SERVICE2, TEST_SERVICE3 }));
65+
private static final Page<Service> mockPageableServiceList = new PageImpl<Service>(Arrays.asList(new Service[] { TEST_SERVICE1, TEST_SERVICE2, TEST_SERVICE3 }));
6066
private static final List<Service> mockPublicServiceList = new ArrayList<Service>(Arrays.asList(new Service[] { TEST_SERVICE1, TEST_SERVICE3 }));
6167

6268
private static final User TEST_SERVICE = new User("123456789");
@@ -90,11 +96,14 @@ public class ServiceControllerTest {
9096
private ServiceController serviceController;
9197

9298
@Before
99+
@SuppressWarnings("unchecked")
93100
public void setup() throws UserNotFoundException {
94101
MockitoAnnotations.initMocks(this);
95102
when(credentials.getUin()).thenReturn("123456789");
96103
when(userRepo.findByUsername(any(String.class))).thenReturn(Optional.of(TEST_SERVICE));
97104
when(systemMonitorService.getOverallStatus()).thenReturn(new OverallStatus(edu.tamu.app.enums.OverallMessageType.SUCCESS, "Success"));
105+
when(serviceRepo.findAll()).thenReturn(mockServiceList);
106+
when(serviceRepo.findAll(any(ServiceSpecification.class), any(Pageable.class))).thenReturn(mockPageableServiceList);
98107
when(serviceRepo.findAllByOrderByStatusDescNameAsc()).thenReturn(mockServiceList);
99108
when(serviceRepo.findByIsPublicOrderByStatusDescNameAsc(true)).thenReturn(mockPublicServiceList);
100109
when(serviceRepo.findOne(any(Long.class))).thenReturn(TEST_SERVICE1);
@@ -133,6 +142,17 @@ private int countPublicServices(List<Service> list) {
133142
return count;
134143
}
135144

145+
@Test
146+
@SuppressWarnings("unchecked")
147+
public void testPage() {
148+
FilteredPageRequest mockFilter = new FilteredPageRequest();
149+
response = serviceController.page(mockFilter);
150+
assertEquals("Not successful at getting paged Services", SUCCESS, response.getMeta().getStatus());
151+
152+
Page<Service> page = (Page<Service>) response.getPayload().get("PageImpl");
153+
assertEquals("The paged list of Services is the wrong length", mockPageableServiceList.getSize(), page.getSize());
154+
}
155+
136156
@Test
137157
public void testService() {
138158
response = serviceController.getService(TEST_SERVICE1.getId());

0 commit comments

Comments
 (0)