use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldSupportDateQueries.
@Test(dataProvider = "SupportedQueryOperators")
public void shouldSupportDateQueries(String queryOperator, SearchFilter.Operator expectedOperator) throws Exception {
// Given
// Note: only second accuracy supported in timestamp format
Date value = new Date(123456789000l);
QueryRequest request = mockQueryRequest(comparisonFilter(new JsonPointer(DATE_ATTRIBUTE), queryOperator, DateUtils.toUTCDateFormat(value)));
Subject subject = new Subject();
// When
applicationsResource.query(request, subject, "/abc");
// Then
// Date should be converted into a time-stamp long value
SearchFilter searchFilter = new SearchFilter(new SearchAttribute(DATE_ATTRIBUTE, "ou"), value.getTime(), expectedOperator);
verify(applicationManagerWrapper).search(eq(subject), eq("/abc"), eq(asSet(searchFilter)));
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldRejectNotQueries.
@Test(expectedExceptions = EntitlementException.class, expectedExceptionsMessageRegExp = ".*not supported.*")
public void shouldRejectNotQueries() throws Exception {
// Given
QueryRequest request = mockQueryRequest(not(QueryFilter.<JsonPointer>alwaysTrue()));
Subject subject = new Subject();
// When
applicationsResource.query(request, subject, "/abc");
// Then - exception
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldRejectUnknownAttributes.
@Test(expectedExceptions = EntitlementException.class, expectedExceptionsMessageRegExp = ".*Unknown query field.*")
public void shouldRejectUnknownAttributes() throws Exception {
// Given
QueryRequest request = mockQueryRequest(equalTo(new JsonPointer("unknown"), "a value"));
Subject subject = new Subject();
// When
applicationsResource.query(request, subject, "/abc");
// Then - exception
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class SmsJsonConverter method toJson.
/**
* Will validate the Map representation of the service configuration against the serviceSchema and return a
* corresponding JSON representation
*
* @param attributeValuePairs The schema attribute values.
* @param realm The realm, or null if global.
* @return Json representation of attributeValuePairs
*/
public JsonValue toJson(String realm, Map<String, Set<String>> attributeValuePairs) {
if (!initialised) {
init();
}
final boolean validAttributes;
try {
if (realm == null) {
validAttributes = schema.validateAttributes(attributeValuePairs);
} else {
validAttributes = schema.validateAttributes(attributeValuePairs, realm);
}
} catch (SMSException e) {
debug.error("schema validation threw an exception while validating the attributes: realm=" + realm + " attributes: " + attributeValuePairs, e);
throw new JsonException("Unable to validate attributes", e);
}
JsonValue parentJson = json(new HashMap<String, Object>());
if (validAttributes) {
for (String attributeName : attributeValuePairs.keySet()) {
String jsonResourceName = attributeNameToResourceName.get(attributeName);
String name;
if (jsonResourceName != null) {
name = jsonResourceName;
} else {
name = attributeName;
}
AttributeSchema attributeSchema = schema.getAttributeSchema(attributeName);
if (shouldBeIgnored(attributeName)) {
continue;
}
AttributeSchema.Type type = attributeSchema.getType();
final Set<String> object = attributeValuePairs.get(attributeName);
Object jsonAttributeValue = null;
if (type == null) {
throw new JsonException("Type not defined.");
}
AttributeSchemaConverter attributeSchemaConverter = attributeSchemaConverters.get(name);
if (isASingleValue(type)) {
if (!object.isEmpty()) {
jsonAttributeValue = attributeSchemaConverter.toJson(object.iterator().next());
}
} else if (containsMultipleValues(type)) {
if (isAMap(attributeSchema.getUIType())) {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> itr = object.iterator();
while (itr.hasNext()) {
Pair<String, String> entry = nameValueParser.parse(itr.next());
map.put(entry.getFirst(), attributeSchemaConverter.toJson(entry.getSecond()));
}
jsonAttributeValue = map;
} else {
List<Object> list = new ArrayList<Object>();
Iterator<String> itr = object.iterator();
while (itr.hasNext()) {
list.add(attributeSchemaConverter.toJson(itr.next()));
}
jsonAttributeValue = list;
}
}
String sectionName = attributeNameToSection.get(attributeName);
if (sectionName != null) {
parentJson.putPermissive(new JsonPointer("/" + sectionName + "/" + name), jsonAttributeValue);
} else {
parentJson.put(name, jsonAttributeValue);
}
}
} else {
throw new JsonException("Invalid attributes");
}
return parentJson;
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class SmsServerPropertiesResource method getSchema.
private JsonValue getSchema(Properties syntaxProperties, Properties titleProperties, boolean isDefault) {
JsonValue template = json(object());
for (String tabName : getTabNames()) {
try {
Document propertySheet = getPropertySheet(tabName);
Map<String, Set<String>> options = getOptions(propertySheet, tabName);
List<String> sectionNames = getSectionNames(propertySheet);
Set<String> optionalAttributes = getOptionalAttributes(propertySheet, tabName);
int sectionOrder = 0;
for (String sectionName : sectionNames) {
final String sectionPath = "/properties/" + tabName + "/" + sectionName;
template.putPermissive(new JsonPointer(sectionPath + "/title"), titleProperties.getProperty(sectionName));
template.putPermissive(new JsonPointer(sectionPath + "/propertyOrder"), sectionOrder++);
int attributeOrder = 0;
for (String attributeName : getAttributeNamesForSection(sectionName, propertySheet)) {
final String title = titleProperties.getProperty(attributeName);
String property = syntaxProperties.getProperty(attributeName);
if (property == null) {
property = "";
}
final String type = syntaxRawToReal.get(property);
final Set<String> attributeOptions = getAttributeOptions(options, attributeName, type);
final boolean isOptional;
if (isDefault) {
isOptional = optionalAttributes.contains(attributeName);
} else {
isOptional = true;
}
final String path = sectionPath + "/" + attributeName;
if (attributeOptions != null && !attributeOptions.isEmpty()) {
template.putPermissive(new JsonPointer(path + "/type/enum"), attributeOptions);
} else {
template.putPermissive(new JsonPointer(path + "/type"), type);
}
template.putPermissive(new JsonPointer(path + "/title"), title);
template.putPermissive(new JsonPointer(path + "/propertyOrder"), attributeOrder++);
template.putPermissive(new JsonPointer(path + "/required"), !isOptional);
template.putPermissive(new JsonPointer(path + "/pattern"), ".+");
allAttributeNamesInNamedTabs.add(attributeName);
}
}
} catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException e) {
logger.error("Error reading property sheet for tab " + tabName, e);
}
}
return template;
}
Aggregations