Search in sources :

Example 56 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.

the class TestSwaggerLoader method mockLocalResource.

private void mockLocalResource(Map<String, String> resourceMap) {
    Holder<String> pathHolder = new Holder<>();
    URL url = new MockUp<URL>() {

        @Mock
        String getPath() {
            return pathHolder.value;
        }

        @Mock
        String toExternalForm() {
            return pathHolder.value;
        }
    }.getMockInstance();
    ClassLoader classLoader = new MockUp<ClassLoader>() {

        @Mock
        URL getResource(String name) {
            pathHolder.value = name;
            return resourceMap.containsKey(name) ? url : null;
        }

        @Mock
        InputStream getResourceAsStream(String name) {
            String content = resourceMap.get(name);
            return content == null ? null : new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
        }
    }.getMockInstance();
    new Expectations(JvmUtils.class) {

        {
            JvmUtils.findClassLoader();
            result = classLoader;
        }
    };
    new MockUp<IOUtils>() {

        @Mock
        String toString(URL url, Charset encoding) {
            return resourceMap.get(url.getPath());
        }
    };
}
Also used : Expectations(mockit.Expectations) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Holder(org.apache.servicecomb.foundation.common.Holder) Charset(java.nio.charset.Charset) MockUp(mockit.MockUp) URL(java.net.URL) Mock(mockit.Mock) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 57 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceWatchTask method testWatchFailed.

@Test
public void testWatchFailed(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    initWatch(serviceRegistryConfig, srClient, microservice);
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            callback.fail(new RuntimeExceptionWithoutStackTrace("test failed"));
        }
    };
    Holder<Throwable> holder = new Holder<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onException(ExceptionEvent event) {
            holder.value = event.getThrowable();
        }
    });
    Assert.assertNull(holder.value);
    microserviceWatchTask.run();
    Assert.assertEquals("test failed", holder.value.getMessage());
}
Also used : RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) ExceptionEvent(org.apache.servicecomb.serviceregistry.event.ExceptionEvent) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 58 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceWatchTask method testWatchInstanceChanged.

@Test
public void testWatchInstanceChanged(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    initWatch(serviceRegistryConfig, srClient, microservice);
    MicroserviceInstanceChangedEvent changedEvent = new MicroserviceInstanceChangedEvent();
    MicroserviceKey key = new MicroserviceKey();
    key.setAppId(microservice.getAppId());
    key.setVersion(microservice.getVersion());
    key.setServiceName(microservice.getServiceName());
    changedEvent.setKey(key);
    changedEvent.setInstance(microservice.getInstance());
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            callback.success(changedEvent);
        }
    };
    Holder<MicroserviceInstanceChangedEvent> holder = new Holder<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onException(MicroserviceInstanceChangedEvent event) {
            holder.value = event;
        }
    });
    changedEvent.setAction(WatchAction.CREATE);
    microserviceWatchTask.run();
    Assert.assertEquals(WatchAction.CREATE, holder.value.getAction());
    changedEvent.setAction(WatchAction.DELETE);
    microserviceWatchTask.run();
    Assert.assertEquals(WatchAction.DELETE, holder.value.getAction());
    changedEvent.setAction(WatchAction.UPDATE);
    microserviceWatchTask.run();
    Assert.assertEquals(WatchAction.UPDATE, holder.value.getAction());
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) MicroserviceKey(org.apache.servicecomb.registry.api.MicroserviceKey) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 59 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class EndpointTest method should_set_endpoint_to_invocation_when_map_arguments.

@Test
void should_set_endpoint_to_invocation_when_map_arguments() {
    SwaggerEnvironment environment = new SwaggerEnvironment();
    SwaggerConsumer consumer = environment.createConsumer(TestSchema.class, SwaggerGenerator.generate(TestSchema.class));
    SwaggerConsumerOperation operation = consumer.findOperation("say");
    Endpoint endpoint = new Endpoint(Mockito.mock(Transport.class), null);
    Invocation invocation = Mockito.mock(Invocation.class);
    Holder<Object> holder = new Holder<>();
    Mockito.doAnswer(invocationOnMock -> {
        holder.value = invocationOnMock.getArguments()[0];
        return null;
    }).when(invocation).setEndpoint(Mockito.any());
    Map<String, Object> argsMap = of("endpoint", endpoint);
    operation.getArgumentsMapper().invocationArgumentToSwaggerArguments(invocation, argsMap);
    assertThat(holder.value).isSameAs(endpoint);
}
Also used : Endpoint(org.apache.servicecomb.core.Endpoint) Invocation(org.apache.servicecomb.core.Invocation) SwaggerConsumer(org.apache.servicecomb.swagger.engine.SwaggerConsumer) SwaggerConsumerOperation(org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation) Holder(org.apache.servicecomb.foundation.common.Holder) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) Transport(org.apache.servicecomb.core.Transport) Test(org.junit.jupiter.api.Test)

Example 60 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestGeneric method testHolderUser_rt.

@Test
public void testHolderUser_rt() {
    Holder<User> holder = new Holder<>(new User());
    @SuppressWarnings("unchecked") Holder<User> result = consumers.getSCBRestTemplate().postForObject("/holderUser", holder, Holder.class);
    assertEquals(result.value.jsonString(), expectUserStr);
}
Also used : User(org.apache.servicecomb.it.schema.User) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test)

Aggregations

Holder (org.apache.servicecomb.foundation.common.Holder)108 Test (org.junit.Test)88 MockUp (mockit.MockUp)36 AtomicLong (java.util.concurrent.atomic.AtomicLong)28 Invocation (org.apache.servicecomb.core.Invocation)24 Expectations (mockit.Expectations)22 Vertx (io.vertx.core.Vertx)20 List (java.util.List)18 Response (org.apache.servicecomb.swagger.invocation.Response)18 Mock (mockit.Mock)16 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)16 ArrayList (java.util.ArrayList)14 Map (java.util.Map)14 SCBEngine (org.apache.servicecomb.core.SCBEngine)14 ExpectedException (org.junit.rules.ExpectedException)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 Status (javax.ws.rs.core.Response.Status)12 Deencapsulation (mockit.Deencapsulation)12 Mocked (mockit.Mocked)12 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)12