Skip to content

Commit

Permalink
RolesStepDefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hdelegido committed Apr 5, 2024
1 parent 54fba72 commit 0fd6126
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

@RepositoryRestResource
public interface RoleRepository extends CrudRepository<Role, Long>, PagingAndSortingRepository<Role, Long> {
Role findByName(String oldName);
}
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));
}
}
39 changes: 39 additions & 0 deletions src/test/resources/features/ManageRoles.feature
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

0 comments on commit 0fd6126

Please sign in to comment.