aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/test/SimpleProtocolTestCase.java
blob: 3089a42ab2c49cc655d0a0531999424ef8a0ef21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.test;

import com.yahoo.component.Version;
import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.Routable;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Simon Thoresen Hult
 */
public class SimpleProtocolTestCase {

    private static final Version VERSION = new Version(1);
    private static final SimpleProtocol PROTOCOL = new SimpleProtocol();

    @Test
    void requireThatNameIsSet() {
        assertEquals(SimpleProtocol.NAME, PROTOCOL.getName());
    }


    @Test
    void requireThatMessageCanBeEncodedAndDecoded() {
        SimpleMessage msg = new SimpleMessage("foo");
        byte[] buf = PROTOCOL.encode(Version.emptyVersion, msg);
        Routable routable = PROTOCOL.decode(Version.emptyVersion, buf);
        assertNotNull(routable);
        assertEquals(SimpleMessage.class, routable.getClass());
        msg = (SimpleMessage) routable;
        assertEquals("foo", msg.getValue());
    }

    @Test
    void requireThatReplyCanBeDecoded() {
        SimpleReply reply = new SimpleReply("foo");
        byte[] buf = PROTOCOL.encode(Version.emptyVersion, reply);
        Routable routable = PROTOCOL.decode(Version.emptyVersion, buf);
        assertNotNull(routable);
        assertEquals(SimpleReply.class, routable.getClass());
        reply = (SimpleReply) routable;
        assertEquals("foo", reply.getValue());
    }

    @Test
    void requireThatUnknownRoutablesAreNotEncoded() {
        assertNull(PROTOCOL.encode(VERSION, new EmptyReply()));
    }

    @Test
    void requireThatEmptyBufferIsNotDecoded() {
        assertNull(PROTOCOL.decode(VERSION, new byte[0]));
    }

    @Test
    void requireThatUnknownBufferIsNotDecoded() {
        assertNull(PROTOCOL.decode(VERSION, new byte[]{'U'}));
    }

}