This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 9f18a56cd7b2f172401bc1c4aedebbda27e1e03f Author: liubao <[email protected]> AuthorDate: Thu Aug 16 21:26:25 2018 +0800 [SCB-847]add validation related test case --- .../servicecomb/demo/jaxrs/client/JaxrsClient.java | 2 + .../client/validation/ValidationServiceClient.java | 77 ++++++++++++++++++++++ .../jaxrs/server/validation/ValidationService.java | 43 ++++++++++++ .../jaxrs/server/validation/ValidationModel.java | 57 ++++++++++++++++ 4 files changed, 179 insertions(+) diff --git a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java index 2bd2a74..d116af4 100644 --- a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java +++ b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java @@ -36,6 +36,7 @@ import org.apache.servicecomb.demo.compute.Person; import org.apache.servicecomb.demo.jaxrs.client.beanParam.BeanParamPojoClient; import org.apache.servicecomb.demo.jaxrs.client.beanParam.BeanParamRestTemplateClient; import org.apache.servicecomb.demo.jaxrs.client.pojoDefault.DefaultModelServiceClient; +import org.apache.servicecomb.demo.jaxrs.client.validation.ValidationServiceClient; import org.apache.servicecomb.demo.validator.Student; import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.foundation.common.utils.Log4jUtils; @@ -85,6 +86,7 @@ public class JaxrsClient { BeanParamRestTemplateClient beanParamRestTemplateClient = new BeanParamRestTemplateClient(); beanParamRestTemplateClient.testAll(); DefaultModelServiceClient.run(); + ValidationServiceClient.run(); } private static void testCompute(RestTemplate template) throws Exception { diff --git a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java new file mode 100644 index 0000000..727a050 --- /dev/null +++ b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.demo.jaxrs.client.validation; + +import java.util.ArrayList; + +import org.apache.servicecomb.core.CseContext; +import org.apache.servicecomb.demo.TestMgr; +import org.apache.servicecomb.demo.jaxrs.server.validation.ValidationModel; +import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; +import org.springframework.web.client.RestTemplate; + +public class ValidationServiceClient { + private static RestTemplate template = RestTemplateBuilder.create(); + + private static String urlPrefix = "cse://jaxrs/ValidationService"; + + public static void run() { + // highway do not support this feature + CseContext.getInstance().getConsumerProviderManager().setTransport("jaxrs", "rest"); + testValidation(); + } + + private static void testValidation() { + ValidationModel model = new ValidationModel(); + model.setAge(20); + model.setMembers(new ArrayList<>()); + model.setName("name"); + ValidationModel result = template.postForObject(urlPrefix + "/validate", model, ValidationModel.class); + TestMgr.check(result.getAge(), 20); + TestMgr.check(result.getName(), "name"); + TestMgr.check(result.getMembers().size(), 0); + + try { + model.setAge(null); + template.postForObject(urlPrefix + "/validate", model, ValidationModel.class); + TestMgr.check(false, true); + } catch (InvocationException e) { + TestMgr.check(e.getErrorData().toString().contains("age"), true); + } + + try { + model.setAge(20); + model.setMembers(null); + template.postForObject(urlPrefix + "/validate", model, ValidationModel.class); + TestMgr.check(false, true); + } catch (InvocationException e) { + TestMgr.check(e.getErrorData().toString().contains("member"), true); + } + + String strResult = template.getForObject(urlPrefix + "/validateQuery?name=", String.class); + TestMgr.check(strResult, ""); + + try { + template.getForObject(urlPrefix + "/validateQuery", String.class); + TestMgr.check(false, true); + } catch (InvocationException e) { + TestMgr.check(e.getErrorData().toString().contains("null"), true); + } + } +} diff --git a/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/validation/ValidationService.java b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/validation/ValidationService.java new file mode 100644 index 0000000..7324669 --- /dev/null +++ b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/validation/ValidationService.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.demo.jaxrs.server.validation; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + +import org.apache.servicecomb.provider.rest.common.RestSchema; + +@RestSchema(schemaId = "ValidationService") +@Path("ValidationService") +public class ValidationService { + @Path("/validate") + @POST + public ValidationModel errorCode(@Valid ValidationModel request) { + return request; + } + + @Path("/validateQuery") + @GET + public String queryValidate(@NotNull @QueryParam("name") String name) { + return name; + } +} diff --git a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/jaxrs/server/validation/ValidationModel.java b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/jaxrs/server/validation/ValidationModel.java new file mode 100644 index 0000000..09e2612 --- /dev/null +++ b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/jaxrs/server/validation/ValidationModel.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.demo.jaxrs.server.validation; + +import java.util.List; + +import javax.validation.constraints.NotNull; + +public class ValidationModel { + @NotNull + private Integer age; + + @NotNull + private List<String> members; + + @NotNull + private String name; + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public List<String> getMembers() { + return members; + } + + public void setMembers(List<String> members) { + this.members = members; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}
