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

Guy Paddock
27.46.2017 114516cdeb8c20f90a3c0d884effb5b145c4079b
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * 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 2016 ForgeRock AS.
 */
package org.forgerock.opendj.rest2ldap;
 
import static org.assertj.core.api.Assertions.*;
import static org.forgerock.http.util.Json.*;
import static org.forgerock.json.resource.Requests.*;
import static org.forgerock.json.resource.ResourcePath.*;
import static org.forgerock.opendj.rest2ldap.Rest2Ldap.*;
import static org.forgerock.util.Options.*;
 
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
 
import org.forgerock.api.CrestApiProducer;
import org.forgerock.api.models.ApiDescription;
import org.forgerock.api.models.Items;
import org.forgerock.api.models.Resource;
import org.forgerock.api.models.Services;
import org.forgerock.http.routing.UriRouterContext;
import org.forgerock.http.util.Json;
import org.forgerock.json.JsonValue;
import org.forgerock.json.resource.Request;
import org.forgerock.json.resource.RequestHandler;
import org.forgerock.services.context.Context;
import org.forgerock.services.context.RootContext;
import org.forgerock.testng.ForgeRockTestCase;
import org.forgerock.util.Options;
import org.testng.annotations.Test;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
 
/**
 * This class tests that the {@link Rest2LdapJsonConfigurator} class can successfully create its
 * model and generate its API description from the json configuration files.
 */
@Test
@SuppressWarnings("javadoc")
public class Rest2LdapJsonConfiguratorTest extends ForgeRockTestCase {
    private static final String ID = "frapi:opendj:rest2ldap";
    private static final String VERSION = "4.0.0";
 
    private static final Path SERVLET_MODULE_PATH =
        getPathToMavenModule("opendj-rest2ldap-servlet");
 
    private static final Path CONFIG_DIR = Paths.get(
        SERVLET_MODULE_PATH.toString(), "src", "main", "webapp", "WEB-INF", "classes", "rest2ldap");
 
    @Test
    public void testConfigureEndpointsWithApiDescription() throws Exception {
        final DescribableRequestHandler handler =
            createDescribableHandler(CONFIG_DIR.resolve("endpoints").toFile());
 
        final ApiDescription api = requestApi(handler, "api/users/bjensen");
 
        assertThat(api).isNotNull();
 
        // Ensure we can can pretty print and parse back the generated api description
        parseJson(prettyPrint(api));
 
        assertThat(api.getId()).isEqualTo(ID);
        assertThat(api.getVersion()).isEqualTo(VERSION);
 
        assertThat(api.getPaths().getNames()).containsOnly(
            "/api/users",
            "/api/read-only-users",
            "/api/groups");
 
        assertThat(api.getDefinitions().getNames()).containsOnly(
            "frapi:opendj:rest2ldap:object:1.0",
            "frapi:opendj:rest2ldap:group:1.0",
            "frapi:opendj:rest2ldap:user:1.0",
            "frapi:opendj:rest2ldap:posixUser:1.0");
 
        final Services services = api.getServices();
 
        assertThat(services.getNames()).containsOnly(
            "frapi:opendj:rest2ldap:user:1.0:read-write",
            "frapi:opendj:rest2ldap:user:1.0:read-only",
            "frapi:opendj:rest2ldap:group:1.0:read-write");
 
        final String[] readOnlyServices = new String[] {
            "frapi:opendj:rest2ldap:user:1.0:read-only"
        };
 
        for (String serviceName : readOnlyServices) {
            final Resource service = services.get(serviceName);
            final Items items = service.getItems();
 
            assertThat(service.getCreate()).isNull();
            assertThat(items.getCreate()).isNull();
            assertThat(items.getUpdate()).isNull();
            assertThat(items.getDelete()).isNull();
            assertThat(items.getPatch()).isNull();
 
            assertThat(items.getRead()).isNotNull();
        }
 
        final String[] writableServices = new String[] {
            "frapi:opendj:rest2ldap:user:1.0:read-write",
            "frapi:opendj:rest2ldap:group:1.0:read-write"
        };
 
        for (String serviceName : writableServices) {
            final Resource service = services.get(serviceName);
            final Items items = service.getItems();
 
            assertThat(service.getCreate()).isNotNull();
            assertThat(items.getCreate()).isNotNull();
            assertThat(items.getUpdate()).isNotNull();
            assertThat(items.getDelete()).isNotNull();
            assertThat(items.getPatch()).isNotNull();
            assertThat(items.getRead()).isNotNull();
        }
    }
 
    private RequestHandler createRequestHandler(final File endpointsDir) throws IOException {
        return Rest2LdapJsonConfigurator.configureEndpoints(endpointsDir, Options.defaultOptions());
    }
 
    private DescribableRequestHandler createDescribableHandler(final File endpointsDir)
    throws Exception {
        final RequestHandler rh = createRequestHandler(endpointsDir);
        final DescribableRequestHandler handler = new DescribableRequestHandler(rh);
 
        handler.api(new CrestApiProducer(ID, VERSION));
 
        return handler;
    }
 
    private ApiDescription requestApi(final DescribableRequestHandler handler,
                                      final String uriPath) {
        final Context context = newRouterContext(uriPath);
        final Request request = newApiRequest(resourcePath(uriPath));
 
        return handler.handleApiRequest(context, request);
    }
 
    private Context newRouterContext(final String uriPath) {
        Context ctx = new RootContext();
 
        ctx = new Rest2LdapContext(ctx, rest2Ldap(defaultOptions()));
        ctx = new UriRouterContext(ctx, null, uriPath, Collections.<String, String> emptyMap());
 
        return ctx;
    }
 
    private String prettyPrint(Object o) throws Exception {
        final ObjectMapper objectMapper =
            new ObjectMapper().registerModules(
                new Json.LocalizableStringModule(),
                new Json.JsonValueModule());
 
        final ObjectWriter writer = objectMapper.writer().withDefaultPrettyPrinter();
 
        return writer.writeValueAsString(o);
    }
 
    private static JsonValue parseJson(final String json) throws Exception {
        try (StringReader r = new StringReader(json)) {
            return new JsonValue(readJsonLenient(r));
        }
    }
 
    private static Path getPathToClass(Class<?> clazz) {
        return Paths.get(clazz.getProtectionDomain().getCodeSource().getLocation().getPath());
    }
 
    private static Path getPathToMavenModule(String moduleName) {
        final Path testClassPath = getPathToClass(Rest2LdapJsonConfiguratorTest.class);
 
        return Paths.get(testClassPath.toString(), "..", "..", "..", moduleName);
    }
}