Skip to content

Commit 61cb719

Browse files
authored
Merge pull request #23 from TAMULib/sprint3-b03270-active-notification-endpoint
Sprint3 b03270 active notification endpoint
2 parents 761c46c + bbd8aea commit 61cb719

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
import static edu.tamu.framework.enums.BusinessValidationType.NONEXISTS;
88
import static edu.tamu.framework.enums.BusinessValidationType.UPDATE;
99

10+
import java.util.ArrayList;
11+
import java.util.List;
12+
1013
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RequestParam;
1116
import org.springframework.web.bind.annotation.RestController;
1217

18+
import edu.tamu.app.enums.NotificationLocation;
1319
import edu.tamu.app.model.Notification;
1420
import edu.tamu.app.model.repo.NotificationRepo;
1521
import edu.tamu.framework.aspect.annotation.ApiMapping;
1622
import edu.tamu.framework.aspect.annotation.ApiValidatedModel;
1723
import edu.tamu.framework.aspect.annotation.ApiValidation;
1824
import edu.tamu.framework.aspect.annotation.ApiVariable;
1925
import edu.tamu.framework.aspect.annotation.Auth;
26+
import edu.tamu.framework.aspect.annotation.SkipAop;
2027
import edu.tamu.framework.model.ApiResponse;
2128

2229
@RestController
@@ -61,4 +68,27 @@ public ApiResponse remove(@ApiValidatedModel Notification notification) {
6168
notificationRepo.delete(notification);
6269
return new ApiResponse(SUCCESS);
6370
}
71+
72+
@SkipAop
73+
@RequestMapping("/notification/active")
74+
public String getActiveNotifications(@RequestParam(value = "location", defaultValue = "ALL") String locationString) {
75+
String notificationString = "";
76+
List<Notification> notificationList;
77+
if (locationString.equals("ALL")) {
78+
notificationList = notificationRepo.findByIsActive(true);
79+
} else {
80+
try {
81+
NotificationLocation location = NotificationLocation.valueOf(locationString);
82+
notificationList = notificationRepo.findByIsActiveAndLocations(true, location);
83+
} catch (IllegalArgumentException e) {
84+
notificationList = new ArrayList<Notification>();
85+
}
86+
}
87+
for (Notification notification : notificationList) {
88+
notificationString += "<p>" + notification.getBody() + "</p>";
89+
}
90+
91+
return notificationString;
92+
}
93+
6494
}

src/main/java/edu/tamu/app/enums/NotificationLocation.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
public enum NotificationLocation {
44

5-
EVANS,
5+
MAIN,
66
CUSHING,
77
MSL,
8-
WCL
8+
WCL,
9+
PSEL,
10+
QATAR
911
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package edu.tamu.app.model.repo;
22

3+
import java.util.List;
4+
35
import org.springframework.data.jpa.repository.JpaRepository;
46

7+
import edu.tamu.app.enums.NotificationLocation;
58
import edu.tamu.app.model.Notification;
69
import edu.tamu.app.model.repo.custom.NotificationRepoCustom;
710

811
public interface NotificationRepo extends JpaRepository<Notification, Long>, NotificationRepoCustom {
912

13+
public List<Notification> findByIsActive(boolean isActive);
14+
15+
public List<Notification> findByIsActiveAndLocations(boolean isActive, NotificationLocation location);
16+
1017
public void delete(Notification notification);
1118

1219
}

src/test/java/controller/NotificationControllerTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ public class NotificationControllerTest {
4040
protected static final String TEST_NOTIFICATION2_BODY = "Test Notification Body 2";
4141
protected static final String TEST_NOTIFICATION3_BODY = "Test Notification Body 3";
4242
protected static final String TEST_MODIFIED_NOTIFICATION_BODY = "Test Modified Notification Body";
43+
protected static final String TEST_QUERY_PARAM = "CUSHING";
44+
protected static final String TEST_NOTIFICATION_TEXT = "<p>Test Notification Body 1</p><p>Test Notification Body 2</p><p>Test Notification Body 3</p>";
4345
protected static final boolean TEST_IS_ACTIVE = true;
46+
protected static final boolean TEST_ALTERNATIVE_IS_ACTIVE = false;
4447
protected static final List<NotificationLocation> TEST_LOCATIONS = Arrays.asList(new NotificationLocation[] {NotificationLocation.CUSHING});
4548

4649
protected static Notification TEST_NOTIFICATION1 = new Notification(TEST_NOTIFICATION1_NAME, TEST_NOTIFICATION1_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
4750
protected static Notification TEST_NOTIFICATION2= new Notification(TEST_NOTIFICATION2_NAME,TEST_NOTIFICATION2_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
48-
protected static Notification TEST_NOTIFICATION3 = new Notification(TEST_NOTIFICATION3_NAME, TEST_NOTIFICATION3_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
51+
protected static Notification TEST_NOTIFICATION3 = new Notification(TEST_NOTIFICATION3_NAME, TEST_NOTIFICATION3_BODY, TEST_ALTERNATIVE_IS_ACTIVE, TEST_LOCATIONS);
4952
protected static Notification TEST_MODIFIED_NOTIFICATION = new Notification(TEST_MODIFIED_NOTIFICATION_NAME, TEST_NOTIFICATION2_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
5053

5154
protected static List<Notification> mockNotificationList = new ArrayList<Notification>(Arrays.asList(new Notification[] { TEST_NOTIFICATION1, TEST_NOTIFICATION2, TEST_NOTIFICATION3 }));
@@ -69,6 +72,7 @@ public void setUp() {
6972
when(notificationRepo.findOne(any(Long.class))).thenReturn(TEST_NOTIFICATION1);
7073
when(notificationRepo.create(any(String.class), any(String.class), any(boolean.class), anyListOf(NotificationLocation.class))).thenReturn(TEST_NOTIFICATION1);
7174
when(notificationRepo.update(any(Notification.class))).thenReturn(TEST_MODIFIED_NOTIFICATION);
75+
when(notificationRepo.findByIsActiveAndLocations(any(boolean.class), any(NotificationLocation.class))).thenReturn(mockNotificationList);
7276
doNothing().when(notificationRepo).delete(any(Notification.class));
7377
}
7478

@@ -112,6 +116,12 @@ public void testRemove() {
112116
assertEquals("Not successful at removing Notification", SUCCESS, response.getMeta().getType());
113117
}
114118

119+
@Test
120+
public void testActiveNotifications() {
121+
String notifications = notificationController.getActiveNotifications(TEST_QUERY_PARAM);
122+
assertEquals("Active Notifications not returned correctly", TEST_NOTIFICATION_TEXT, notifications);
123+
}
124+
115125
@After
116126
public void cleanUp() {
117127
response = null;

0 commit comments

Comments
 (0)