-
Notifications
You must be signed in to change notification settings - Fork 214
/
ThingSearchRouteTest.java
executable file
·105 lines (88 loc) · 3.94 KB
/
ThingSearchRouteTest.java
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
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.endpoints.routes.thingsearch;
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat;
import java.util.List;
import org.eclipse.ditto.gateway.service.endpoints.EndpointTestBase;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObject;
import org.junit.Before;
import org.junit.Test;
import akka.http.javadsl.model.FormData;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.Route;
import akka.http.javadsl.testkit.TestRoute;
import akka.japi.Pair;
/**
* Builder for creating Akka HTTP routes for {@code /search/things}.
*/
public final class ThingSearchRouteTest extends EndpointTestBase {
private ThingSearchRoute thingsSearchRoute;
private TestRoute underTest;
@Before
public void setUp() {
thingsSearchRoute = new ThingSearchRoute(routeBaseProperties);
final Route route = extractRequestContext(ctx -> thingsSearchRoute.buildSearchRoute(ctx, dittoHeaders));
underTest = testRoute(handleExceptions(() -> route));
}
@Test
public void postSearchThingsShouldGetParametersFromBody() {
final var formData = FormData.create(
List.of(
new Pair<>("filter", "and(like(definition,\"*test*\"))"),
new Pair<>("option", "sort(+thingId)"),
new Pair<>("option","limit(0,5)"),
new Pair<>("namespaces","org.eclipse.ditto,foo.bar")
));
final var result = underTest.run(HttpRequest.POST("/search/things")
.withEntity(formData.toEntity()));
result.assertStatusCode(StatusCodes.OK);
result.assertEntity("{\"type\":\"thing-search.commands:queryThings\",\"filter\":\"and(and(like(definition,\\\"*test*\\\")))\",\"options\":[\"limit(0\",\"5)\",\"sort(+thingId)\"],\"namespaces\":[\"foo.bar\",\"org.eclipse.ditto\"]}");
}
@Test
public void searchThingsShouldGetFilter() {
final var form = "and(and(like(definition,\"*test*\")))";
final var result = underTest.run(
HttpRequest.GET("/search/things?filter=and(like(definition,\"*test*\"))&option=sort(+thingId)&option=limit(0,5)&namespaces=org.eclipse.ditto,foo.bar"));
result.assertStatusCode(StatusCodes.OK);
assertThat(JsonObject.of(result.entityString()))
.contains(
JsonKey.of("filter"),
form
);
}
@Test
public void countThingsShouldGetFilterFromBody() {
final var formData = FormData.create(
List.of(
new Pair<>("filter", "and(like(definition,\"*test*\"))"),
new Pair<>("option", "sort(+thingId)"),
new Pair<>("option","limit(0,5)"),
new Pair<>("namespaces","org.eclipse.ditto,foo.bar")
));
final var result = underTest.run(HttpRequest.POST("/search/things/count")
.withEntity(formData.toEntity()));
result.assertStatusCode(StatusCodes.OK);
assertThat(JsonObject.of(result.entityString()))
.contains(
JsonKey.of("filter"),
"and(and(like(definition,\"*test*\")))"
);
}
@Test
public void countThingsShouldAssertBadRequest() {
final var result = underTest.run(HttpRequest.POST("/search/things/count"));
result.assertStatusCode(StatusCodes.UNSUPPORTED_MEDIA_TYPE);
}
}