Skip to content

Commit

Permalink
add cel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLiu1123 committed Sep 28, 2023
1 parent 32b6692 commit 8465263
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
13 changes: 11 additions & 2 deletions examples/proto-validate/src/main/proto/fm/foo/v1/foo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ syntax = "proto3";
package fm.foo.v1;

import "google/protobuf/empty.proto";

import "buf/validate/validate.proto";

option java_multiple_files = true;
option java_package = "com.freemanan.foo.v1.api";

message Foo {
string id = 1;
string id = 1 [(buf.validate.field).cel = {
id: "id",
message: "id length must be at least 5 characters",
expression: "this.size() >= 5",
}];
string name = 2 [(buf.validate.field).string = {min_len: 5}];
repeated string hobbies = 3 [(buf.validate.field).repeated = {min_items: 1}];

option (buf.validate.message).cel = {
id: "foo",
message: "not a valid Foo",
expression: "this.name != 'aaaaa' && !('coding' in this.hobbies)",
};
}

service FooService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,46 @@ class ProtoValidateAppTest {
@Test
void testInsertFoo() {
Foo foo = fooBlockingStub.insertFoo(Foo.newBuilder()
.setId("001")
.setId("00001")
.setName("Freeman")
.addHobbies("Coding")
.build());
assertThat(foo.getId()).isEqualTo("001");
assertThat(foo.getId()).isEqualTo("00001");
assertThat(foo.getName()).isEqualTo("Freeman");
}

@Test
void testInsertFoo_whenInvalidArgument() {
assertThatCode(() -> fooBlockingStub.insertFoo(
Foo.newBuilder().setId("001").setName("Free").build()))
Foo.newBuilder().setId("00001").setName("Free").build()))
.isInstanceOf(StatusRuntimeException.class)
.hasMessage(
"INVALID_ARGUMENT: value length must be at least 5 characters, value must contain at least 1 item(s)");
}

@Test
void testInsertFoo_whenUsingCel() {
assertThatCode(() -> fooBlockingStub.insertFoo(Foo.newBuilder()
.setId("") // invalid
.setName("aaaaa") // invalid
.addHobbies("movies")
.build()))
.isInstanceOf(StatusRuntimeException.class)
.hasMessage("INVALID_ARGUMENT: not a valid Foo, id length must be at least 5 characters");

assertThatCode(() -> fooBlockingStub.insertFoo(Foo.newBuilder()
.setId("") // invalid
.setName("aaaaaa")
.addHobbies("coding") // invalid
.build()))
.isInstanceOf(StatusRuntimeException.class)
.hasMessage("INVALID_ARGUMENT: not a valid Foo, id length must be at least 5 characters");

assertThatCode(() -> fooBlockingStub.insertFoo(Foo.newBuilder()
.setId("11111")
.setName("aaaaaa")
.addHobbies("movies")
.build()))
.doesNotThrowAnyException();
}
}

0 comments on commit 8465263

Please sign in to comment.