Skip to content

Commit

Permalink
closes #149: finalized namespace and collection names validations (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Senic authored Apr 17, 2023
1 parent 9b48a95 commit 1268419
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/stargate/sgv2/jsonapi/StargateJsonApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,22 @@
in = ParameterIn.PATH,
name = "namespace",
required = true,
schema = @Schema(implementation = String.class, pattern = "\\w+"),
schema =
@Schema(
implementation = String.class,
pattern = "[a-zA-Z][a-zA-Z0-9_]*",
maxLength = 48),
description = "The namespace where the collection is located.",
example = "cycling"),
@Parameter(
in = ParameterIn.PATH,
name = "collection",
required = true,
schema = @Schema(implementation = String.class, pattern = "\\w+"),
schema =
@Schema(
implementation = String.class,
pattern = "[a-zA-Z][a-zA-Z0-9_]*",
maxLength = 48),
description = "The name of the collection.",
example = "events")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -127,8 +129,16 @@ public CollectionResource(CommandProcessor commandProcessor) {
@POST
public Uni<RestResponse<CommandResult>> postCommand(
@NotNull @Valid CollectionCommand command,
@PathParam("namespace") String namespace,
@PathParam("collection") String collection) {
@PathParam("namespace")
@NotNull
@Pattern(regexp = "[a-zA-Z][a-zA-Z0-9_]*")
@Size(min = 1, max = 48)
String namespace,
@PathParam("collection")
@NotNull
@Pattern(regexp = "[a-zA-Z][a-zA-Z0-9_]*")
@Size(min = 1, max = 48)
String collection) {

// create context
CommandContext commandContext = new CommandContext(namespace, collection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -75,7 +77,12 @@ public NamespaceResource(CommandProcessor commandProcessor) {
})))
@POST
public Uni<RestResponse<CommandResult>> postCommand(
@NotNull @Valid NamespaceCommand command, @PathParam("namespace") String namespace) {
@NotNull @Valid NamespaceCommand command,
@PathParam("namespace")
@NotNull
@Pattern(regexp = "[a-zA-Z][a-zA-Z0-9_]*")
@Size(min = 1, max = 48)
String namespace) {

// create context
CommandContext commandContext = new CommandContext(namespace, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public void malformedBody() {
public void unknownCommand() {
String json =
"""
{
"unknownCommand": {
}
}
""";
{
"unknownCommand": {
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
Expand All @@ -83,6 +83,56 @@ public void unknownCommand() {
.body("errors[0].exceptionClass", is("InvalidTypeIdException"));
}

@Test
public void invalidNamespaceName() {
String json =
"""
{
"insertOne": {
"document": {}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, "7_no_leading_number", collectionName)
.then()
.statusCode(200)
.body(
"errors[0].message",
startsWith("Request invalid, the field postCommand.namespace not valid"))
.body("errors[0].exceptionClass", is("ConstraintViolationException"));
}

@Test
public void invalidCollectionName() {
String json =
"""
{
"insertOne": {
"document": {}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), "7_no_leading_number")
.then()
.statusCode(200)
.body(
"errors[0].message",
startsWith("Request invalid, the field postCommand.collection not valid"))
.body("errors[0].exceptionClass", is("ConstraintViolationException"));
}

@Test
public void emptyBody() {
given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ public void unknownCommand() {
.body("errors[0].exceptionClass", is("InvalidTypeIdException"));
}

@Test
public void invalidNamespaceName() {
String json =
"""
{
"createCollection": {
"name": "ignore_me"
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(NamespaceResource.BASE_PATH, "7_no_leading_number")
.then()
.statusCode(200)
.body(
"errors[0].message",
startsWith("Request invalid, the field postCommand.namespace not valid"))
.body("errors[0].exceptionClass", is("ConstraintViolationException"));
}

@Test
public void emptyBody() {
given()
Expand Down

0 comments on commit 1268419

Please sign in to comment.