Skip to content

Commit

Permalink
Merge 7ea5691 into 4f8f810
Browse files Browse the repository at this point in the history
  • Loading branch information
acsukesh committed Apr 10, 2018
2 parents 4f8f810 + 7ea5691 commit 47234cb
Show file tree
Hide file tree
Showing 20 changed files with 532 additions and 69 deletions.
Expand Up @@ -28,9 +28,11 @@
import org.apache.servicecomb.demo.DemoConst;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.demo.compute.Person;
import org.apache.servicecomb.demo.validator.Student;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -57,6 +59,7 @@ public static void run() throws Exception {
CodeFirstRestTemplate codeFirstClient = new CodeFirstRestTemplateJaxrs();
codeFirstClient.testCodeFirst(templateNew, "jaxrs", "/codeFirstJaxrs/");
testCompute(templateNew);
testValidator(templateNew);
}

private static void testCompute(RestTemplate template) throws Exception {
Expand All @@ -76,6 +79,24 @@ private static void testCompute(RestTemplate template) throws Exception {
}
}

private static void testValidator(RestTemplate template) throws Exception {
String microserviceName = "jaxrs";
for (String transport : DemoConst.transports) {
CseContext.getInstance().getConsumerProviderManager().setTransport(microserviceName, transport);
TestMgr.setMsg(microserviceName, transport);

String cseUrlPrefix = "cse://" + microserviceName + "/validator/";

testValidatorAddSuccess(template, cseUrlPrefix);
testValidatorAddFail(template, cseUrlPrefix);
testValidatorSayHiSuccess(template, cseUrlPrefix);
testValidatorSayHiFail(template, cseUrlPrefix);
testValidatorExchangeSuccess(template, cseUrlPrefix);
testValidatorExchangeFail(template, cseUrlPrefix);
}
}


private static void testGet(RestTemplate template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
Expand Down Expand Up @@ -147,4 +168,73 @@ private static void testRawJsonParam(RestTemplate template, String cseUrlPrefix)
TestMgr.check("hello Tom",
template.postForObject(cseUrlPrefix + "/compute/testrawjson", jsonPerson, String.class));
}

private static void testValidatorAddFail(RestTemplate template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
params.put("b", "3");
boolean isExcep = false;
try {
template.postForObject(cseUrlPrefix + "add", params, Integer.class);
} catch (InvocationException e) {
isExcep = true;
TestMgr.check(490, e.getStatus().getStatusCode());
}

TestMgr.check(true, isExcep);
}

private static void testValidatorAddSuccess(RestTemplate template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
params.put("b", "20");
int result = template.postForObject(cseUrlPrefix + "add", params, Integer.class);
TestMgr.check(25, result);
}

private static void testValidatorSayHiFail(RestTemplate template, String cseUrlPrefix) {
boolean isExcep = false;
try {
template.exchange(cseUrlPrefix + "sayhi/{name}", HttpMethod.PUT, null, String.class, "te");
} catch (InvocationException e) {
isExcep = true;
TestMgr.check(490, e.getStatus().getStatusCode());
}
TestMgr.check(true, isExcep);
}

private static void testValidatorSayHiSuccess(RestTemplate template, String cseUrlPrefix) {
ResponseEntity<String> responseEntity =
template.exchange(cseUrlPrefix + "sayhi/{name}", HttpMethod.PUT, null, String.class, "world");
TestMgr.check(202, responseEntity.getStatusCode());
TestMgr.check("world sayhi", responseEntity.getBody());
}

private static void testValidatorExchangeFail(RestTemplate template, String cseUrlPrefix) {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON);
Student student = new Student();
student.setName("");
student.setAge(25);
boolean isExcep = false;
try {
HttpEntity<Student> requestEntity = new HttpEntity<>(student, headers);
template.exchange(cseUrlPrefix + "/sayhello",
HttpMethod.POST,
requestEntity,
Student.class);
} catch (InvocationException e) {
isExcep = true;
TestMgr.check(490, e.getStatus().getStatusCode());
}
TestMgr.check(true, isExcep);
}

private static void testValidatorExchangeSuccess(RestTemplate template, String cseUrlPrefix) {
Student student = new Student();
student.setName("test");
student.setAge(15);
Student result = template.postForObject(cseUrlPrefix + "sayhello", student, Student.class);
TestMgr.check("hello test 15", result);
}
}
129 changes: 65 additions & 64 deletions demo/demo-jaxrs/jaxrs-server/pom.xml
Expand Up @@ -17,68 +17,69 @@
-->

<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-jaxrs</artifactId>
<version>1.0.0-m2-SNAPSHOT</version>
</parent>
<artifactId>jaxrs-server</artifactId>
<name>Java Chassis::Demo::JAXRS::Server</name>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-schema</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-jaxrs</artifactId>
</dependency>
</dependencies>

<properties>
<demo.main>org.apache.servicecomb.demo.jaxrs.server.JaxrsServer</demo.main>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>docker-build-config</artifactId>
<version>1.0.0-m2-SNAPSHOT</version>
</mixin>
</mixins>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-jaxrs</artifactId>
<version>1.0.0-m2-SNAPSHOT</version>
</parent>
<artifactId>jaxrs-server</artifactId>
<name>Java Chassis::Demo::JAXRS::Server</name>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-schema</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-invocation-validator</artifactId>
</dependency>
</dependencies>
<properties>
<demo.main>org.apache.servicecomb.demo.jaxrs.server.JaxrsServer</demo.main>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>docker-build-config</artifactId>
<version>1.0.0-m2-SNAPSHOT</version>
</mixin>
</mixins>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@@ -0,0 +1,61 @@
/*
* 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;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.servicecomb.demo.validator.Student;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
import org.hibernate.validator.constraints.Length;

@RestSchema(schemaId = "validator")
@Path("/validator")
@Produces(MediaType.APPLICATION_JSON)
public class Validator {

@Path("/add")
@POST
public int add(@FormParam("a") int a, @Min(20) @FormParam("b") int b) {
return a + b;
}

@Path("/sayhi/{name}")
@PUT
public String sayHi(@Length(min = 3) @PathParam("name") String name) {
ContextUtils.getInvocationContext().setStatus(202);
return name + " sayhi";
}

@Path("/sayhello")
@POST
public Student sayHello(@Valid Student student) {
student.setName("hello " + student.getName());
student.setAge(student.getAge());
return student;
}

}
@@ -0,0 +1,58 @@
/*
* 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.validator;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

public class Student {
@NotNull
private String name;

@Max(20)
private int age;

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public Student() {
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Student(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return name + " " + age;
}
}
1 change: 0 additions & 1 deletion demo/pom.xml
Expand Up @@ -47,7 +47,6 @@
<module>demo-multiple</module>
<module>demo-signature</module>
<module>demo-edge</module>

<module>perf</module>
</modules>

Expand Down
5 changes: 5 additions & 0 deletions java-chassis-dependencies/pom.xml
Expand Up @@ -899,6 +899,11 @@
<artifactId>swagger-invocation-springmvc</artifactId>
<version>1.0.0-m2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-invocation-validator</artifactId>
<version>1.0.0-m2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>service-registry</artifactId>
Expand Down

0 comments on commit 47234cb

Please sign in to comment.