Skip to content

Commit 77e745b

Browse files
committed
✅ Add test for MongoRequest with path template mount
1 parent 971d76e commit 77e745b

File tree

3 files changed

+88
-23
lines changed

3 files changed

+88
-23
lines changed

commons/src/main/java/org/restheart/exchange/MongoRequest.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ protected MongoRequest(HttpServerExchange exchange, String whereUri, String what
275275
}
276276

277277
this.rsOps = anyRsSet ? Optional.of(_rsOps) : Optional.empty();
278-
LOGGER.debug("ReplicaSet connection options: {}", _rsOps);
278+
LOGGER.trace("ReplicaSet connection options: {}", _rsOps);
279279
}
280280

281281
private String defaultWriteMode() {
@@ -330,11 +330,11 @@ public static MongoRequest of(HttpServerExchange exchange) {
330330
public static boolean isReservedDbName(String dbName) {
331331
return dbName == null
332332
? false
333-
: "".equals(dbName)
334-
|| dbName.equalsIgnoreCase(ADMIN)
335-
|| dbName.equalsIgnoreCase(CONFIG)
336-
|| dbName.equalsIgnoreCase(LOCAL)
337-
|| dbName.startsWith(SYSTEM);
333+
: dbName.isEmpty()
334+
|| dbName.equalsIgnoreCase(ADMIN)
335+
|| dbName.equalsIgnoreCase(CONFIG)
336+
|| dbName.equalsIgnoreCase(LOCAL)
337+
|| dbName.startsWith(SYSTEM);
338338
}
339339

340340
/**
@@ -345,10 +345,10 @@ public static boolean isReservedDbName(String dbName) {
345345
public static boolean isReservedCollectionName(String collectionName) {
346346
return collectionName == null
347347
? false
348-
: "".equals(collectionName)
349-
|| collectionName.startsWith(SYSTEM)
350-
|| collectionName.endsWith(FS_CHUNKS_SUFFIX)
351-
|| collectionName.equals(META_COLLNAME);
348+
: collectionName.isEmpty()
349+
|| collectionName.startsWith(SYSTEM)
350+
|| collectionName.endsWith(FS_CHUNKS_SUFFIX)
351+
|| collectionName.equals(META_COLLNAME);
352352
}
353353

354354
/**
@@ -491,15 +491,15 @@ static TYPE selectRequestType(String[] pathTokens) {
491491
* @return the mongo resource uri
492492
*/
493493
private String mongoUriFromRequestPath(String path) {
494-
// don't unmap URIs statring with /_sessions
494+
// don't unmap URIs starting with /_sessions
495495
if (path.startsWith("/".concat(_SESSIONS))) {
496496
return path;
497497
}
498498

499499
if (this.pathTemplateMatch == null) {
500500
return mongoUriFromPathMatch(path);
501501
} else {
502-
return mongoUruFromPathTemplateMatch(path);
502+
return mongoUriFromPathTemplateMatch(path);
503503
}
504504
}
505505

@@ -520,7 +520,7 @@ private String mongoUriFromPathMatch(String requestPath) {
520520
return mongoUri.isEmpty() ? SLASH : mongoUri;
521521
}
522522

523-
private String mongoUruFromPathTemplateMatch(String requestPath) {
523+
private String mongoUriFromPathTemplateMatch(String requestPath) {
524524
// requestPath=/api/a/b
525525
// what=*
526526
// where=/api/{*} -> /api/a/b
@@ -566,7 +566,7 @@ private String requestPathFromPathMatch(String mongoUri) {
566566
}
567567
} else {
568568
requestPath = removeTrailingSlashes(
569-
requestPath.replaceFirst("^" + Pattern.quote(this.whatUri), this.whereUri));
569+
requestPath.replaceFirst("^" + Pattern.quote(this.whatUri), this.whereUri));
570570
}
571571

572572
if (requestPath.isEmpty()) {
@@ -590,7 +590,7 @@ private String requestPathFromPathTemplateMatch(String mongoUri) {
590590
}
591591
} else {
592592
requestPath = removeTrailingSlashes(
593-
requestPath.replaceFirst("^" + Pattern.quote(resolvedWhat), resolvedWhere));
593+
requestPath.replaceFirst("^" + Pattern.quote(resolvedWhat), resolvedWhere));
594594
}
595595

596596
return requestPath.isEmpty() ? SLASH : requestPath;

commons/src/test/java/org/restheart/exchange/MongoRequestTest.java

+69-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020

2121
package org.restheart.exchange;
2222

23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.when;
26+
2327
import org.junit.jupiter.api.AfterAll;
2428
import org.junit.jupiter.api.AfterEach;
25-
import static org.junit.jupiter.api.Assertions.assertEquals;
2629
import org.junit.jupiter.api.BeforeAll;
2730
import org.junit.jupiter.api.BeforeEach;
2831
import org.junit.jupiter.api.Test;
29-
import static org.mockito.Mockito.mock;
30-
import static org.mockito.Mockito.when;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3134

3235
import io.undertow.server.HttpServerExchange;
3336
import io.undertow.util.HttpString;
@@ -37,9 +40,12 @@
3740

3841
/**
3942
*
40-
* @author Maurizio Turatti {@literal <[email protected]>}
43+
* @author Maurizio Turatti {@literal <[email protected]>}, Andrea Di Cesare {@literal <[email protected]>}
4144
*/
4245
public class MongoRequestTest {
46+
47+
private static final Logger LOG = LoggerFactory.getLogger(MongoRequestTest.class);
48+
4349
/**
4450
*
4551
*/
@@ -211,6 +217,65 @@ public void testGetMappedRequestUri2() {
211217
assertEquals("/db/coll/x", request.getMongoResourceUri());
212218
}
213219

220+
/**
221+
*
222+
*/
223+
@Test
224+
public void testPathTemplatedMappedRequest() {
225+
var requestPath = "/acme/coll";
226+
227+
// Here mimic MongoService that attach PathTemplateMatch to the exchange
228+
PathTemplateMatcher<MongoMount> templateMongoMounts = new PathTemplateMatcher<>();
229+
String whatUri = "/restheart/{tenant}_{coll}";
230+
String whereUri = "/{tenant}/{coll}";
231+
var mongoMount = new MongoMount(whatUri,whereUri);
232+
templateMongoMounts.add(mongoMount.uri, mongoMount);
233+
234+
var tmm = templateMongoMounts.match(requestPath);
235+
236+
HttpServerExchange ex = mock(HttpServerExchange.class);
237+
when(ex.getRequestPath()).thenReturn(requestPath);
238+
when(ex.getRequestMethod()).thenReturn(HttpString.EMPTY);
239+
when(ex.getAttachment(PathTemplateMatch.ATTACHMENT_KEY)).thenReturn(tmm);
240+
241+
MongoRequest request = MongoRequest.init(ex, whereUri, whatUri);
242+
243+
assertEquals("/restheart/acme_coll", request.getMongoResourceUri());
244+
assertEquals("restheart", request.getDBName());
245+
assertEquals("acme_coll", request.getCollectionName());
246+
}
247+
248+
/**
249+
* helper class to store mongo mounts info
250+
*/
251+
private static record MongoMount(String resource, String uri) {
252+
public MongoMount(String resource, String uri) {
253+
if (uri == null) {
254+
throw new IllegalArgumentException("'where' cannot be null. check your 'mongo-mounts'.");
255+
}
256+
257+
if (!uri.startsWith("/")) {
258+
throw new IllegalArgumentException("'where' must start with \"/\". check your 'mongo-mounts'");
259+
}
260+
261+
if (resource == null) {
262+
throw new IllegalArgumentException("'what' cannot be null. check your 'mongo-mounts'.");
263+
}
264+
265+
if (!uri.startsWith("/") && !uri.equals("*")) {
266+
throw new IllegalArgumentException("'what' must be * (all db resorces) or start with \"/\". (eg. /db/coll) check your 'mongo-mounts'");
267+
}
268+
269+
this.resource = resource;
270+
this.uri = org.restheart.utils.URLUtils.removeTrailingSlashes(uri);
271+
}
272+
273+
@Override
274+
public String toString() {
275+
return "MongoMount(" + uri + " -> " + resource + ")";
276+
}
277+
}
278+
214279
/**
215280
*
216281
*/

mongodb/src/main/java/org/restheart/mongodb/MongoService.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public void init() {
9898

9999
// check that all mounts are either all paths or all path templates
100100
boolean allPathTemplates = MongoServiceConfiguration.get().getMongoMounts()
101-
.stream()
102-
.map(m -> (String) m.get(MONGO_MOUNT_WHERE_KEY))
103-
.allMatch(url -> isPathTemplate(url));
101+
.stream()
102+
.map(m -> (String) m.get(MONGO_MOUNT_WHERE_KEY))
103+
.allMatch(MongoService::isPathTemplate);
104104

105105
if (!allPathTemplates) {
106106
// init mongoMounts
@@ -121,7 +121,7 @@ public void handle(MongoRequest request, MongoResponse response) throws Exceptio
121121
if (mclient != null) {
122122
this.pipeline.handleRequest(request.getExchange());
123123
} else {
124-
final var error = "MongoDB is not availabe";
124+
final var error = "MongoDB is not available";
125125

126126
response.setInError(500, error);
127127
LOGGER.error(error);

0 commit comments

Comments
 (0)