Search in sources :

Example 1 with AbstractHttpServletRequest

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method scheduleInvocationException.

@Test
public void scheduleInvocationException(@Mocked OperationMeta operationMeta) {
    Executor executor = new ReactiveExecutor();
    requestEx = new AbstractHttpServletRequest() {
    };
    requestEx.setAttribute(RestConst.REST_REQUEST, requestEx);
    new Expectations() {

        {
            restOperation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExecutor();
            result = executor;
        }
    };
    Holder<Throwable> result = new Holder<>();
    Error error = new Error("run on executor");
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void runOnExecutor() {
            throw error;
        }

        @Override
        public void sendFailResponse(Throwable throwable) {
            result.value = throwable;
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.restOperationMeta = restOperation;
    restInvocation.scheduleInvocation();
    Assert.assertSame(error, result.value);
}
Also used : Expectations(mockit.Expectations) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Holder(javax.xml.ws.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Test(org.junit.Test)

Example 2 with AbstractHttpServletRequest

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method scheduleInvocationTimeout.

@Test
public void scheduleInvocationTimeout(@Mocked OperationMeta operationMeta) {
    Executor executor = Runnable::run;
    new Expectations() {

        {
            restOperation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExecutor();
            result = executor;
            operationMeta.getMicroserviceQualifiedName();
            result = "sayHi";
        }
    };
    requestEx = new AbstractHttpServletRequest() {
    };
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void runOnExecutor() {
            throw new Error("run on executor");
        }

        @Override
        public void sendFailResponse(Throwable throwable) {
            throw (Error) throwable;
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.restOperationMeta = restOperation;
    // will not throw exception
    restInvocation.scheduleInvocation();
}
Also used : Expectations(mockit.Expectations) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Test(org.junit.Test)

Example 3 with AbstractHttpServletRequest

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest in project incubator-servicecomb-java-chassis by apache.

the class TestRestProducerInvocation method findRestOperationNormal.

@Test
public void findRestOperationNormal(@Mocked MicroserviceMeta microserviceMeta, @Mocked ServicePathManager servicePathManager, @Mocked OperationLocator locator) {
    requestEx = new AbstractHttpServletRequest() {

        @Override
        public String getRequestURI() {
            return "/path";
        }

        @Override
        public String getMethod() {
            return "GET";
        }

        @Override
        public String getHeader(String name) {
            return "ms";
        }
    };
    Map<String, String> pathVars = new HashMap<>();
    new Expectations(ServicePathManager.class) {

        {
            microserviceMetaManager.ensureFindValue("ms");
            result = microserviceMeta;
            ServicePathManager.getServicePathManager(microserviceMeta);
            result = servicePathManager;
            servicePathManager.producerLocateOperation(anyString, anyString);
            result = locator;
            locator.getPathVarMap();
            result = pathVars;
            locator.getOperation();
            result = restOperationMeta;
        }
    };
    restProducerInvocation = new RestProducerInvocation();
    initRestProducerInvocation();
    restProducerInvocation.findRestOperation();
    Assert.assertSame(restOperationMeta, restProducerInvocation.restOperationMeta);
    Assert.assertSame(pathVars, requestEx.getAttribute(RestConst.PATH_PARAMETERS));
}
Also used : Expectations(mockit.Expectations) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with AbstractHttpServletRequest

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method scheduleInvocationNormal.

@Test
public void scheduleInvocationNormal(@Mocked OperationMeta operationMeta) {
    long time = 123;
    new MockUp<System>() {

        @Mock
        long nanoTime() {
            return time;
        }
    };
    Holder<InvocationStartEvent> eventHolder = new Holder<>();
    Object subscriber = new Object() {

        @Subscribe
        public void onStart(InvocationStartEvent event) {
            eventHolder.value = event;
        }
    };
    EventManager.register(subscriber);
    Executor executor = new ReactiveExecutor();
    requestEx = new AbstractHttpServletRequest() {
    };
    requestEx.setAttribute(RestConst.REST_REQUEST, requestEx);
    new Expectations() {

        {
            restOperation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExecutor();
            result = executor;
        }
    };
    Holder<Boolean> result = new Holder<>();
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void runOnExecutor() {
            result.value = true;
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.restOperationMeta = restOperation;
    restInvocation.scheduleInvocation();
    EventManager.unregister(subscriber);
    Assert.assertTrue(result.value);
    Assert.assertEquals(time, invocation.getStartTime());
    Assert.assertSame(invocation, eventHolder.value.getInvocation());
}
Also used : Expectations(mockit.Expectations) Holder(javax.xml.ws.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) MockUp(mockit.MockUp) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) InvocationStartEvent(org.apache.servicecomb.core.event.InvocationStartEvent) Test(org.junit.Test)

Example 5 with AbstractHttpServletRequest

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method findRestOperationNormal.

@Test
public void findRestOperationNormal(@Mocked MicroserviceMeta microserviceMeta, @Mocked ServicePathManager servicePathManager, @Mocked OperationLocator locator) {
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected OperationLocator locateOperation(ServicePathManager servicePathManager) {
            return locator;
        }
    };
    requestEx = new AbstractHttpServletRequest() {
    };
    restInvocation.requestEx = requestEx;
    Map<String, String> pathVars = new HashMap<>();
    new Expectations(ServicePathManager.class) {

        {
            ServicePathManager.getServicePathManager(microserviceMeta);
            result = servicePathManager;
            locator.getPathVarMap();
            result = pathVars;
            locator.getOperation();
            result = restOperation;
        }
    };
    restInvocation.findRestOperation(microserviceMeta);
    Assert.assertSame(restOperation, restInvocation.restOperationMeta);
    Assert.assertSame(pathVars, requestEx.getAttribute(RestConst.PATH_PARAMETERS));
}
Also used : Expectations(mockit.Expectations) OperationLocator(org.apache.servicecomb.common.rest.locator.OperationLocator) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) HashMap(java.util.HashMap) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager) Test(org.junit.Test)

Aggregations

AbstractHttpServletRequest (org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest)6 Test (org.junit.Test)6 Expectations (mockit.Expectations)5 Executor (java.util.concurrent.Executor)3 ReactiveExecutor (org.apache.servicecomb.core.executor.ReactiveExecutor)3 HashMap (java.util.HashMap)2 Holder (javax.xml.ws.Holder)2 MockUp (mockit.MockUp)2 OperationLocator (org.apache.servicecomb.common.rest.locator.OperationLocator)1 ServicePathManager (org.apache.servicecomb.common.rest.locator.ServicePathManager)1 InvocationStartEvent (org.apache.servicecomb.core.event.InvocationStartEvent)1