-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added post method for search and count
- Loading branch information
Showing
3 changed files
with
219 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
.../org/eclipse/ditto/gateway/service/endpoints/routes/thingsearch/ThingSearchRouteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 akka.http.javadsl.model.*; | ||
import akka.http.javadsl.server.Route; | ||
import akka.http.javadsl.testkit.TestRoute; | ||
import akka.japi.Pair; | ||
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 java.util.List; | ||
|
||
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat; | ||
|
||
/** | ||
* 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%28like%28definition%2C%22%2Atest%2A%22%29%29)\",\"options\":[\"sort%28%2BthingId%29\",\"limit%280%2C5%29\"],\"namespaces\":[\"org.eclipse.ditto%2Cfoo.bar\"]}"); | ||
} | ||
|
||
@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%28like%28definition%2C%22%2Atest%2A%22%29%29)" | ||
); | ||
} | ||
|
||
@Test | ||
public void countThingsShouldAssertBadRequest() { | ||
final var result = underTest.run(HttpRequest.POST("/search/things/count")); | ||
result.assertStatusCode(StatusCodes.UNSUPPORTED_MEDIA_TYPE); | ||
} | ||
} |