Search in sources :

Example 1 with Holder

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

the class TestGeneric method testHolderUser_rest.

@SuppressWarnings("unchecked")
private void testHolderUser_rest() {
    Holder<User> holder = new Holder<>(new User());
    Holder<User> result = intf.testHolderUser(holder);
    TestMgr.check("{\"name\":\"nameA\",\"age\":100,\"index\":0,\"names\":null}", result.value.jsonString());
    result = restTemplate.postForObject(prefix + "/holderUser", holder, Holder.class);
    TestMgr.check("{\"name\":\"nameA\",\"age\":100,\"index\":0,\"names\":null}", result.value.jsonString());
}
Also used : User(org.apache.servicecomb.demo.server.User) Holder(org.apache.servicecomb.foundation.common.Holder)

Example 2 with Holder

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

the class TestObject method testObject.

@SuppressWarnings("unchecked")
private void testObject() {
    // int
    Object result = intf.testObject(1);
    TestMgr.check(1, result);
    TestMgr.check(Integer.class, result.getClass());
    result = restTemplate.postForObject(prefix + "/object", 1, Integer.class);
    TestMgr.check(1, result);
    TestMgr.check(Integer.class, result.getClass());
    // string
    result = intf.testObject("str");
    TestMgr.check("str", result);
    TestMgr.check(String.class, result.getClass());
    result = restTemplate.postForObject(prefix + "/object", "str", String.class);
    TestMgr.check("str", result);
    TestMgr.check(String.class, result.getClass());
    // emptyObject
    result = intf.testObject(new EmptyObject());
    // result may not be an empty map in highway
    // TestMgr.check("{}", result);
    TestMgr.check(true, Map.class.isAssignableFrom(result.getClass()));
    result = restTemplate.postForObject(prefix + "/object", new EmptyObject(), EmptyObject.class);
    TestMgr.check(EmptyObject.class, result.getClass());
    result = restTemplate.postForObject(prefix + "/object", new EmptyObject(), EmptyObject.class);
    TestMgr.check(EmptyObject.class, result.getClass());
    // map
    Map<String, String> map = Collections.singletonMap("k", "v");
    result = intf.testObject(map);
    TestMgr.check("{k=v}", result);
    TestMgr.check(true, Map.class.isAssignableFrom(result.getClass()));
    result = restTemplate.postForObject(prefix + "/object", map, Map.class);
    TestMgr.check("{k=v}", result);
    TestMgr.check(true, Map.class.isAssignableFrom(result.getClass()));
    // list
    List<String> list = Collections.singletonList("v");
    result = intf.testObject(list);
    TestMgr.check("[v]", result);
    TestMgr.check(true, List.class.isAssignableFrom(result.getClass()));
    result = restTemplate.postForObject(prefix + "/object", list, List.class);
    TestMgr.check("[v]", result);
    TestMgr.check(true, List.class.isAssignableFrom(result.getClass()));
    // generic
    Holder<String> holder = new Holder<>("v");
    result = intf.testObject(holder);
    TestMgr.check("v", ((Map<String, String>) result).get("value"));
    TestMgr.check(LinkedHashMap.class, result.getClass());
    result = restTemplate.postForObject(prefix + "/object", holder, Holder.class);
    TestMgr.check("v", ((Map<String, String>) result).get("value"));
    TestMgr.check(LinkedHashMap.class, result.getClass());
}
Also used : EmptyObject(org.apache.servicecomb.demo.EmptyObject) Holder(org.apache.servicecomb.foundation.common.Holder) EmptyObject(org.apache.servicecomb.demo.EmptyObject) List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with Holder

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

the class TestVertxServerRequestToHttpServletRequest method setBodyBuffer.

@Test
public void setBodyBuffer() {
    Holder<Buffer> bodyHolder = new Holder<>();
    context = new MockUp<RoutingContext>() {

        @Mock
        HttpServerRequest request() {
            return vertxRequest;
        }

        @Mock
        void setBody(Buffer body) {
            bodyHolder.value = body;
        }
    }.getMockInstance();
    request = new VertxServerRequestToHttpServletRequest(context);
    Buffer bodyBuffer = Buffer.buffer();
    request.setBodyBuffer(bodyBuffer);
    Assert.assertSame(bodyBuffer, bodyHolder.value);
    Assert.assertSame(bodyBuffer, request.getBodyBuffer());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 4 with Holder

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

the class TestVertxUtils method testGetOrCreateVertx.

@Test
public void testGetOrCreateVertx() throws InterruptedException {
    Vertx vertx = VertxUtils.getOrCreateVertxByName("ut", null);
    Holder<String> name = new Holder<>();
    CountDownLatch latch = new CountDownLatch(1);
    vertx.runOnContext(v -> {
        name.value = Thread.currentThread().getName();
        latch.countDown();
    });
    latch.await();
    Assert.assertEquals(name.value, "ut-vert.x-eventloop-thread-0");
    VertxUtils.blockCloseVertxByName("ut");
}
Also used : Holder(org.apache.servicecomb.foundation.common.Holder) Vertx(io.vertx.core.Vertx) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with Holder

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

the class TestInspectorBootListener method enabled.

@Test
public void enabled() {
    Holder<Object> holder = new Holder<>();
    SCBEngine scbEngine = SCBBootstrap.createSCBEngineForTest();
    scbEngine.setProducerMicroserviceMeta(new MicroserviceMeta(scbEngine, "ms", false));
    scbEngine.setProducerProviderManager(new ProducerProviderManager(scbEngine) {

        @Override
        public SchemaMeta registerSchema(String schemaId, Object instance) {
            if ("inspector".equals(schemaId)) {
                holder.value = instance;
            }
            return null;
        }
    });
    InspectorConfig inspectorConfig = new InspectorConfig().setEnabled(true);
    new InspectorBootListener(inspectorConfig, null).onAfterTransport(new BootEvent(scbEngine, EventType.AFTER_TRANSPORT));
    Assert.assertNotNull(holder.value);
}
Also used : SCBEngine(org.apache.servicecomb.core.SCBEngine) Holder(org.apache.servicecomb.foundation.common.Holder) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) ProducerProviderManager(org.apache.servicecomb.core.provider.producer.ProducerProviderManager) Test(org.junit.Test)

Aggregations

Holder (org.apache.servicecomb.foundation.common.Holder)54 Test (org.junit.Test)44 MockUp (mockit.MockUp)18 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Invocation (org.apache.servicecomb.core.Invocation)12 Expectations (mockit.Expectations)11 Vertx (io.vertx.core.Vertx)10 List (java.util.List)9 Response (org.apache.servicecomb.swagger.invocation.Response)9 Mock (mockit.Mock)8 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 SCBEngine (org.apache.servicecomb.core.SCBEngine)7 ExpectedException (org.junit.rules.ExpectedException)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Status (javax.ws.rs.core.Response.Status)6 Deencapsulation (mockit.Deencapsulation)6 Mocked (mockit.Mocked)6 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)6