Skip to content

Commit

Permalink
Finished commands and tests for AddConstantRoleMapper, AddLogicalRole…
Browse files Browse the repository at this point in the history
…Mapper, AddSimplePermissionMapper
  • Loading branch information
Ondrej Lukas committed Sep 23, 2016
1 parent d9315f6 commit 86ec0ee
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wildfly.extras.creaper.commands.elytron.mapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jboss.dmr.ModelNode;
import org.wildfly.extras.creaper.core.online.OnlineCommand;
Expand Down Expand Up @@ -101,13 +102,11 @@ public Builder mappingMode(MappingMode mappingMode) {
return this;
}

public Builder addPermissionMapping(PermissionMapping permissionMapping) {
this.permissionMappings.add(permissionMapping);
return this;
}

public Builder addPermissionMappings(List<PermissionMapping> permissionMappings) {
this.permissionMappings.addAll(permissionMappings);
public Builder addPermissionMappings(PermissionMapping... permissionMappings) {
if (permissionMappings == null) {
throw new IllegalArgumentException("PermissionMapping added to simple-permission-mapper must not be null");
}
Collections.addAll(this.permissionMappings, permissionMappings);
return this;
}

Expand Down Expand Up @@ -157,33 +156,27 @@ public static final class PermissionMappingBuilder {
private List<String> principals = new ArrayList<String>();
private List<Permission> permissions = new ArrayList<Permission>();

public PermissionMappingBuilder addRole(String role) {
this.roles.add(role);
return this;
}

public PermissionMappingBuilder addRoles(List<String> roles) {
this.roles.addAll(roles);
return this;
}

public PermissionMappingBuilder addPrincipal(String principal) {
this.principals.add(principal);
return this;
}

public PermissionMappingBuilder addPrincipals(List<String> principals) {
this.principals.addAll(principals);
public PermissionMappingBuilder addRoles(String... roles) {
if (roles == null) {
throw new IllegalArgumentException("Roles added to permission-mapping of simple-permission-mapper must not be null");
}
Collections.addAll(this.roles, roles);
return this;
}

public PermissionMappingBuilder addPermission(Permission permission) {
this.permissions.add(permission);
public PermissionMappingBuilder addPrincipals(String... principals) {
if (principals == null) {
throw new IllegalArgumentException("Principals added to permission-mapping of simple-permission-mapper must not be null");
}
Collections.addAll(this.principals, principals);
return this;
}

public PermissionMappingBuilder addPermissions(List<Permission> permissions) {
this.permissions.addAll(permissions);
public PermissionMappingBuilder addPermissions(Permission... permissions) {
if (permissions == null) {
throw new IllegalArgumentException("Permissions added to permission-mapping of simple-permission-mapper must not be null");
}
Collections.addAll(this.permissions, permissions);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
import org.wildfly.extras.creaper.core.online.operations.Values;
import org.wildfly.extras.creaper.core.online.operations.admin.Administration;

/**
*
* @author olukas
*/
public final class AddPropertiesRealm implements OnlineCommand {

private static final String REALM_TYPE = "properties-realm";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ public void addDuplicateConstantRoleMapperNotAllowed() throws Exception {
+ " already exists in configuration, exception should be thrown");
}

public void addDuplicateConstantRoleMapperAllowed() throws Exception {
AddConstantRoleMapper addConstantRoleMapper = new AddConstantRoleMapper.Builder(TEST_CONSTANT_ROLE_MAPPER_NAME)
.addRoles("AnyRole")
.build();

AddConstantRoleMapper addConstantRoleMapper2 = new AddConstantRoleMapper.Builder(TEST_CONSTANT_ROLE_MAPPER_NAME)
.addRoles("AnyRole2")
.replaceExisting()
.build();

client.apply(addConstantRoleMapper);
assertTrue("Constant role mapper should be created", ops.exists(TEST_CONSTANT_ROLE_MAPPER_ADDRESS));
client.apply(addConstantRoleMapper2);
assertTrue("Constant role mapper should be created", ops.exists(TEST_CONSTANT_ROLE_MAPPER_ADDRESS));
// check whether it was really rewritten
assertRoles(TEST_CONSTANT_ROLE_MAPPER_ADDRESS, "AnyRole2");
}

@Test(expected = IllegalArgumentException.class)
public void addConstantRoleMapper_nullName() throws Exception {
new AddConstantRoleMapper.Builder(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.wildfly.extras.creaper.commands.elytron.mapper;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.commands.elytron.AbstractElytronOnlineTest;
import org.wildfly.extras.creaper.core.CommandFailedException;
import org.wildfly.extras.creaper.core.online.ModelNodeResult;
import org.wildfly.extras.creaper.core.online.operations.Address;

@RunWith(Arquillian.class)
Expand Down Expand Up @@ -54,4 +59,96 @@ public void addLogicalRoleMappers() throws Exception {
assertTrue("Second logical role mapper should be created", ops.exists(TEST_LOGICAL_ROLE_MAPPER_ADDRESS2));
}

@Test
public void addFullLogicalRoleMapper() throws Exception {
AddConstantRoleMapper addConstantRoleMapper = new AddConstantRoleMapper.Builder("creaper-contant-role-mapper-1")
.addRoles("AnyRole1")
.build();
AddConstantRoleMapper addConstantRoleMapper2 = new AddConstantRoleMapper.Builder("creaper-contant-role-mapper-2")
.addRoles("AnyRole2")
.build();
client.apply(addConstantRoleMapper);
client.apply(addConstantRoleMapper2);

AddLogicalRoleMapper addLogicalRoleMapper = new AddLogicalRoleMapper.Builder(TEST_LOGICAL_ROLE_MAPPER_NAME)
.logicalOperation(AddLogicalRoleMapper.LogicalOperation.OR)
.left("creaper-contant-role-mapper-1")
.right("creaper-contant-role-mapper-2")
.build();

client.apply(addLogicalRoleMapper);

assertTrue("Logical role mapper should be created", ops.exists(TEST_LOGICAL_ROLE_MAPPER_ADDRESS));

checkLogicalRoleMapperAttribute("logical-operation", "OR");
checkLogicalRoleMapperAttribute("left", "creaper-contant-role-mapper-1");
checkLogicalRoleMapperAttribute("right", "creaper-contant-role-mapper-2");
}

@Test(expected = CommandFailedException.class)
public void addLogicalRoleMapperNotAllowed() throws Exception {
AddLogicalRoleMapper addLogicalRoleMapper = new AddLogicalRoleMapper.Builder(TEST_LOGICAL_ROLE_MAPPER_NAME)
.logicalOperation(AddLogicalRoleMapper.LogicalOperation.OR)
.build();

AddLogicalRoleMapper addLogicalRoleMapper2 = new AddLogicalRoleMapper.Builder(TEST_LOGICAL_ROLE_MAPPER_NAME)
.logicalOperation(AddLogicalRoleMapper.LogicalOperation.AND)
.build();

client.apply(addLogicalRoleMapper);
assertTrue("Logical role mapper should be created", ops.exists(TEST_LOGICAL_ROLE_MAPPER_ADDRESS));
client.apply(addLogicalRoleMapper2);
fail("Logical role mapper CreaperTestLogicalRoleMapper already exists in configuration, exception should be thrown");
}

@Test
public void addLogicalRoleMapperAllowed() throws Exception {
AddLogicalRoleMapper addLogicalRoleMapper = new AddLogicalRoleMapper.Builder(TEST_LOGICAL_ROLE_MAPPER_NAME)
.logicalOperation(AddLogicalRoleMapper.LogicalOperation.OR)
.build();

AddLogicalRoleMapper addLogicalRoleMapper2 = new AddLogicalRoleMapper.Builder(TEST_LOGICAL_ROLE_MAPPER_NAME)
.logicalOperation(AddLogicalRoleMapper.LogicalOperation.AND)
.replaceExisting()
.build();

client.apply(addLogicalRoleMapper);
assertTrue("Logical role mapper should be created", ops.exists(TEST_LOGICAL_ROLE_MAPPER_ADDRESS));
client.apply(addLogicalRoleMapper2);
assertTrue("Logical role mapper should be created", ops.exists(TEST_LOGICAL_ROLE_MAPPER_ADDRESS));
// check whether it was really rewritten
checkLogicalRoleMapperAttribute("logical-operation", "AND");
}

@Test(expected = IllegalArgumentException.class)
public void addLogicalRoleMapper_nullName() throws Exception {
new AddLogicalRoleMapper.Builder(null)
.logicalOperation(AddLogicalRoleMapper.LogicalOperation.OR)
.build();
fail("Creating command with null name should throw exception");
}

@Test(expected = IllegalArgumentException.class)
public void addLogicalRoleMapper_emptyName() throws Exception {
new AddLogicalRoleMapper.Builder("")
.logicalOperation(AddLogicalRoleMapper.LogicalOperation.OR)
.build();
fail("Creating command with empty name should throw exception");
}

@Test(expected = IllegalArgumentException.class)
public void addLogicalRoleMapper_nullLogicalOperation() throws Exception {
new AddLogicalRoleMapper.Builder(TEST_LOGICAL_ROLE_MAPPER_NAME)
.logicalOperation(null)
.build();
fail("Creating command with null logical operation should throw exception");
}

private void checkLogicalRoleMapperAttribute(String attribute, String expectedValue) throws IOException {
ModelNodeResult readAttribute = ops.readAttribute(TEST_LOGICAL_ROLE_MAPPER_ADDRESS, attribute);
readAttribute.assertSuccess("Read operation for " + attribute + " failed");
assertEquals("Read operation for " + attribute + " return wrong value", expectedValue,
readAttribute.stringValue());
}

}
Loading

0 comments on commit 86ec0ee

Please sign in to comment.