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