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/servicecomb-java-chassis.git
commit cf54932515627d5a681e00d6b894559984a19576 Author: weichao666 <[email protected]> AuthorDate: Fri Nov 23 16:26:03 2018 +0800 [SCB-925] add cookies,query and header integration-tests --- .../servicecomb/common/rest/codec/RestCodec.java | 3 +- .../rest/codec/param/CookieProcessorCreator.java | 14 ++-- .../rest/codec/param/FormProcessorCreator.java | 16 ++--- .../rest/codec/param/HeaderProcessorCreator.java | 14 ++-- .../rest/codec/param/QueryProcessorCreator.java | 10 +-- .../rest/codec/param/TestCookieProcessor.java | 6 +- .../common/rest/codec/param/TestFormProcessor.java | 4 +- .../rest/codec/param/TestHeaderProcessor.java | 4 +- .../rest/codec/param/TestQueryProcessor.java | 2 +- .../it/testcase/TestAnnotatedAttribute.java | 79 ++++++++++++++++++++++ .../servicecomb/it/testcase/TestDefaultValue.java | 25 +++++++ .../schema/AnnotatedAttributeSpringmvcSchema.java | 12 ++++ .../it/schema/DefaultValueSpringmvcSchema.java | 10 +++ 13 files changed, 157 insertions(+), 42 deletions(-) diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java index 5642642..ac65d61 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestCodec.java @@ -24,6 +24,7 @@ import javax.ws.rs.core.Response.Status; import org.apache.servicecomb.common.rest.definition.RestOperationMeta; import org.apache.servicecomb.common.rest.definition.RestParam; +import org.apache.servicecomb.foundation.common.utils.ExceptionUtils; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,7 +67,7 @@ public final class RestCodec { } catch (Exception e) { LOG.error("Parameter is not valid for operation {}. ", restOperation.getOperationMeta().getMicroserviceQualifiedName(), - e); + ExceptionUtils.getExceptionMessageWithoutTrace(e)); // give standard http error code for invalid parameter throw new InvocationException(Status.BAD_REQUEST, "Parameter is not valid."); } diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/CookieProcessorCreator.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/CookieProcessorCreator.java index 894a379..80f1231 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/CookieProcessorCreator.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/CookieProcessorCreator.java @@ -47,7 +47,7 @@ public class CookieProcessorCreator implements ParamValueProcessorCreator { Cookie[] cookies = request.getCookies(); Object value = null; if (cookies == null || cookies.length == 0) { - value = checkRequiredAndDefaultValue(value); + value = checkRequiredAndDefaultValue(); return convertValue(value, targetType); } @@ -57,20 +57,16 @@ public class CookieProcessorCreator implements ParamValueProcessorCreator { } } if (value == null) { - value = checkRequiredAndDefaultValue(value); + value = checkRequiredAndDefaultValue(); } return convertValue(value, targetType); } - private Object checkRequiredAndDefaultValue(Object value) { + private Object checkRequiredAndDefaultValue() throws Exception { if (isRequired()) { - throw new InvocationException(Status.BAD_REQUEST, "Parameter is not valid, required is true"); + throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } - Object defaultValue = getDefaultValue(); - if (defaultValue != null) { - return defaultValue; - } - return value; + return getDefaultValue(); } @Override diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/FormProcessorCreator.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/FormProcessorCreator.java index 7e4f448..bbb70d2 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/FormProcessorCreator.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/FormProcessorCreator.java @@ -44,7 +44,7 @@ public class FormProcessorCreator implements ParamValueProcessorCreator { } @Override - public Object getValue(HttpServletRequest request) { + public Object getValue(HttpServletRequest request) throws Exception { @SuppressWarnings("unchecked") Map<String, Object> forms = (Map<String, Object>) request.getAttribute(RestConst.FORM_PARAMETERS); if (forms != null && !forms.isEmpty()) { @@ -55,28 +55,24 @@ public class FormProcessorCreator implements ParamValueProcessorCreator { Object values = request.getParameterValues(paramPath); //Even if the paramPath does not exist, it won't be null at now, may be optimized in the future if (values == null) { - values = checkRequiredAndDefaultValue(values); + values = checkRequiredAndDefaultValue(); } return convertValue(values, targetType); } Object value = request.getParameter(paramPath); if (value == null) { - value = checkRequiredAndDefaultValue(value); + value = checkRequiredAndDefaultValue(); } return convertValue(value, targetType); } - private Object checkRequiredAndDefaultValue(Object values) { + private Object checkRequiredAndDefaultValue() throws Exception { if (isRequired()) { - throw new InvocationException(Status.BAD_REQUEST, "Parameter is not valid, required is true"); - } - Object defaultValue = getDefaultValue(); - if (defaultValue != null) { - return defaultValue; + throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } - return values; + return getDefaultValue(); } @Override diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/HeaderProcessorCreator.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/HeaderProcessorCreator.java index d946afd..726e637 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/HeaderProcessorCreator.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/HeaderProcessorCreator.java @@ -53,7 +53,7 @@ public class HeaderProcessorCreator implements ParamValueProcessorCreator { Enumeration<?> headerValues = request.getHeaders(paramPath); //Even if the paramPath does not exist, it won't be null at now, may be optimized in the future if (headerValues == null) { - Object obj = checkRequiredAndDefaultValue(headerValues); + Object obj = checkRequiredAndDefaultValue(); if (obj instanceof Enumeration) { headerValues = (Enumeration<?>) obj; } @@ -64,22 +64,18 @@ public class HeaderProcessorCreator implements ParamValueProcessorCreator { } else { value = request.getHeader(paramPath); if (value == null) { - value = checkRequiredAndDefaultValue(value); + value = checkRequiredAndDefaultValue(); } } return convertValue(value, targetType); } - private Object checkRequiredAndDefaultValue(Object headerValue) { + private Object checkRequiredAndDefaultValue() throws Exception { if (isRequired()) { - throw new InvocationException(Status.BAD_REQUEST, "Parameter is not valid, required is true"); + throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } - Object defaultValue = getDefaultValue(); - if (defaultValue != null) { - return defaultValue; - } - return headerValue; + return getDefaultValue(); } @Override diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java index b449ae2..2cff361 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java @@ -64,7 +64,7 @@ public class QueryProcessorCreator implements ParamValueProcessorCreator { value = request.getParameterValues(paramPath); //Even if the paramPath does not exist, it won't be null at now, may be optimized in the future if (value == null) { - value = checkRequiredAndDefaultValue(value); + value = checkRequiredAndDefaultValue(); } } else { value = request.getParameter(paramPath); @@ -75,7 +75,7 @@ public class QueryProcessorCreator implements ParamValueProcessorCreator { } } if (value == null) { - value = checkRequiredAndDefaultValue(value); + value = checkRequiredAndDefaultValue(); } if (null != collectionFormat) { value = collectionFormat.splitParam((String) value); @@ -85,15 +85,15 @@ public class QueryProcessorCreator implements ParamValueProcessorCreator { return convertValue(value, targetType); } - private Object checkRequiredAndDefaultValue(Object value) { + private Object checkRequiredAndDefaultValue() throws Exception { if (isRequired()) { - throw new InvocationException(Status.BAD_REQUEST, "Parameter is not valid, required is true"); + throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } Object defaultValue = getDefaultValue(); if (!ignoreDefaultValue && defaultValue != null) { return defaultValue; } - return value; + return null; } @Override diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestCookieProcessor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestCookieProcessor.java index 4628b58..ecb347e 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestCookieProcessor.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestCookieProcessor.java @@ -75,7 +75,7 @@ public class TestCookieProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } @@ -94,7 +94,7 @@ public class TestCookieProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } @@ -128,7 +128,7 @@ public class TestCookieProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestFormProcessor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestFormProcessor.java index 35f869d..b91b18a 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestFormProcessor.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestFormProcessor.java @@ -122,7 +122,7 @@ public class TestFormProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } @@ -140,7 +140,7 @@ public class TestFormProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java index 70b605b..37ddc56 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java @@ -111,7 +111,7 @@ public class TestHeaderProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } @@ -129,7 +129,7 @@ public class TestHeaderProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java index c7e6d5b..4a3e6d7 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessor.java @@ -105,7 +105,7 @@ public class TestQueryProcessor { processor.getValue(request); Assert.assertEquals("required is true, throw exception", "not throw exception"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Parameter is not valid, required is true")); + Assert.assertTrue(e.getMessage().contains("Parameter is required.")); } } diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAnnotatedAttribute.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAnnotatedAttribute.java index 732f62c..232c1a1 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAnnotatedAttribute.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAnnotatedAttribute.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.Map; import org.apache.servicecomb.it.Consumers; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.junit.Test; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -54,6 +55,16 @@ public class TestAnnotatedAttribute { } @Test + public void fromCookieRequired_springmvc_rt() { + fromCookieRequired_rt(consumersSpringmvc); + } + + @Test + public void fromCookieDefaultValue_springmvc_rt() { + fromCookieDefaultValue_rt(consumersSpringmvc); + } + + @Test public void fromPath_springmvc_rt() { fromPath_rt(consumersSpringmvc); } @@ -109,6 +120,74 @@ public class TestAnnotatedAttribute { assertEquals("default,fromValue,fromName", result.getBody()); } + protected void fromCookieRequired_rt(Consumers<AnnotatedAttributeIntf> consumers) { + HttpHeaders headers = new HttpHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + try { + consumers.getSCBRestTemplate() + .exchange("/fromCookieRequired", + HttpMethod.GET, + requestEntity, + String.class); + assertEquals("required is true, throw exception", "but not throw exception"); + } catch (InvocationException e) { + assertEquals(400, e.getStatusCode()); + assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=Parameter is not valid.]", + e.getMessage()); + } + headers.add(HttpHeaders.COOKIE, "input1=default1"); + requestEntity = new HttpEntity<>(headers); + try { + consumers.getSCBRestTemplate() + .exchange("/fromCookieRequired", + HttpMethod.GET, + requestEntity, + String.class); + assertEquals("required is true, throw exception", "but not throw exception"); + } catch (InvocationException e) { + assertEquals(400, e.getStatusCode()); + assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=Parameter is not valid.]", + e.getMessage()); + } + headers.add(HttpHeaders.COOKIE, "input=joker"); + requestEntity = new HttpEntity<>(headers); + ResponseEntity<String> result = consumers.getSCBRestTemplate() + .exchange("/fromCookieRequired", + HttpMethod.GET, + requestEntity, + String.class); + assertEquals("joker", result.getBody()); + } + + protected void fromCookieDefaultValue_rt(Consumers<AnnotatedAttributeIntf> consumers) { + HttpHeaders headers = new HttpHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + ResponseEntity<String> result = consumers.getSCBRestTemplate() + .exchange("/fromCookieDefaultValue", + HttpMethod.GET, + requestEntity, + String.class); + assertEquals("default", result.getBody()); + + headers.add(HttpHeaders.COOKIE, "input1=jokers"); + requestEntity = new HttpEntity<>(headers); + result = consumers.getSCBRestTemplate() + .exchange("/fromCookieDefaultValue", + HttpMethod.GET, + requestEntity, + String.class); + assertEquals("default", result.getBody()); + + headers.add(HttpHeaders.COOKIE, "input=joker"); + requestEntity = new HttpEntity<>(headers); + result = consumers.getSCBRestTemplate() + .exchange("/fromCookieDefaultValue", + HttpMethod.GET, + requestEntity, + String.class); + assertEquals("joker", result.getBody()); + } + protected void fromPath_rt(Consumers<AnnotatedAttributeIntf> consumers) { String result = consumers.getSCBRestTemplate() .getForObject("/fromPath/{1}/{2}/{3}", diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java index 1c172ee..8062ca1 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultValue.java @@ -19,6 +19,7 @@ package org.apache.servicecomb.it.testcase; import static org.junit.Assert.assertEquals; import org.apache.servicecomb.it.Consumers; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.junit.Test; public class TestDefaultValue { @@ -217,6 +218,18 @@ public class TestDefaultValue { } @Test + public void stringQueryTrue_springmvc_rt() { + try { + consumersSpringmvc.getSCBRestTemplate().getForObject("/stringQueryTrue", String.class); + assertEquals("required is true, throw exception", "not throw exception"); + } catch (InvocationException e) { + assertEquals(400, e.getStatusCode()); + assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=Parameter is not valid.]", + e.getMessage()); + } + } + + @Test public void intHeader_springmvc_intf() { assertEquals(defaultInt, consumersSpringmvc.getIntf().intHeader(null)); } @@ -249,6 +262,18 @@ public class TestDefaultValue { } @Test + public void stringHeaderTrue_springmvc_rt() { + try { + consumersSpringmvc.getSCBRestTemplate().getForObject("/stringHeaderTrue", String.class); + assertEquals("required is true, throw exception", "not throw exception"); + } catch (InvocationException e) { + assertEquals(400, e.getStatusCode()); + assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=Parameter is not valid.]", + e.getMessage()); + } + } + + @Test public void intForm_springmvc_intf() { assertEquals(defaultInt, consumersSpringmvc.getIntf().intForm(null)); } diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AnnotatedAttributeSpringmvcSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AnnotatedAttributeSpringmvcSchema.java index 6b8691d..9439eeb 100644 --- a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AnnotatedAttributeSpringmvcSchema.java +++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AnnotatedAttributeSpringmvcSchema.java @@ -49,6 +49,18 @@ public class AnnotatedAttributeSpringmvcSchema { return inputs + "," + inputs2 + "," + inputs3; } + @GetMapping("fromCookieRequired") + public String fromCookieRequired( + @CookieValue(name = "input", required = true) String input) { + return input; + } + + @GetMapping("fromCookieDefaultValue") + public String fromCookieDefaultValue( + @CookieValue(name = "input", required = true, defaultValue = "default") String input) { + return input; + } + @GetMapping("fromPath/{input}/{input2}/{input3}") public String fromPath(@PathVariable("input") String inputs, @PathVariable(value = "input2") String inputs2, @PathVariable(name = "input3") String inputs3) { diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DefaultValueSpringmvcSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DefaultValueSpringmvcSchema.java index 9ed160c..0685858 100644 --- a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DefaultValueSpringmvcSchema.java +++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/DefaultValueSpringmvcSchema.java @@ -71,11 +71,21 @@ public class DefaultValueSpringmvcSchema { return input; } + @GetMapping("stringQueryTrue") + public String stringQueryTrue(@RequestParam(value = "input") String input) { + return input; + } + @GetMapping("stringHeader") public String stringHeader(@RequestHeader(value = "input", defaultValue = "string") String input) { return input; } + @GetMapping("stringHeaderTrue") + public String stringHeaderTrue(@RequestHeader(value = "input") String input) { + return input; + } + @ApiImplicitParams({ @ApiImplicitParam(name = "input", dataType = "string", paramType = "form", value = "", defaultValue = "string", required = false)}) @PostMapping(path = "stringForm")
