Search in sources :

Example 1 with URLPathBuilder

use of io.servicecomb.common.rest.definition.path.URLPathBuilder in project java-chassis by ServiceComb.

the class TestVertxHttpMethod method testDoMethod.

@Test
public void testDoMethod(@Mocked HttpClient httpClient) throws Exception {
    Context context = new MockUp<Context>() {

        @Mock
        public void runOnContext(Handler<Void> action) {
            action.handle(null);
        }
    }.getMockInstance();
    HttpClientWithContext httpClientWithContext = new HttpClientWithContext(httpClient, context);
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
    RestOperationMeta swaggerRestOperation = Mockito.mock(RestOperationMeta.class);
    Endpoint endpoint = Mockito.mock(Endpoint.class);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    URLPathBuilder urlPathBuilder = Mockito.mock(URLPathBuilder.class);
    Mockito.when(swaggerRestOperation.getPathBuilder()).thenReturn(urlPathBuilder);
    operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    Mockito.when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
    Mockito.when(invocation.getEndpoint()).thenReturn(endpoint);
    Mockito.when(request.exceptionHandler(Mockito.any())).then(answer -> null);
    this.doMethod(httpClientWithContext, invocation, asyncResp);
    Assert.assertTrue(true);
}
Also used : HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Context(io.vertx.core.Context) Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) AsyncResponse(io.servicecomb.core.AsyncResponse) Mock(mockit.Mock) Test(org.junit.Test)

Example 2 with URLPathBuilder

use of io.servicecomb.common.rest.definition.path.URLPathBuilder in project java-chassis by ServiceComb.

the class TestVertxHttpMethod method testCreateRequestPath.

@Test
public void testCreateRequestPath() throws Exception {
    Invocation invocation = Mockito.mock(Invocation.class);
    RestOperationMeta restOperationMeta = Mockito.mock(RestOperationMeta.class);
    URLPathBuilder urlPathBuilder = Mockito.mock(URLPathBuilder.class);
    Mockito.when(restOperationMeta.getPathBuilder()).thenReturn(urlPathBuilder);
    String pathUrl = this.createRequestPath(invocation, restOperationMeta);
    Assert.assertNull(pathUrl);
}
Also used : Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) Test(org.junit.Test)

Example 3 with URLPathBuilder

use of io.servicecomb.common.rest.definition.path.URLPathBuilder in project java-chassis by ServiceComb.

the class TestPath method testUrlPathBuilder.

@Test
public void testUrlPathBuilder() throws Exception {
    Map<String, RestParam> paramMap = new HashMap<>();
    Parameter pathParameter = new PathParameter();
    pathParameter.setName("id");
    RestParam oRestParam = new RestParam(0, pathParameter, int.class);
    paramMap.put(oRestParam.getParamName(), oRestParam);
    Parameter queryParameter = new QueryParameter();
    queryParameter.setName("q");
    oRestParam = new RestParam(1, queryParameter, String.class);
    paramMap.put(oRestParam.getParamName(), oRestParam);
    URLPathBuilder oURLPathBuilder = new URLPathBuilder("/root/{id}", paramMap);
    Object[] args = new Object[] { 100, "query" };
    Assert.assertEquals("/root/100?q=query", oURLPathBuilder.createRequestPath(args));
    Assert.assertEquals("/root/100", oURLPathBuilder.createPathString(args));
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) HashMap(java.util.HashMap) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) PathParameter(io.swagger.models.parameters.PathParameter) Test(org.junit.Test)

Example 4 with URLPathBuilder

use of io.servicecomb.common.rest.definition.path.URLPathBuilder in project java-chassis by ServiceComb.

the class RestOperationMeta method init.

public void init(OperationMeta operationMeta) {
    this.operationMeta = operationMeta;
    Swagger swagger = operationMeta.getSchemaMeta().getSwagger();
    Operation operation = operationMeta.getSwaggerOperation();
    this.produces = operation.getProduces();
    if (produces == null) {
        this.produces = swagger.getProduces();
    }
    setAbsolutePath(concatPath(swagger.getBasePath(), operationMeta.getOperationPath()));
    this.createProduceProcessors();
    Method method = operationMeta.getMethod();
    Type[] genericParamTypes = method.getGenericParameterTypes();
    if (genericParamTypes.length != operation.getParameters().size()) {
        throw new Error("Param count is not equal between swagger and method,  path=" + absolutePath);
    }
    // 初始化所有rest param
    for (int idx = 0; idx < genericParamTypes.length; idx++) {
        Parameter parameter = operation.getParameters().get(idx);
        Type genericParamType = genericParamTypes[idx];
        RestParam param = new RestParam(idx, parameter, genericParamType);
        addParam(param);
    }
    this.pathBuilder = new URLPathBuilder(absolutePath, paramMap);
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Swagger(io.swagger.models.Swagger) Parameter(io.swagger.models.parameters.Parameter) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method)

