Skip to content

Commit 6bb6bd3

Browse files
committed
submitting feature proposal
1 parent 0dbf470 commit 6bb6bd3

10 files changed

Lines changed: 118 additions & 32 deletions

File tree

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.security.access.prepost.PreAuthorize;
88
import org.springframework.web.bind.annotation.PathVariable;
9-
import org.springframework.web.bind.annotation.RequestBody;
109
import org.springframework.web.bind.annotation.RequestMapping;
1110
import org.springframework.web.bind.annotation.RestController;
1211

1312
import com.fasterxml.jackson.core.JsonParseException;
1413
import com.fasterxml.jackson.databind.JsonMappingException;
1514

1615
import edu.tamu.app.exception.UserNotFoundException;
17-
import edu.tamu.app.model.request.FeatureRequest;
16+
import edu.tamu.app.model.FeatureProposal;
1817
import edu.tamu.app.service.ProjectService;
1918
import edu.tamu.weaver.response.ApiResponse;
19+
import edu.tamu.weaver.validation.aspect.annotation.WeaverValidatedModel;
2020

2121
@RestController
2222
@RequestMapping("/projects")
@@ -37,12 +37,10 @@ public ApiResponse getById(@PathVariable Long id) throws JsonParseException, Jso
3737
return projectService.getById(id);
3838
}
3939

40-
// TODO: the following method needs to create FeatureRequest from a FeatureProposal
41-
4240
@RequestMapping("/feature")
4341
@PreAuthorize("hasRole('SERVICE_MANAGER')")
44-
public ApiResponse submitFeatureRequest(@RequestBody FeatureRequest request) throws UserNotFoundException {
45-
return projectService.submitFeatureRequest(request);
42+
public ApiResponse submitFeatureRequest(@WeaverValidatedModel FeatureProposal proposal) throws UserNotFoundException {
43+
return projectService.submitFeatureRequest(proposal);
4644
}
4745

4846
}

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

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

3-
import static javax.persistence.CascadeType.MERGE;
43
import static javax.persistence.CascadeType.REFRESH;
54
import static javax.persistence.FetchType.EAGER;
65

