summaryrefslogtreecommitdiff
path: root/SDL_Core/mobile/android/SyncProxyAndroid/instrumentTest/java/com/ford/syncV4/proxy/converter/DefaultRPCRequestConverterTest.java
blob: 2487ec8a1cd079624f7ec0ac48992bcc3041ad28 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.ford.syncV4.proxy.converter;

import android.test.InstrumentationTestCase;

import com.ford.syncV4.marshal.JsonRPCMarshaller;
import com.ford.syncV4.protocol.IProtocolListener;
import com.ford.syncV4.protocol.ProtocolMessage;
import com.ford.syncV4.protocol.WiProProtocol;
import com.ford.syncV4.protocol.enums.MessageType;
import com.ford.syncV4.proxy.RPCRequestFactory;
import com.ford.syncV4.proxy.rpc.PutFile;
import com.ford.syncV4.proxy.rpc.Show;
import com.ford.syncV4.proxy.rpc.TestCommon;
import com.ford.syncV4.proxy.rpc.enums.TextAlignment;

import org.hamcrest.core.IsNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.nio.charset.Charset;
import java.util.List;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;

/**
 * Tests for DefaultRPCRequestConverter class.
 *
 * Created by enikolsky on 2014-01-21.
 */
public class DefaultRPCRequestConverterTest extends InstrumentationTestCase {
    private static final byte PROTOCOL_VERSION = (byte) 2;
    private static final int PUTFILE_FUNCTIONID = 32;
    private static final String OFFSET = "offset";
    private static final String LENGTH = "length";
    private static final String NUMBER_OF_OBJECTS_IS_INCORRECT =
            "Number of objects is incorrect";
    private DefaultRPCRequestConverter converter;
    private JsonRPCMarshaller marshaller;
    private int maxDataSize;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        TestCommon.setupMocking(this);

        converter = new DefaultRPCRequestConverter();
        marshaller = new JsonRPCMarshaller();

