Reputation: 2101
Given the following code:
public enum Roles {
ADMIN("admin"),
OPERATOR("operator"),
SYSTEM_ADMIN("system-admin"),
SYSTEM_VIEWER("system-viewer"),
TENANT_ADMIN("admin"),
TENANT_OPERATOR("operator"),
TENANT_VIEWER("viewer");
private String role;
private Roles(String role) { this.role = role; }
public String getRole() {
return role;
}
}
public class TenantMapping {
private String tenant;
private Roles role;
public TenantMapping(String tenant, Roles role) {
super();
this.tenant = tenant;
this.role = role;
}
//accessors
}
Scenario: Create new users
When REST Create new user "system_systemuser" with tenants list
| system | SYSTEM_ADMIN |
@When("^REST Create new user \"(.*)\" with tenants list$")
public void createNewUser(String newUsername, Map<String, Roles> tenantsMap) {
try {
List<TenantMapping> tenantMappingsList = new ArrayList<>();
tenantsMap.forEach((key, value) -> tenantMappingsList.add(new TenantMapping(key, value)));
....
}
usersRest.json maybe relevant fragment:
"tenant_mappings": [
{
"tenant": "system",
"role": "system-admin"
}
...
]
When I run the scenario I get:
java.lang.AssertionError: 13:09:43: Failed to get the Object from usersRest.json file com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of com.rest.testhandlers.restassured.system.users.enums.Roles from String value 'system-admin': value not one of declared Enum instance names: [ADMIN, OPERATOR, SYSTEM_ADMIN, SYSTEM_VIEWER, TENANT_ADMIN, TENANT_OPERATOR, TENANT_VIEWER]
and when I try:
Scenario: Create new users
When REST Create new user "system_systemuser" with tenants list
| system | system-admin |
I get:
cucumber.deps.com.thoughtworks.xstream.converters.ConversionException: Couldn't convert system-admin to com.rest.testhandlers.restassured.system.users.enums.Roles. Legal values are [ADMIN, OPERATOR, SYSTEM_ADMIN, SYSTEM_VIEWER, TENANT_ADMIN, TENANT_OPERATOR, TENANT_VIEWER]
Why?
Upvotes: 2
Views: 1921
Reputation: 699
My guess is that there is some attempt at casting from string to emum here which is causing the first issue? In the first attempt you are passing the correct capitalised ENUM value in your Gherkin datatable, but in the tenantMapping constructor you decLare you are passing a Roles ENUM object (containing all values etc.) and but actually pass a String from the dataTable with value 'SYSTEM_ADMIN ':
public TenantMapping(String tenant, **Roles role**) {
super();
this.tenant = tenant;
this.role = role;
}
Perhaps you should instead call the ENUM constructor in this function using the (uppercase) role value and then ask the enum for its current role (lowercase) and save that in your mapping:
public TenantMapping(String tenant, **String role**) {
super();
this.tenant = tenant;
// better to use some case switch to cover all options in your solution
if (role.equals("SYSTEM_ADMIN"){Roles.SYSTEM_ADMIN;}
// the Roles Enum calls its private constructor and sets current 'role' value
this.role = Roles.getRole();
}
Upvotes: 2
Reputation: 12039
I get:
cucumber.deps.com.thoughtworks.xstream.converters.ConversionException: Couldn't convert system-admin to com.rest.testhandlers.restassured.system.users.enums.Roles. Legal values are [ADMIN, OPERATOR, SYSTEM_ADMIN, SYSTEM_VIEWER, TENANT_ADMIN, TENANT_OPERATOR, TENANT_VIEWER]
Why?
While you do create your enum using the lower case name ADMIN("admin")
, neither Jackson nor XStream knows that you want to use this value. So they both only look at the name of the enum which is uppercased.
You can work around this by going through the documentations for XStream and/or Jackson and adding the right annotations.
Btw, you version of Cucumber is old. You may want to upgrade.
Upvotes: 1