Skip to content

Commit 58c79a2

Browse files
authored
Merge pull request #15 from TAMULib/sprint2-bug-fixes
Sprint2 bug fixes
2 parents 463fde1 + f865289 commit 58c79a2

5 files changed

Lines changed: 42 additions & 22 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
import org.springframework.data.domain.Sort;
1414
import org.springframework.data.domain.Sort.Direction;
1515
import org.springframework.messaging.simp.SimpMessagingTemplate;
16+
import org.springframework.transaction.annotation.Transactional;
1617
import org.springframework.web.bind.annotation.RestController;
1718

1819
import com.fasterxml.jackson.databind.JsonNode;
1920
import com.fasterxml.jackson.databind.node.ArrayNode;
2021

2122
import edu.tamu.app.model.Note;
2223
import edu.tamu.app.model.repo.NoteRepo;
24+
import edu.tamu.app.model.repo.ServiceRepo;
2325
import edu.tamu.app.model.request.FilteredPageRequest;
2426
import edu.tamu.framework.aspect.annotation.ApiCredentials;
2527
import edu.tamu.framework.aspect.annotation.ApiData;
@@ -37,6 +39,9 @@ public class NoteController {
3739

3840
@Autowired
3941
private NoteRepo noteRepo;
42+
43+
@Autowired
44+
private ServiceRepo serviceRepo;
4045

4146
@Autowired
4247
private SimpMessagingTemplate simpMessagingTemplate;
@@ -58,8 +63,9 @@ public ApiResponse getNote(@ApiVariable Long id) {
5863
@ApiValidation(business = { @ApiValidation.Business(value = CREATE), @ApiValidation.Business(value = EXISTS) })
5964
public ApiResponse create(@ApiValidatedModel Note note, @ApiCredentials Credentials credentials) {
6065
note = noteRepo.create(note, credentials);
61-
simpMessagingTemplate.convertAndSend("/channel/note", new ApiResponse(SUCCESS, noteRepo.findAll()));
62-
return new ApiResponse(SUCCESS, note);
66+
ApiResponse response = new ApiResponse(SUCCESS, note);
67+
simpMessagingTemplate.convertAndSend("/channel/note/new", response);
68+
return response;
6369
}
6470

6571
@ApiMapping("/update")
@@ -70,12 +76,13 @@ public ApiResponse update(@ApiValidatedModel Note note) {
7076
return new ApiResponse(SUCCESS, note);
7177
}
7278

79+
@Transactional
7380
@ApiMapping("/remove")
7481
@Auth(role = "ROLE_SERVICE_MANAGER")
7582
public ApiResponse remove(@ApiValidatedModel Note note) {
7683
noteRepo.delete(note);
7784
simpMessagingTemplate.convertAndSend("/channel/note", new ApiResponse(SUCCESS, noteRepo.findAll()));
78-
return new ApiResponse(SUCCESS);
85+
return new ApiResponse(SUCCESS, serviceRepo.getOne(note.getService().getId()));
7986
}
8087

8188
@ApiMapping("/page")
@@ -89,9 +96,7 @@ public ApiResponse page(@ApiData JsonNode dataNode) {
8996
}
9097

9198
Map<String, String[]> filters = new HashMap<String, String[]>();
92-
System.out.println("dataNode: " + dataNode.get("filters").get("title"));
9399
filters.put("title", arrayNodeToStringArray((ArrayNode) dataNode.get("filters").get("title")));
94-
95100
FilteredPageRequest filteredPageRequest = new FilteredPageRequest(dataNode.get("page").get("number").asInt(), dataNode.get("page").get("size").asInt(), sortDirection, dataNode.get("direction").get("properties").asText(), filters);
96101
Page<Note> notes = noteRepo.findAll(filteredPageRequest);
97102
return new ApiResponse(SUCCESS, notes);

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package edu.tamu.app.model;
22

3+
import static javax.persistence.CascadeType.DETACH;
4+
import static javax.persistence.CascadeType.PERSIST;
35
import static javax.persistence.CascadeType.REFRESH;
46
import static javax.persistence.CascadeType.REMOVE;
57
import static javax.persistence.FetchType.EAGER;
68

79
import java.util.ArrayList;
8-
import java.util.HashSet;
910
import java.util.List;
10-
import java.util.Set;
1111

1212
import javax.persistence.Column;
1313
import javax.persistence.ElementCollection;
@@ -56,19 +56,20 @@ public class Service extends BaseEntity {
5656

5757
@Column(nullable = false)
5858
private Boolean onShortList;
59-
59+
6060
@Lob
6161
@Column(nullable = true)
6262
private String description;
6363

64-
@OneToMany(fetch = EAGER, cascade = { REFRESH, REMOVE }, mappedBy = "service")
64+
@OneToMany(fetch = EAGER, cascade = { DETACH, REMOVE, PERSIST, REFRESH }, mappedBy = "service")
65+
@Fetch(FetchMode.SELECT)
6566
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, scope = Note.class, property = "id")
6667
@JsonIdentityReference(alwaysAsId = true)
67-
private Set<Note> notes;
68+
private List<Note> notes;
6869

6970
public Service() {
7071
setModelValidator(new ServiceValidator());
71-
setNotes(new HashSet<Note>());
72+
setNotes(new ArrayList<Note>());
7273
setAliases(new ArrayList<String>());
7374
}
7475

@@ -115,16 +116,18 @@ public void setServiceUrl(String serviceUrl) {
115116
this.serviceUrl = serviceUrl;
116117
}
117118

118-
public Set<Note> getNotes() {
119+
public List<Note> getNotes() {
119120
return notes;
120121
}
121122

122-
public void setNotes(Set<Note> notes) {
123+
public void setNotes(List<Note> notes) {
123124
this.notes = notes;
124125
}
125126

126127
public void addNote(Note note) {
127-
this.notes.add(note);
128+
if (!this.notes.contains(note)) {
129+
this.notes.add(note);
130+
}
128131
}
129132

130133
public void removeNote(Note note) {

src/main/java/edu/tamu/app/model/repo/impl/NoteRepoImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ public class NoteRepoImpl implements NoteRepoCustom {
3232
public Note create(Note note, Credentials credentials) {
3333
note.setAuthor(userRepo.findByUin(credentials.getUin()));
3434
note = noteRepo.save(note);
35-
Service service = note.getService();
35+
Service service = serviceRepo.findOne(note.getService().getId());
3636
service.addNote(note);
37-
serviceRepo.saveAndFlush(service);
38-
simpMessagingTemplate.convertAndSend("/channel/service/" + note.getService().getId(), new ApiResponse(SUCCESS, note.getService()));
37+
service = serviceRepo.save(service);
38+
simpMessagingTemplate.convertAndSend("/channel/service/" + note.getService().getId(), new ApiResponse(SUCCESS, service));
39+
3940
return note;
4041
}
4142
}

src/main/resources/config/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ auth.security.jwt-expiration: 120000
3535
app.security.secret: verysecretsecret
3636

3737
# Required in framework - CoreCorsFilter
38-
app.security.allow-access: http://localhost
38+
app.security.allow-access: http://localhost, http://laddusaw.tamu.edu
3939

4040
# required in framework - CoreControllerAspect
4141
app.aspect.retry: 3

src/test/java/controller/NoteControllerTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
import org.springframework.test.context.junit4.SpringRunner;
2222

2323
import edu.tamu.app.controller.NoteController;
24+
import edu.tamu.app.enums.NoteType;
25+
import edu.tamu.app.enums.Status;
2426
import edu.tamu.app.model.AppUser;
2527
import edu.tamu.app.model.Note;
28+
import edu.tamu.app.model.Service;
2629
import edu.tamu.app.model.repo.AppUserRepo;
2730
import edu.tamu.app.model.repo.NoteRepo;
31+
import edu.tamu.app.model.repo.ServiceRepo;
2832
import edu.tamu.framework.model.ApiResponse;
2933
import edu.tamu.framework.model.Credentials;
3034

@@ -39,25 +43,30 @@ public class NoteControllerTest {
3943
protected static final String TEST_NOTE_TITLE2 = "Test Note Title 2";
4044
protected static final String TEST_NOTE_TITLE3 = "Test Note Title 3";
4145
protected static final String TEST_MODIFIED_NOTE_TITLE = "Modified Note Title";
46+
protected static final String TEST_SERVICE_NAME = "Test Service";
4247

48+
protected static Service TEST_SERVICE = new Service(TEST_SERVICE_NAME, Status.UP, false, true, true, "", "");
4349
protected static Note TEST_NOTE1 = new Note(TEST_NOTE_TITLE1, TEST_USER1);
4450
protected static Note TEST_NOTE2 = new Note(TEST_NOTE_TITLE2, TEST_USER1);
4551
protected static Note TEST_NOTE3 = new Note(TEST_NOTE_TITLE3, TEST_USER1);
46-
protected static Note TEST_MODIFIED_NOTE = new Note(TEST_MODIFIED_NOTE_TITLE, TEST_USER2);
52+
protected static Note TEST_MODIFIED_NOTE = new Note(TEST_MODIFIED_NOTE_TITLE, TEST_USER2, NoteType.ISSUE, "", TEST_SERVICE);
4753
protected static List<Note> mockNoteList = new ArrayList<Note>(Arrays.asList(new Note[] { TEST_NOTE1, TEST_NOTE2, TEST_NOTE3 }));
4854

4955
protected static AppUser user = new AppUser("123456789");
5056

5157
protected static ApiResponse response;
5258

53-
@Mock
54-
protected static AppUserRepo userRepo;
55-
5659
@Mock
5760
protected static Credentials credentials;
5861

62+
@Mock
63+
protected static AppUserRepo userRepo;
64+
5965
@Mock
6066
protected NoteRepo noteRepo;
67+
68+
@Mock
69+
protected ServiceRepo serviceRepo;
6170

6271
@Mock
6372
protected SimpMessagingTemplate simpMessagingTemplate;
@@ -74,6 +83,8 @@ public void setUp() {
7483
when(noteRepo.findOne(any(Long.class))).thenReturn(TEST_NOTE1);
7584
when(noteRepo.create(any(Note.class), any(Credentials.class))).thenReturn(TEST_NOTE1);
7685
when(noteRepo.save(any(Note.class))).thenReturn(TEST_MODIFIED_NOTE);
86+
when(serviceRepo.getOne(any(Long.class))).thenReturn(TEST_SERVICE);
87+
doNothing().when(noteRepo).delete(any(Note.class));
7788
doNothing().when(noteRepo).delete(any(Note.class));
7889
}
7990

0 commit comments

Comments
 (0)