Example 5 with URLPathBuilder

use of io.servicecomb.common.rest.definition.path.URLPathBuilder in project java-chassis by ServiceComb.

the class TestGenericServletMockRequest method testGenericServletMockRequest.

@Test
public void testGenericServletMockRequest() throws Exception {
    Invocation invocation = Mockito.mock(Invocation.class);
    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    RestOperationMeta swaggerOperation = Mockito.mock(RestOperationMeta.class);
    Mockito.when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerOperation);
    GenericServletMockRequest lGenericServletMockRequest = new GenericServletMockRequest(invocation);
    URLPathBuilder uRLPathBuilder = Mockito.mock(URLPathBuilder.class);
    Mockito.when(swaggerOperation.getPathBuilder()).thenReturn(uRLPathBuilder);
    Mockito.when(swaggerOperation.getPathBuilder().createPathString(null)).thenReturn("test");
    RestParam param = Mockito.mock(RestParam.class);
    Mockito.when(swaggerOperation.getParamByName("1")).thenReturn(param);
    Mockito.when(param.getValue(null)).thenReturn("1");
    List<RestParam> restParam = new ArrayList<>();
    restParam.add(param);
    Mockito.when(swaggerOperation.getParamList()).thenReturn(restParam);
    Assert.assertEquals("1", lGenericServletMockRequest.getParameter("1"));
    Assert.assertNull(lGenericServletMockRequest.getParameterValues("1"));
    Assert.assertEquals("1", lGenericServletMockRequest.getHeader("1"));
    Assert.assertEquals(1, lGenericServletMockRequest.getIntHeader("1"));
    Assert.assertEquals("test", lGenericServletMockRequest.getServletPath());
    Mockito.when(swaggerOperation.getParamByName("test")).thenReturn(null);
    Assert.assertEquals(null, lGenericServletMockRequest.getParameter("test"));
    Assert.assertNull(lGenericServletMockRequest.getParameterValues("test"));
    Map<String, String[]> paramMap = new HashMap<String, String[]>();
    paramMap.put(null, null);
    Assert.assertEquals(paramMap, lGenericServletMockRequest.getParameterMap());
    try {
        lGenericServletMockRequest.getAttribute("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getAttributeNames();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getCharacterEncoding();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.setCharacterEncoding("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getContentLength();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getContentLengthLong();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getContentType();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getInputStream();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getParameterNames();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getProtocol();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getScheme();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getServerName();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getServerPort();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getReader();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRemoteAddr();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRemoteHost();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.setAttribute("", "");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.removeAttribute("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getLocale();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getLocales();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isSecure();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRequestDispatcher("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRealPath("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRemotePort();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getLocalName();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getLocalAddr();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getLocalPort();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getServletContext();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.startAsync();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.startAsync(null, null);
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isAsyncStarted();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isAsyncSupported();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getAsyncContext();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getDispatcherType();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getAuthType();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getCookies();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getDateHeader("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getHeaders("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getHeaderNames();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getPathTranslated();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getContextPath();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getQueryString();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRemoteUser();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isUserInRole("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getUserPrincipal();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRequestedSessionId();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRequestURI();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getRequestURL();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getSession(true);
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getSession();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.changeSessionId();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isRequestedSessionIdValid();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isRequestedSessionIdFromCookie();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isRequestedSessionIdFromURL();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.isRequestedSessionIdFromUrl();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.authenticate(null);
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.login("", "");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.logout();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getParts();
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.getPart("");
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    try {
        lGenericServletMockRequest.upgrade(null);
    } catch (Error e) {
        Assert.assertEquals("not supported method", e.getMessage());
    }
    Assert.assertNull(lGenericServletMockRequest.getMethod());
    Assert.assertEquals("test", lGenericServletMockRequest.getPathInfo());
}
Also used : Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) RestParam(io.servicecomb.common.rest.definition.RestParam) Test(org.junit.Test)

Aggregations

URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)7 Test (org.junit.Test)6 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)5 Invocation (io.servicecomb.core.Invocation)5 OperationMeta (io.servicecomb.core.definition.OperationMeta)4 Endpoint (io.servicecomb.core.Endpoint)3 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)2 AsyncResponse (io.servicecomb.core.AsyncResponse)2 Parameter (io.swagger.models.parameters.Parameter)2 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 HashMap (java.util.HashMap)2 RestParam (io.servicecomb.common.rest.definition.RestParam)1 HttpClientWithContext (io.servicecomb.foundation.vertx.client.http.HttpClientWithContext)1 Operation (io.swagger.models.Operation)1 Swagger (io.swagger.models.Swagger)1 PathParameter (io.swagger.models.parameters.PathParameter)1 QueryParameter (io.swagger.models.parameters.QueryParameter)1 Context (io.vertx.core.Context)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 Method (java.lang.reflect.Method)1