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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ build/

### VS Code ###
.vscode/

### Claude ###
.CLAUDE.md
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class Loyalty {
startDate
}
class Inventory { }
class Category { }
class Category {
name
}
class Product {
id
name
Expand Down
47 changes: 29 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -89,12 +89,12 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>7.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
Expand All @@ -107,17 +107,6 @@
<version>7.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand All @@ -126,6 +115,28 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.GET, "/identity").authenticated()
.requestMatchers(HttpMethod.POST, "/users").anonymous()
.requestMatchers(HttpMethod.POST, "/users/*").denyAll()
.requestMatchers(HttpMethod.POST, "/*/*").authenticated()
.requestMatchers(HttpMethod.POST, "/*").authenticated()
.requestMatchers(HttpMethod.PUT, "/*/*").authenticated()
.requestMatchers(HttpMethod.PATCH, "/*/*").authenticated()
.requestMatchers(HttpMethod.DELETE, "/*/*").authenticated()
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Category.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cat.udl.eps.softarch.demo.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotEmpty;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.*;
import org.hibernate.validator.constraints.Length;

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


@NotEmpty
@NotBlank
@Length(min = 1, max = 50)
@Column(unique = true)
private String name;

@NotBlank
@Length(min = 1, max = 255)
@Column(length = 255)
private String description;

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


import cat.udl.eps.softarch.demo.domain.Category;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

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

@RepositoryRestResource
public interface CategoryRepository extends CrudRepository<Category, Long>, PagingAndSortingRepository<Category, Long> {

Optional<Category> findByName(String name);
List<Category> findByDescription(String description);

}
7 changes: 2 additions & 5 deletions src/test/java/cat/udl/eps/softarch/demo/CucumberTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package cat.udl.eps.softarch.demo;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import io.cucumber.junit.platform.engine.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty"}, features="src/test/resources")
@Cucumber
public class CucumberTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ public void iLoginAsWithPassword(String username, String password) {
public void iMNotLoggedIn() {
currentUsername = currentPassword = null;
}

@Given("^I'm logged in as admin$")
public void iMLoggedInAsAdmin() {
AuthenticationStepDefs.currentUsername = "admin";
AuthenticationStepDefs.currentPassword = "password";
}

@Given("^I'm logged in as regular user$")
public void iMLoggedInAsRegularUser() {
AuthenticationStepDefs.currentUsername = "user";
AuthenticationStepDefs.currentPassword = "password";
}
}
105 changes: 105 additions & 0 deletions src/test/java/cat/udl/eps/softarch/demo/steps/CategoryStepDefs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Category;
import cat.udl.eps.softarch.demo.repository.CategoryRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import static org.junit.jupiter.api.Assertions.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.Optional;

import static org.hamcrest.Matchers.is;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class CategoryStepDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private CategoryRepository categoryRepository;

private static Category currentCategory;

@Given("^There is no registered category with name \"([^\"]*)\"$")
public void thereIsNoRegisteredCategoryWithName(String name) {
assertFalse(categoryRepository.findByName(name).isPresent(),
"Category \"" + name + "\" shouldn't exist");
}

@Given("^There is a registered category with name \"([^\"]*)\" and description \"([^\"]*)\"$")
public void thereIsARegisteredCategoryWithNameAndDescription(String name, String description) {
if (!categoryRepository.findByName(name).isPresent()) {
Category category = new Category();
category.setName(name);
category.setDescription(description);
categoryRepository.save(category);
}
}

@When("^I register a new category with name \"([^\"]*)\" and description \"([^\"]*)\"$")
public void iRegisterANewCategoryWithNameAndDescription(String name, String description) throws Exception {
currentCategory = new Category();
currentCategory.setName(name);
currentCategory.setDescription(description);

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

@And("^It has been created a category with name \"([^\"]*)\" and description \"([^\"]*)\"$")
public void itHasBeenCreatedACategoryWithNameAndDescription(String name, String description) throws Exception {
stepDefs.result = stepDefs.mockMvc.perform(
get("/categories/search/findByName")
.param("name", name)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(name)))
.andExpect(jsonPath("$.description", is(description)));
}

@And("^I can retrieve the category with name \"([^\"]*)\"$")
public void iCanRetrieveTheCategoryWithName(String name) throws Exception {
stepDefs.result = stepDefs.mockMvc.perform(
get("/categories/search/findByName")
.param("name", name)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(name)));
}

@And("^The category description remains \"([^\"]*)\"$")
public void theCategoryDescriptionRemains(String description) throws Exception {
// After a 409 conflict, we need to fetch the existing category to verify its description
Optional<Category> existingCategory = categoryRepository.findByName(currentCategory.getName());
assertTrue(existingCategory.isPresent(), "Category should exist");
assertEquals(description, existingCategory.get().getDescription(),
"Description should remain unchanged");
}

@And("^It has not been created a category with name \"([^\"]*)\"$")
public void itHasNotBeenCreatedACategoryWithName(String name) throws Exception {
stepDefs.result = stepDefs.mockMvc.perform(
get("/categories/search/findByName")
.param("name", name)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andExpect(status().isNotFound());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cat.udl.eps.softarch.demo.steps;

import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
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;
Expand All @@ -13,7 +14,7 @@
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

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

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

@Given("^There is a registered user with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$")
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/cat/udl/eps/softarch/demo/steps/StepDefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -32,9 +32,9 @@
loader = SpringBootContextLoader.class
)
@DirtiesContext
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ActiveProfiles("Test")
@ActiveProfiles("test")
@CucumberContextConfiguration
public class StepDefs {

Expand Down
Loading