Search in sources :

Example 11 with RestOperationMeta

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

the class TestHttpRequestParamCreator method testCreateMockParam.

@Test
public void testCreateMockParam() throws Exception {
    ProducerHttpRequestArgMapper lHttpRequestParamCreator = new ProducerHttpRequestArgMapper(0);
    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);
    HttpServletRequest lHttpServletRequest = (HttpServletRequest) lHttpRequestParamCreator.createContextArg(invocation);
    Assert.assertNull(lHttpServletRequest.getMethod());
}
Also used : ProducerHttpRequestArgMapper(io.servicecomb.provider.rest.common.ProducerHttpRequestArgMapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) Test(org.junit.Test)

Example 12 with RestOperationMeta

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

the class RestEngineSchemaListener method onSchemaLoaded.

@Override
public void onSchemaLoaded(SchemaMeta... schemaMetas) {
    // 此时相应的ServicePathManager可能正在被使用,为避免太高的复杂度,使用copy on write逻辑
    Map<String, ServicePathManager> mgrMap = new HashMap<>();
    for (SchemaMeta schemaMeta : schemaMetas) {
        MicroserviceMeta microserviceMeta = schemaMeta.getMicroserviceMeta();
        ServicePathManager mgr = findPathManager(mgrMap, microserviceMeta);
        if (mgr.isSchemaExists(schemaMeta.getSchemaId())) {
            LOGGER.info("on schema loaded, exists schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
            continue;
        }
        LOGGER.info("on schema loaded, new schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
        mgr.addSchema(schemaMeta.getSchemaId());
        for (OperationMeta operationMeta : schemaMeta.getOperations()) {
            RestOperationMeta restOperationMeta = new RestOperationMeta();
            restOperationMeta.init(operationMeta);
            operationMeta.putExtData(RestConst.SWAGGER_REST_OPERATION, restOperationMeta);
            mgr.addResource(restOperationMeta);
        }
    }
    for (ServicePathManager mgr : mgrMap.values()) {
        // 对具有动态path operation进行排序
        mgr.sortPath();
        mgr.printService();
        mgr.saveToMicroserviceMeta();
    }
}
Also used : HashMap(java.util.HashMap) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Example 13 with RestOperationMeta

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

the class TestLocator method testLocateDynamic.

@Test
public void testLocateDynamic() {
    MicroserviceMeta msm = new MicroserviceMeta("app:ms");
    ServicePathManager spm = new ServicePathManager(msm);
    RestOperationMeta rom = createRestOperatonMeta("GET", "abc/{id}");
    spm.addResource(rom);
    try {
        spm.locateOperation("abc/10", "PUT");
    } catch (InvocationException e) {
        Assert.assertEquals("Method Not Allowed", ((CommonExceptionData) e.getErrorData()).getMessage());
    }
    OperationLocator locator = spm.locateOperation("abc/10", "GET");
    Assert.assertEquals("10", locator.getPathVarMap().get("id"));
}
Also used : RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) InvocationException(io.servicecomb.core.exception.InvocationException) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) Test(org.junit.Test)

Example 14 with RestOperationMeta

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

the class TestLocator method testServicePathManager.

@Test
public void testServicePathManager() {
    MicroserviceMeta msm = new MicroserviceMeta("app:ms");
    ServicePathManager spm = new ServicePathManager(msm);
    RestOperationMeta rom = createRestOperatonMeta("GET", "abc/{id}");
    spm.addResource(rom);
    rom = createRestOperatonMeta("GET", "abc/{id}/xxx");
    spm.addResource(rom);
    Assert.assertEquals("abc/{id}", spm.getDynamicPathOperationList().get(0).getAbsolutePath());
    spm.sortPath();
    Assert.assertEquals("abc/{id}/xxx", spm.getDynamicPathOperationList().get(0).getAbsolutePath());
    spm.printService();
    spm.doPrintService();
}
Also used : RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) Test(org.junit.Test)

Example 15 with RestOperationMeta

use of io.servicecomb.common.rest.definition.RestOperationMeta 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

RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)23 Test (org.junit.Test)14 OperationMeta (io.servicecomb.core.definition.OperationMeta)12 Invocation (io.servicecomb.core.Invocation)10 AsyncResponse (io.servicecomb.core.AsyncResponse)6 HttpClientRequest (io.vertx.core.http.HttpClientRequest)6 URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)5 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)5 IpPort (io.servicecomb.foundation.common.net.IpPort)5 HttpClient (io.vertx.core.http.HttpClient)4 Endpoint (io.servicecomb.core.Endpoint)3 InvocationException (io.servicecomb.core.exception.InvocationException)3 RestServerRequestInternal (io.servicecomb.common.rest.codec.RestServerRequestInternal)2 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)2 ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)2 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)2 HttpClientWithContext (io.servicecomb.foundation.vertx.client.http.HttpClientWithContext)2 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 HashMap (java.util.HashMap)2 MockUp (mockit.MockUp)2