Skip to content

Commit

Permalink
✨ Updates destination model to include attributes
Browse files Browse the repository at this point in the history
- photo2
- meta
- description
  • Loading branch information
logikasciuro committed Aug 3, 2023
1 parent 236da07 commit 3c60e9e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,27 @@ public class Destination {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String photo;
private String photo;
private String photo2;
private String meta;
private String description;
private BigDecimal Price;

public Destination(@Valid DestinationCreateRecord record) {
this.name = record.name();
this.photo = record.photo();
this.Price = record.price();
this.photo2 = record.photo2();
this.Price = record.price();
this.meta = record.meta();
this.description = record.description();
}

public void update(@Valid DestinationUpdateRecord record) {
this.name = record.name();
this.photo = record.photo();
this.photo2 = record.photo2();
this.meta = record.meta();
this.description = record.description();
this.Price = record.price();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record DestinationCreateRecord(
@NotBlank
String name,
@NotBlank
String photo,
@NotBlank
String photo2,
@NotBlank
@Size(max = 160)
String meta,
String description,
@DecimalMin(value = "0.0", inclusive = false)
@Digits(integer = 5, fraction=2)
BigDecimal price
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ public record DestinationDetailRecord(
Long id,
String name,
String photo,
String photo2,
String meta,
String description,
BigDecimal price
) {
public DestinationDetailRecord(Destination destination) {
this(
destination.getId(),
destination.getName(),
destination.getPhoto(),
destination.getPrice()
);
this(destination.getId(), destination.getName(), destination.getPhoto(), destination.getPhoto2(), destination.getMeta(), destination.getDescription(), destination.getPrice());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record DestinationUpdateRecord(
@NotBlank
String name,
@NotBlank
String photo,
@NotBlank
String photo2,
@NotBlank
@Size(max = 160)
String meta,
String description,
@DecimalMin(value = "0.0", inclusive = false)
@Digits(integer = 5, fraction=2)
BigDecimal price
) {

}

5 changes: 4 additions & 1 deletion src/test/java/ecureuill/milhasapi/GenerateData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public static Destination randomDestination(){
return new Destination(
faker.random().nextLong(),
faker.address().cityName(),
faker.internet().url(),
faker.internet().url(),
faker.internet().url(),
faker.lorem().characters(1, 160, true, true, true),
faker.lorem().paragraph(2),
new BigDecimal(200)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.notNull;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Collections;
Expand Down Expand Up @@ -54,10 +55,10 @@ public class DestinationControllerUnitTest {
@DisplayName("Should return DestinationDetailed object when body is valid")
void testSaveStatusCodeCreated() throws Exception {
var data = GenerateData.randomDestination();
var record = new DestinationCreateRecord(data.getName(), data.getPhoto(), data.getPrice());
var record = new DestinationCreateRecord(data.getName(), data.getPhoto(), data.getPhoto2(), data.getMeta(), data.getDescription(), data.getPrice());
var json = destinationCreateRecord.write(record).getJson();

var responseJson = destinationDetailRecord.write( new DestinationDetailRecord(data.getId(), data.getName(), data.getPhoto(), data.getPrice())).getJson();
var responseJson = destinationDetailRecord.write( new DestinationDetailRecord(data.getId(), data.getName(), data.getPhoto(), data.getPhoto2(), data.getMeta(), data.getDescription(), data.getPrice())).getJson();

Mockito.when(repository.save(any())).thenReturn(data);

Expand All @@ -72,7 +73,7 @@ var record = new DestinationCreateRecord(data.getName(), data.getPhoto(), data.g
@DisplayName("Should return status CREATED when body is valid")
void testSaveContent() throws Exception {
var data = GenerateData.randomDestination();
var record = new DestinationCreateRecord(data.getName(), data.getPhoto(), data.getPrice());
var record = new DestinationCreateRecord(data.getName(), data.getPhoto(), data.getPhoto2(), data.getMeta(), data.getDescription(), data.getPrice());
var json = destinationCreateRecord.write(record).getJson();

Mockito.when(repository.save(any())).thenReturn(data);
Expand All @@ -87,7 +88,7 @@ var record = new DestinationCreateRecord(data.getName(), data.getPhoto(), data.g
@Test
@DisplayName("Should return status BADREQUEST when body is invalid")
void testSaveStatusBadRequest() throws Exception {
var record = new DestinationCreateRecord(null, null, null);
var record = new DestinationCreateRecord(null, null, null, null, null, null);
var json = destinationCreateRecord.write(record).getJson();

mockMvc.perform(
Expand Down Expand Up @@ -173,7 +174,7 @@ void testGetOneStatusNotFound() throws Exception {
void testUpdateStatusOK()throws Exception {

var data = GenerateData.randomDestination();
var record = new DestinationUpdateRecord(data.getName(), data.getPhoto(), data.getPrice());
var record = new DestinationUpdateRecord(data.getName(), data.getPhoto(), data.getPhoto2(), data.getMeta(), data.getDescription(), data.getPrice());
var json = destinationUpdateRecord.write(record).getJson();

Mockito.when(repository.getReferenceById(data.getId())).thenReturn(data);
Expand All @@ -189,7 +190,7 @@ var record = new DestinationUpdateRecord(data.getName(), data.getPhoto(), data.g
void testUpdateStatusNotFound()throws Exception {

var data = GenerateData.randomDestination();
var record = new DestinationUpdateRecord(data.getName(), data.getPhoto(), data.getPrice());
var record = new DestinationUpdateRecord(data.getName(), data.getPhoto(), data.getPhoto2(), data.getMeta(), data.getDescription(), data.getPrice());
var json = destinationUpdateRecord.write(record).getJson();

Mockito.when(repository.getReferenceById(data.getId())).thenThrow(EntityNotFoundException.class);
Expand Down

0 comments on commit 3c60e9e

Please sign in to comment.