Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cat.udl.eps.softarch.demo.repository;

import cat.udl.eps.softarch.demo.domain.Category;
import cat.udl.eps.softarch.demo.domain.Product;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
Expand All @@ -23,6 +24,7 @@ public interface ProductRepository extends CrudRepository<Product, Long>, Paging
List<Product> findByKcalLessThanEqual(int kcal);
List<Product> findByIngredientsContaining(String ingredient);
List<Product> findByAllergensContaining(String allergen);
List<Product> findByCategory(Category category);

//TODO List<Product> findByOrders(Order order);
// List<Product> findByBaskets(Basket basket);
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/cat/udl/eps/softarch/demo/steps/ProductStepDefs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Product;
import cat.udl.eps.softarch.demo.repository.ProductRepository;
import org.springframework.http.MediaType;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import io.cucumber.java.Before;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;




public class ProductStepDefs {

@Autowired
private StepDefs stepDefs;

public static String currentUsername;
public static String currentPassword;
public static Product currentProduct;

@Autowired
private ProductRepository productRepository;

@Before
public void setUp(){
//Mirar si es bona practica ficar un step de el RegisterStepDefs
}

@When("^I register a new product with name \"([^\"]*)\"$")
public void iRegisterANewProductWithName(String name) throws Exception {
currentProduct = new Product();
currentProduct.setName(name);

stepDefs.result = stepDefs.mockMvc.perform(
post("/products")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(currentProduct))
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate())
).andDo(print());
}


@And("^The product with name \"([^\"]*)\" is not registered$")
public void theProductWithNameIsNotRegistered(String productName) throws Throwable {
stepDefs.result = stepDefs.mockMvc.perform(
get("/products")
.param("name", productName)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andExpect(status().isNotFound());
}
}
36 changes: 36 additions & 0 deletions src/test/resources/features/RegisterProduct.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Feature: Register Product
In order to manage my products
As a user
I want to register new products


Scenario: Register a new product successfully
Given I login as "demo" with password "password"
When I register a new product with name "Coffee"
Then The response code is 201


Scenario: Register a new product that already exists
Given I login as "demo" with password "password"
When I register a new product with name "Coffee"
Then The response code is 409
And The product with name "Orange" is not registered


Scenario: Register a product when not authenticated
Given I'm not logged in
When I register a new product with name "Apple"
Then The response code is 403
And The product with name "Apple" is not registered


Scenario: Register a product with empty name
Given I login as "demo" with password "password"
When I register a new product with name ""
Then The response code is 400
And The product with name "" is not registered


## TODO check user roles
## check db initialization records
## check if ...
1 change: 1 addition & 0 deletions src/test/resources/features/RegisterUser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Feature: Register User
As a user
I want to register myself and get an account

Scenario: Register succesfully
Given There is no registered user with username "user"
And I'm not logged in
When I register a new user with username "user", email "user@sample.app" and password "password"
Expand Down