Search in sources :

Example 21 with JsonPointer

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)));
}
Also used : SearchAttribute(com.sun.identity.entitlement.util.SearchAttribute) QueryRequest(org.forgerock.json.resource.QueryRequest) SearchFilter(com.sun.identity.entitlement.util.SearchFilter) JsonPointer(org.forgerock.json.JsonPointer) Date(java.util.Date) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 22 with JsonPointer

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
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) JsonPointer(org.forgerock.json.JsonPointer) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 23 with JsonPointer

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
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) JsonPointer(org.forgerock.json.JsonPointer) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 24 with JsonPointer

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;
}
Also used : JsonException(org.forgerock.json.JsonException) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) JsonPointer(org.forgerock.json.JsonPointer) AttributeSchema(com.sun.identity.sm.AttributeSchema) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashBiMap(org.forgerock.guava.common.collect.HashBiMap) BiMap(org.forgerock.guava.common.collect.BiMap) Pair(org.forgerock.util.Pair)

Example 25 with JsonPointer

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;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) XPathExpressionException(javax.xml.xpath.XPathExpressionException) JsonValue(org.forgerock.json.JsonValue) JsonPointer(org.forgerock.json.JsonPointer) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

JsonPointer (org.forgerock.json.JsonPointer)64 Test (org.testng.annotations.Test)40 QueryRequest (org.forgerock.json.resource.QueryRequest)34 JsonValue (org.forgerock.json.JsonValue)21 QueryResponse (org.forgerock.json.resource.QueryResponse)19 Context (org.forgerock.services.context.Context)18 RealmContext (org.forgerock.openam.rest.RealmContext)17 Collection (java.util.Collection)15 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)13 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)13 ClientContext (org.forgerock.services.context.ClientContext)13 Subject (javax.security.auth.Subject)10 ResourceException (org.forgerock.json.resource.ResourceException)10 ResourceResponse (org.forgerock.json.resource.ResourceResponse)10 ArrayList (java.util.ArrayList)9 BadRequestException (org.forgerock.json.resource.BadRequestException)9 SearchFilter (com.sun.identity.entitlement.util.SearchFilter)8 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 List (java.util.List)7