use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.
the class RangerServiceDefServiceBase method populateXXToRangerEnumDef.
public RangerEnumDef populateXXToRangerEnumDef(XXEnumDef xObj) {
RangerEnumDef vObj = new RangerEnumDef();
vObj.setItemId(xObj.getItemId());
vObj.setName(xObj.getName());
vObj.setDefaultIndex(xObj.getDefaultindex());
List<XXEnumElementDef> xElements = daoMgr.getXXEnumElementDef().findByEnumDefId(xObj.getId());
List<RangerEnumElementDef> elements = new ArrayList<RangerEnumElementDef>();
for (XXEnumElementDef xEle : xElements) {
RangerEnumElementDef element = populateXXToRangerEnumElementDef(xEle);
elements.add(element);
}
vObj.setElements(elements);
return vObj;
}
use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.
the class TestRangerValidator method test_getEnumDefaultIndex.
@Test
public void test_getEnumDefaultIndex() {
RangerEnumDef enumDef = mock(RangerEnumDef.class);
Assert.assertEquals(-1, _validator.getEnumDefaultIndex(null));
when(enumDef.getDefaultIndex()).thenReturn(null);
Assert.assertEquals(0, _validator.getEnumDefaultIndex(enumDef));
when(enumDef.getDefaultIndex()).thenReturn(-5);
Assert.assertEquals(-5, _validator.getEnumDefaultIndex(enumDef));
}
use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.
the class ValidationTestUtils method createEnumDefs.
List<RangerEnumDef> createEnumDefs(Object[][] input) {
if (input == null) {
return null;
}
List<RangerEnumDef> defs = new ArrayList<>();
for (Object[] row : input) {
RangerEnumDef enumDef = mock(RangerEnumDef.class);
switch(row.length) {
case 3:
List<RangerEnumElementDef> elements = createEnumElementDefs((String[]) row[2]);
when(enumDef.getElements()).thenReturn(elements);
// by default set default index to last element
when(enumDef.getDefaultIndex()).thenReturn(elements.size() - 1);
case 2:
String enumName = (String) row[1];
when(enumDef.getName()).thenReturn(enumName);
case 1:
when(enumDef.getItemId()).thenReturn((Long) row[0]);
}
defs.add(enumDef);
}
return defs;
}
use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.
the class RangerServiceDefValidator method isValidEnums.
boolean isValidEnums(List<RangerEnumDef> enumDefs, List<ValidationFailureDetails> failures) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("==> RangerServiceDefValidator.isValidEnums(%s, %s)", enumDefs, failures));
}
boolean valid = true;
if (CollectionUtils.isEmpty(enumDefs)) {
LOG.debug("enum def collection passed in was null/empty. Ok.");
} else {
Set<String> names = new HashSet<>();
Set<Long> ids = new HashSet<>();
for (RangerEnumDef enumDef : enumDefs) {
if (enumDef == null) {
ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_NULL_OBJECT;
failures.add(new ValidationFailureDetailsBuilder().field("enum def").isMissing().errorCode(error.getErrorCode()).becauseOf(error.getMessage()).build());
valid = false;
} else {
// enum-names and ids must non-blank and be unique to a service definition
String enumName = enumDef.getName();
valid = isUnique(enumName, names, "enum def name", "enum defs", failures) && valid;
valid = isUnique(enumDef.getItemId(), ids, "enum def itemId", "enum defs", failures) && valid;
// enum must contain at least one valid value and those values should be non-blank and distinct
if (CollectionUtils.isEmpty(enumDef.getElements())) {
ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_NO_VALUES;
failures.add(new ValidationFailureDetailsBuilder().field("enum values").subField(enumName).isMissing().errorCode(error.getErrorCode()).becauseOf(error.getMessage(enumName)).build());
valid = false;
} else {
valid = isValidEnumElements(enumDef.getElements(), failures, enumName) && valid;
// default index should be valid
int defaultIndex = getEnumDefaultIndex(enumDef);
if (defaultIndex < 0 || defaultIndex >= enumDef.getElements().size()) {
// max index is one less than the size of the elements list
ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_INVALID_DEFAULT_INDEX;
failures.add(new ValidationFailureDetailsBuilder().field("enum default index").subField(enumName).isSemanticallyIncorrect().errorCode(error.getErrorCode()).becauseOf(error.getMessage(defaultIndex, enumName)).build());
valid = false;
}
}
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("<== RangerServiceDefValidator.isValidEnums(%s, %s): %s", enumDefs, failures, valid));
}
return valid;
}
use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.
the class TestRangerServiceDefServiceBase method rangerServiceDef.
private RangerServiceDef rangerServiceDef() {
List<RangerServiceConfigDef> configs = new ArrayList<RangerServiceConfigDef>();
List<RangerResourceDef> resources = new ArrayList<RangerResourceDef>();
List<RangerAccessTypeDef> accessTypes = new ArrayList<RangerAccessTypeDef>();
List<RangerPolicyConditionDef> policyConditions = new ArrayList<RangerPolicyConditionDef>();
List<RangerContextEnricherDef> contextEnrichers = new ArrayList<RangerContextEnricherDef>();
List<RangerEnumDef> enums = new ArrayList<RangerEnumDef>();
RangerServiceDef rangerServiceDef = new RangerServiceDef();
rangerServiceDef.setId(Id);
rangerServiceDef.setImplClass("RangerServiceHdfs");
rangerServiceDef.setLabel("HDFS Repository");
rangerServiceDef.setDescription("HDFS Repository");
rangerServiceDef.setRbKeyDescription(null);
rangerServiceDef.setUpdatedBy("Admin");
rangerServiceDef.setUpdateTime(new Date());
rangerServiceDef.setConfigs(configs);
rangerServiceDef.setResources(resources);
rangerServiceDef.setAccessTypes(accessTypes);
rangerServiceDef.setPolicyConditions(policyConditions);
rangerServiceDef.setContextEnrichers(contextEnrichers);
rangerServiceDef.setEnums(enums);
return rangerServiceDef;
}
Aggregations