generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SILVA-514): adding favorite function on the backend (#423)
- Loading branch information
1 parent
2391107
commit c903285
Showing
23 changed files
with
1,056 additions
and
318 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...c/main/java/ca/bc/gov/restapi/results/common/exception/UserFavoriteNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ca.bc.gov.restapi.results.common.exception; | ||
|
||
public class UserFavoriteNotFoundException extends NotFoundGenericException { | ||
|
||
public UserFavoriteNotFoundException() { | ||
super("UserFavoriteEntity"); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...nd/src/main/java/ca/bc/gov/restapi/results/postgres/endpoint/OpeningFavoriteEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ca.bc.gov.restapi.results.postgres.endpoint; | ||
|
||
import ca.bc.gov.restapi.results.postgres.service.UserOpeningService; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping(path = "/api/openings/favorites", produces = MediaType.APPLICATION_JSON_VALUE) | ||
@RequiredArgsConstructor | ||
public class OpeningFavoriteEndpoint { | ||
|
||
private final UserOpeningService userOpeningService; | ||
|
||
@GetMapping | ||
public List<Long> getFavorites() { | ||
return userOpeningService.listUserFavoriteOpenings(); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
@ResponseStatus(HttpStatus.ACCEPTED) | ||
public void addToFavorites(@PathVariable Long id) { | ||
userOpeningService.addUserFavoriteOpening(id); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void removeFromFavorites(@PathVariable Long id) { | ||
userOpeningService.removeUserFavoriteOpening(id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...a/ca/bc/gov/restapi/results/postgres/endpoint/OpeningFavoriteEndpointIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package ca.bc.gov.restapi.results.postgres.endpoint; | ||
|
||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import ca.bc.gov.restapi.results.extensions.AbstractTestContainerIntegrationTest; | ||
import ca.bc.gov.restapi.results.extensions.WithMockJwt; | ||
import ca.bc.gov.restapi.results.postgres.repository.UserOpeningRepository; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
@DisplayName("Integration Test | Favorite Openings Endpoint") | ||
@TestMethodOrder(OrderAnnotation.class) | ||
@WithMockJwt | ||
@AutoConfigureMockMvc | ||
class OpeningFavoriteEndpointIntegrationTest extends AbstractTestContainerIntegrationTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private UserOpeningRepository userOpeningRepository; | ||
|
||
@Test | ||
@Order(1) | ||
@DisplayName("No favorites to begin with") | ||
void shouldBeEmpty() throws Exception { | ||
|
||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.get("/api/openings/favorites") | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$").isEmpty()); | ||
} | ||
|
||
@Test | ||
@Order(2) | ||
@DisplayName("Should add to favorite") | ||
void shouldAddToFavorite() throws Exception { | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.put("/api/openings/favorites/{openingId}", 101) | ||
.with(csrf().asHeader()) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isAccepted()) | ||
.andExpect(content().string(StringUtils.EMPTY)); | ||
|
||
assertThat(userOpeningRepository.findAll()) | ||
.isNotNull() | ||
.isNotEmpty() | ||
.hasSize(1); | ||
} | ||
|
||
@Test | ||
@Order(3) | ||
@DisplayName("Should not add to favorite if doesn't exist") | ||
void shouldNotAddIfDoesNotExist() throws Exception { | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.put("/api/openings/favorites/{openingId}", 987) | ||
.with(csrf().asHeader()) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()) | ||
.andExpect(content().string(StringUtils.EMPTY)); | ||
//.andExpect(content().string("UserOpening record(s) not found!")); | ||
} | ||
|
||
@Test | ||
@Order(4) | ||
@DisplayName("Multiple requests to add to favorite should not fail, nor duplicate") | ||
void shouldAddToFavoriteAgain() throws Exception { | ||
shouldAddToFavorite(); | ||
} | ||
|
||
@Test | ||
@Order(5) | ||
@DisplayName("Should see list of favorites") | ||
void shouldBeAbleToSeeOpening() throws Exception { | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.get("/api/openings/favorites") | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(jsonPath("$.[0]").value(101)); | ||
} | ||
|
||
@Test | ||
@Order(6) | ||
@DisplayName("Should remove from favorite") | ||
void shouldRemoveFromFavorites() throws Exception { | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.delete("/api/openings/favorites/{openingId}", 101) | ||
.with(csrf().asHeader()) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNoContent()) | ||
.andExpect(content().string(StringUtils.EMPTY)); | ||
|
||
assertThat(userOpeningRepository.findAll()) | ||
.isNotNull() | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
@Order(7) | ||
@DisplayName("Should thrown an error if trying to remove entry that doesn't exist") | ||
void shouldThrownErrorIfNoFavoriteFound() throws Exception { | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.delete("/api/openings/favorites/{openingId}", 101) | ||
.with(csrf().asHeader()) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()) | ||
.andExpect(content().string(StringUtils.EMPTY)); | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.