Skip to content

Commit ba6e3fc

Browse files
Merge pull request #19 from UdL-EPS-SoftArch/product
Refactor ProductRepository: add method to find products by category
2 parents 88a37dc + 4737dc2 commit ba6e3fc

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

src/main/java/cat/udl/eps/softarch/demo/repository/ProductRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cat.udl.eps.softarch.demo.repository;
22

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

2729
//TODO List<Product> findByOrders(Order order);
2830
// List<Product> findByBaskets(Basket basket);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cat.udl.eps.softarch.demo.steps;
2+
3+
import cat.udl.eps.softarch.demo.domain.Product;
4+
import cat.udl.eps.softarch.demo.repository.ProductRepository;
5+
import org.springframework.http.MediaType;
6+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
10+
import io.cucumber.java.Before;
11+
import io.cucumber.java.en.And;
12+
import io.cucumber.java.en.When;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
15+
16+
17+
18+
public class ProductStepDefs {
19+
20+
@Autowired
21+
private StepDefs stepDefs;
22+
23+
public static String currentUsername;
24+
public static String currentPassword;
25+
public static Product currentProduct;
26+
27+
@Autowired
28+
private ProductRepository productRepository;
29+
30+
@Before
31+
public void setUp(){
32+
//Mirar si es bona practica ficar un step de el RegisterStepDefs
33+
}
34+
35+
@When("^I register a new product with name \"([^\"]*)\"$")
36+
public void iRegisterANewProductWithName(String name) throws Exception {
37+
currentProduct = new Product();
38+
currentProduct.setName(name);
39+
40+
stepDefs.result = stepDefs.mockMvc.perform(
41+
post("/products")
42+
.contentType(MediaType.APPLICATION_JSON)
43+
.content(stepDefs.mapper.writeValueAsString(currentProduct))
44+
.accept(MediaType.APPLICATION_JSON)
45+
.with(AuthenticationStepDefs.authenticate())
46+
).andDo(print());
47+
}
48+
49+
50+
@And("^The product with name \"([^\"]*)\" is not registered$")
51+
public void theProductWithNameIsNotRegistered(String productName) throws Throwable {
52+
stepDefs.result = stepDefs.mockMvc.perform(
53+
get("/products")
54+
.param("name", productName)
55+
.accept(MediaType.APPLICATION_JSON)
56+
.with(AuthenticationStepDefs.authenticate()))
57+
.andExpect(status().isNotFound());
58+
}
59+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Feature: Register Product
2+
In order to manage my products
3+
As a user
4+
I want to register new products
5+
6+
7+
Scenario: Register a new product successfully
8+
Given I login as "demo" with password "password"
9+
When I register a new product with name "Coffee"
10+
Then The response code is 201
11+
12+
13+
Scenario: Register a new product that already exists
14+
Given I login as "demo" with password "password"
15+
When I register a new product with name "Coffee"
16+
Then The response code is 409
17+
And The product with name "Orange" is not registered
18+
19+
20+
Scenario: Register a product when not authenticated
21+
Given I'm not logged in
22+
When I register a new product with name "Apple"
23+
Then The response code is 403
24+
And The product with name "Apple" is not registered
25+
26+
27+
Scenario: Register a product with empty name
28+
Given I login as "demo" with password "password"
29+
When I register a new product with name ""
30+
Then The response code is 400
31+
And The product with name "" is not registered
32+
33+
34+
## TODO check user roles
35+
## check db initialization records
36+
## check if ...

src/test/resources/features/RegisterUser.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Feature: Register User
33
As a user
44
I want to register myself and get an account
55

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

0 commit comments

Comments
 (0)