Search in sources :

Example 1 with RestParam

use of org.apache.servicecomb.common.rest.definition.RestParam in project incubator-servicecomb-java-chassis by apache.

the class TestRestCodec method testRestToArgsInstanceExcetpion.

@Test
public void testRestToArgsInstanceExcetpion(@Mocked HttpServletRequest request, @Mocked RestOperationMeta restOperation, @Mocked RestParam restParam, @Mocked ParamValueProcessor processer) throws Exception {
    List<RestParam> params = new ArrayList<>();
    params.add(restParam);
    InvocationException exception = new InvocationException(Status.BAD_REQUEST, "Parameter is not valid.");
    new Expectations() {

        {
            restOperation.getParamList();
            result = params;
            restParam.getParamProcessor();
            result = processer;
            processer.getValue(request);
            result = exception;
        }
    };
    boolean success = false;
    try {
        RestCodec.restToArgs(request, restOperation);
        success = true;
    } catch (InvocationException e) {
        Assert.assertEquals(e.getStatusCode(), Status.BAD_REQUEST.getStatusCode());
    }
    Assert.assertEquals(success, false);
}
Also used : Expectations(mockit.Expectations) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) ArrayList(java.util.ArrayList) RestParam(org.apache.servicecomb.common.rest.definition.RestParam) Test(org.junit.Test)

Example 2 with RestParam

use of org.apache.servicecomb.common.rest.definition.RestParam in project incubator-servicecomb-java-chassis by apache.

the class RestCodec method argsToRest.

public static void argsToRest(Object[] args, RestOperationMeta restOperation, RestClientRequest clientRequest) throws Exception {
    int paramSize = restOperation.getParamList().size();
    if (paramSize == 0) {
        return;
    }
    if (paramSize != args.length) {
        throw new Exception("wrong number of arguments");
    }
    for (int idx = 0; idx < paramSize; idx++) {
        RestParam param = restOperation.getParamList().get(idx);
        param.getParamProcessor().setValue(clientRequest, args[idx]);
    }
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) RestParam(org.apache.servicecomb.common.rest.definition.RestParam)

Example 3 with RestParam

use of org.apache.servicecomb.common.rest.definition.RestParam in project incubator-servicecomb-java-chassis by apache.

the class InvocationToHttpServletRequest method getParameter.

@Override
public String getParameter(String name) {
    RestParam param = swaggerOperation.getParamByName(name);
    if (param == null) {
        return null;
    }
    Object value = param.getValue(args);
    if (value == null) {
        return null;
    }
    return String.valueOf(value);
}
Also used : RestParam(org.apache.servicecomb.common.rest.definition.RestParam)

Example 4 with RestParam

use of org.apache.servicecomb.common.rest.definition.RestParam in project incubator-servicecomb-java-chassis by apache.

the class TestRestCodec method beforeClass.

@BeforeClass
public static void beforeClass() {
    restOperation = Mockito.mock(RestOperationMeta.class);
    // clientRequest = Mockito.mock(RestClientRequest.class);
    paramList = new ArrayList<>();
    Parameter hp = new HeaderParameter();
    hp.setName("header");
    paramList.add(new RestParam(0, hp, int.class));
    when(restOperation.getParamList()).thenReturn(paramList);
}
Also used : RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) Parameter(io.swagger.models.parameters.Parameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) RestParam(org.apache.servicecomb.common.rest.definition.RestParam) BeforeClass(org.junit.BeforeClass)

Example 5 with RestParam

use of org.apache.servicecomb.common.rest.definition.RestParam in project incubator-servicecomb-java-chassis by apache.

the class TestRestCodec method testRestToArgsExcetpion.

@Test
public void testRestToArgsExcetpion(@Mocked HttpServletRequest request, @Mocked RestOperationMeta restOperation, @Mocked RestParam restParam, @Mocked ParamValueProcessor processer) throws Exception {
    List<RestParam> params = new ArrayList<>();
    params.add(restParam);
    new Expectations() {

        {
            restOperation.getParamList();
            result = params;
            restParam.getParamProcessor();
            result = processer;
            processer.getValue(request);
            result = new Exception("bad request parame");
        }
    };
    boolean success = false;
    try {
        RestCodec.restToArgs(request, restOperation);
        success = true;
    } catch (InvocationException e) {
        Assert.assertEquals(590, e.getStatusCode());
        Assert.assertEquals("Parameter is not valid.", ((CommonExceptionData) e.getErrorData()).getMessage());
    }
    Assert.assertEquals(success, false);
}
Also used : Expectations(mockit.Expectations) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) ArrayList(java.util.ArrayList) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) RestParam(org.apache.servicecomb.common.rest.definition.RestParam) Test(org.junit.Test)

Aggregations

RestParam (org.apache.servicecomb.common.rest.definition.RestParam)8 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)4 ArrayList (java.util.ArrayList)2 Expectations (mockit.Expectations)2 Test (org.junit.Test)2 HeaderParameter (io.swagger.models.parameters.HeaderParameter)1 Parameter (io.swagger.models.parameters.Parameter)1 HashMap (java.util.HashMap)1 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)1 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)1 BeforeClass (org.junit.BeforeClass)1