mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Yannick Lecaillez
06.00.2016 db74322864687d2ecdf69d46bbf96dfd8c0f0448
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2013-2016 ForgeRock AS.
 */
package org.forgerock.opendj.rest2ldap;
 
import static org.forgerock.json.JsonValue.json;
 
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
 
import org.forgerock.http.util.Json;
import org.forgerock.json.JsonPointer;
import org.forgerock.json.JsonValue;
import org.forgerock.json.resource.ResourceResponse;
import org.forgerock.json.resource.Responses;
import org.forgerock.services.context.RootContext;
 
/** Unit test utility methods, including fluent methods for creating JSON objects. */
public final class TestUtils {
 
    /**
     * Returns a {@code Resource} containing the provided JSON content. The ID
     * and revision will be taken from the "_id" and "_rev" fields respectively.
     *
     * @param content
     *            The JSON content.
     * @return A {@code Resource} containing the provided JSON content.
     */
    public static ResourceResponse asResource(final JsonValue content) {
        return Responses.newResourceResponse(content.get("_id").asString(), content.get("_rev").asString(), content);
    }
 
    /**
     * Creates a JSON value for the provided object.
     *
     * @param object
     *            The object.
     * @return The JSON value.
     */
    public static JsonValue content(final Object object) {
        return json(object);
    }
 
    /**
     * Creates a root context to be passed in with client requests.
     *
     * @return The root context.
     */
    public static RootContext ctx() {
        return new RootContext();
    }
 
    /**
     * Creates a JSON value for the provided object. This is the same as
     * {@link #content(Object)} but can yield more readable test data in data
     * providers.
     *
     * @param object
     *            The object.
     * @return The JSON value.
     */
    public static JsonValue expected(final Object object) {
        return json(object);
    }
 
    /**
     * Creates a list of JSON pointers from the provided string representations.
     *
     * @param fields
     *            The list of JSON pointer strings.
     * @return The list of parsed JSON pointers.
     */
    public static List<JsonPointer> filter(final String... fields) {
        final List<JsonPointer> result = new ArrayList<>(fields.length);
        for (final String field : fields) {
            result.add(new JsonPointer(field));
        }
        return result;
    }
 
    /**
     * Return {@link JsonValue} corresponding to the provided json blob.
     *
     * @param jsonStr
     *          JSON blob.
     * @return A {@link JsonValue} corresponding to the provided json blob.
     */
    public static JsonValue parseJson(final String jsonStr) throws IOException {
        return new JsonValue(Json.readJsonLenient(new StringReader(toValidJson(jsonStr))));
    }
 
    /**
     * Allows usage of single quote character in json string used in unit tests.
     *
     * @param jsonStr
     *          The json string to convert to valid json.
     * @return A Json compliant string.
     */
    public static String toValidJson(final String jsonStr) {
        return jsonStr.replace("'", "\"");
    }
 
    private TestUtils() {
        // Prevent instantiation.
    }
 
}