@@ -31,10 +30,10 @@ public abstract class AbstractIdea extends ValidatingBaseEntity {
3130
@UpdateTimestamp
3231
private Calendar lastModified;
3332

34-
@ManyToOne(fetch = EAGER, cascade = MERGE, optional = false)
33+
@ManyToOne(fetch = EAGER, cascade = REFRESH, optional = false)
3534
private Service service;
3635

37-
@ManyToOne(cascade = REFRESH, optional = false)
36+
@ManyToOne(fetch = EAGER, cascade = REFRESH, optional = false)
3837
private User author;
3938

4039
public AbstractIdea() {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.List;
88

99
import javax.persistence.CascadeType;
10+
import javax.persistence.Column;
1011
import javax.persistence.Entity;
1112
import javax.persistence.JoinTable;
1213
import javax.persistence.ManyToMany;
@@ -16,9 +17,11 @@
1617

1718
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
1819
import com.fasterxml.jackson.annotation.JsonIdentityReference;
20+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1921
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
2022

2123
@Entity
24+
@JsonIgnoreProperties(value = { "voters" }, allowGetters = true)
2225
public class FeatureProposal extends AbstractIdea {
2326

2427
@ManyToMany(fetch = EAGER, cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE })
@@ -33,6 +36,9 @@ public class FeatureProposal extends AbstractIdea {
3336
@Fetch(value = SELECT)
3437
private List<User> voters;
3538

39+
@Column(nullable = false)
40+
private boolean submitted;
41+
3642
public FeatureProposal() {
3743
super();
3844
setup();
@@ -62,6 +68,7 @@ public FeatureProposal(Idea idea) {
6268
private void setup() {
6369
this.ideas = new ArrayList<Idea>();
6470
this.voters = new ArrayList<User>();
71+
this.submitted = false;
6572
}
6673

6774
public List<Idea> getIdeas() {
@@ -108,4 +115,12 @@ public int getVotes() {
108115
return this.voters.size();
109116
}
110117

118+
public boolean isSubmitted() {
119+
return submitted;
120+
}
121+
122+
public void setSubmitted(boolean submitted) {
123+
this.submitted = submitted;
124+
}
125+
111126
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ public Idea(String title, String description) {
2121
this.elevated = false;
2222
}
2323

24-
public Idea(String title, String description, boolean elevated) {
25-
super(title, description);
26-
this.elevated = elevated;
27-
}
28-
2924
public Idea(ServiceRequest serviceRequest) {
3025
this(serviceRequest.getTitle(), serviceRequest.getDescription());
3126
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public FeatureProposal create(FeatureProposal featureProposal, Credentials crede
4848
public FeatureProposal create(Idea idea) {
4949
idea.setElevated(true);
5050
idea = ideaRepo.save(idea);
51-
FeatureProposal featureProposal = featureProposalRepo.save(new FeatureProposal(idea));
52-
idea.setElevated(true);
53-
ideaRepo.save(idea);
51+
simpMessagingTemplate.convertAndSend("/channel/ideas/update", new ApiResponse(SUCCESS, idea));
52+
FeatureProposal featureProposal = featureProposalRepo.save(new FeatureProposal(idea));
5453
simpMessagingTemplate.convertAndSend("/channel/feature-proposals/create", new ApiResponse(SUCCESS, featureProposal));
5554
return featureProposal;
5655
}
5756

5857
@Override
5958
public FeatureProposal update(FeatureProposal featureProposal) {
59+
featureProposal = featureProposalRepo.save(featureProposal);
6060
simpMessagingTemplate.convertAndSend("/channel/feature-proposals/update", new ApiResponse(SUCCESS, featureProposal));
6161
return featureProposal;
6262
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Idea create(Idea idea, Credentials credentials) throws UserNotFoundExcept
4141

4242
@Override
4343
public Idea update(Idea idea) {
44+
idea = ideaRepo.save(idea);
4445
simpMessagingTemplate.convertAndSend("/channel/ideas/update", new ApiResponse(SUCCESS, idea));
4546
return idea;
4647
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package edu.tamu.app.model.request;
22

3+
import edu.tamu.app.model.FeatureProposal;
4+
35
public class FeatureRequest extends AbstractRequest {
46

57
private static final long serialVersionUID = -6953745180846929244L;
@@ -23,6 +25,10 @@ public FeatureRequest(RequestType type, String title, String description, Long p
2325
this.projectId = projectId;
2426
}
2527

28+
public FeatureRequest(FeatureProposal proposal) {
29+
this(RequestType.FEATURE, proposal.getTitle(), proposal.getDescription(), proposal.getService().getProjectId());
30+
}
31+
2632
public Long getProjectId() {
2733
return projectId;
2834
}

src/main/java/edu/tamu/app/service/ProjectService.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package edu.tamu.app.service;
22

3+
import static edu.tamu.weaver.response.ApiStatus.SUCCESS;
4+
35
import java.io.IOException;
46
import java.net.MalformedURLException;
57
import java.net.URL;
68

79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.messaging.simp.SimpMessagingTemplate;
912
import org.springframework.stereotype.Service;
1013
import org.springframework.web.client.RestTemplate;
1114

1215
import com.fasterxml.jackson.core.JsonParseException;
1316
import com.fasterxml.jackson.databind.JsonMappingException;
1417
import com.fasterxml.jackson.databind.ObjectMapper;
1518

19+
import edu.tamu.app.model.FeatureProposal;
20+
import edu.tamu.app.model.repo.FeatureProposalRepo;
1621
import edu.tamu.app.model.request.FeatureRequest;
1722
import edu.tamu.app.model.request.IssueRequest;
18-
import edu.tamu.weaver.response.ApiResponse;;
23+
import edu.tamu.weaver.response.ApiResponse;
24+
import edu.tamu.weaver.response.ApiStatus;;
1925

2026
@Service
2127
public class ProjectService {
@@ -29,6 +35,12 @@ public class ProjectService {
2935
@Autowired
3036
private RestTemplate restTemplate;
3137

38+
@Autowired
39+
private FeatureProposalRepo featureProposalRepo;
40+
41+
@Autowired
42+
private SimpMessagingTemplate simpMessagingTemplate;
43+
3244
public ApiResponse getAll() throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
3345
return objectMapper.readValue(new URL(projectsUrl), ApiResponse.class);
3446
}
@@ -37,8 +49,14 @@ public ApiResponse getById(Long id) throws JsonParseException, JsonMappingExcept
3749
return objectMapper.readValue(new URL(projectsUrl + "/" + id), ApiResponse.class);
3850
}
3951

40-
public ApiResponse submitFeatureRequest(FeatureRequest request) {
41-
return restTemplate.postForObject(projectsUrl + "/feature", request, ApiResponse.class);
52+
public ApiResponse submitFeatureRequest(FeatureProposal proposal) {
53+
ApiResponse response = restTemplate.postForObject(projectsUrl + "/feature", new FeatureRequest(proposal), ApiResponse.class);
54+
if (response.getMeta().getStatus().equals(ApiStatus.SUCCESS)) {
55+
proposal.setSubmitted(true);
56+
proposal = featureProposalRepo.save(proposal);
57+
simpMessagingTemplate.convertAndSend("/channel/feature-proposals/update", new ApiResponse(SUCCESS, proposal));
58+
}
59+
return response;
4260
}
4361

4462
public ApiResponse submitIssueRequest(IssueRequest request) {

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,22 @@
3131
public class FeatureProposalTest {
3232

3333
private static final String TEST_FEATURE_PROPOSAL_TITLE = "Feature Proposal Title";
34+
private static final String TEST_FEATURE_PROPOSAL_DESCRIPTION = "Test Feature Proposal Description";
35+
36+
private static final String TEST_SERVICE_NAME = "Test Service Name";
3437
private static final String TEST_SERVICE_URL = "https://library.tamu.edu";
35-
private static final String TEST_DESCRIPTION = "Test Service Description";
38+
private static final String TEST_SERVICE_DESCRIPTION = "Test Service Description";
39+
3640
private static final String TEST_ALTERNATIVE_FEATURE_PROPOSAL_TITLE = "Alternative Feature Proposal Title";
37-
private static final String TEST_SERVICE_NAME = "Test Service Name";
38-
private static final String TEST_ALTERNATIVE_SERVICE_NAME = "Different Service Name";
39-
private static final String TEST_FEATURE_PROPOSAL_DESCRIPTION = "Test Feature Proposal Description";
4041
private static final String TEST_ALTERNATIVE_FEATURE_PROPOSAL_DESCRIPTION = "Alternative Feature Proposal Description";
42+
43+
private static final String TEST_ALTERNATIVE_SERVICE_NAME = "Different Service Name";
44+
4145
private static final Boolean TEST_IS_AUTO = false;
4246
private static final Boolean TEST_IS_PUBLIC = true;
4347
private static final Boolean TEST_ON_SHORT_LIST = true;
4448
private static final Status TEST_SERVICE_STATUS = Status.UP;
49+
4550
private Service service1;
4651
private Service service2;
4752

@@ -82,8 +87,8 @@ public class FeatureProposalTest {
8287
@Before
8388
public void setUp() throws UserNotFoundException {
8489
testUser = userRepo.create(TEST_CREDENTIALS.getUin(), TEST_CREDENTIALS.getEmail(), TEST_CREDENTIALS.getFirstName(), TEST_CREDENTIALS.getLastName(), Role.valueOf(TEST_CREDENTIALS.getRole()));
85-
service1 = serviceRepo.create(new Service(TEST_SERVICE_NAME, TEST_SERVICE_STATUS, TEST_IS_AUTO, TEST_IS_PUBLIC, TEST_ON_SHORT_LIST, TEST_SERVICE_URL, TEST_DESCRIPTION));
86-
service2 = serviceRepo.create(new Service(TEST_ALTERNATIVE_SERVICE_NAME, TEST_SERVICE_STATUS, TEST_IS_AUTO, TEST_IS_PUBLIC, TEST_ON_SHORT_LIST, TEST_SERVICE_URL, TEST_DESCRIPTION));
90+
service1 = serviceRepo.create(new Service(TEST_SERVICE_NAME, TEST_SERVICE_STATUS, TEST_IS_AUTO, TEST_IS_PUBLIC, TEST_ON_SHORT_LIST, TEST_SERVICE_URL, TEST_SERVICE_DESCRIPTION));
91+
service2 = serviceRepo.create(new Service(TEST_ALTERNATIVE_SERVICE_NAME, TEST_SERVICE_STATUS, TEST_IS_AUTO, TEST_IS_PUBLIC, TEST_ON_SHORT_LIST, TEST_SERVICE_URL, TEST_SERVICE_DESCRIPTION));
8792
testFeatureProposal = featureProposalRepo.create(new FeatureProposal(TEST_FEATURE_PROPOSAL_TITLE, TEST_FEATURE_PROPOSAL_DESCRIPTION, testUser, service1), TEST_CREDENTIALS);
8893
}
8994

src/test/java/edu/tamu/app/service/ProjectServiceTest.java

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.net.MalformedURLException;
88
import java.util.List;
99

10+
import org.junit.After;
1011
import org.junit.Test;
1112
import org.junit.runner.RunWith;
1213
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,9 +22,18 @@
2122
import com.fasterxml.jackson.databind.ObjectMapper;
2223

2324
import edu.tamu.app.WebServerInit;
25+
import edu.tamu.app.enums.Role;
26+
import edu.tamu.app.enums.Status;
27+
import edu.tamu.app.exception.UserNotFoundException;
2428
import edu.tamu.app.mock.projects.MockProjects;
29+
import edu.tamu.app.model.FeatureProposal;
30+
import edu.tamu.app.model.Service;
31+
import edu.tamu.app.model.User;
32+
import edu.tamu.app.model.repo.FeatureProposalRepo;
33+
import edu.tamu.app.model.repo.ServiceRepo;
34+
import edu.tamu.app.model.repo.UserRepo;
2535
import edu.tamu.app.model.request.AbstractRequest;
26-
import edu.tamu.app.model.request.FeatureRequest;
36+
import edu.tamu.app.model.request.AbstractRequest.RequestType;
2737
import edu.tamu.app.model.request.IssueRequest;
2838
import edu.tamu.app.model.response.Project;
2939
import edu.tamu.weaver.auth.model.Credentials;
@@ -35,6 +45,26 @@
3545
@SpringBootTest(classes = { WebServerInit.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
3646
public class ProjectServiceTest {
3747

48+
private static final String TEST_SERVICE_NAME = "Test Service Name";
49+
private static final String TEST_SERVICE_URL = "https://library.tamu.edu";
50+
private static final String TEST_SERVICE_DESCRIPTION = "Test Service Description";
51+
private static final Boolean TEST_IS_AUTO = false;
52+
private static final Boolean TEST_IS_PUBLIC = true;
53+
private static final Boolean TEST_ON_SHORT_LIST = true;
54+
private static final Status TEST_SERVICE_STATUS = Status.UP;
55+
56+
private static final String TEST_FEATURE_PROPOSAL_TITLE = "Feature Proposal Title";
57+
private static final String TEST_FEATURE_PROPOSAL_DESCRIPTION = "Test Feature Proposal Description";
58+
59+
private static final Credentials TEST_CREDENTIALS = new Credentials();
60+
{
61+
TEST_CREDENTIALS.setUin("123456789");
62+
TEST_CREDENTIALS.setEmail("aggieJack@tamu.edu");
63+
TEST_CREDENTIALS.setFirstName("Aggie");
64+
TEST_CREDENTIALS.setLastName("Jack");
65+
TEST_CREDENTIALS.setRole("ROLE_USER");
66+
}
67+
3868
@Autowired
3969
private MockProjects mockReader;
4070

@@ -44,11 +74,21 @@ public class ProjectServiceTest {
4474
@Autowired
4575
private ObjectMapper objectMapper;
4676

77+
@Autowired
78+
private UserRepo userRepo;
79+
80+
@Autowired
81+
private ServiceRepo serviceRepo;
82+
83+
@Autowired
84+
private FeatureProposalRepo featureProposalRepo;
85+
4786
@Test
4887
public void getAll() throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
4988
ApiResponse response = projectService.getAll();
5089
assertEquals("Response was not a success!", ApiStatus.SUCCESS, response.getMeta().getStatus());
51-
List<Project> projects = objectMapper.convertValue(response.getPayload().get("ArrayList<Project>"), new TypeReference<List<Project>>() {});
90+
List<Project> projects = objectMapper.convertValue(response.getPayload().get("ArrayList<Project>"), new TypeReference<List<Project>>() {
91+
});
5292
List<Project> mockProjects = mockReader.getAllProjects();
5393
assertEquals("Projects response size was not as expected!", mockProjects.size(), projects.size());
5494
for (int i = 0; i < projects.size(); i++) {
@@ -71,11 +111,13 @@ public void getById() throws JsonParseException, JsonMappingException, Malformed
71111
}
72112

73113
@Test
74-
public void submitFeatureRequest() {
75-
FeatureRequest request = new FeatureRequest(AbstractRequest.RequestType.FEATURE, "Test feature request", "This is a test feature request on project 1!", 1L);
76-
ApiResponse response = projectService.submitFeatureRequest(request);
114+
public void submitFeatureRequest() throws UserNotFoundException {
115+
User testUser = userRepo.create(TEST_CREDENTIALS.getUin(), TEST_CREDENTIALS.getEmail(), TEST_CREDENTIALS.getFirstName(), TEST_CREDENTIALS.getLastName(), Role.valueOf(TEST_CREDENTIALS.getRole()));
116+
Service service = serviceRepo.create(new Service(TEST_SERVICE_NAME, TEST_SERVICE_STATUS, TEST_IS_AUTO, TEST_IS_PUBLIC, TEST_ON_SHORT_LIST, TEST_SERVICE_URL, TEST_SERVICE_DESCRIPTION));
117+
FeatureProposal newFeatureProposal = featureProposalRepo.create(new FeatureProposal(TEST_FEATURE_PROPOSAL_TITLE, TEST_FEATURE_PROPOSAL_DESCRIPTION, testUser, service), TEST_CREDENTIALS);
118+
ApiResponse response = projectService.submitFeatureRequest(newFeatureProposal);
77119
assertEquals("Response was not a success!", ApiStatus.SUCCESS, response.getMeta().getStatus());
78-
assertEquals("Response message was not correct!", "Successfully submitted " + request.getType().getName() + " request!", response.getMeta().getMessage());
120+
assertEquals("Response message was not correct!", "Successfully submitted " + RequestType.FEATURE.getName() + " request!", response.getMeta().getMessage());
79121
}
80122

81123
@Test
@@ -86,4 +128,11 @@ public void submitIssueRequest() {
86128
assertEquals("Response message was not correct!", "Successfully submitted " + request.getType().getName() + " request!", response.getMeta().getMessage());
87129
}
88130

131+
@After
132+
public void cleanUp() {
133+
featureProposalRepo.deleteAll();
134+
serviceRepo.deleteAll();
135+
userRepo.deleteAll();
136+
}
137+
89138
}

0 commit comments

Comments
 (0)