RoleMapperFactory { public List<RoleMapperHolder> parseRoleMapper(Buffer buffer) throws ValidationException { ValidationResult validationResult = Validator.validateStatic(buffer, mapperSchema, LOGGER); if (!validationResult.isSuccess()) { throw new ValidationException(validationResult); } List<RoleMapperHolder> result = new ArrayList<>(); Mappings mappings = Json.decodeValue(buffer, Mappings.class); for (Mapping mapping : mappings.mappings) { if (properties != null) { mapping.pattern = StringUtils.replaceWildcardConfigs(mapping.pattern, properties); mapping.role = StringUtils.replaceWildcardConfigs(mapping.role, properties); } result.add(new RoleMapperHolder(Pattern.compile(mapping.pattern), mapping.role, mapping.keepOriginalRole, mapping.continueMapping, mapping.keepResultingRole)); } return result; } RoleMapperFactory(Map<String, Object> properties); List<RoleMapperHolder> parseRoleMapper(Buffer buffer);  }
@Test public void testInvalidMapperJson() throws ValidationException { thrown.expect(ValidationException.class); thrown.expectMessage("Unable to parse json"); roleMapperFactory.parseRoleMapper(Buffer.buffer(INVALID_MAPPER_JSON)); }
@Test public void testValidRoleMapperConfig(TestContext context) throws ValidationException { List<RoleMapperHolder> result = roleMapperFactory.parseRoleMapper(Buffer.buffer(VALID_MAPPER_RESOURCE)); context.assertNotNull(result); context.assertEquals(1, result.size()); RoleMapperHolder holder = result.get(0); context.assertNotNull(holder.getPattern()); context.assertNotNull(holder.getRole()); }
@Test public void testAdditionalMapperPropertiesNotAllowed(TestContext context) { try { roleMapperFactory.parseRoleMapper(Buffer.buffer(ADDITIONAL_PROP_MAPPER_RESOURCE)); context.fail("Should have thrown a ValidationException since 'notAllowedProperty' property is not allowed"); } catch (ValidationException ex) { context.assertNotNull(ex.getValidationDetails()); context.assertEquals(1, ex.getValidationDetails().size()); for (Object obj : ex.getValidationDetails()) { JsonObject jsonObject = (JsonObject) obj; if ("additionalProperties".equalsIgnoreCase(jsonObject.getString("keyword"))) { context.assertEquals("notAllowedProperty", jsonObject.getJsonArray("unwanted").getString(0)); } } } }
@Test public void testEmptyMapperPropertiesNotAllowed(TestContext context) { try { roleMapperFactory.parseRoleMapper(Buffer.buffer(EMPTY_PROP_MAPPER_RESOURCE)); context.fail("Should have thrown a ValidationException since 'minLength' is set to 1 in the schema"); } catch (ValidationException ex) { context.assertNotNull(ex.getValidationDetails()); context.assertEquals(2, ex.getValidationDetails().size()); for (Object obj : ex.getValidationDetails()) { JsonObject jsonObject = (JsonObject) obj; context.assertEquals("minLength", jsonObject.getString("type")); } } }