Search in sources :

Example 11 with Envelope

use of com.kixeye.chassis.transport.dto.Envelope in project chassis by Kixeye.

the class WebSocketTransportTest method testWebSocketServiceWithProtobuf.

@Test
public void testWebSocketServiceWithProtobuf() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");
    properties.put("http.enabled", "false");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.hostname", "localhost");
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
    context.setEnvironment(environment);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(TransportConfiguration.class);
    context.register(TestWebSocketService.class);
    WebSocketClient wsClient = new WebSocketClient();
    try {
        context.refresh();
        final MessageSerDe serDe = context.getBean(ProtobufMessageSerDe.class);
        final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
        messageRegistry.registerType("stuff", TestObject.class);
        wsClient.start();
        QueuingWebSocketListener webSocket = new QueuingWebSocketListener(serDe, messageRegistry, null);
        Session session = wsClient.connect(webSocket, new URI("ws://localhost:" + properties.get("websocket.port") + "/" + serDe.getMessageFormatName())).get(5000, TimeUnit.MILLISECONDS);
        Envelope envelope = new Envelope("getStuff", null, null, Lists.newArrayList(new Header("testheadername", Lists.newArrayList("testheaderval"))), null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        TestObject response = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals("stuff", response.value);
        byte[] rawStuff = serDe.serialize(new TestObject("more stuff"));
        envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        response = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals("stuff", response.value);
        envelope = new Envelope("getStuff", null, null, null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        response = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals("more stuff", response.value);
        rawStuff = serDe.serialize(new TestObject(RandomStringUtils.randomAlphanumeric(100)));
        envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        ServiceError error = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(error);
        Assert.assertEquals(ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE, error.code);
        envelope = new Envelope("expectedError", null, null, null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        error = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(error);
        Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.code, error.code);
        Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.description, error.description);
        envelope = new Envelope("unexpectedError", null, null, null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        error = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(error);
        Assert.assertEquals(ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE, error.code);
    } finally {
        try {
            wsClient.stop();
        } finally {
            context.close();
            while (context.isActive()) {
                Thread.sleep(100);
            }
        }
    }
}
Also used : ServiceError(com.kixeye.chassis.transport.dto.ServiceError) HashMap(java.util.HashMap) WebSocketMessageRegistry(com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry) JsonJacksonMessageSerDe(com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe) ProtobufMessageSerDe(com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe) YamlJacksonMessageSerDe(com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe) XmlMessageSerDe(com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe) MessageSerDe(com.kixeye.chassis.transport.serde.MessageSerDe) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Envelope(com.kixeye.chassis.transport.dto.Envelope) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) URI(java.net.URI) Header(com.kixeye.chassis.transport.dto.Header) MapPropertySource(org.springframework.core.env.MapPropertySource) QueuingWebSocketListener(com.kixeye.chassis.transport.websocket.QueuingWebSocketListener) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 12 with Envelope

use of com.kixeye.chassis.transport.dto.Envelope in project chassis by Kixeye.

the class WebSocketEnvelopeTest method testWithPayloadSingleHeaderValue.

@Test
public void testWithPayloadSingleHeaderValue() {
    final String test1Value = RandomStringUtils.randomAlphaNumericString(20, RAND);
    final Envelope baseEnvelope = new Envelope(RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), Lists.newArrayList(new Header("testName", Lists.newArrayList(test1Value))), ByteBuffer.wrap(RandomStringUtils.randomString(20, RAND).getBytes()));
    final WebSocketEnvelope envelope = new WebSocketEnvelope(baseEnvelope);
    Assert.assertEquals(baseEnvelope.action, envelope.getAction());
    Assert.assertEquals(baseEnvelope.typeId, envelope.getTypeId());
    Assert.assertEquals(baseEnvelope.transactionId, envelope.getTransactionId());
    Assert.assertTrue(envelope.hasPayload());
    Assert.assertEquals(baseEnvelope.payload, envelope.getPayload());
    Assert.assertEquals(1, envelope.getHeaders().size());
    Assert.assertEquals(test1Value, envelope.getHeaders().get("testName").iterator().next());
}
Also used : Header(com.kixeye.chassis.transport.dto.Header) Envelope(com.kixeye.chassis.transport.dto.Envelope) Test(org.junit.Test)

Example 13 with Envelope

use of com.kixeye.chassis.transport.dto.Envelope in project chassis by Kixeye.

the class WebSocketEnvelopeTest method testWithPayloadMultiHeaderValue.

