-
Notifications
You must be signed in to change notification settings - Fork 433
/
ValidationTest.java
206 lines (173 loc) · 7.96 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.lognet.springboot.grpc;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
import java.util.Locale;
import io.grpc.Metadata;
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.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
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.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE, properties = {"grpc.port=0"})
@Import(ValidationTest.TestCfg.class)
@ActiveProfiles("disable-security")
public class ValidationTest extends GrpcServerTestBase {
@TestConfiguration
static class TestCfg {
@Bean
public GRpcErrorHandler authErrorHandler() {
return new GRpcErrorHandler() {
@Override
public Status handle(Object message, Status status, Exception exception, Metadata requestHeaders, Metadata responseHeaders) {
responseHeaders.put(Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER), "val");
return super.handle(message,status,exception,requestHeaders,responseHeaders);
}
};
}
}
private GreeterGrpc.GreeterBlockingStub stub;
@SpyBean
HalfCloseInterceptor halfCloseInterceptor;
private static Locale systemDefaultLocale;
@BeforeClass
public static void setLocaleToEnglish() {
systemDefaultLocale = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
}
@AfterClass
public static void resetDefaultLocale() {
Locale.setDefault(systemDefaultLocale);
}
@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")
));
assertResponseHeaders(e);
}
@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()))
));
assertResponseHeaders(e);
}
@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")
));
assertResponseHeaders(e);
}
@Test
public void noHalfCloseAfterFailedValidation() {
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));
verify(halfCloseInterceptor, never()).onHalfClose();
}
String getFieldName(int fieldNumber) {
return GreeterOuterClass.Person.getDescriptor().findFieldByNumber(fieldNumber).getName();
}
private void assertResponseHeaders(StatusRuntimeException e) {
final String header = e.getTrailers().get(Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER));
assertThat(header, Matchers.is("val"));
}
}