Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
Adresses Simone's comments about the user builder and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MariemBaccari committed Mar 9, 2023
1 parent 22e38d9 commit 2d89969
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.github.sdp.mediato.model.Review.MIN_GRADE;
import static com.github.sdp.mediato.model.User.LIMIT_LOCATION_SIZE;

import com.github.sdp.mediato.model.Location;
import com.github.sdp.mediato.model.User;
import com.github.sdp.mediato.model.media.Media;
import com.github.sdp.mediato.model.media.MediaType;
Expand All @@ -17,6 +18,11 @@
*/
public class Preconditions {

public static final double MAX_LATITUDE = 90;
public static final double MAX_LONGITUDE = 180;
public static final double MIN_LATITUDE = -90;
public static final double MIN_LONGITUDE = -180;

/**
* Checks if mandatory user fields are valid
* @param user
Expand Down Expand Up @@ -135,9 +141,18 @@ public static void checkRegisterDate(String registerDate){
* Checks if the location is valid
* @param location
*/
public static void checkLocation(List<Double> location) {
if (location == null || location.isEmpty()) throw new IllegalArgumentException("Location is unknown: list is null or empty");
if (location.size() != LIMIT_LOCATION_SIZE) throw new IllegalArgumentException("Location should contain exactly two doubles");
public static void checkLocation(Location location) {
if (location.isValid() && !locationWithinBounds(location)){
throw new IllegalArgumentException("Location mus be between -90 and 90 for latitude and -180 and 180 for longitude");
}
}

/**
* Checks if the latitude and longitude are within the authorised bounds
*/
public static boolean locationWithinBounds(Location location){
return (location.getLatitude() > MIN_LATITUDE && location.getLatitude() < MAX_LATITUDE)
&& (location.getLongitude() > MIN_LONGITUDE && location.getLongitude() < MAX_LONGITUDE);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/github/sdp/mediato/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class User {
private String birthDate = "";
private String registerDate = "";

private List<Double> location = new ArrayList<>();
private Location location = new Location();
private List<User> friends = new ArrayList<>();
private Map<String, List<Review>> reviews = new HashMap<>();
private User(){}
public User(UserBuilder builder){
private User(UserBuilder builder){
this.id = builder.id;
this.username = builder.username;
this.displayedName = builder.displayedName;
Expand Down Expand Up @@ -57,7 +57,7 @@ public String getRegisterDate() {
return registerDate;
}

public List<Double> getLocation() {
public Location getLocation() {
return location;
}

Expand All @@ -73,7 +73,7 @@ public static class UserBuilder {
private String birthDate = "";
private String registerDate = "";

private List<Double> location = new ArrayList<>();
private Location location = new Location();
private List<User> friends = new ArrayList<>();
private Map<String, List<Review>> reviews = new HashMap<>();

Expand Down Expand Up @@ -109,7 +109,7 @@ public UserBuilder setRegisterDate(String registerDate) {
this.registerDate = registerDate;
return this;
}
public UserBuilder setLocation(List<Double> location) {
public UserBuilder setLocation(Location location) {
Preconditions.checkLocation(location);
this.location = location;
return this;
Expand Down
9 changes: 6 additions & 3 deletions app/src/test/java/com/github/sdp/mediato/modelTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.junit.Assert.*;

import com.github.sdp.mediato.model.Location;
import com.github.sdp.mediato.model.User;

import java.util.ArrayList;
Expand All @@ -15,6 +16,7 @@
public class modelTests {

@Test
//Tests that the user builder registers mandatory attributes correctly
public void user_builder_registers_mandatory_attributes(){
//Build new user
User user = new User.UserBuilder("uniqueId")
Expand All @@ -23,7 +25,7 @@ public void user_builder_registers_mandatory_attributes(){
.setEmail("email")
.setRegisterDate("09/03/2023")
.setBirthDate("09/03/2023")
.setLocation(Arrays.asList(3.14, 3.14))
.setLocation(new Location(3.14, 3.14))
.build();

//Check values
Expand All @@ -33,11 +35,12 @@ public void user_builder_registers_mandatory_attributes(){
Assert.assertEquals("email", user.getEmail());
Assert.assertEquals("09/03/2023", user.getRegisterDate());
Assert.assertEquals("09/03/2023", user.getBirthDate());
assertTrue(user.getLocation().containsAll(Arrays.asList(3.14, 3.14)));
assertTrue(user.getLocation().getLatitude() == 3.14 && user.getLocation().getLongitude() ==3.14);
}

@Test
public void user_builder_fails_with_invalid_strings(){
//Checks that the user builder fails when it's missing mandatory attributes
public void user_builder_fails_with_missing_mandatory_attributes(){
assertThrows(IllegalArgumentException.class,
() -> {
//Build new user with missing attributes
Expand Down

0 comments on commit 2d89969

Please sign in to comment.