Skip to content

Commit

Permalink
Merge pull request #30 from UdL-EPS-SoftArch/location-scenarios
Browse files Browse the repository at this point in the history
Location Features
  • Loading branch information
jorgechp authored Apr 6, 2024
2 parents bb83a13 + a82b1ce commit a47e9e5
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@EqualsAndHashCode(callSuper = false)
public class Location {
@OneToOne
@NotNull
/*@NotNull*/
@JsonIdentityReference(alwaysAsId = true)
private Shelter shelter;

Expand All @@ -24,10 +24,8 @@ public class Location {
@NotBlank
private String address;

@NotNull
private Float latitude;

@NotNull
private Float longitude;

@NotBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import cat.udl.eps.softarch.demo.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;
import java.util.Optional;

@RepositoryRestResource
public interface LocationRepository extends CrudRepository<Location, Long>, PagingAndSortingRepository<Location, Long> {
Location findLocationByAddressAndProvinceAndCityAndPostalCode(@Param("address") String address, @Param("province") String province, @Param("city") String city, @Param("postalCode") Integer postalCode);


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Location;
import cat.udl.eps.softarch.demo.repository.LocationRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;


public class CreateLocationStepsDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private LocationRepository locationRepository;

public static String newResourceUri;

@When("There is already a Location with the following details:$")
public void thereIsAlreadyALocationWithTheFollowingDetails(Map<String, String> locationDetails) throws Throwable{
Location location = new Location();
location.setAddress(locationDetails.get("address"));
location.setProvince(locationDetails.get("province"));
location.setCity(locationDetails.get("city"));
location.setPostalCode(Integer.parseInt(locationDetails.get("postalCode")));
locationRepository.save(location);
}
@When("I create a new Location with the following details:$")
public void iCreateANewLocationWithTheFollowingDetails(Map<String, String> locationDetails) throws Throwable {
Location existingLocation = locationRepository.findLocationByAddressAndProvinceAndCityAndPostalCode(
locationDetails.get("address"),
locationDetails.get("province"),
locationDetails.get("city"),
Integer.parseInt(locationDetails.get("postalCode"))
);

if (existingLocation == null) {

Location location = new Location();
location.setAddress(locationDetails.get("address"));
location.setProvince(locationDetails.get("province"));
location.setCity(locationDetails.get("city"));
location.setPostalCode(Integer.parseInt(locationDetails.get("postalCode")));

// Verify and assign latitude and longitude if exists
if (locationDetails.containsKey("longitude") && locationDetails.get("longitude") != null) {
location.setLongitude(Float.parseFloat(locationDetails.get("longitude")));
}


if (locationDetails.containsKey("latitude") && locationDetails.get("latitude") != null) {
location.setLatitude(Float.parseFloat(locationDetails.get("latitude")));
}

stepDefs.result = stepDefs.mockMvc.perform(
post("/locations")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(location))
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
newResourceUri = stepDefs.result.andReturn().getResponse().getHeader("Location");

} else {
assertThat(existingLocation).isNotNull();
}
}



@And("There is (\\d+) Location created$")
public void thereIsLocationCreated(int locationCreatedNum) {
Assert.assertEquals(locationCreatedNum, locationRepository.count());
}

@And("There is only (\\d+) Location with the details:$")
public void thereIsOnlyLocationWithTheDeatils(int locationCreatedNum, Map<String, String> locationDetails) {
Assert.assertEquals(locationCreatedNum, locationRepository.count());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cat.udl.eps.softarch.demo.steps;


import cat.udl.eps.softarch.demo.domain.Location;
import cat.udl.eps.softarch.demo.repository.LocationRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;


public class DeleteLocationStepsDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private LocationRepository locationRepository;

public static String newResourceUri;
@When("I delete the location with the details:$")
public void iDeleteTheLocationWithTheDetails(Map<String, String> locationDetails) throws Throwable{
Location existingLocation = locationRepository.findLocationByAddressAndProvinceAndCityAndPostalCode(
locationDetails.get("address"),
locationDetails.get("province"),
locationDetails.get("city"),
Integer.parseInt(locationDetails.get("postalCode"))
);

stepDefs.result = stepDefs.mockMvc.perform(
delete("/locations/{id}", (existingLocation != null) ? existingLocation.getId() : "999")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(existingLocation))
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}

@And("There is (\\d+) Location with the details:$")
public void thereIsOnlyLocationWithTheDeatils(int locationNum, Map<String, String> locationDetails) {
Assert.assertEquals(locationNum, locationRepository.count());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cat.udl.eps.softarch.demo.steps;


import cat.udl.eps.softarch.demo.domain.Location;
import cat.udl.eps.softarch.demo.repository.LocationRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;


public class GetLocationStepsDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private LocationRepository locationRepository;

public static String newResourceUri;
@When("I retrieve the location with the details:$")
public void iRetrieveTheLocationWithTheDetails(Map<String, String> locationDetails) throws Throwable{
Location existingLocation = locationRepository.findLocationByAddressAndProvinceAndCityAndPostalCode(
locationDetails.get("address"),
locationDetails.get("province"),
locationDetails.get("city"),
Integer.parseInt(locationDetails.get("postalCode"))
);

stepDefs.result = stepDefs.mockMvc.perform(
get("/locations/{id}", (existingLocation != null) ? existingLocation.getId() : "999")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(existingLocation))
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}

}
72 changes: 72 additions & 0 deletions src/test/resources/features/CreateLocation.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Feature: Create Location
In order to efficiently manage locations
I want to be able to create a new Location record in the system

Background:
Given There is a registered user with username "username" and password "password" and email "[email protected]"

Scenario: Create a new Location with valid address, latitude, longitude, province, city, and postal code
Given I can login with username "username" and password "password"
When I create a new Location with the following details:
| address | Major Street 3 |
| latitude | 40.7128 |
| longitude | -74.0060 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Then The response code is 201
And There is 1 Location created

Scenario: Create a Location with an existing address, city, province, and postal code
Given I can login with username "username" and password "password"
And There is already a Location with the following details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
When I create a new Location with the following details:
| address | Major Street 3 |
| latitude | 41.6167 |
| longitude | 0.6222 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Then The response code is 200
And There is only 1 Location with the details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |

Scenario: Attempt to create a Location with address blank and latitude null
Given I can login with username "username" and password "password"
When I create a new Location with the following details:
| address | |
| longitude | -74.0060 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Then The response code is 400
And There is 0 Location created

Scenario: Attempt to create a Location with longitude null and latitude null
Given I can login with username "username" and password "password"
When I create a new Location with the following details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Then The response code is 201
And There is 1 Location created

Scenario: Attempt to create a Location without being logged in
Given I'm not logged in
When I create a new Location with the following details:
| address | Major Street 3 |
| latitude | 40.7128 |
| longitude | -74.0060 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Then The response code is 401
And There is 0 Location created
41 changes: 41 additions & 0 deletions src/test/resources/features/DeleteLocation.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Feature: Delete Location
In order to delete an existing Location
As a user with appropriate permissions
I want to be able to delete the Location details of a shelter

Background:
Given There is a registered user with username "username" and password "password" and email "[email protected]"
And There is already a Location with the following details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |


Scenario: Delete an existing Location details
Given I can login with username "username" and password "password"
When I delete the location with the details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Then The response code is 200
And There is 0 Location with the details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |

Scenario: Delete Location when I am not logged in
Given I'm not logged in
When I delete the location with the details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Then The response code is 401
And There is 1 Location with the details:
| address | Major Street 3 |
| province | Lleida |
| city | Seròs |
| postalCode | 25183 |
Loading

0 comments on commit a47e9e5

Please sign in to comment.