Skip to content

Commit

Permalink
Correcting Swagger configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariana Azevedo committed Jan 1, 2021
1 parent c5baa2b commit 4aa2bd2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: java -jar -Dspring.profiles.active=prod -Dserver.port=$PORT target/travels-java-api-5.0.0-SNAPSHOT.jar
web: java -jar -Dspring.profiles.active=prod -Dserver.port=$PORT target/travels-java-api-5.0.1-SNAPSHOT.jar
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ The API also was developed to run with an `jar`. In order to generate this `jar`
mvn package
```

It will clean, compile and generate a `jar` at target directory, e.g. `travels-java-api-5.0.0-SNAPSHOT.jar`
It will clean, compile and generate a `jar` at target directory, e.g. `travels-java-api-5.0.1-SNAPSHOT.jar`

### Execution

Expand Down Expand Up @@ -219,7 +219,7 @@ mvn integration-test
In order to run the API, run the jar simply as following:

```bash
java -jar travels-java-api-5.0.0-SNAPSHOT.jar --spring.profiles.active=dev
java -jar travels-java-api-5.0.1-SNAPSHOT.jar --spring.profiles.active=dev
```

or
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.mariazevedo88</groupId>
<artifactId>travels-java-api</artifactId>
<version>5.0.0-SNAPSHOT</version>
<version>5.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>travels-java-api</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testCreateTravelOrderNumber123456() throws ParseException {
TravelDTO dtoNsu123456 = new TravelDTO(null, "123456",
TravelsApiUtil.getLocalDateTimeFromString("2020-08-15T18:32:04.150Z"),
TravelsApiUtil.getLocalDateTimeFromString("2020-08-21T18:32:04.150Z"),
new BigDecimal(100d), TravelTypeEnum.RETURN, 1L);
new BigDecimal(100d), TravelTypeEnum.RETURN.getValue(), 1L);

final HttpHeaders headers = new HttpHeaders();
headers.set("X-api-key", "FX001-ZBSY6YSLP");
Expand All @@ -152,7 +152,7 @@ public void testCreateTravelOrderNumber258963() throws ParseException {
//id=2
TravelDTO dtoNsu258963 = new TravelDTO(null, "258963",
TravelsApiUtil.getLocalDateTimeFromString("2020-08-21T18:32:04.150Z"),
null, new BigDecimal(2546.93), TravelTypeEnum.ONE_WAY, 1L);
null, new BigDecimal(2546.93), TravelTypeEnum.ONE_WAY.getValue(), 1L);

final HttpHeaders headers = new HttpHeaders();
headers.set("X-api-key", "FX001-ZBSY6YSLP");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public TravelController(TravelService travelService) {
}

/**
* Method that creates a travel in the database.
* Method that creates travels in the database.
*
* @author Mariana Azevedo
* @since 02/04/2020
Expand All @@ -76,10 +76,11 @@ public TravelController(TravelService travelService) {
* - id - trip id;
* - orderNumber - identification number of a trip in the system;
* - amount – travel amount; a string of arbitrary length that is parsable as a BigDecimal;
* - initialDate – initial date time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the Local time zone;
* - finalDate – final date time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the Local time zone;
* - startDate – initial date time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the Local time zone;
* - endDate – final date time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the Local time zone;
* - type - trip type: RETURN (with a date to begin and end), ONE_WAY (only with initial date),
* MULTI_CITY (with multiple destinations);
* - account_id - account id of the user in the API.
*
* @param result - Bind result
*
Expand Down Expand Up @@ -146,6 +147,7 @@ public ResponseEntity<Response<TravelDTO>> create(@RequestHeader(value=TravelsAp
* - finalDate – final date time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the Local time zone;
* - type - trip type: RETURN (with a date to begin and end), ONE_WAY (only with initial date),
* MULTI_CITY (with multiple destinations);
* - account_id - account id of the user in the API.
*
* @param result - Bind result
*
Expand Down Expand Up @@ -228,12 +230,12 @@ public ResponseEntity<Response<TravelDTO>> update(@RequestHeader(value=TravelsAp
*/
@GetMapping
@ApiOperation(value = "Route to find all travels of the API in a period of time")
public ResponseEntity<Response<Page<TravelDTO>>> findAllBetweenDates(@RequestHeader(value=TravelsApiUtil.HEADER_TRAVELS_API_VERSION, defaultValue="${api.version}")
public ResponseEntity<Response<List<TravelDTO>>> findAllBetweenDates(@RequestHeader(value=TravelsApiUtil.HEADER_TRAVELS_API_VERSION, defaultValue="${api.version}")
String apiVersion, @RequestHeader(value=TravelsApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate startDate, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate, @PageableDefault(page = 1, size = 10, sort = {"id"}) Pageable pageable)
throws TravelNotFoundException {
LocalDate startDate, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
@PageableDefault(page = 1, size = 10, sort = {"id"}) Pageable pageable) throws TravelNotFoundException {

Response<Page<TravelDTO>> response = new Response<>();
Response<List<TravelDTO>> response = new Response<>();

LocalDateTime startDateTime = TravelsApiUtil.convertLocalDateToLocalDateTime(startDate);
LocalDateTime endDateTime = TravelsApiUtil.convertLocalDateToLocalDateTime(endDate);
Expand All @@ -245,7 +247,10 @@ public ResponseEntity<Response<Page<TravelDTO>>> findAllBetweenDates(@RequestHea
+ " and endDate=" + endDate);
}

Page<TravelDTO> itemsDTO = travels.map(t -> t.convertEntityToDTO());
//Page<TravelDTO> itemsDTO = travels.map(t -> t.convertEntityToDTO());
List<TravelDTO> itemsDTO = new ArrayList<>();
travels.stream().forEach(t -> itemsDTO.add(t.convertEntityToDTO()));

itemsDTO.stream().forEach(dto -> {
try {
createSelfLinkInCollections(apiVersion, apiKey, dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.LocalDateTime;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Length;
import org.modelmapper.ModelMapper;
Expand All @@ -17,7 +18,6 @@
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.zero_x_baadf00d.partialize.annotation.Partialize;

import io.github.mariazevedo88.travelsjavaapi.enumeration.TravelTypeEnum;
import io.github.mariazevedo88.travelsjavaapi.model.travel.Travel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
Expand All @@ -26,7 +26,7 @@
import lombok.Setter;

/**
* Class that implements Transaction data transfer object (DTO)
* Class that implements Travel data transfer object (DTO)
*
* @author Mariana Azevedo
* @since 01/04/2020
Expand All @@ -38,7 +38,7 @@
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Partialize(allowedFields = {"id", "orderNumber", "startDate", "endDate", "amount", "account", "type"},
defaultFields = {"orderNumber", "startDate", "endDate", "amount"})
defaultFields = {"orderNumber", "startDate", "amount"})
public class TravelDTO extends RepresentationModel<TravelDTO> {

private Long id;
Expand All @@ -62,7 +62,9 @@ public class TravelDTO extends RepresentationModel<TravelDTO> {
private BigDecimal amount;

@NotNull(message="Type cannot be null")
private TravelTypeEnum type;
@Pattern(regexp="^(ONE-WAY|RETURN|MULTI-CITY)$",
message="For the type only the values ONE-WAY, RETURN or MULTI-CITY are accepted.")
private String type;

@NotNull(message="Account Id cannot be null")
private Long accountId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class TravelsJavaAPIExceptionHandler<T> {

/**
* Method that handles with a TransactionInvalidUpdateException and returns an error with
* Method that handles with a TravelInvalidUpdateException and returns an error with
* status code = 403.
*
* @author Mariana Azevedo
Expand All @@ -35,7 +35,7 @@ public class TravelsJavaAPIExceptionHandler<T> {
* @return ResponseEntity<Response<T>>
*/
@ExceptionHandler(value = { TravelInvalidUpdateException.class })
protected ResponseEntity<Response<T>> handleTransactionInvalidUpdateException(TravelInvalidUpdateException exception) {
protected ResponseEntity<Response<T>> handleTravelInvalidUpdateException(TravelInvalidUpdateException exception) {

Response<T> response = new Response<>();
response.addErrorMsgToResponse(exception.getLocalizedMessage());
Expand All @@ -44,7 +44,7 @@ protected ResponseEntity<Response<T>> handleTransactionInvalidUpdateException(Tr
}

/**
* Method that handles with a TransactionNotFoundException and returns an error with
* Method that handles with a TravelNotFoundException and returns an error with
* status code = 404.
*
* @author Mariana Azevedo
Expand All @@ -54,7 +54,7 @@ protected ResponseEntity<Response<T>> handleTransactionInvalidUpdateException(Tr
* @return ResponseEntity<Response<T>>
*/
@ExceptionHandler(value = { TravelNotFoundException.class })
protected ResponseEntity<Response<T>> handleTransactionNotFoundException(TravelNotFoundException exception) {
protected ResponseEntity<Response<T>> handleTravelNotFoundException(TravelNotFoundException exception) {

Response<T> response = new Response<>();
response.addErrorMsgToResponse(exception.getLocalizedMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.modelmapper.ModelMapper;

Expand Down Expand Up @@ -48,18 +47,14 @@ public class Travel implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
private String orderNumber;

@NotNull
private LocalDateTime startDate;

private LocalDateTime endDate;

@NotNull
private BigDecimal amount;

@NotNull
@Enumerated(EnumType.STRING)
private TravelTypeEnum type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ protected void configure(HttpSecurity http) throws Exception {
} else {
http.csrf().disable().exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
.antMatchers("/api-travels/v1/auth/**", "/api-travels/v1/user/**", "/configuration/security", "/webjars/**",
"/v2/api-docs", "/swagger-resources/**", "/swagger-ui.html", "/manage/**")
.antMatchers("/api-travels/v1/auth/**", "/api-travels/v1/users/**", "/configuration/security", "/webjars/**",
"/v2/api-docs", "/swagger-resources/**", "/swagger-ui/**", "/manage/**")
.permitAll().anyRequest().authenticated();
http.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
http.headers().cacheControl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public void testSave() throws Exception {

mockMvc.perform(MockMvcRequestBuilders.post(URL).content(getJsonPayload(ID, ORDER_NUMBER,
TravelsApiUtil.getLocalDateTimeFromString(START_DATE.concat("Z")),
TravelsApiUtil.getLocalDateTimeFromString(END_DATE.concat("Z")), VALUE, TYPE, ACCOUNT_ID))
TravelsApiUtil.getLocalDateTimeFromString(END_DATE.concat("Z")), VALUE,
TYPE.getValue(), ACCOUNT_ID))
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
.headers(headers))
.andDo(MockMvcResultHandlers.print())
Expand Down Expand Up @@ -125,7 +126,8 @@ public void testSaveInvalidTravel() throws Exception {

mockMvc.perform(MockMvcRequestBuilders.post(URL).content(getJsonPayload(ID, null,
TravelsApiUtil.getLocalDateTimeFromString(START_DATE.concat("Z")), TravelsApiUtil.
getLocalDateTimeFromString(END_DATE.concat("Z")), VALUE, TYPE, ACCOUNT_ID))
getLocalDateTimeFromString(END_DATE.concat("Z")), VALUE, TYPE.getValue(),
ACCOUNT_ID))
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
.headers(headers))
.andExpect(status().isBadRequest())
Expand Down Expand Up @@ -167,7 +169,7 @@ private Travel getMockTravel() throws ParseException {
* @throws JsonProcessingException
*/
private String getJsonPayload(Long id, String orderNumber, LocalDateTime startDate, LocalDateTime endDate,
BigDecimal amount, TravelTypeEnum type, Long accountId) throws JsonProcessingException {
BigDecimal amount, String type, Long accountId) throws JsonProcessingException {

TravelDTO dto = new TravelDTO(id, orderNumber, startDate, endDate,
amount, type, accountId);
Expand Down

0 comments on commit 4aa2bd2

Please sign in to comment.