        WiProProtocol protocol =
                new WiProProtocol(mock(IProtocolListener.class));
        protocol.setVersion(PROTOCOL_VERSION);
        maxDataSize = WiProProtocol.MAX_DATA_SIZE;
    }

    public void testGetProtocolMessagesForShowShouldReturnCorrectProtocolMessage()
            throws Exception {
        final int correlationID = 1;
        final byte sessionID = (byte) 0;

        Show msg = new Show();
        msg.setAlignment(TextAlignment.CENTERED);
        msg.setCorrelationID(correlationID);

        final List<ProtocolMessage> protocolMessages =
                converter.getProtocolMessages(msg, sessionID, marshaller,
                        PROTOCOL_VERSION);

        assertThat(protocolMessages.size(), is(1));

        ProtocolMessage pm = protocolMessages.get(0);
        assertThat(pm.getData(), notNullValue());
        assertThat(pm.getData().length, greaterThan(0));
        assertThat(pm.getSessionID(), is(sessionID));
        assertThat(pm.getMessageType(), is(MessageType.RPC));
        assertThat(pm.getFunctionID(), is(13));
        assertThat(pm.getCorrID(), is(correlationID));
        assertThat(pm.getBulkData(), nullValue());
    }

    public void testGetProtocolMessagesForSmallPutFileShouldReturnCorrectProtocolMessage()
            throws Exception {
        final int correlationID = 1;
        final byte sessionID = (byte) 0;
        final byte[] data = TestCommon.getRandomBytes(32);

        PutFile msg = RPCRequestFactory.buildPutFile();
        msg.setSyncFileName("file");
        msg.setBulkData(data);
        msg.setCorrelationID(correlationID);

        final List<ProtocolMessage> protocolMessages =
                converter.getProtocolMessages(msg, sessionID, marshaller,
                        PROTOCOL_VERSION);

        assertThat(NUMBER_OF_OBJECTS_IS_INCORRECT, protocolMessages.size(),
                is(1));

        ProtocolMessage pm = protocolMessages.get(0);
        commonPutFileProtocolMessageAsserts(pm);
        assertThat(pm.getData(), IsNull.notNullValue());
        assertThat(pm.getJsonSize(), greaterThan(0));
        assertThat(pm.getSessionID(), is(sessionID));
        assertThat(pm.getCorrID(), is(correlationID));
        assertThat(pm.getBulkData(), is(data));
    }

    // or should it?
    public void testGetProtocolMessagesForPutFileWithoutBulkDataShouldReturnCorrectProtocolMessage()
            throws Exception {
        final int correlationID = 1;
        final byte sessionID = (byte) 0;

        PutFile msg = RPCRequestFactory.buildPutFile();
        msg.setSyncFileName("file");
        msg.setCorrelationID(correlationID);

        final List<ProtocolMessage> protocolMessages =
                converter.getProtocolMessages(msg, sessionID, marshaller,
                        PROTOCOL_VERSION);

        assertThat(NUMBER_OF_OBJECTS_IS_INCORRECT, protocolMessages.size(),
                is(1));

        ProtocolMessage pm = protocolMessages.get(0);
        commonPutFileProtocolMessageAsserts(pm);
        assertThat(pm.getData(), notNullValue());
        assertThat(pm.getJsonSize(), greaterThan(0));
        assertThat(pm.getSessionID(), is(sessionID));
        assertThat(pm.getCorrID(), is(correlationID));
        assertThat(pm.getBulkData(), nullValue());
    }

    public void testMediumPutFileShouldNotBeSplitIntoTwoProtocolMessages()
            throws Exception {
        final int correlationID = 1;
        final byte sessionID = (byte) 0;

        final int extraDataSize = 10;
        final int dataSize = maxDataSize + extraDataSize;
        final byte[] data = TestCommon.getRandomBytes(dataSize);

        PutFile msg = RPCRequestFactory.buildPutFile();
        msg.setBulkData(data);
        msg.setCorrelationID(correlationID);

        final List<ProtocolMessage> protocolMessages =
                converter.getProtocolMessages(msg, sessionID, marshaller,
                        PROTOCOL_VERSION);

        assertThat(NUMBER_OF_OBJECTS_IS_INCORRECT, protocolMessages.size(),
                is(1));

        ProtocolMessage pm0 = protocolMessages.get(0);
        commonPutFileProtocolMessageAsserts(pm0);
        final byte[] json0 = pm0.getData();
        checkNoOffsetAndLengthInJSON(json0);
        assertThat(pm0.getJsonSize(), is(json0.length));
        assertThat(pm0.getSessionID(), is(sessionID));
        assertThat(pm0.getCorrID(), is(correlationID));
        assertThat(pm0.getBulkData(), is(data));
    }

    // check a corner case when "data size" == "max data size" * N
    public void testPutFileWithDivisibleBulkDataSizeShouldNotBeSplitIntoTwoProtocolMessages()
            throws Exception {
        final int correlationID = 1;
        final byte sessionID = (byte) 0;

        final int dataSize = maxDataSize * 2;
        final byte[] data = TestCommon.getRandomBytes(dataSize);

        PutFile msg = RPCRequestFactory.buildPutFile();
        msg.setBulkData(data);
        msg.setCorrelationID(correlationID);

        final List<ProtocolMessage> protocolMessages =
                converter.getProtocolMessages(msg, sessionID, marshaller,
                        PROTOCOL_VERSION);

        assertThat(NUMBER_OF_OBJECTS_IS_INCORRECT, protocolMessages.size(),
                is(1));

        ProtocolMessage pm0 = protocolMessages.get(0);
        commonPutFileProtocolMessageAsserts(pm0);
        final byte[] json0 = pm0.getData();
        checkNoOffsetAndLengthInJSON(json0);
        assertThat(pm0.getJsonSize(), is(json0.length));
        assertThat(pm0.getSessionID(), is(sessionID));
        assertThat(pm0.getCorrID(), is(correlationID));
        assertThat(pm0.getBulkData(), is(data));
    }

    public void testBigPutFileShouldNotBeSplitIntoMultipleProtocolMessages()
            throws Exception {
        final int correlationID = 1;
        final byte sessionID = (byte) 0;

        final int dataCopies = 12;
        final int extraDataSize = 20;
        final int dataSize = (maxDataSize * dataCopies) + extraDataSize;
        final byte[] data = TestCommon.getRandomBytes(dataSize);

        PutFile msg = RPCRequestFactory.buildPutFile();
        msg.setBulkData(data);
        msg.setCorrelationID(correlationID);

        final List<ProtocolMessage> protocolMessages =
                converter.getProtocolMessages(msg, sessionID, marshaller,
                        PROTOCOL_VERSION);

        assertThat(NUMBER_OF_OBJECTS_IS_INCORRECT, protocolMessages.size(),
                is(1));

        final ProtocolMessage pm = protocolMessages.get(0);
        final byte[] json = pm.getData();
        checkNoOffsetAndLengthInJSON(json);
        assertThat(pm.getBulkData(), is(data));
    }

    public void testGetProtocolMessagesShouldThrowNullPointerExceptionWhenRequestIsNull() {
        try {
            converter.getProtocolMessages(null, (byte) 0, marshaller,
                    PROTOCOL_VERSION);
            fail("Should have thrown NullPointerException");
        } catch (NullPointerException e) {
            // success
        }
    }

    public void testGetProtocolMessagesShouldThrowNullPointerExceptionWhenMarshallerIsNull() {
        try {
            Show msg = new Show();

            converter.getProtocolMessages(msg, (byte) 0, null,
                    PROTOCOL_VERSION);
            fail("Should have thrown NullPointerException");
        } catch (NullPointerException e) {
            // success
        }
    }

    private void commonPutFileProtocolMessageAsserts(
            ProtocolMessage protocolMessage) {
        assertThat(protocolMessage.getVersion(), is(PROTOCOL_VERSION));
        assertThat(protocolMessage.getFunctionID(), is(PUTFILE_FUNCTIONID));
        assertThat(protocolMessage.getMessageType(), is(MessageType.RPC));
    }

    private void checkNoOffsetAndLengthInJSON(byte[] data)
            throws JSONException {
        assertThat(data, notNullValue());

        JSONObject jsonObject =
                new JSONObject(new String(data, Charset.defaultCharset()));
        assertThat(jsonObject.has(OFFSET), is(false));
        assertThat(jsonObject.has(LENGTH), is(false));
    }
}