-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/cat/udl/eps/softarch/demo/steps/ManageRolesStepDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Role; | ||
import cat.udl.eps.softarch.demo.repository.RoleRepository; | ||
import io.cucumber.java.en.Given; | ||
import io.cucumber.java.en.Then; | ||
import io.cucumber.java.en.When; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ManageRolesStepDefs { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private RoleRepository roleRepository; | ||
|
||
private Role createdRole; | ||
private Role existingRole; | ||
|
||
@Given("There is a role with name {string}") | ||
public void thereIsARoleWithName(String roleName) { | ||
existingRole = new Role(); | ||
existingRole.setName(roleName); | ||
|
||
// Mocking the behavior of the repository to return the existing role | ||
when(roleRepository.findByName(roleName)).thenReturn(existingRole); | ||
} | ||
|
||
@When("^I create a role with name \"([^\"]*)\"$") | ||
public void iCreateARoleWithName(String roleName) throws Exception { | ||
Role role = new Role(); | ||
role.setName(roleName); | ||
|
||
createdRole = roleRepository.save(role); | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/roles") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(role)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
|
||
@When("^I update the role with name \"([^\"]*)\" to have name \"([^\"]*)\"$") | ||
public void iUpdateTheRoleWithNameToHaveName(String oldName, String newName) throws Exception { | ||
Role role = roleRepository.findByName(oldName); | ||
role.setName(newName); | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
put("/roles/{id}", role.getId()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(role)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
|
||
@When("^I delete the role with name \"([^\"]*)\"$") | ||
public void iDeleteTheRoleWithName(String roleName) throws Exception { | ||
Role role = roleRepository.findByName(roleName); | ||
|
||
stepDefs.mockMvc.perform( | ||
delete("/roles/{id}", role.getId()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
|
||
@When("I retrieve the role with name {string}") | ||
public void iRetrieveTheRoleWithName(String roleName) throws Exception { | ||
// Implementation already exists in the RoleStepDefs class | ||
} | ||
|
||
@Then("^The response code is (\\d+)$") | ||
public void theResponseCodeIs(int statusCode) throws Exception { | ||
stepDefs.result.andExpect(status().is(statusCode)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Feature: Manage Roles | ||
In order to manage roles in the system | ||
As a user with appropriate permissions | ||
I want to be able to create, retrieve, update, and delete roles | ||
|
||
Background: | ||
Given There is a registered user with username "username" and password "password" and email "[email protected]" | ||
And I can login with username "username" and password "password" | ||
|
||
Scenario: Create a new role | ||
When I create a role with name "new_role" | ||
Then The response code is 201 | ||
|
||
Scenario: Retrieve existing role details | ||
Given There is a role with name "existing_role" | ||
When I retrieve the role with name "existing_role" | ||
Then The response code is 200 | ||
|
||
Scenario: Update existing role details | ||
Given There is a role with name "existing_role" | ||
When I update the role with name "existing_role" to have name "updated_role" | ||
Then The response code is 200 | ||
|
||
Scenario: Delete existing role | ||
Given There is a role with name "role_to_delete" | ||
When I delete the role with name "role_to_delete" | ||
Then The response code is 204 | ||
|
||
Scenario: Get non-existent role details | ||
When I retrieve the role with name "non_existent_role" | ||
Then The response code is 404 | ||
|
||
Scenario: Update non-existent role | ||
When I update the role with name "non_existent_role" to have name "updated_role" | ||
Then The response code is 404 | ||
|
||
Scenario: Delete non-existent role | ||
When I delete the role with name "non_existent_role" | ||
Then The response code is 404 |