@Test
public void testWithPayloadMultiHeaderValue() {
    final String test1Value = RandomStringUtils.randomAlphaNumericString(20, RAND);
    final String test2Value = RandomStringUtils.randomAlphaNumericString(20, RAND);
    final String test3Value = RandomStringUtils.randomAlphaNumericString(20, RAND);
    final Envelope baseEnvelope = new Envelope(RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), Lists.newArrayList(new Header("testName", Lists.newArrayList(test1Value, test2Value, test3Value))), ByteBuffer.wrap(RandomStringUtils.randomString(20, RAND).getBytes()));
    final WebSocketEnvelope envelope = new WebSocketEnvelope(baseEnvelope);
    Assert.assertEquals(baseEnvelope.action, envelope.getAction());
    Assert.assertEquals(baseEnvelope.typeId, envelope.getTypeId());
    Assert.assertEquals(baseEnvelope.transactionId, envelope.getTransactionId());
    Assert.assertTrue(envelope.hasPayload());
    Assert.assertEquals(baseEnvelope.payload, envelope.getPayload());
    Assert.assertEquals(3, envelope.getHeaders().size());
    Assert.assertTrue(envelope.getHeaders().containsEntry("testName", test1Value));
    Assert.assertTrue(envelope.getHeaders().containsEntry("testName", test2Value));
    Assert.assertTrue(envelope.getHeaders().containsEntry("testName", test3Value));
}
Also used : Header(com.kixeye.chassis.transport.dto.Header) Envelope(com.kixeye.chassis.transport.dto.Envelope) Test(org.junit.Test)

Example 14 with Envelope

use of com.kixeye.chassis.transport.dto.Envelope in project chassis by Kixeye.

the class WebSocketEnvelopeTest method testWithPayloadSameHeaderMultiValue.

@Test
public void testWithPayloadSameHeaderMultiValue() {
    final String test1Value = RandomStringUtils.randomAlphaNumericString(20, RAND);
    final String test2Value = RandomStringUtils.randomAlphaNumericString(20, RAND);
    final String test3Value = RandomStringUtils.randomAlphaNumericString(20, RAND);
    final Envelope baseEnvelope = new Envelope(RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), Lists.newArrayList(new Header("testName", Lists.newArrayList(test1Value, test2Value)), new Header("testName", Lists.newArrayList(test3Value))), ByteBuffer.wrap(RandomStringUtils.randomString(20, RAND).getBytes()));
    final WebSocketEnvelope envelope = new WebSocketEnvelope(baseEnvelope);
    Assert.assertEquals(baseEnvelope.action, envelope.getAction());
    Assert.assertEquals(baseEnvelope.typeId, envelope.getTypeId());
    Assert.assertEquals(baseEnvelope.transactionId, envelope.getTransactionId());
    Assert.assertTrue(envelope.hasPayload());
    Assert.assertEquals(baseEnvelope.payload, envelope.getPayload());
    Assert.assertEquals(3, envelope.getHeaders().size());
    Assert.assertTrue(envelope.getHeaders().containsEntry("testName", test1Value));
    Assert.assertTrue(envelope.getHeaders().containsEntry("testName", test2Value));
    Assert.assertTrue(envelope.getHeaders().containsEntry("testName", test3Value));
}
Also used : Header(com.kixeye.chassis.transport.dto.Header) Envelope(com.kixeye.chassis.transport.dto.Envelope) Test(org.junit.Test)

Example 15 with Envelope

use of com.kixeye.chassis.transport.dto.Envelope in project chassis by Kixeye.

the class WebSocketEnvelopeTest method testWithPayloadNoHeaders.

@Test
public void testWithPayloadNoHeaders() {
    final Envelope baseEnvelope = new Envelope(RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), RandomStringUtils.randomAlphaNumericString(20, RAND), null, ByteBuffer.wrap(RandomStringUtils.randomString(20, RAND).getBytes()));
    final WebSocketEnvelope envelope = new WebSocketEnvelope(baseEnvelope);
    Assert.assertEquals(baseEnvelope.action, envelope.getAction());
    Assert.assertEquals(baseEnvelope.typeId, envelope.getTypeId());
    Assert.assertEquals(baseEnvelope.transactionId, envelope.getTransactionId());
    Assert.assertTrue(envelope.hasPayload());
    Assert.assertEquals(baseEnvelope.payload, envelope.getPayload());
    Assert.assertEquals(0, envelope.getHeaders().size());
}
Also used : Envelope(com.kixeye.chassis.transport.dto.Envelope) Test(org.junit.Test)

Aggregations

Envelope (com.kixeye.chassis.transport.dto.Envelope)16 Test (org.junit.Test)13 Header (com.kixeye.chassis.transport.dto.Header)10 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)7 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)7 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)7 URI (java.net.URI)7 HashMap (java.util.HashMap)7 Session (org.eclipse.jetty.websocket.api.Session)7 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7 StandardEnvironment (org.springframework.core.env.StandardEnvironment)7 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)7 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)6 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)6 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)6 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1