|
| 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 | +} |
0 commit comments