forked from LogNet/grpc-spring-boot-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidationTest.java
149 lines (117 loc) · 5.66 KB
/
ValidationTest.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package org.lognet.springboot.grpc;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.GreeterGrpc;
import io.grpc.examples.GreeterOuterClass;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.junit.Assert.assertThrows;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE, properties = {"grpc.port=0"})
public class ValidationTest extends GrpcServerTestBase {
private GreeterGrpc.GreeterBlockingStub stub;
@Before
public void setUp() throws Exception {
stub = GreeterGrpc.newBlockingStub(getChannel());
}
@Test
public void stringValidationTest() {
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
.setAge(49)// valid
.clearName()//invalid
.build());
});
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));
assertThat(e.getMessage(), Matchers.allOf(
Matchers.containsStringIgnoringCase("must not be empty"),
Matchers.containsStringIgnoringCase(getFieldName(GreeterOuterClass.Person.NAME_FIELD_NUMBER))
));
}
@Test
public void numberValidationTest() {
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
.setAge(121)//invalid
.setName("Dexter")//valid
.build());
});
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));
assertThat(e.getMessage(), Matchers.allOf(
Matchers.containsStringIgnoringCase("must be less than or equal to"),
Matchers.containsStringIgnoringCase(getFieldName(GreeterOuterClass.Person.AGE_FIELD_NUMBER))
));
}
@Test
public void fieldsValidationTest() {
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
.setAge(121)//invalid
.clearName()//invalid
.build());
});
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));
assertThat(e.getMessage(), Matchers.allOf(
Matchers.containsStringIgnoringCase(getFieldName(GreeterOuterClass.Person.AGE_FIELD_NUMBER)),
Matchers.containsStringIgnoringCase("must be less than or equal to"),
Matchers.containsStringIgnoringCase(getFieldName(GreeterOuterClass.Person.NAME_FIELD_NUMBER)),
Matchers.containsStringIgnoringCase("must not be empty"),
Matchers.containsStringIgnoringCase(getFieldName(GreeterOuterClass.Person.ADDRESS_FIELD_NUMBER)),
Matchers.containsStringIgnoringCase("must be true")
));
}
@Test
public void crossFieldsValidationTest() {
final GreeterOuterClass.Person person = GreeterOuterClass.Person.newBuilder()
.setAge(4)//valid
.setName("Bob")//valid
.build();
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
stub.helloPersonValidResponse(person);
});
// Person constraint should fail
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));
assertThat(e.getMessage(),Matchers.allOf(
Matchers.containsStringIgnoringCase("Person with name"),
Matchers.containsStringIgnoringCase("can't be of age"),
Matchers.containsStringIgnoringCase(person.getName()),
Matchers.containsStringIgnoringCase(Integer.toString(person.getAge()))
));
}
@Test
public void validMessageValidationTest() {
//valid
final GreeterOuterClass.Person response = stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
.setAge(3)//valid
.setName("Dexter")//valid
.setAddress(GreeterOuterClass.Address.newBuilder())
.build());
assertThat(response.getNickName(),Matchers.not(emptyOrNullString()));
}
@Test
public void invalidResponseMessageValidationTest() {
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
stub.helloPersonInvalidResponse(GreeterOuterClass.Person.newBuilder()
.setAge(3)//valid
.setName("Dexter")//valid
.setAddress(GreeterOuterClass.Address.newBuilder())
.build());
});
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.FAILED_PRECONDITION));
assertThat(e.getMessage(), Matchers.allOf(
Matchers.containsStringIgnoringCase(getFieldName(GreeterOuterClass.Person.NICKNAME_FIELD_NUMBER)),
Matchers.containsStringIgnoringCase("must not be empty")
));
}
String getFieldName(int fieldNumber) {
return GreeterOuterClass.Person.getDescriptor().findFieldByNumber(fieldNumber).getName();
}
}