Skip to content

Commit 8259dd1

Browse files
authored
Merge branch 'main' into customer
2 parents 4003775 + aedf143 commit 8259dd1

14 files changed

Lines changed: 278 additions & 60 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ build/
3030

3131
### VS Code ###
3232
.vscode/
33+
34+
### Claude ###
35+
.CLAUDE.md

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ class Loyalty {
6161
accumulatedPoints
6262
}
6363
class Inventory { }
64-
class Category { }
64+
class Category {
65+
name
66+
}
6567
class Product {
6668
id
6769
name

pom.xml

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<dependency>
7878
<groupId>org.projectlombok</groupId>
7979
<artifactId>lombok</artifactId>
80-
<optional>true</optional>
80+
<scope>provided</scope>
8181
</dependency>
8282
<dependency>
8383
<groupId>org.springframework.boot</groupId>
@@ -89,12 +89,12 @@
8989
<artifactId>spring-security-test</artifactId>
9090
<scope>test</scope>
9191
</dependency>
92-
<dependency>
93-
<groupId>io.cucumber</groupId>
94-
<artifactId>cucumber-junit</artifactId>
95-
<version>7.19.0</version>
96-
<scope>test</scope>
97-
</dependency>
92+
<dependency>
93+
<groupId>io.cucumber</groupId>
94+
<artifactId>cucumber-junit-platform-engine</artifactId>
95+
<version>7.19.0</version>
96+
<scope>test</scope>
97+
</dependency>
9898
<dependency>
9999
<groupId>io.cucumber</groupId>
100100
<artifactId>cucumber-java</artifactId>
@@ -107,17 +107,6 @@
107107
<version>7.19.0</version>
108108
<scope>test</scope>
109109
</dependency>
110-
<dependency>
111-
<groupId>org.junit.vintage</groupId>
112-
<artifactId>junit-vintage-engine</artifactId>
113-
<scope>test</scope>
114-
<exclusions>
115-
<exclusion>
116-
<groupId>org.hamcrest</groupId>
117-
<artifactId>hamcrest-core</artifactId>
118-
</exclusion>
119-
</exclusions>
120-
</dependency>
121110
</dependencies>
122111

123112
<build>
@@ -126,6 +115,28 @@
126115
<groupId>org.springframework.boot</groupId>
127116
<artifactId>spring-boot-maven-plugin</artifactId>
128117
</plugin>
118+
<plugin>
119+
<groupId>org.apache.maven.plugins</groupId>
120+
<artifactId>maven-surefire-plugin</artifactId>
121+
<version>3.2.5</version>
122+
<configuration>
123+
<useModulePath>false</useModulePath>
124+
</configuration>
125+
</plugin>
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-compiler-plugin</artifactId>
129+
<version>3.14.0</version>
130+
<configuration>
131+
<annotationProcessorPaths>
132+
<path>
133+
<groupId>org.projectlombok</groupId>
134+
<artifactId>lombok</artifactId>
135+
<version>1.18.34</version>
136+
</path>
137+
</annotationProcessorPaths>
138+
</configuration>
139+
</plugin>
129140
</plugins>
130141
</build>
131142

src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
3030
.requestMatchers(HttpMethod.GET, "/identity").authenticated()
3131
.requestMatchers(HttpMethod.POST, "/users").anonymous()
3232
.requestMatchers(HttpMethod.POST, "/users/*").denyAll()
33-
.requestMatchers(HttpMethod.POST, "/*/*").authenticated()
33+
.requestMatchers(HttpMethod.POST, "/*").authenticated()
3434
.requestMatchers(HttpMethod.PUT, "/*/*").authenticated()
3535
.requestMatchers(HttpMethod.PATCH, "/*/*").authenticated()
3636
.requestMatchers(HttpMethod.DELETE, "/*/*").authenticated()
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package cat.udl.eps.softarch.demo.domain;
22

3-
import jakarta.persistence.Entity;
4-
import jakarta.persistence.GeneratedValue;
5-
import jakarta.persistence.GenerationType;
6-
import jakarta.persistence.Id;
7-
import jakarta.validation.constraints.NotEmpty;
3+
import jakarta.persistence.*;
4+
import jakarta.validation.constraints.NotBlank;
85
import lombok.*;
6+
import org.hibernate.validator.constraints.Length;
97

108
@EqualsAndHashCode(callSuper = true)
119
@Entity(name = "Category")
@@ -18,7 +16,14 @@ public class Category extends UriEntity<Long> {
1816
@GeneratedValue(strategy = GenerationType.IDENTITY)
1917
private Long id;
2018

21-
22-
@NotEmpty
19+
@NotBlank
20+
@Length(min = 1, max = 50)
21+
@Column(unique = true)
2322
private String name;
23+
24+
@NotBlank
25+
@Length(min = 1, max = 255)
26+
@Column(length = 255)
27+
private String description;
28+
2429
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cat.udl.eps.softarch.demo.repository;
2+
3+
4+
import cat.udl.eps.softarch.demo.domain.Category;
5+
import org.springframework.data.repository.CrudRepository;
6+
import org.springframework.data.repository.PagingAndSortingRepository;
7+
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
8+
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
@RepositoryRestResource
13+
public interface CategoryRepository extends CrudRepository<Category, Long>, PagingAndSortingRepository<Category, Long> {
14+
15+
Optional<Category> findByName(String name);
16+
List<Category> findByDescription(String description);
17+
18+
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package cat.udl.eps.softarch.demo;
22

3-
import io.cucumber.junit.Cucumber;
4-
import io.cucumber.junit.CucumberOptions;
5-
import org.junit.runner.RunWith;
3+
import io.cucumber.junit.platform.engine.Cucumber;
64

7-
@RunWith(Cucumber.class)
8-
@CucumberOptions(plugin={"pretty"}, features="src/test/resources")
5+
@Cucumber
96
public class CucumberTest {
107

118
}

src/test/java/cat/udl/eps/softarch/demo/steps/AuthenticationStepDefs.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,16 @@ public void iLoginAsWithPassword(String username, String password) {
3333
public void iMNotLoggedIn() {
3434
currentUsername = currentPassword = null;
3535
}
36+
37+
@Given("^I'm logged in as admin$")
38+
public void iMLoggedInAsAdmin() {
39+
AuthenticationStepDefs.currentUsername = "admin";
40+
AuthenticationStepDefs.currentPassword = "password";
41+
}
42+
43+
@Given("^I'm logged in as regular user$")
44+
public void iMLoggedInAsRegularUser() {
45+
AuthenticationStepDefs.currentUsername = "user";
46+
AuthenticationStepDefs.currentPassword = "password";
47+
}
3648
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package cat.udl.eps.softarch.demo.steps;
2+
3+
import cat.udl.eps.softarch.demo.domain.Category;
4+
import cat.udl.eps.softarch.demo.repository.CategoryRepository;
5+
import io.cucumber.java.en.And;
6+
import io.cucumber.java.en.Given;
7+
import io.cucumber.java.en.When;
8+
import static org.junit.jupiter.api.Assertions.*;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.MediaType;
11+
12+
import java.util.Optional;
13+
14+
import static org.hamcrest.Matchers.is;
15+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20+
21+
public class CategoryStepDefs {
22+
23+
@Autowired
24+
private StepDefs stepDefs;
25+
26+
@Autowired
27+
private CategoryRepository categoryRepository;
28+
29+
private static Category currentCategory;
30+
31+
@Given("^There is no registered category with name \"([^\"]*)\"$")
32+
public void thereIsNoRegisteredCategoryWithName(String name) {
33+
assertFalse(categoryRepository.findByName(name).isPresent(),
34+
"Category \"" + name + "\" shouldn't exist");
35+
}
36+
37+
@Given("^There is a registered category with name \"([^\"]*)\" and description \"([^\"]*)\"$")
38+
public void thereIsARegisteredCategoryWithNameAndDescription(String name, String description) {
39+
if (!categoryRepository.findByName(name).isPresent()) {
40+
Category category = new Category();
41+
category.setName(name);
42+
category.setDescription(description);
43+
categoryRepository.save(category);
44+
}
45+
}
46+
47+
@When("^I register a new category with name \"([^\"]*)\" and description \"([^\"]*)\"$")
48+
public void iRegisterANewCategoryWithNameAndDescription(String name, String description) throws Exception {
49+
currentCategory = new Category();
50+
currentCategory.setName(name);
51+
currentCategory.setDescription(description);
52+
53+
stepDefs.result = stepDefs.mockMvc.perform(
54+
post("/categories")
55+
.contentType(MediaType.APPLICATION_JSON)
56+
.content(stepDefs.mapper.writeValueAsString(currentCategory))
57+
.accept(MediaType.APPLICATION_JSON)
58+
.with(AuthenticationStepDefs.authenticate())
59+
).andDo(print());
60+
}
61+
62+
@And("^It has been created a category with name \"([^\"]*)\" and description \"([^\"]*)\"$")
63+
public void itHasBeenCreatedACategoryWithNameAndDescription(String name, String description) throws Exception {
64+
stepDefs.result = stepDefs.mockMvc.perform(
65+
get("/categories/search/findByName")
66+
.param("name", name)
67+
.accept(MediaType.APPLICATION_JSON)
68+
.with(AuthenticationStepDefs.authenticate()))
69+
.andDo(print())
70+
.andExpect(status().isOk())
71+
.andExpect(jsonPath("$.name", is(name)))
72+
.andExpect(jsonPath("$.description", is(description)));
73+
}
74+
75+
@And("^I can retrieve the category with name \"([^\"]*)\"$")
76+
public void iCanRetrieveTheCategoryWithName(String name) throws Exception {
77+
stepDefs.result = stepDefs.mockMvc.perform(
78+
get("/categories/search/findByName")
79+
.param("name", name)
80+
.accept(MediaType.APPLICATION_JSON)
81+
.with(AuthenticationStepDefs.authenticate()))
82+
.andDo(print())
83+
.andExpect(status().isOk())
84+
.andExpect(jsonPath("$.name", is(name)));
85+
}
86+
87+
@And("^The category description remains \"([^\"]*)\"$")
88+
public void theCategoryDescriptionRemains(String description) throws Exception {
89+
// After a 409 conflict, we need to fetch the existing category to verify its description
90+
Optional<Category> existingCategory = categoryRepository.findByName(currentCategory.getName());
91+
assertTrue(existingCategory.isPresent(), "Category should exist");
92+
assertEquals(description, existingCategory.get().getDescription(),
93+
"Description should remain unchanged");
94+
}
95+
96+
@And("^It has not been created a category with name \"([^\"]*)\"$")
97+
public void itHasNotBeenCreatedACategoryWithName(String name) throws Exception {
98+
stepDefs.result = stepDefs.mockMvc.perform(
99+
get("/categories/search/findByName")
100+
.param("name", name)
101+
.accept(MediaType.APPLICATION_JSON)
102+
.with(AuthenticationStepDefs.authenticate()))
103+
.andExpect(status().isNotFound());
104+
}
105+
}

src/test/java/cat/udl/eps/softarch/demo/steps/RegisterStepDefs.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cat.udl.eps.softarch.demo.steps;
22

33
import static org.hamcrest.Matchers.is;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
56
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
67
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@@ -13,7 +14,7 @@
1314
import io.cucumber.java.en.Given;
1415
import io.cucumber.java.en.When;
1516
import org.json.JSONObject;
16-
import org.junit.Assert;
17+
import org.junit.jupiter.api.Assertions.*;
1718
import org.springframework.beans.factory.annotation.Autowired;
1819
import org.springframework.http.MediaType;
1920

@@ -29,9 +30,7 @@ public class RegisterStepDefs {
2930

3031
@Given("^There is no registered user with username \"([^\"]*)\"$")
3132
public void thereIsNoRegisteredUserWithUsername(String user) {
32-
Assert.assertFalse("User \""
33-
+ user + "\"shouldn't exist",
34-
userRepository.existsById(user));
33+
assertFalse(userRepository.existsById(user), "User \"" + user + "\"shouldn't exist");
3534
}
3635

3736
@Given("^There is a registered user with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$")

0 commit comments

Comments
 (0)