Skip to content

Commit 7aea9f1

Browse files
authored
Merge pull request #21 from TAMULib/sprint3-b03241-CRUD-for-notifications
Sprint3 b03241 crud for notifications
2 parents 6d5f63b + b5f8873 commit 7aea9f1

9 files changed

Lines changed: 132 additions & 43 deletions

File tree

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static edu.tamu.framework.enums.BusinessValidationType.UPDATE;
99

1010
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.messaging.simp.SimpMessagingTemplate;
1211
import org.springframework.web.bind.annotation.RestController;
1312

1413
import edu.tamu.app.model.Notification;
@@ -27,16 +26,13 @@ public class NotificationController {
2726
@Autowired
2827
private NotificationRepo notificationRepo;
2928

30-
@Autowired
31-
private SimpMessagingTemplate simpMessagingTemplate;
32-
3329
@ApiMapping("/all")
3430
@Auth(role="ROLE_STAFF")
3531
public ApiResponse getAllNotifications() {
3632
return new ApiResponse(SUCCESS, notificationRepo.findAll());
3733
}
3834

39-
@ApiMapping("/get/{id}")
35+
@ApiMapping("/{id}")
4036
@Auth(role="ROLE_STAFF")
4137
public ApiResponse getNotification(@ApiVariable Long id) {
4238
return new ApiResponse(SUCCESS, notificationRepo.findOne(id));
@@ -46,17 +42,15 @@ public ApiResponse getNotification(@ApiVariable Long id) {
4642
@Auth(role="ROLE_WEB_MANAGER")
4743
@ApiValidation(business = { @ApiValidation.Business(value = CREATE), @ApiValidation.Business(value = EXISTS) })
4844
public ApiResponse create(@ApiValidatedModel Notification notification) {
49-
notification = notificationRepo.create(notification.getName(), notification.getBody());
50-
simpMessagingTemplate.convertAndSend("/channel/notification/", new ApiResponse(SUCCESS, notification));
45+
notification = notificationRepo.create(notification.getName(), notification.getBody(), notification.getIsActive(), notification.getLocations());
5146
return new ApiResponse(SUCCESS, notification);
5247
}
5348

5449
@ApiMapping("/update")
5550
@Auth(role="ROLE_WEB_MANAGER")
5651
@ApiValidation(business = { @ApiValidation.Business(value = UPDATE), @ApiValidation.Business(value = NONEXISTS) })
5752
public ApiResponse update(@ApiValidatedModel Notification notification) {
58-
notification = notificationRepo.save(notification);
59-
simpMessagingTemplate.convertAndSend("/channel/notification/", new ApiResponse(SUCCESS, notification));
53+
notification = notificationRepo.update(notification);
6054
return new ApiResponse(SUCCESS, notification);
6155
}
6256

@@ -65,7 +59,6 @@ public ApiResponse update(@ApiValidatedModel Notification notification) {
6559
@ApiValidation(business = { @ApiValidation.Business(value = DELETE), @ApiValidation.Business(value = NONEXISTS) })
6660
public ApiResponse remove(@ApiValidatedModel Notification notification) {
6761
notificationRepo.delete(notification);
68-
simpMessagingTemplate.convertAndSend("/channel/notification/", new ApiResponse(SUCCESS, notificationRepo.findAll()));
6962
return new ApiResponse(SUCCESS);
7063
}
7164
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package edu.tamu.app.enums;
2+
3+
public enum NotificationLocation {
4+
5+
EVANS,
6+
CUSHING,
7+
MSL,
8+
WCL
9+
}

src/main/java/edu/tamu/app/model/Notification.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
package edu.tamu.app.model;
22

3+
import static javax.persistence.FetchType.EAGER;
4+
5+
import java.util.List;
6+
37
import javax.persistence.Column;
8+
import javax.persistence.ElementCollection;
49
import javax.persistence.Entity;
5-
import javax.validation.constraints.Size;
610

11+
import edu.tamu.app.enums.NotificationLocation;
12+
import edu.tamu.app.model.validation.NotificationValidator;
713
import edu.tamu.framework.model.BaseEntity;
814

915
@Entity
1016
public class Notification extends BaseEntity {
1117

12-
@Size(min = 1)
1318
@Column(nullable = false)
1419
private String name;
1520

16-
@Size(min = 1)
1721
@Column(nullable = false)
1822
private String body;
1923

20-
public Notification() {}
24+
@Column(nullable = false)
25+
private boolean isActive;
26+
27+
@ElementCollection(fetch = EAGER)
28+
private List<NotificationLocation> locations;
2129

22-
public Notification(String name, String body) {
30+
public Notification() {
31+
setModelValidator(new NotificationValidator());
32+
}
33+
34+
public Notification(String name, String body, boolean isActive, List<NotificationLocation> locations) {
2335
this();
2436
setName(name);
2537
setBody(body);
38+
setIsActive(isActive);
39+
setLocations(locations);
2640
}
2741

2842
public String getName() {
@@ -40,4 +54,20 @@ public String getBody() {
4054
public void setBody(String body) {
4155
this.body = body;
4256
}
57+
58+
public Boolean getIsActive() {
59+
return isActive;
60+
}
61+
62+
public void setIsActive(Boolean isActive) {
63+
this.isActive = isActive;
64+
}
65+
66+
public List<NotificationLocation> getLocations() {
67+
return locations;
68+
}
69+
70+
public void setLocations(List<NotificationLocation> locations) {
71+
this.locations = locations;
72+
}
4373
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
import edu.tamu.app.model.repo.custom.NotificationRepoCustom;
77

88
public interface NotificationRepo extends JpaRepository<Notification, Long>, NotificationRepoCustom {
9+
10+
public void delete(Notification notification);
911

1012
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package edu.tamu.app.model.repo.custom;
22

3+
import java.util.List;
4+
5+
import edu.tamu.app.enums.NotificationLocation;
36
import edu.tamu.app.model.Notification;
47

58
public interface NotificationRepoCustom {
69

7-
public Notification create(String name, String body);
10+
public Notification create(String name, String body, boolean isActive, List<NotificationLocation> locations);
11+
12+
public Notification update(Notification notification);
13+
14+
public void delete(Notification notification);
815

916
}
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
package edu.tamu.app.model.repo.impl;
22

3+
import static edu.tamu.framework.enums.ApiResponseType.SUCCESS;
4+
5+
import java.util.List;
6+
37
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.messaging.simp.SimpMessagingTemplate;
49

10+
import edu.tamu.app.enums.NotificationLocation;
511
import edu.tamu.app.model.Notification;
612
import edu.tamu.app.model.repo.NotificationRepo;
713
import edu.tamu.app.model.repo.custom.NotificationRepoCustom;
14+
import edu.tamu.framework.model.ApiResponse;
815

916
public class NotificationRepoImpl implements NotificationRepoCustom {
1017

1118
@Autowired
12-
NotificationRepo notificationRepo;
19+
private NotificationRepo notificationRepo;
20+
21+
@Autowired
22+
private SimpMessagingTemplate simpMessagingTemplate;
1323

1424
@Override
15-
public Notification create(String name, String body) {
16-
return notificationRepo.save(new Notification(name, body));
25+
public Notification create(String name, String body, boolean isActive, List<NotificationLocation> locations) {
26+
Notification notification = notificationRepo.save(new Notification(name, body, isActive, locations));
27+
simpMessagingTemplate.convertAndSend("/channel/notification/create", new ApiResponse(SUCCESS, notification));
28+
return notification;
29+
}
30+
31+
@Override
32+
public Notification update(Notification notification) {
33+
notification = notificationRepo.save(notification);
34+
simpMessagingTemplate.convertAndSend("/channel/notification/update", new ApiResponse(SUCCESS, notification));
35+
return notification;
36+
}
37+
38+
@Override
39+
public void delete(Notification notification) {
40+
notificationRepo.delete(notification.getId());
41+
simpMessagingTemplate.convertAndSend("/channel/notification/delete", new ApiResponse(SUCCESS, notification.getId()));
1742
}
1843
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package edu.tamu.app.model.validation;
2+
3+
import edu.tamu.framework.enums.InputValidationType;
4+
import edu.tamu.framework.validation.BaseModelValidator;
5+
import edu.tamu.framework.validation.InputValidator;
6+
7+
public class NotificationValidator extends BaseModelValidator {
8+
9+
public NotificationValidator() {
10+
String nameProperty = "name";
11+
this.addInputValidator(new InputValidator(InputValidationType.required, "Notifications require a name", nameProperty, true));
12+
this.addInputValidator(new InputValidator(InputValidationType.minlength, "Notification name must be at least 3 characters", nameProperty, 3));
13+
14+
String bodyProperty = "body";
15+
this.addInputValidator(new InputValidator(InputValidationType.required, "Notifications require a body", bodyProperty, true));
16+
this.addInputValidator(new InputValidator(InputValidationType.minlength, "Notification body must be at least 3 characters", bodyProperty, 3));
17+
this.addInputValidator(new InputValidator(InputValidationType.maxlength, "Notifications must be no more than 140 cahracters", bodyProperty, 140));
18+
19+
String isActiveProperty = "isActive";
20+
this.addInputValidator(new InputValidator(InputValidationType.required, "Notifcations must have a value for is active", isActiveProperty, true));
21+
22+
String locationsProperty = "locations";
23+
this.addInputValidator(new InputValidator(InputValidationType.required, "Notifications must have a display location", locationsProperty, true));
24+
}
25+
}

src/test/java/controller/NotificationControllerTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static edu.tamu.framework.enums.ApiResponseType.SUCCESS;
44
import static org.junit.Assert.assertEquals;
55
import static org.mockito.Matchers.any;
6+
import static org.mockito.Matchers.anyListOf;
67
import static org.mockito.Mockito.doNothing;
78
import static org.mockito.Mockito.when;
89

@@ -22,6 +23,7 @@
2223
import org.springframework.test.context.junit4.SpringRunner;
2324

2425
import edu.tamu.app.controller.NotificationController;
26+
import edu.tamu.app.enums.NotificationLocation;
2527
import edu.tamu.app.model.Notification;
2628
import edu.tamu.app.model.repo.NotificationRepo;
2729
import edu.tamu.framework.model.ApiResponse;
@@ -38,11 +40,13 @@ public class NotificationControllerTest {
3840
protected static final String TEST_NOTIFICATION2_BODY = "Test Notification Body 2";
3941
protected static final String TEST_NOTIFICATION3_BODY = "Test Notification Body 3";
4042
protected static final String TEST_MODIFIED_NOTIFICATION_BODY = "Test Modified Notification Body";
43+
protected static final boolean TEST_IS_ACTIVE = true;
44+
protected static final List<NotificationLocation> TEST_LOCATIONS = Arrays.asList(new NotificationLocation[] {NotificationLocation.CUSHING});
4145

42-
protected static Notification TEST_NOTIFICATION1 = new Notification(TEST_NOTIFICATION1_NAME, TEST_NOTIFICATION1_BODY);
43-
protected static Notification TEST_NOTIFICATION2= new Notification(TEST_NOTIFICATION2_NAME,TEST_NOTIFICATION2_BODY);
44-
protected static Notification TEST_NOTIFICATION3 = new Notification(TEST_NOTIFICATION3_NAME, TEST_NOTIFICATION3_BODY);
45-
protected static Notification TEST_MODIFIED_NOTIFICATION = new Notification(TEST_MODIFIED_NOTIFICATION_NAME, TEST_NOTIFICATION2_BODY);
46+
protected static Notification TEST_NOTIFICATION1 = new Notification(TEST_NOTIFICATION1_NAME, TEST_NOTIFICATION1_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
47+
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);
49+
protected static Notification TEST_MODIFIED_NOTIFICATION = new Notification(TEST_MODIFIED_NOTIFICATION_NAME, TEST_NOTIFICATION2_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
4650

4751
protected static List<Notification> mockNotificationList = new ArrayList<Notification>(Arrays.asList(new Notification[] { TEST_NOTIFICATION1, TEST_NOTIFICATION2, TEST_NOTIFICATION3 }));
4852

@@ -63,8 +67,8 @@ public void setUp() {
6367

6468
when(notificationRepo.findAll()).thenReturn(mockNotificationList);
6569
when(notificationRepo.findOne(any(Long.class))).thenReturn(TEST_NOTIFICATION1);
66-
when(notificationRepo.create(any(String.class), any(String.class))).thenReturn(TEST_NOTIFICATION1);
67-
when(notificationRepo.save(any(Notification.class))).thenReturn(TEST_MODIFIED_NOTIFICATION);
70+
when(notificationRepo.create(any(String.class), any(String.class), any(boolean.class), anyListOf(NotificationLocation.class))).thenReturn(TEST_NOTIFICATION1);
71+
when(notificationRepo.update(any(Notification.class))).thenReturn(TEST_MODIFIED_NOTIFICATION);
6872
doNothing().when(notificationRepo).delete(any(Notification.class));
6973
}
7074

src/test/java/edu/tamu/app/model/NotificationTest.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import static org.junit.Assert.assertEquals;
44

5-
import javax.validation.ConstraintViolationException;
5+
import java.util.Arrays;
6+
import java.util.List;
67

78
import org.junit.After;
89
import org.junit.Test;
@@ -14,6 +15,7 @@
1415
import org.springframework.test.context.web.WebAppConfiguration;
1516

1617
import edu.tamu.app.WebServerInit;
18+
import edu.tamu.app.enums.NotificationLocation;
1719
import edu.tamu.app.model.repo.NotificationRepo;
1820

1921
@WebAppConfiguration
@@ -25,40 +27,32 @@ public class NotificationTest {
2527
protected static final String TEST_NOTIFICATION_BODY = "Test Notification Body";
2628
protected static final String TEST_ALTERNATE_NOTIFICATION_NAME = "Different Notification Name";
2729
protected static final String TEST_ALTERNATE_NOTIFICATION_BODY = "Different Notification Body";
30+
protected static final boolean TEST_IS_ACTIVE = true;
31+
protected static final List<NotificationLocation> TEST_LOCATIONS = Arrays.asList(new NotificationLocation[] {NotificationLocation.CUSHING});
2832

2933
@Autowired
3034
NotificationRepo notificationRepo;
3135

3236
@Test
3337
public void testCreate() {
3438
long initialCount = notificationRepo.count();
35-
notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY);
39+
notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
3640
assertEquals("The number of Notifications did not increase by one", initialCount + 1, notificationRepo.count());
3741
}
3842

3943
@Test(expected = DataIntegrityViolationException.class)
4044
public void testNameNotNull() {
41-
notificationRepo.create(null, TEST_NOTIFICATION_BODY);
45+
notificationRepo.create(null, TEST_NOTIFICATION_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
4246
}
4347

4448
@Test(expected = DataIntegrityViolationException.class)
4549
public void testBodyNotNull() {
46-
notificationRepo.create(TEST_NOTIFICATION_NAME, null);
47-
}
48-
49-
@Test(expected = ConstraintViolationException.class)
50-
public void testNameNotEmpty() {
51-
notificationRepo.create("", TEST_NOTIFICATION_BODY);
52-
}
53-
54-
@Test(expected = ConstraintViolationException.class)
55-
public void testBodyNotEmpty() {
56-
notificationRepo.create(TEST_NOTIFICATION_NAME, "");
50+
notificationRepo.create(TEST_NOTIFICATION_NAME, null, TEST_IS_ACTIVE, TEST_LOCATIONS);
5751
}
5852

5953
@Test
6054
public void testUpdateName() {
61-
Notification notification = notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY);
55+
Notification notification = notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
6256
notification.setName(TEST_ALTERNATE_NOTIFICATION_NAME);
6357
notificationRepo.save(notification);
6458
notification = notificationRepo.findOne(notification.getId());
@@ -67,17 +61,17 @@ public void testUpdateName() {
6761

6862
@Test
6963
public void testUpdateBody() {
70-
Notification notification = notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY);
64+
Notification notification = notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
7165
notification.setBody(TEST_ALTERNATE_NOTIFICATION_BODY);
72-
notificationRepo.save(notification);
66+
notificationRepo.update(notification);
7367
notification = notificationRepo.findOne(notification.getId());
7468
assertEquals("Notification body was not changed", TEST_ALTERNATE_NOTIFICATION_BODY, notification.getBody());
7569
}
7670

7771
@Test
7872
public void testDelete() {
7973
long initialCount = notificationRepo.count();
80-
Notification notification = notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY);
74+
Notification notification = notificationRepo.create(TEST_NOTIFICATION_NAME, TEST_NOTIFICATION_BODY, TEST_IS_ACTIVE, TEST_LOCATIONS);
8175
assertEquals("Notification not created", initialCount + 1, notificationRepo.count());
8276
notificationRepo.delete(notification);
8377
assertEquals("Notification was not deleted", initialCount, notificationRepo.count());

0 commit comments

Comments
 (0)